STK++ 0.9.13
STK_lapack_Qr.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff, Université Lille 1, Inria
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23*/
24
25/*
26 * Project: stkpp::Algebra
27 * created on: 20 nov. 2013
28 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
36#ifndef STK_LAPACK_QR_H
37#define STK_LAPACK_QR_H
38
42
43#include "STK_IQr.h"
44#include "STK_lapack_Util.h"
45
46namespace STK
47{
48
49namespace lapack
50{
51class Qr;
52}
53
54namespace hidden
55{
60template<>
61struct AlgebraTraits< lapack::Qr >
62{
63 typedef ArrayXX Array;
64 typedef typename Array::Col ColVector;
65 typedef typename Array::Row RowVector;
66};
67
68} // namespace hidden
69
70
71namespace lapack
72{
87class Qr: public IQr<Qr >
88{
89 public:
90 typedef IQr<Qr > Base;
91 using Base::Q_;
92 using Base::R_;
97 inline Qr( ArrayXX const& data, bool ref = false): Base(data, ref) {}
101 template<class Derived>
102 Qr( ArrayBase<Derived> const& data): Base(data){}
106 inline Qr( Qr const& decomp): Base(decomp) {}
108 inline virtual ~Qr() {}
110 inline virtual Qr* clone() const { return new Qr(*this);}
112 inline Qr& operator=(Qr const& decomp)
113 {
114 Base::operator =(decomp);
115 return *this;
116 }
121 bool runImpl();
122
123 private:
125 bool computeQr(CArrayXX& a, CVectorX& tau);
126};
129/* private method for computing the Qr decomposition using a CArrayXX array */
131{
132 // start qr iterations
133 int lWork =-1, m = a.sizeRows(), n= a.sizeCols();
134 int info;
135 Real iwork;
136 Real *p_work;
137
138 // get size for work
139 info = geqrf(m, n, a.p_data(), m, tau.p_data(), &iwork, lWork);
140 // check any error
141 if (info!=0)
142 {
143 if (info>0)
145 return false;
146 }
148 return false;
149 }
150 // create
151 lWork = (int)iwork;
152 p_work = new Real[lWork];
153 info = geqrf(m, n, a.p_data(), m, tau.p_data(), p_work, lWork);
154 // release working set
155 delete[] p_work;
156 // check any error
157 if (info!=0)
158 {
159 if (info>0)
161 return false;
162 }
164 return false;
165 }
166 return true;
167}
168/* Computing the QR decomposition of the matrix Q_. */
169inline bool Qr::runImpl()
170{
171 int begin = Q_.beginRows(); // same first index checked at construction of QR
172 int end = std::min(Q_.endRows(), Q_.endCols());
173 // compute results
174 CArrayXX a = Q_;
175 CVectorX tau(std::min(Q_.sizeRows(), Q_.sizeCols()));
176 a.shift(0, 0);
177 tau.shift(0);
178 if (!computeQr(a, tau)) { return false;};
179 // get results
180 a.shift(begin, begin);
181 tau.shift(begin);
182 R_.resize(Q_.rows(), Q_.cols());
183 for (int i=Q_.beginRows(); i< Q_.endRows(); ++i)
184 {
185 int jmax = std::min(i, Q_.endCols());
186 for (int j=Q_.beginCols(); j< jmax; ++j) { Q_(i,j) = a(i,j);}
187 for (int j=i; j< Q_.endCols(); ++j) { R_(i,j) = a(i,j);}
188 }
189 for (int j=Q_.beginCols(); j<end; ++j) { Q_(j,j) = -tau[j];}
190 return true;
191}
192
193
194} // namespace lapack
195
196} // namespace STK
197
198#endif /* STK_LAPACK_QR_H */
In this file, we define the final class Array2D.
In this file we implement the final class CArrayVector.
In this file we implement the final class CArray.
In this file we define the Interface class IQr (QR decomposition).
#define STKERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:49
#define STKERROR_1ARG(Where, Arg, Error)
Definition STK_Macros.h:61
In this file we define and implement utilities classes and methods for the interface with lapack.
hidden::Traits< Array2D< Real > >::Col Col
hidden::Traits< Array2D< Real > >::Row Row
Derived & resize(Range const &I, Range const &J)
resize the array.
Derived & shift(int beginRows, int beginCols)
shift the Array.
Type *const & p_data() const
The class IQr is an interface class for the method computing the QR Decomposition of a ArrayXX.
Definition STK_IQr.h:102
IQr & operator=(IQr const &decomp)
Operator = : overwrite the this with decomp.
Definition STK_IQr.h:139
ArrayUpperTriangularXX R_
R Array of th QR decomposition.
Definition STK_IQr.h:219
Array Q_
Q Array of the QR decomposition.
Definition STK_IQr.h:217
String msg_error_
String with the last error message.
Definition STK_IRunner.h:96
String const & error() const
get the last error message.
Definition STK_IRunner.h:82
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
The class Qr perform the QR decomposition of an ArrayXX.
Definition STK_Qr.h:98
virtual Qr * clone() const
clone pattern
Qr(ArrayBase< Derived > const &data)
Constructor.
Qr(ArrayXX const &data, bool ref=false)
Default constructor.
Qr(Qr const &decomp)
Copy constructor.
bool computeQr(CArrayXX &a, CVectorX &tau)
private method for computing the Qr decomposition using a CArrayXX array
virtual ~Qr()
virtual destructor
bool runImpl()
Run qr decomposition Launch geqrf LAPACK routine to perform the qr decomposition.
Qr & operator=(Qr const &decomp)
Operator = : overwrite the Qr with decomp.
int geqrf(int m, int n, Real *a, int lda, Real *tau, Real *work, int lWork)
wrapper of the LAPACK DGEQRF routine: computes the Qr decomposition of a matrix.
double Real
STK fundamental type of Real values.
The namespace STK is the main domain space of the Statistical ToolKit project.
traits class for the algebra methods.