STK++ 0.9.13
STK::IQr< Derived > Class Template Reference

The class IQr is an interface class for the method computing the QR Decomposition of a ArrayXX. More...

#include <STK_IQr.h>

Inheritance diagram for STK::IQr< Derived >:
Inheritance graph

Public Member Functions

virtual ~IQr ()
 virtual destructor
 
IQroperator= (IQr const &decomp)
 Operator = : overwrite the this with decomp.
 
bool isCompQ () const
 
int rank () const
 
Array constQ () const
 give the matrix Q of the QR decomposition.
 
ArrayUpperTriangularXX constR () const
 give the matrix R of the QR decomposition.
 
void setCompQ (bool compq)
 Set the compq_ flag.
 
virtual bool run ()
 Compute the QR decomposition.
 
void compQ ()
 Compute Q (to use after run).
 
void popBackCols (int n=1)
 Delete the n last columns and update the QR decomposition.
 
void eraseCol (int pos)
 Delete the column pos and update the QR decomposition.
 
template<class ColVector_ >
void pushBackCol (ColVector_ const &T)
 Add a column with value T and update th QR decomposition.
 
template<class ColVector_ >
void insertCol (ColVector_ const &T, int pos)
 Add a column with value T at the position pos and update the QR decomposition.
 
template<class OtherDerived >
void setData (ExprBase< OtherDerived > const &data)
 overloading of setData.
 
- Public Member Functions inherited from STK::IRunnerBase
String consterror () const
 get the last error message.
 
- Public Member Functions inherited from STK::IRecursiveTemplate< Derived >
Derived & asDerived ()
 static cast : return a reference of this with a cast to the derived class.
 
Derived constasDerived () const
 static cast : return a const reference of this with a cast to the derived class.
 
Derived * asPtrDerived ()
 static cast : return a ptr on a Derived of this with a cast to the derived class.
 
Derived constasPtrDerived () const
 static cast : return a ptr on a constant Derived of this with a cast to the derived class.
 
Derived * clone () const
 create a leaf using the copy constructor of the Derived class.
 
Derived * clone (bool isRef) const
 create a leaf using the copy constructor of the Derived class and a flag determining if the clone is a reference or not.
 

Protected Types

typedef hidden::AlgebraTraits< Derived >::Array Array
 
typedef hidden::AlgebraTraits< Derived >::ColVector ColVector
 
typedef hidden::AlgebraTraits< Derived >::RowVector RowVector
 

Protected Member Functions

 IQr (Array const &Q, bool ref=false)
 Constructor.
 
template<class DerivedExpr >
 IQr (ExprBase< DerivedExpr > const &Q)
 Constructor.
 
 IQr (IQr const &decomp)
 Copy constructor.
 
- Protected Member Functions inherited from STK::IRunnerBase
 IRunnerBase ()
 default constructor
 
 IRunnerBase (IRunnerBase const &runner)
 copy constructor
 
virtual ~IRunnerBase ()
 destructor
 
virtual void update ()
 update the runner.
 
- Protected Member Functions inherited from STK::IRecursiveTemplate< Derived >
 IRecursiveTemplate ()
 constructor.
 
 ~IRecursiveTemplate ()
 destructor.
 

Protected Attributes

Array Q_
 Q Array of the QR decomposition.
 
ArrayUpperTriangularXX R_
 R Array of th QR decomposition.
 
bool compq_
 is Q computed ?
 
int rank_
 estimated rank_ of the matrix A
 
- Protected Attributes inherited from STK::IRunnerBase
String msg_error_
 String with the last error message.
 
bool hasRun_
 true if run has been used, false otherwise
 

Detailed Description

template<class Derived>
class STK::IQr< Derived >

The class IQr is an interface class for the method computing the QR Decomposition of a ArrayXX.

The QR decomposition of a matrix require

  • Input: A matrix of size M-by-N
  • Output:
    1. Q Array of size M-by-N.
    2. R Upper Triangular matrix of dimension min(M,N)-by-N
    3. $ A = QR $

Computation of matrices Q and R is triggered by the run() method. Some tuning parameters can be adjusted before running.

The matrix Q is represented as a product of elementary reflectors Q = H(1) H(2) . . . H(k), where k = min(m,n).

Each H(i) has the form H(i) = I - tau * v * v'

Note
This interface class implement the virtual run method of IRunnerBase by calling the derived method runImpl

Definition at line 101 of file STK_IQr.h.

Member Typedef Documentation

◆ Array

template<class Derived >
typedef hidden::AlgebraTraits<Derived>::Array STK::IQr< Derived >::Array
protected

Definition at line 104 of file STK_IQr.h.

◆ ColVector

template<class Derived >
typedef hidden::AlgebraTraits<Derived>::ColVector STK::IQr< Derived >::ColVector
protected

Definition at line 105 of file STK_IQr.h.

◆ RowVector

template<class Derived >
typedef hidden::AlgebraTraits<Derived>::RowVector STK::IQr< Derived >::RowVector
protected

Definition at line 106 of file STK_IQr.h.

Constructor & Destructor Documentation

◆ IQr() [1/3]

template<class Derived >
STK::IQr< Derived >::IQr ( Array const Q,
bool  ref = false 
)
inlineprotected

Constructor.

Parameters
QThe matrix to decompose
refis Q_ a reference ? (avoid data copy, but the original data set is destroyed)

Definition at line 112 of file STK_IQr.h.

113 : IRunnerBase(), Q_(Q, ref), compq_(false), rank_(0)
114 {
115 if (Q.beginRows() != Q.beginCols())
116 STKRUNTIME_ERROR_NO_ARG(IQR::IQR,Wrong data set: Q.beginRows() must be equal to Q.beginCols());
117 }
#define STKRUNTIME_ERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:138
Array const & Q() const
give the matrix Q of the QR decomposition.
Definition STK_IQr.h:155
Array Q_
Q Array of the QR decomposition.
Definition STK_IQr.h:217
int rank_
estimated rank_ of the matrix A
Definition STK_IQr.h:223
bool compq_
is Q computed ?
Definition STK_IQr.h:221
IRunnerBase()
default constructor
Definition STK_IRunner.h:68

References STK::IQr< Derived >::Q(), and STKRUNTIME_ERROR_NO_ARG.

◆ IQr() [2/3]

template<class Derived >
template<class DerivedExpr >
STK::IQr< Derived >::IQr ( ExprBase< DerivedExpr > const Q)
inlineprotected

Constructor.

Parameters
QThe matrix to decompose

Definition at line 122 of file STK_IQr.h.

122 : IRunnerBase(), compq_(false), rank_(0)
123 {
124 if (Q.beginRows() != Q.beginCols())
125 { STKRUNTIME_ERROR_NO_ARG(IQR::IQR,Wrong data set: Q.beginRows() must be equal to Q.beginCols());}
126 Q_ = Q.asDerived();
127 }

References STK::IQr< Derived >::Q(), STK::IQr< Derived >::Q_, and STKRUNTIME_ERROR_NO_ARG.

◆ IQr() [3/3]

template<class Derived >
STK::IQr< Derived >::IQr ( IQr< Derived > const decomp)
inlineprotected

Copy constructor.

Parameters
decompthe decomposition to copy

Definition at line 131 of file STK_IQr.h.

132 : Q_(decomp.Q_), R_(decomp.R_), compq_(decomp.compq_), rank_(decomp.rank_)
133 {}
ArrayUpperTriangularXX R_
R Array of th QR decomposition.
Definition STK_IQr.h:219

◆ ~IQr()

template<class Derived >
virtual STK::IQr< Derived >::~IQr ( )
inlinevirtual

virtual destructor

Definition at line 137 of file STK_IQr.h.

137{}

Member Function Documentation

◆ compQ()

template<class Derived >
void STK::IQr< Derived >::compQ ( )

Compute Q (to use after run).

After the run process, Q_ store the householder vector in its column. Call compQ, if you want to obtain Q in its true form. Without effect if (compq_ == true)

Definition at line 245 of file STK_IQr.h.

246{
247#ifdef STK_ALGEBRA_VERBOSE
248 stk_cout << _T("Entering IQr::compQ()") << _T("\n");
249#endif
250 // if Q_ is computed yet
251 if (compq_) return;
252 // number of non zero cols of Q_
253 int ncol = std::min(Q_.sizeRows(), Q_.sizeCols()), lastCol;
254 // add or remove the column
255 if (ncol < Q_.sizeCols())
256 {
257 Q_.popBackCols(Q_.sizeCols() - ncol);
258 Q_.popBackCols(Q_.sizeCols() - ncol);
259 lastCol = Q_.lastIdxCols();
260 }
261 else
262 {
263 lastCol = Q_.lastIdxCols();
264 if (ncol < Q_.sizeRows())
265 {
266 Q_.pushBackCols(Q_.sizeRows() -ncol);
267 // Initialize added columns
268 Q_.col( _R( lastCol+1, Q_.lastIdxCols()) ).setValue(0);
269 for (int i=lastCol+1; i< Q_.endCols(); ++i) { Q_(i, i) = 1.0;}
270 }
271 }
272 // compute other columns
273 for (int i=lastCol; i>=Q_.beginCols(); i--)
274 {
275 // create reference on current householder vector
276 ColVector u(Q_, _R(i, Q_.lastIdxRows()), i);
277 // Apply Householder vector to the right of the matrix
278 applyLeftHouseholderVector( Q_( _R(i, Q_.lastIdxRows()), _R(i+1, Q_.lastIdxCols())), u);
279 // update the ith column
280 Q_( _R(Q_.beginRows(),i-1) , i ) = 0.0; // 0:(i-1)
281 Q_( _R(i+1, Q_.lastIdxRows()), i ) *= Q_(i,i); // (i+1):M
282 Q_( i , i ) += 1.0; // i:i
283 // update the column i
284 }
285 // Q_ is now computed
286 compq_ = true;
287#ifdef STK_ALGEBRA_VERBOSE
288 stk_cout << _T("Terminating IQr::compQ().") << _T("\n");
289#endif
290}
#define _R(first, last)
Utility macro that can be used in a similar way that first:last.
Definition STK_Range.h:53
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
hidden::AlgebraTraits< Derived >::ColVector ColVector
Definition STK_IQr.h:105
void applyLeftHouseholderVector(ArrayBase< Lhs > const &M, ExprBase< Rhs > const &v)
left multiplication by a Householder vector.

References _R, _T, STK::applyLeftHouseholderVector(), and stk_cout.

◆ eraseCol()

template<class Derived >
void STK::IQr< Derived >::eraseCol ( int  pos)

Delete the column pos and update the QR decomposition.

Parameters
posthe position of the column to delete

Definition at line 298 of file STK_IQr.h.

299{
300 if (pos < R_.beginCols())
302 if (R_.lastIdxCols() < pos)
304 // if Q_ is not computed yet
305 if (!compq_) compQ();
306 // compute the number of iteration for updating to zeroed
307 int niter = std::min(R_.lastIdxCols(), R_.lastIdxRows());//R_.beginCols()-1+std::min(R_.sizeRows(), R_.sizeCols());
308 // Zeroed the remaining elements (z)
309 for (int iter = pos+1; iter<= niter; iter++)
310 {
311 Real sinus, cosinus;
312 // compute the Givens rotation
313 R_(iter-1, iter) = compGivens( R_(iter-1, iter), R_(iter, iter), cosinus, sinus);
314 R_(iter, iter) = 0.0;
315 // if necessary update R_ and Q_
316 if (sinus)
317 {
318 // create a reference on the sub-ArrayXX
319 ArrayUpperTriangularXX Rsub(R_.col( _R(iter+1, R_.lastIdxCols()) ), true);
320 // Update the next rows (iter1:ncolr_) of R_
321 leftGivens(Rsub, iter-1, iter, cosinus, sinus);
322 // Update the cols of Q_
323 rightGivens(Q_, iter-1, iter, cosinus, sinus);
324 }
325 }
326 // erase the column pos
327 R_.eraseCols(pos);
328 // update the range of the remaining cols of the container
329 R_.update( Range(pos, std::min(R_.lastIdxRows(), R_.lastIdxCols()), 0));
330}
#define STKOUT_OF_RANGE_1ARG(Where, Arg, Error)
Definition STK_Macros.h:93
int lastIdxCols() const
int lastIdxRows() const
void update(Range const &J)
Update columns of the array at a specified range.
Col col(int j) const
access to a part of a column.
int beginCols() const
void eraseCols(int pos, int n=1)
Delete n columns at the specified position of the array.
void compQ()
Compute Q (to use after run).
Definition STK_IQr.h:245
void eraseCol(int pos)
Delete the column pos and update the QR decomposition.
Definition STK_IQr.h:298
void rightGivens(ArrayBase< TContainer2D > &M, int j1, int j2, typename TContainer2D::Type const &cosinus, typename TContainer2D::Type const &sinus)
Apply Givens rotation.
Definition STK_Givens.h:119
Real compGivens(Type const &y, Type const &z, Type &cosinus, Type &sinus)
Compute Givens rotation.
Definition STK_Givens.h:75
void leftGivens(ArrayBase< TContainer2D > &M, int i1, int i2, typename TContainer2D::Type const &cosinus, typename TContainer2D::Type const &sinus)
left multiplication by a Givens ArrayXX.
Definition STK_Givens.h:148
Array2DUpperTriangular< Real > ArrayUpperTriangularXX
Specialization of the Array2D class for Type values.
double Real
STK fundamental type of Real values.
TRange< UnknownSize > Range
Definition STK_Range.h:59

References _R, STK::compGivens(), STK::IQr< Qr >::eraseCol(), STK::leftGivens(), STK::rightGivens(), and STKOUT_OF_RANGE_1ARG.

Referenced by main().

◆ insertCol()

template<class Derived >
template<class ColVector_ >
void STK::IQr< Derived >::insertCol ( ColVector_ const T,
int  pos 
)

Add a column with value T at the position pos and update the QR decomposition.

Parameters
Tthe column to insert
posthe position of the column to insert

Definition at line 370 of file STK_IQr.h.

371{
372 STK_STATIC_ASSERT(ColVector_::structure_==(int)Arrays::vector_||ColVector_::structure_==(int)Arrays::point_,YOU_HAVE_TO_USE_A_VECTOR_OR_POINT_IN_THIS_METHOD)
373 if (pos < R_.beginCols())
375 if (R_.lastIdxCols() < pos)
377 if (T.range() != Q_.rows())
378 { STKRUNTIME_ERROR_1ARG(Qr::insertCol,pos,T.range() != Q_.rows());}
379 // if Q_ is not computed yet
380 if (!compq_) compQ();
381 // Adding a column to R
382 R_.insertCols(pos);
383 // update the range of the remaining cols of R_
384 R_.update( _R(pos+1, std::min(R_.lastIdxRows(), R_.lastIdxCols())) );
385 for (int i=pos+1; i< std::min(R_.endRows(), R_.endCols()); ++i) R_(i,i) = 0.0;
386
387 VectorX Rpos = Q_.transpose() * T;
388 // Zeroed the unwanted elements
389 for (int iter= Q_.lastIdxCols(), iterm1= Q_.lastIdxCols()-1; iter>pos; iterm1--, iter--)
390 {
391 Real sinus, cosinus;
392 // compute the Givens rotation
393 Rpos[iterm1] = compGivens(Rpos[iterm1], Rpos[iter], cosinus, sinus);
394 // apply Givens rotation if necessary
395 if (sinus)
396 {
397 // create a reference on the sub-ArrayXX
398 ArrayUpperTriangularXX Rsub(R_.col(_R(iter, R_.lastIdxCols())), true);
399 // Update the next rows (iter:ncolr_) of R_
400 leftGivens( Rsub, iterm1, iter, cosinus, sinus);
401 // Update the cols of Q_
402 rightGivens(Q_, iterm1, iter, cosinus, sinus);
403 }
404 }
405 // update R_
406 R_.col(pos) = Rpos.sub(R_.rangeRowsInCol(pos));
407 R_.update(pos);
408}
#define STKRUNTIME_ERROR_1ARG(Where, Arg, Error)
Definition STK_Macros.h:129
#define STK_STATIC_ASSERT(COND, MSG)
TransposeOperator< Derived > const transpose() const
int endRows() const
void insertCols(int pos, int n=1)
Insert n columns at the index pos to the array.
int endCols() const
void insertCol(ColVector_ const &T, int pos)
Add a column with value T at the position pos and update the QR decomposition.
Definition STK_IQr.h:370
@ point_
row oriented vector/array/expression
@ vector_
column oriented vector/array/expression
Array2DVector< Real > VectorX

References _R, STK::compGivens(), STK::IQr< Qr >::insertCol(), STK::leftGivens(), STK::Arrays::point_, STK::rightGivens(), STK_STATIC_ASSERT, STKOUT_OF_RANGE_1ARG, STKRUNTIME_ERROR_1ARG, STK::IRunnerSupervised< YArray_, XArray_, Weights_ >::update(), and STK::Arrays::vector_.

Referenced by main().

◆ isCompQ()

template<class Derived >
bool STK::IQr< Derived >::isCompQ ( ) const
inline
Returns
true if Q_ is computed, false otherwise

Definition at line 149 of file STK_IQr.h.

149{ return compq_;}

References STK::IQr< Derived >::compq_.

◆ operator=()

template<class Derived >
IQr & STK::IQr< Derived >::operator= ( IQr< Derived > const decomp)
inline

Operator = : overwrite the this with decomp.

Definition at line 139 of file STK_IQr.h.

140 {
141 Q_ = decomp.Q_;
142 R_ = decomp.R_;
143 compq_ = decomp.compq_;
144 rank_ = decomp.rank_;
145 return *this;
146 }

References STK::IQr< Derived >::compq_, STK::IQr< Derived >::Q_, STK::IQr< Derived >::R_, and STK::IQr< Derived >::rank_.

◆ popBackCols()

template<class Derived >
void STK::IQr< Derived >::popBackCols ( int  n = 1)

Delete the n last columns and update the QR decomposition.

Parameters
nnumber of column to delete

Definition at line 293 of file STK_IQr.h.

293{ R_.popBackCols(n);}
void popBackCols(int n=1)
Delete last columns of the array.

◆ pushBackCol()

template<class Derived >
template<class ColVector_ >
void STK::IQr< Derived >::pushBackCol ( ColVector_ const T)

Add a column with value T and update th QR decomposition.

Parameters
Tthe column to add

Definition at line 335 of file STK_IQr.h.

336{
337 STK_STATIC_ASSERT(ColVector_::structure_==(int)Arrays::vector_||ColVector_::structure_==(int)Arrays::point_,YOU_HAVE_TO_USE_A_VECTOR_OR_POINT_IN_THIS_METHOD)
338 // check conditions
339 if (T.range() != Q_.rows())
340 { STKRUNTIME_ERROR_NO_ARG(Qr::pushBackCol,T.range() != Q_.rows());}
341 // if Q_ is not computed yet
342 if (!compq_) compQ();
343 // Adding a column to R
344 int lastColR = R_.endCols();
345 // Create an auxiliary container
346 VectorX Rncolr = Q_.transpose() * T; // Rncolr of size Q_.cols()
347 // update Q_
348 for (int iter = Q_.lastIdxCols()-1, iter1 = Q_.lastIdxCols(); iter>=lastColR; iter--, iter1--)
349 {
350 Real sinus, cosinus;
351 // compute the Givens rotation
352 Rncolr[iter] = compGivens( Rncolr[iter], Rncolr[iter1], cosinus, sinus);
353 // apply Givens rotation if necessary
354 if (sinus)
355 { rightGivens(Q_, iter, iter1, cosinus, sinus);}
356 }
357 // update R_
359 R_.col(lastColR).copy(Rncolr.sub(R_.rangeRowsInCol(lastColR)));
360}
void pushBackCols(int n=1)
Add n columns at the end of the array.
void pushBackCol(ColVector_ const &T)
Add a column with value T and update th QR decomposition.
Definition STK_IQr.h:335

References STK::compGivens(), STK::Arrays::point_, STK::IQr< Qr >::pushBackCol(), STK::rightGivens(), STK_STATIC_ASSERT, STKRUNTIME_ERROR_NO_ARG, and STK::Arrays::vector_.

◆ Q()

template<class Derived >
Array const & STK::IQr< Derived >::Q ( ) const
inline

give the matrix Q of the QR decomposition.

Returns
the matrix Q of the QR decomposition

Definition at line 155 of file STK_IQr.h.

155{ return Q_;}

References STK::IQr< Derived >::Q_.

Referenced by STK::IQr< Derived >::IQr(), STK::IQr< Derived >::IQr(), and main().

◆ R()

template<class Derived >
ArrayUpperTriangularXX const & STK::IQr< Derived >::R ( ) const
inline

give the matrix R of the QR decomposition.

Returns
the matrix R of the QR decomposition

Definition at line 159 of file STK_IQr.h.

159{ return R_;}

References STK::IQr< Derived >::R_.

Referenced by main().

◆ rank()

template<class Derived >
int STK::IQr< Derived >::rank ( ) const
inline
Returns
the rank of the matrix Q

Definition at line 151 of file STK_IQr.h.

151{ return rank_;}

References STK::IQr< Derived >::rank_.

◆ run()

template<class Derived >
bool STK::IQr< Derived >::run ( )
virtual

Compute the QR decomposition.

Delegate to Derived classes the concrete computation of the decomposition using runImpl method.

Returns
true if the computation succeed, false otherwise

Implements STK::IRunnerBase.

Definition at line 227 of file STK_IQr.h.

228{
229 // if Q is empty, default values are ok
230 if (Q_.empty()) { compq_ = true; return true;}
231 // compute QR decomposition
232 if (!this->asDerived().runImpl()) return false;
233 // compute Q if asked
234 if (compq_) compQ();
235 // compute rank_ using maximal number of R_
236 int r = R_.beginRows();
237 Real tol = Arithmetic<Real>::epsilon() * std::abs(R_(r,r));
238
239 int n = std::min(R_.endRows(), R_.endCols());
240 for(r= R_.beginRows(), rank_=0; r<n; ++r, ++rank_) { if (std::abs(R_(r,r)) < tol) break;}
241 return true;
242}
int beginRows() const
Derived & asDerived()
static cast : return a reference of this with a cast to the derived class.

Referenced by main().

◆ setCompQ()

template<class Derived >
void STK::IQr< Derived >::setCompQ ( bool  compq)
inline

Set the compq_ flag.

By default compq is true.

Parameters
compqtrue if we want to compute the Q matrix, false otherwise

Definition at line 165 of file STK_IQr.h.

165{ compq_ = compq;}

References STK::IQr< Derived >::compq_.

◆ setData()

template<class Derived >
template<class OtherDerived >
void STK::IQr< Derived >::setData ( ExprBase< OtherDerived > const data)
inline

overloading of setData.

Parameters
datathe data set to set.

Definition at line 204 of file STK_IQr.h.

205 { Q_ = data.asDerived(); R_.clear(); compq_ = false; rank_ = 0;}
void clear()
clear the object.

References STK::IArray2D< Derived >::clear(), STK::IQr< Derived >::compq_, STK::IQr< Derived >::Q_, STK::IQr< Derived >::R_, and STK::IQr< Derived >::rank_.

Member Data Documentation

◆ compq_

template<class Derived >
bool STK::IQr< Derived >::compq_
protected

◆ Q_

template<class Derived >
Array STK::IQr< Derived >::Q_
protected

Q Array of the QR decomposition.

Definition at line 217 of file STK_IQr.h.

Referenced by STK::IQr< Derived >::IQr(), STK::IQr< Derived >::operator=(), STK::IQr< Derived >::Q(), and STK::IQr< Derived >::setData().

◆ R_

template<class Derived >
ArrayUpperTriangularXX STK::IQr< Derived >::R_
protected

R Array of th QR decomposition.

Definition at line 219 of file STK_IQr.h.

Referenced by STK::IQr< Derived >::operator=(), STK::IQr< Derived >::R(), and STK::IQr< Derived >::setData().

◆ rank_

template<class Derived >
int STK::IQr< Derived >::rank_
protected

estimated rank_ of the matrix A

Definition at line 223 of file STK_IQr.h.

Referenced by STK::IQr< Derived >::operator=(), STK::IQr< Derived >::rank(), and STK::IQr< Derived >::setData().


The documentation for this class was generated from the following file: