STK++ 0.9.13
STK_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 * Purpose: Define the Qr Class.
28 * Author: Serge Iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 *
30 **/
31
36#ifndef STK_QR_H
37#define STK_QR_H
38
39#include "STK_IQr.h"
40#include "STK_Givens.h"
43
44#ifdef STK_ALGEBRA_DEBUG
46
47template< class Container2D >
48void print(Container2D const& A, STK::String const& name)
49{
50 stk_cout << "print: " << name << _T("\n";);
51 stk_cout << name << _T(".isRef() =") << A.isRef() << _T("\n");
52 stk_cout << name << _T(".cols() =") << A.cols() << _T("\n");
53 stk_cout << name << _T(".rows() =") << A.rows() << _T("\n\n");
54}
55
56void print(ArrayXX const& A, STK::String const& name)
57{
58 stk_cout << "print: " << name << _T("\n";);
59 stk_cout << name << _T(".isRef() =") << A.isRef() << _T("\n");
60 stk_cout << name << _T(".cols() =") << A.cols() << _T("\n");
61 stk_cout << name << _T(".rows() =") << A.rows() << _T("\n\n");
62 stk_cout << name << _T(".rangeCols().isRef() =") << A.rangeCols().isRef() << _T("\n");
63 stk_cout << name << _T(".rangeCols() =\n") << A.rangeCols() << _T("\n");
64}
65#endif
66
67namespace STK
68{
69// forward declaration
70class Qr;
71
72namespace hidden
73{
77template<>
79{
80 typedef ArrayXX Array;
81 typedef typename Array::Col ColVector;
82 typedef typename Array::Row RowVector;
83};
84
85} // namespace hidden
86
97class Qr: public IQr<Qr >
98{
99 public :
102 typedef IQr<Qr> Base;
103 using Base::Q_;
104 using Base::R_;
105
110 inline Qr( ArrayXX const& A, bool ref = false): Base(A, ref) {}
114 template<class Derived>
115 inline Qr( ExprBase<Derived> const& data): Base(data) {}
119 inline Qr( Qr const& decomp): Base(decomp) {}
121 inline virtual ~Qr() {}
123 inline virtual Qr* clone() const { return new Qr(*this);}
125 inline Qr& operator=(Qr const& decomp)
126 { Base::operator=(decomp);
127 return *this;
128 }
130 inline bool runImpl() { qr(); return true;}
131
132 private:
134 void qr();
135};
136
137/* Computation of the QR decomposition */
138inline void Qr::qr()
139{
140 R_.resize(Q_.rows(), Q_.cols());
141 // start left householder reflections
142 Range r(Q_.rows()), c(Q_.cols());
143 for(int j = R_.beginRows(); (j < R_.endRows()) && (j < R_.endCols()) ; ++j)
144 {
145 ColVector u(Q_, r, j);// get a reference on the j-th column in the range r
146 R_(j, j) = house(u); // compute the housolder vector
147 applyLeftHouseholderVector(Q_.sub(r, c.incFirst(1)), u); // apply-it to the remaining part of Q_
148 R_.row(j, c).copy(Q_.row(j, c)); // copy current row of Q_ in the range c in R_
149 r.incFirst(1); // decrease the range
150 }
151}
152
153
154} // namespace STK
155
156#endif
157// STK_QR_H
158
A Array2DPoint is a one dimensional horizontal container.
In this file, we define the final class Array2D.
This file define methods for displaying Arrays and Expressions.
In this file we define Givens methods used by the Algebra classes.
In this file we define the Interface class IQr (QR decomposition).
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
hidden::Traits< Array2D< Real > >::Col Col
hidden::Traits< Array2D< Real > >::Row Row
int beginRows() const
int endRows() const
Derived & resize(Range const &I, Range const &J)
resize the array.
int endCols() const
Row row(int i) const
access to a part of a row.
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
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
Qr(ArrayXX const &A, bool ref=false)
Default constructor.
Definition STK_Qr.h:110
IQr< Qr > Base
Definition STK_Qr.h:102
Qr & operator=(Qr const &decomp)
Operator = : overwrite the Qr with decomp.
Definition STK_Qr.h:125
virtual Qr * clone() const
clone pattern
Definition STK_Qr.h:123
virtual ~Qr()
virtual destructor
Definition STK_Qr.h:121
Qr(Qr const &decomp)
Copy constructor.
Definition STK_Qr.h:119
hidden::AlgebraTraits< Qr >::ColVector ColVector
Definition STK_Qr.h:100
bool runImpl()
Compute the QR decomposition.
Definition STK_Qr.h:130
void qr()
Compute the qr decomposition of the matrix Q_.
Definition STK_Qr.h:138
Qr(ExprBase< Derived > const &data)
Constructor.
Definition STK_Qr.h:115
hidden::AlgebraTraits< Qr >::RowVector RowVector
Definition STK_Qr.h:101
Index sub-vector region: Specialization when the size is unknown.
Definition STK_Range.h:265
void applyLeftHouseholderVector(ArrayBase< Lhs > const &M, ExprBase< Rhs > const &v)
left multiplication by a Householder vector.
Real house(ArrayBase< Vector > &x)
Compute the Householder vector v of a vector x.
std::basic_string< Char > String
STK fundamental type of a String.
The namespace STK is the main domain space of the Statistical ToolKit project.
traits class for the algebra methods.