STK++ 0.9.13
STK::lapack::MultiLeastSquare< ArrayB, ArrayA > Class Template Reference

The class MultiLeastSQquare solve the least square problem when the response b is multidimensional. More...

#include <STK_lapack_MultiLeastSquare.h>

Inheritance diagram for STK::lapack::MultiLeastSquare< ArrayB, ArrayA >:
Inheritance graph

Public Types

typedef ILeastSquare< MultiLeastSquare< ArrayB, ArrayA > > Base
 

Public Member Functions

 MultiLeastSquare (ArrayB const &b, ArrayA const &a, bool isBref=false, bool isAref=false)
 constructor
 
template<class ArrayB_ , class ArrayA_ >
 MultiLeastSquare (ExprBase< ArrayB_ > const &b, ExprBase< ArrayA_ > const &a)
 template constructor
 
virtual ~MultiLeastSquare ()
 Destructor.
 
Real rcond () const
 
CVectorX consts () const
 return the array with the singular values of A
 
void setRcond (Real rcond)
 
bool runImpl ()
 solve the multi-linear least square problem.
 
template<class Weights >
bool runImpl (Weights const &weights)
 solve the weighted least square problem.
 
bool runImpl ()
 
- Public Member Functions inherited from STK::ILeastSquare< MultiLeastSquare< ArrayB, ArrayA > >
virtual ~ILeastSquare ()
 Destructor.
 
Integer constrank () const
 
ArrayA consta () const
 
ArrayB constb () const
 
ArrayB constx () const
 
virtual bool run ()
 Compute the Least-Square solution.
 
bool run (VecWeights const &weights)
 Compute the weighted Least-Square solution.
 
bool run (Weights const &weights)
 
- 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 Attributes

Real rcond_
 condition number used for determining the effective rank of A
 
CVectorX s_
 Array of the singular values.
 
- Protected Attributes inherited from STK::ILeastSquare< MultiLeastSquare< ArrayB, ArrayA > >
ArrayB b_
 Array or vector of the left hand side.
 
ArrayA a_
 Array of the right hand side.
 
ArrayB x_
 Array of the solution (a vector if b is a vector, a matrix otherwise)
 
Integer rank_
 rank of 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
 

Private Member Functions

bool computeLS (CArrayXX &b, CArrayXX &a)
 private method for computing the LS solution
 

Additional Inherited Members

- Protected Types inherited from STK::ILeastSquare< MultiLeastSquare< ArrayB, ArrayA > >
typedef hidden::AlgebraTraits< MultiLeastSquare< ArrayB, ArrayA > >::ArrayB ArrayB
 
typedef hidden::AlgebraTraits< MultiLeastSquare< ArrayB, ArrayA > >::ArrayA ArrayA
 
- Protected Member Functions inherited from STK::ILeastSquare< MultiLeastSquare< ArrayB, ArrayA > >
 ILeastSquare (ArrayB const &b, ArrayA const &a, bool isBref=false, bool isAref=false)
 Default constructor.
 
 ILeastSquare (ExprBase< OtherArrayB > const &b, ExprBase< OtherArrayA > const &a)
 template 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.
 

Detailed Description

template<class ArrayB, class ArrayA>
class STK::lapack::MultiLeastSquare< ArrayB, ArrayA >

The class MultiLeastSQquare solve the least square problem when the response b is multidimensional.

The class MultiLeastSquare allows to solve the least-square problem

\[
\min_{x} \|b - A*x\|^2.
\]

  • A is an m-by-n matrix which may be rank-deficient.
  • B can be a vector or a Matrix.

It computes the minimum-norm solution to a real linear least squares problem: minimize 2-norm(| b - A*x |) using the singular value decomposition (SVD) of A. A is an M-by-N matrix which may be rank-deficient.

See also
STK::ILeastSquare, STK::MultiLeastSquare, STK::lapack::MultiLeastSquare

Definition at line 101 of file STK_lapack_MultiLeastSquare.h.

Member Typedef Documentation

◆ Base

Constructor & Destructor Documentation

◆ MultiLeastSquare() [1/2]

template<class ArrayB , class ArrayA >
STK::lapack::MultiLeastSquare< ArrayB, ArrayA >::MultiLeastSquare ( ArrayB const b,
ArrayA const a,
bool  isBref = false,
bool  isAref = false 
)
inline

constructor

Parameters
b,athe left hand side and the right hand side of the least square problem.
isBref,isArefare the left hand side and the right hand side references ?

Definition at line 113 of file STK_lapack_MultiLeastSquare.h.

114 : Base(b, a, isBref, isAref), rcond_(-1) {};
ILeastSquare< MultiLeastSquare< ArrayB, ArrayA > > Base
Real rcond_
condition number used for determining the effective rank of A

◆ MultiLeastSquare() [2/2]

template<class ArrayB , class ArrayA >
template<class ArrayB_ , class ArrayA_ >
STK::lapack::MultiLeastSquare< ArrayB, ArrayA >::MultiLeastSquare ( ExprBase< ArrayB_ > const b,
ExprBase< ArrayA_ > const a 
)
inline

template constructor

Parameters
b,athe left hand side and the right hand side of the least square problem.

Definition at line 120 of file STK_lapack_MultiLeastSquare.h.

121 : Base(b, a), rcond_(-1) {}

◆ ~MultiLeastSquare()

Destructor.

Definition at line 123 of file STK_lapack_MultiLeastSquare.h.

123{};

Member Function Documentation

◆ computeLS()

template<class ArrayB , class ArrayA >
bool STK::lapack::MultiLeastSquare< ArrayB, ArrayA >::computeLS ( CArrayXX b,
CArrayXX a 
)
private

private method for computing the LS solution

Definition at line 196 of file STK_lapack_MultiLeastSquare.h.

197{
198 Range arows = a.rows(), acols = a.cols(), brows = b.rows(), bcols = b.cols();
199 int m = arows.size(), n= acols.size(), nrhs = bcols.size();
200 // resize if necessary
201 if (brows.size()<n)
202 {
203 if (!b.isRef())
204 {
205 CArrayXX tmp(b);
206 b.resize(acols,bcols);
207 b.sub(brows, bcols) = tmp;
208 }
209 else
211 }
212 int lda = a.sizeRows(), ldb = b.sizeRows();
213 // shift to 0
214 a.shift(0,0);
215 b.shift(0,0);
216 s_.resize(m); s_ = 0.;
217 Range srange = s_.range();
218 s_.shift(0);
219 // get sizes
220 Real wkopt;
221 int lWork = -1, iwkopt;
222 int info = gelsd( m, n, nrhs
223 , a.p_data(), lda
224 , b.p_data(), ldb
225 , s_.p_data()
226 , &rcond_, &rank_
227 , &wkopt, lWork, &iwkopt);
228 if( info != 0 )
229 {
230 if (info>0)
232 return false;
233 }
235 return false;
236 }
237 // get working sizes
238 lWork = (int)wkopt;
239 Real* work = new Real[lWork];
240 int* iwork = new int[iwkopt];
241 // Solve the least square problem
242 info = gelsd( m, n, nrhs
243 , a.p_data(), lda
244 , b.p_data(), ldb
245 , s_.p_data()
246 , &rcond_, &rank_
247 , work, lWork, iwork);
248 delete[] iwork;
249 delete[] work;
250 if( info != 0 )
251 {
252 if (info>0)
254 return false;
255 }
257 return false;
258 }
259 // shift back
260 a.shift(arows.begin(), acols.begin());
261 b.shift(brows.begin(), bcols.begin());
262 s_.shift(srange.begin());
263 x_ = b.sub(acols,bcols);
264 return true;
265}
#define STKERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:49
#define STKERROR_1ARG(Where, Arg, Error)
Definition STK_Macros.h:61
#define STKRUNTIME_ERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:138
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
ArrayB x_
Array of the solution (a vector if b is a vector, a matrix otherwise)
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
bool computeLS(CArrayXX &b, CArrayXX &a)
private method for computing the LS solution
CVectorX s_
Array of the singular values.
int gelsd(int m, int n, int nrhs, Real *a, int lda, Real *b, int ldb, Real *s, Real *rcond, int *rank, Real *work, int lWork, int *iwork)
wrapper of the LAPACK GELSD routine: computes the minimum-norm solution to a real linear least square...
double Real
STK fundamental type of Real values.
CArray< Real, UnknownSize, UnknownSize, Arrays::by_col_ > CArrayXX
Definition STK_CArray.h:50
TRange< UnknownSize > Range
Definition STK_Range.h:59

References STK::lapack::MultiLeastSquare< ArrayB, ArrayA >::computeLS(), STK::lapack::gelsd(), STK::ICArray< Derived >::isRef(), STK::ICArray< Derived >::p_data(), STK::ICArray< Derived >::resize(), STK::ICArray< Derived >::shift(), STKERROR_1ARG, STKERROR_NO_ARG, STKRUNTIME_ERROR_NO_ARG, and STK::ICArray< Derived >::sub().

Referenced by STK::lapack::MultiLeastSquare< ArrayB, ArrayA >::computeLS().

◆ rcond()

template<class ArrayB , class ArrayA >
Real STK::lapack::MultiLeastSquare< ArrayB, ArrayA >::rcond ( ) const
inline
Returns
the condition number

Definition at line 125 of file STK_lapack_MultiLeastSquare.h.

125{ return rcond_;}

References STK::lapack::MultiLeastSquare< ArrayB, ArrayA >::rcond_.

Referenced by STK::lapack::MultiLeastSquare< ArrayB, ArrayA >::setRcond().

◆ runImpl() [1/3]

solve the multi-linear least square problem.

Launch gelsd LAPACK routine to perform the decomposition.

Returns
true if no error occur, false otherwise

Definition at line 159 of file STK_lapack_MultiLeastSquare.h.

160{
161 Range brows = (b_.sizeRows()< a_.sizeCols()) ? a_.cols() : b_.rows();
162 // local arrays, b is resized if necessary
163 CArrayXX a(a_), b(brows, b_.cols());
164 b.sub(b_.rows(), b_.cols()) = b_;
165 // start
166 return computeLS(b,a);
167}
ArrayB b_
Array or vector of the left hand side.

References STK::ICArray< Derived >::sub().

◆ runImpl() [2/3]

Definition at line 190 of file STK_lapack_MultiLeastSquare.h.

191{ return computeLS(b_,a_);}

◆ runImpl() [3/3]

template<class ArrayB , class ArrayA >
template<class Weights >
bool STK::lapack::MultiLeastSquare< ArrayB, ArrayA >::runImpl ( Weights const weights)

solve the weighted least square problem.

Launch gelsd LAPACK routine to perform the decomposition.

Returns
true if no error occur, false otherwise

Definition at line 174 of file STK_lapack_MultiLeastSquare.h.

175{
177 Range brows = (b_.sizeRows()< a_.sizeCols()) ? a_.cols() : b_.rows();
178 // local arrays, b is resized if necessary
179 CArrayXX a(w.sqrt().diagonalize() * a_), b(brows, b_.cols());
180 b.sub(b_.rows(), b_.cols()) = w.sqrt().diagonalize() * b_;
181 // start
182 return computeLS(b,a);
183}
#define STK_STATIC_ASSERT_ONE_DIMENSION_ONLY(EXPR)

References STK_STATIC_ASSERT_ONE_DIMENSION_ONLY, and STK::ICArray< Derived >::sub().

◆ s()

return the array with the singular values of A

Definition at line 127 of file STK_lapack_MultiLeastSquare.h.

127{ return s_;}

References STK::lapack::MultiLeastSquare< ArrayB, ArrayA >::s_.

◆ setRcond()

template<class ArrayB , class ArrayA >
void STK::lapack::MultiLeastSquare< ArrayB, ArrayA >::setRcond ( Real  rcond)
inline
Parameters
rcondthe condition number. If rcond<0, the machine precision is used.

Definition at line 130 of file STK_lapack_MultiLeastSquare.h.

References STK::lapack::MultiLeastSquare< ArrayB, ArrayA >::rcond(), and STK::lapack::MultiLeastSquare< ArrayB, ArrayA >::rcond_.

Member Data Documentation

◆ rcond_

condition number used for determining the effective rank of A

Definition at line 145 of file STK_lapack_MultiLeastSquare.h.

Referenced by STK::lapack::MultiLeastSquare< ArrayB, ArrayA >::rcond(), and STK::lapack::MultiLeastSquare< ArrayB, ArrayA >::setRcond().

◆ s_

Array of the singular values.

Definition at line 147 of file STK_lapack_MultiLeastSquare.h.

Referenced by STK::lapack::MultiLeastSquare< ArrayB, ArrayA >::s().


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