STK++ 0.9.13
STK::lapack::Svd Class Reference

{ More...

#include <STK_lapack_Svd.h>

Inheritance diagram for STK::lapack::Svd:
Inheritance graph

Public Types

typedef ISvd< SvdBase
 
typedef hidden::Traits< CArrayXX >::Col ColVector
 
typedef hidden::Traits< CArrayXX >::Row RowVector
 

Public Member Functions

 Svd (CArrayXX const &A, bool ref=false, bool withU=true, bool withV=true)
 Default constructor.
 
template<class OtherArray >
 Svd (ArrayBase< OtherArray > const &A, bool withU=true, bool withV=true)
 constructor with other kind of array/expression
 
 Svd (Svd const &decomp)
 Copy constructor.
 
virtual ~Svd ()
 virtual destructor
 
virtual Svdclone () const
 clone pattern
 
Svdoperator= (Svd const &decomp)
 Operator = : overwrite the Svd with decomp.
 
char jobz () const
 
void setJobz (char jobz)
 set the option chosen for the svd
 
bool runImpl ()
 Run svd decomposition.
 
- Public Member Functions inherited from STK::ISvd< Svd >
Type det () const
 
Type trace () const
 
Type norm () const
 
int rank () const
 
ArrayU constU () const
 
ArrayV constV () const
 
ArrayD constD () const
 
virtual bool run ()
 implement the run method
 
void setData (OtherArray const &A, bool withU=true, bool withV=true)
 Set a new data set to ISvd class.
 
OtherArrayginv (OtherArray &res) const
 Compute the generalized inverse of the matrix and put the result in res.
 
- 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.
 

Private Member Functions

bool computeSvd (CArrayXX &a, CArrayXX &u, CVectorX &s, CArrayXX &v)
 compute the svd decomposition.
 

Private Attributes

char jobz_
 option
 

Additional Inherited Members

- Protected Types inherited from STK::ISvd< Svd >
typedef hidden::AlgebraTraits< Svd >::ArrayU ArrayU
 
typedef hidden::AlgebraTraits< Svd >::ArrayD ArrayD
 
typedef hidden::AlgebraTraits< Svd >::ArrayV ArrayV
 
typedef ArrayU::Type Type
 
- Protected Member Functions inherited from STK::ISvd< Svd >
 ISvd (ArrayU const &A, bool ref, bool withU=true, bool withV=true)
 Default constructor.
 
 ISvd (ArrayBase< OtherDerived > const &A, bool withU=true, bool withV=true)
 constructor with other kind of array/expression
 
 ISvd (ISvd const &S)
 Copy Constructor.
 
virtual ~ISvd ()
 destructor.
 
ISvdoperator= (const ISvd &S)
 Operator = : overwrite the ISvd with S.
 
virtual void finalize ()
 Finalize any operations that have to be done after the computation of the decomposition.
 
int nrowU () const
 
int ncolU () const
 
int nrowD () const
 
int ncolD () const
 
int nrowV () const
 
int ncolV () const
 
- 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 inherited from STK::ISvd< Svd >
ArrayU U_
 U_ matrix.
 
ArrayV V_
 V_ matrix.
 
ArrayD D_
 Diagonal array of the singular values.
 
bool withU_
 Compute U_ ?
 
bool withV_
 Compute V_ ?
 
Type norm_
 trace norm
 
int rank_
 rank
 
Type trace_
 trace norm
 
Type det_
 determinant
 
- 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

{

Svd computes the SVD decomposition of a real matrix using the Lapack routine dgeqrf.

The method take as:

  • input: a matrix A(nrow,ncol)
  • output:
    1. U Array (nrow,ncol).
    2. D diagonal matrix (min(norw,ncol))
    3. V Array (ncol,ncol). and perform the decomposition:
  • A = UDV' (transpose V).

    Output can be tuned using jobz option:

    *      jobz is Char*1
    *      Specifies options for computing all or part of the matrix u:
    *      = 'A':  all m columns of u and all n rows of v**T are
    *              returned in the arrays u and vt;
    *      = 'S':  the first min(m,n) columns of u and the first
    *              min(m,n) rows of v**T are returned in the arrays U
    *              and vt;
    *      = 'O':  If m >= n, the first n columns of u are overwritten
    *              on the array a and all rows of v**T are returned in
    *              the array vt;
    *              otherwise, all columns of u are returned in the
    *              array u and the first m rows of v**T are overwritten
    *              in the array a;
    *      = 'N':  no columns of u or rows of v**T are computed.
    *   
See also
SK::ISvd, STK::Svd

Definition at line 107 of file STK_lapack_Svd.h.

Member Typedef Documentation

◆ Base

Definition at line 110 of file STK_lapack_Svd.h.

◆ ColVector

◆ RowVector

Constructor & Destructor Documentation

◆ Svd() [1/3]

STK::lapack::Svd::Svd ( CArrayXX const A,
bool  ref = false,
bool  withU = true,
bool  withV = true 
)
inline

Default constructor.

Parameters
Amatrix to decompose
refif true, U_ is a reference of A.
withUif true save the left Householder transforms in U_.
withVif true save the right Householder transforms in V_.

Definition at line 131 of file STK_lapack_Svd.h.

132 : Base(A, ref, withU, withV), jobz_( (withU|withV) ? 'O':'N') {}

◆ Svd() [2/3]

template<class OtherArray >
STK::lapack::Svd::Svd ( ArrayBase< OtherArray > const A,
bool  withU = true,
bool  withV = true 
)
inline

constructor with other kind of array/expression

Parameters
Athe matrix/expression to decompose.
withUif true save the left Householder transforms in U_.
withVif true save the right Householder transforms in V_.

Definition at line 139 of file STK_lapack_Svd.h.

140 : Base(A, withU, withV), jobz_( (withU|withV) ? 'O':'N') {}

◆ Svd() [3/3]

STK::lapack::Svd::Svd ( Svd const decomp)
inline

Copy constructor.

Parameters
decompthe decomposition to copy

Definition at line 144 of file STK_lapack_Svd.h.

144: Base(decomp), jobz_(decomp.jobz_) {}

◆ ~Svd()

virtual STK::lapack::Svd::~Svd ( )
inlinevirtual

virtual destructor

Definition at line 146 of file STK_lapack_Svd.h.

146{}

Member Function Documentation

◆ clone()

virtual Svd * STK::lapack::Svd::clone ( ) const
inlinevirtual

clone pattern

Definition at line 148 of file STK_lapack_Svd.h.

148{ return new Svd(*this);}
Svd(CArrayXX const &A, bool ref=false, bool withU=true, bool withV=true)
Default constructor.

◆ computeSvd()

bool STK::lapack::Svd::computeSvd ( CArrayXX a,
CArrayXX u,
CVectorX s,
CArrayXX v 
)
inlineprivate

compute the svd decomposition.

a contains either u, vt (if jobz_=='O') or is destroyed at the end of the oputput.

Definition at line 202 of file STK_lapack_Svd.h.

203{
204 int m = a.sizeRows(), n = a.sizeCols(), nbSv = std::min(m,n);
205 a.shift(0,0);
206 // Workspace and status variables:
207 double workSize;
208 double *p_work = &workSize;
209 int* p_iwork = new int[8*nbSv];
210 int lwork = -1;
211 int info;
212 //
213 // Call dgesdd_ with lwork = -1 to query optimal workspace size:
214 info = gesdd( jobz_, m, n
215 , a.p_data(), m, s.p_data(), u.p_data(), m, vt.p_data(), n
216 , p_work, lwork, p_iwork);
217 // check any error
218 if (info!=0)
219 {
220 if (info>0)
222 return false;
223 }
224 this->msg_error_= STKERROR_1ARG(lapack::Svd::computeSvd get,-info,error parameter);
225 return false;
226 }
227 // optimal storage dimension is returned in work[0]
228 lwork = workSize;
229 p_work = new Real[lwork];
230 // resize u
231 if ( !((jobz_ == 'O' && m >= n) || (jobz_ == 'N')) )
232 {
233 int ucol = (jobz_ == 'A' || jobz_ == 'O') ? m : nbSv;
234 u.resize(m, ucol);
235 }
236 if ( !((jobz_ == 'O' && m < n) || (jobz_ == 'N')) )
237 {
238 int ldvt = (jobz_ == 'A' || jobz_ == 'O') ? n : nbSv;
239 vt.resize(ldvt,n);
240 }
241 s.resize(nbSv).shift(0);
242 u.shift(0,0);
243 vt.shift(0,0);
244
245 // Call dgesdd_ to do the actual computation:
246 info = gesdd( jobz_, m, n
247 , a.p_data(), a.sizeRows(), s.p_data()
248 , u.p_data(), u.sizeRows()
249 , vt.p_data(), vt.sizeRows()
250 , p_work, lwork, p_iwork);
251 // clean
252 delete[] p_work;
253 delete[] p_iwork;
254 // check any error
255 if (info!=0)
256 {
257 if (info>0)
259 return false;
260 }
261 this->msg_error_= STKERROR_1ARG(lapack::Svd::computeSvd get,-info,error parameter);
262 return false;
263 }
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
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 computeSvd(CArrayXX &a, CArrayXX &u, CVectorX &s, CArrayXX &v)
compute the svd decomposition.
int gesdd(char jobz, int m, int n, Real *a, int lda, Real *s, Real *u, int ldu, Real *vt, int ldvt, Real *work, int lWork, int *iWork)
wrapper of the LAPACK DGESDD routine.
double Real
STK fundamental type of Real values.

References computeSvd(), STK::IRunnerBase::error(), STK::lapack::gesdd(), jobz_, STK::IRunnerBase::msg_error_, STK::ICArray< Derived >::p_data(), STK::ICArray< Derived >::resize(), STK::ICArray< Derived >::shift(), STKERROR_1ARG, and STKERROR_NO_ARG.

Referenced by computeSvd(), and runImpl().

◆ jobz()

char STK::lapack::Svd::jobz ( ) const
inline
Returns
the option chosen for the svd

Definition at line 158 of file STK_lapack_Svd.h.

158{ return jobz_;}

References jobz_.

Referenced by setJobz().

◆ operator=()

Svd & STK::lapack::Svd::operator= ( Svd const decomp)
inline

Operator = : overwrite the Svd with decomp.

Definition at line 150 of file STK_lapack_Svd.h.

151 {
152 Base::operator=(decomp);
153 jobz_ = decomp.jobz_;
154 return *this;
155 }
ISvd & operator=(const ISvd &S)
Operator = : overwrite the ISvd with S.
Definition STK_ISvd.h:101

References jobz_, and STK::ISvd< Svd >::operator=().

◆ runImpl()

bool STK::lapack::Svd::runImpl ( )
inline

Run svd decomposition.

Definition at line 175 of file STK_lapack_Svd.h.

176{
177 if (jobz_ == 'A' || jobz_ == 'S')
178 {
179 msg_error_ = _T("In lapack::Svd::runImpl, the options 'A' and 'S' are not available");
180 return false;
181 }
182 int beginRow = U_.beginRows(), beginCol = U_.beginCols();
183 bool result = true;
184 // compute results
185 if ( (jobz_ == 'O' && U_.sizeRows() >= U_.sizeCols()) || jobz_ == 'N')
186 {
187 if (!computeSvd(U_, U_, D_, V_)) { result = false;}
188 }
189 else
190 { // jobz_ == 'O' and m<n, V_ will contain u and U_ will contain vt
191 if (!computeSvd(U_, V_, D_, V_)) { result = false;};
192 U_.exchange(V_); // !now U_ is (m,m) and V_ is (m,n)
193 }
194 U_.shift(beginRow, beginCol);
195 D_.shift(beginCol);
196 transpose(V_.shift(beginCol));
197 return result;
198}
#define _T(x)
Let x unmodified.
ArrayD D_
Diagonal array of the singular values.
Definition STK_ISvd.h:193
ArrayV V_
V_ matrix.
Definition STK_ISvd.h:191
ArrayU U_
U_ matrix.
Definition STK_ISvd.h:189
TContainer2D & transpose(ArrayBase< TContainer2D > &A)
The transpose method allow to transpose an array in place.

References _T, computeSvd(), STK::ISvd< Svd >::D_, jobz_, STK::IRunnerBase::msg_error_, STK::transpose(), STK::ISvd< Svd >::U_, and STK::ISvd< Svd >::V_.

◆ setJobz()

void STK::lapack::Svd::setJobz ( char  jobz)
inline

set the option chosen for the svd

Definition at line 161 of file STK_lapack_Svd.h.

161{ jobz_ = jobz;}
char jobz() const

References jobz(), and jobz_.

Member Data Documentation

◆ jobz_

char STK::lapack::Svd::jobz_
private

option

Definition at line 168 of file STK_lapack_Svd.h.

Referenced by computeSvd(), jobz(), operator=(), runImpl(), and setJobz().


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