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

Compute the Singular Value Decomposition of an array. More...

#include <STK_ISvd.h>

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

Public Member Functions

Type det () const
Type trace () const
Type norm () const
int rank () const
ArrayU const & U () const
ArrayV const & V () const
ArrayD const & D () const
virtual bool run ()
 implement the run method
template<class OtherArray>
void setData (OtherArray const &A, bool withU=true, bool withV=true)
 Set a new data set to ISvd class.
template<class OtherArray>
OtherArray & ginv (OtherArray &res) const
 Compute the generalized inverse of the matrix and put the result in res.
Public Member Functions inherited from STK::IRunnerBase
String const & error () 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 const & asDerived () 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 const * asPtrDerived () 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 >::ArrayU ArrayU
typedef hidden::AlgebraTraits< Derived >::ArrayD ArrayD
typedef hidden::AlgebraTraits< Derived >::ArrayV ArrayV
typedef ArrayU::Type Type

Protected Member Functions

 ISvd (ArrayU const &A, bool ref, bool withU=true, bool withV=true)
 Default constructor.
template<class OtherDerived>
 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.
ISvd & operator= (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

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

template<class Derived>
class STK::ISvd< Derived >

Compute the Singular Value Decomposition of an array.

The method take as:

  • input: A matrix A(nrow,ncol)
  • output:
    1. U Array (nrow,ncolU).
    2. D Vector (ncol)
    3. V Array (ncol,ncol). and perform the decomposition:
  • A = UDV' U can have more columns than A, and it is possible to compute some (all) vectors of Ker(A).

Definition at line 60 of file STK_ISvd.h.

Member Typedef Documentation

◆ ArrayD

template<class Derived>
typedef hidden::AlgebraTraits<Derived>::ArrayD STK::ISvd< Derived >::ArrayD
protected

Definition at line 64 of file STK_ISvd.h.

◆ ArrayU

template<class Derived>
typedef hidden::AlgebraTraits<Derived>::ArrayU STK::ISvd< Derived >::ArrayU
protected

Definition at line 63 of file STK_ISvd.h.

◆ ArrayV

template<class Derived>
typedef hidden::AlgebraTraits<Derived>::ArrayV STK::ISvd< Derived >::ArrayV
protected

Definition at line 65 of file STK_ISvd.h.

◆ Type

template<class Derived>
typedef ArrayU::Type STK::ISvd< Derived >::Type
protected

Definition at line 66 of file STK_ISvd.h.

Constructor & Destructor Documentation

◆ ISvd() [1/3]

template<class Derived>
STK::ISvd< Derived >::ISvd ( ArrayU const & A,
bool ref,
bool withU = true,
bool withV = true )
inlineprotected

Default constructor.

Parameters
Athe matrix 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 74 of file STK_ISvd.h.

75 : U_(A, ref), V_(), D_()
76 , withU_(withU), withV_(withV), norm_(0), rank_(0), trace_(0), det_(0)
77 {}
Compute the Singular Value Decomposition of an array.
Definition STK_ISvd.h:61
bool withU_
Compute U_ ?
Definition STK_ISvd.h:195
Type norm_
trace norm
Definition STK_ISvd.h:199
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
Type trace_
trace norm
Definition STK_ISvd.h:203
int rank_
rank
Definition STK_ISvd.h:201
bool withV_
Compute V_ ?
Definition STK_ISvd.h:197
Type det_
determinant
Definition STK_ISvd.h:205

◆ ISvd() [2/3]

template<class Derived>
template<class OtherDerived>
STK::ISvd< Derived >::ISvd ( ArrayBase< OtherDerived > const & A,
bool withU = true,
bool withV = true )
inlineprotected

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 84 of file STK_ISvd.h.

85 : U_(A), V_(), D_()
86 , withU_(withU), withV_(withV), norm_(0), rank_(0), trace_(0), det_(0)
87 {}

◆ ISvd() [3/3]

template<class Derived>
STK::ISvd< Derived >::ISvd ( ISvd< Derived > const & S)
inlineprotected

Copy Constructor.

Parameters
Sthe Svd to copy

Definition at line 91 of file STK_ISvd.h.

92 : U_(S.U_, S.U_.isRef()), V_(S.V_), D_(S.D_)
95 {}

◆ ~ISvd()

template<class Derived>
virtual STK::ISvd< Derived >::~ISvd ( )
inlineprotectedvirtual

destructor.

Definition at line 97 of file STK_ISvd.h.

97{}

Member Function Documentation

◆ D()

template<class Derived>
ArrayD const & STK::ISvd< Derived >::D ( ) const
inline
Returns
D

Definition at line 152 of file STK_ISvd.h.

152{ return D_;}

Referenced by main().

◆ det()

template<class Derived>
Type STK::ISvd< Derived >::det ( ) const
inline
Returns
the product of the singular values

Definition at line 140 of file STK_ISvd.h.

140{ return det_;}

Referenced by STK::hidden::InvertMatrixImpl< Matrix, Size_ >::invertMatrixXX().

◆ finalize()

template<class Derived>
virtual void STK::ISvd< Derived >::finalize ( )
inlineprotectedvirtual

Finalize any operations that have to be done after the computation of the decomposition.

Definition at line 111 of file STK_ISvd.h.

112 {
113 // compute sign and trace
114 trace_ = 0.;
115 int s = 1;
116 for (int i=D_.begin(); i< D_.end(); ++i )
117 {
118 Type value = D_[i];
119 s *= sign(value);
120 trace_ += value;
121 }
122
123 // compute norm_ (2-norm) and determinant
124 norm_ = D_.maxElt();
125 det_ = 0;
126 if (D_.abs().minElt() > 0)
127 { det_ = s * std::exp(D_.abs().log().sum());}
128
129 // compute norm_ rank_
130 rank_ = D_.size(); // full rank
132 for (int i=D_.begin(); i< D_.end(); ++i )
133 { if (std::abs(D_[i]) < tol) { rank_--;}}
134
135 }
ArrayU::Type Type
Definition STK_ISvd.h:66
Type sign(Type const &x, Type const &y=Type(1))
template sign value sign(x) * y: Type should be an arithmetic type.
Definition STK_Misc.h:55

Referenced by STK::ISvd< Svd >::run().

◆ ginv()

template<class Derived>
template<class OtherArray>
OtherArray & STK::ISvd< Derived >::ginv ( OtherArray & res) const

Compute the generalized inverse of the matrix and put the result in res.

Parameters
resarray with the result
Returns
the result

Definition at line 227 of file STK_ISvd.h.

228{
230 if(tol==0) { tol = Arithmetic<Type>::epsilon();}
231 // compute D
232 if (U_.cols() != D_.range())
233 {
234 Array2DDiagonal<Real> diag(U_.cols(), 0.0);
235 for (int i= D_.begin(); i< D_.end(); ++i) { diag[i]=D_[i];}
236 res = V_ * diag.safeInverse(tol) * U_.transpose();
237 }
238 else
239 {
240 res = V_ * D_.diagonalize().safeInverse(tol) * U_.transpose();
241 }
242 // compute and return UD^{-1}V'
243 return (res);
244}

References D_, norm_, STK::ExprBase< Derived >::safeInverse(), U_, and V_.

Referenced by STK::hidden::InvertMatrixImpl< Matrix, Size_ >::invertMatrixXX().

◆ ncolD()

template<class Derived>
int STK::ISvd< Derived >::ncolD ( ) const
inlineprotected
Returns
the number of columns of D_

Definition at line 214 of file STK_ISvd.h.

214{ return D_.sizeCols();}

◆ ncolU()

template<class Derived>
int STK::ISvd< Derived >::ncolU ( ) const
inlineprotected
Returns
the number of columns of U_

Definition at line 210 of file STK_ISvd.h.

210{ return U_.sizeCols();}

◆ ncolV()

template<class Derived>
int STK::ISvd< Derived >::ncolV ( ) const
inlineprotected
Returns
the number of columns of V_

Definition at line 218 of file STK_ISvd.h.

218{ return V_.sizeCols();}

◆ norm()

template<class Derived>
Type STK::ISvd< Derived >::norm ( ) const
inline
Returns
the norm of the matrix

Definition at line 144 of file STK_ISvd.h.

144{ return norm_;}

◆ nrowD()

template<class Derived>
int STK::ISvd< Derived >::nrowD ( ) const
inlineprotected
Returns
the number of columns of D_

Definition at line 212 of file STK_ISvd.h.

212{ return D_.sizeRows();}

◆ nrowU()

template<class Derived>
int STK::ISvd< Derived >::nrowU ( ) const
inlineprotected
Returns
the number of rows of U_

Definition at line 208 of file STK_ISvd.h.

208{ return U_.sizeRows();}

◆ nrowV()

template<class Derived>
int STK::ISvd< Derived >::nrowV ( ) const
inlineprotected
Returns
the number of rows of V_

Definition at line 216 of file STK_ISvd.h.

216{ return V_.sizeRows();}

◆ operator=()

template<class Derived>
ISvd & STK::ISvd< Derived >::operator= ( const ISvd< Derived > & S)
inlineprotected

Operator = : overwrite the ISvd with S.

Parameters
Sthe Svd to copy

Definition at line 101 of file STK_ISvd.h.

102 {
103 U_ = S.U_; V_ = S.V_; D_ = S.D_;
106 return *this;
107 }

◆ rank()

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

Definition at line 146 of file STK_ISvd.h.

146{ return rank_;}

◆ run()

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

implement the run method

Implements STK::IRunnerBase.

Reimplemented in STK::WeightedSvd< Array, WRows, WCols >.

Definition at line 155 of file STK_ISvd.h.

156 {
157 if (U_.empty()) { return true;}
158 // compute Svd decomposition
159 if (!this->asDerived().runImpl()) return false;
160 finalize();
161 return true;
162 }
Derived & asDerived()
static cast : return a reference of this with a cast to the derived class.
virtual void finalize()
Finalize any operations that have to be done after the computation of the decomposition.
Definition STK_ISvd.h:111

Referenced by STK::hidden::InvertMatrixImpl< Matrix, Size_ >::invertMatrixXX(), main(), and STK::WeightedSvd< Array, WRows, WCols >::run().

◆ setData()

template<class Derived>
template<class OtherArray>
void STK::ISvd< Derived >::setData ( OtherArray const & A,
bool withU = true,
bool withV = true )
inline

Set a new data set to ISvd class.

Note
Take care that if U_ was previously a reference, it cannot be modified.
Warning
A^{-1}A give identity matrix if m<=n, and AA^{-1} give identity matrix if n<=m
Parameters
Ais the matrix to decompose.
withUif true, we save the left Housolder transforms in U_.
withVif true, we save the right Housolder transforms in V_.

Definition at line 173 of file STK_ISvd.h.

174 {
175 U_ = A; // Copy A in U_
176 withU_ = withU; // copy withU_ value
177 withV_ = withV; // copy withV_ value
178 V_.resize(0,0), D_.resize(0);
179 }

◆ trace()

template<class Derived>
Type STK::ISvd< Derived >::trace ( ) const
inline
Returns
the sum of the singular values

Definition at line 142 of file STK_ISvd.h.

142{ return trace_;}

◆ U()

template<class Derived>
ArrayU const & STK::ISvd< Derived >::U ( ) const
inline
Returns
U

Definition at line 148 of file STK_ISvd.h.

148{ return U_;}

Referenced by main().

◆ V()

template<class Derived>
ArrayV const & STK::ISvd< Derived >::V ( ) const
inline
Returns
V

Definition at line 150 of file STK_ISvd.h.

150{ return V_;}

Referenced by main().

Member Data Documentation

◆ D_

template<class Derived>
ArrayD STK::ISvd< Derived >::D_
protected

Diagonal array of the singular values.

Definition at line 193 of file STK_ISvd.h.

Referenced by ginv(), and STK::ISvd< Svd >::operator=().

◆ det_

template<class Derived>
Type STK::ISvd< Derived >::det_
protected

determinant

Definition at line 205 of file STK_ISvd.h.

Referenced by STK::ISvd< Svd >::operator=().

◆ norm_

template<class Derived>
Type STK::ISvd< Derived >::norm_
protected

trace norm

Definition at line 199 of file STK_ISvd.h.

Referenced by ginv(), and STK::ISvd< Svd >::operator=().

◆ rank_

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

rank

Definition at line 201 of file STK_ISvd.h.

Referenced by STK::ISvd< Svd >::operator=().

◆ trace_

template<class Derived>
Type STK::ISvd< Derived >::trace_
protected

trace norm

Definition at line 203 of file STK_ISvd.h.

Referenced by STK::ISvd< Svd >::operator=().

◆ U_

template<class Derived>
ArrayU STK::ISvd< Derived >::U_
protected

U_ matrix.

Definition at line 189 of file STK_ISvd.h.

Referenced by ginv(), and STK::ISvd< Svd >::operator=().

◆ V_

template<class Derived>
ArrayV STK::ISvd< Derived >::V_
protected

V_ matrix.

Definition at line 191 of file STK_ISvd.h.

Referenced by ginv(), and STK::ISvd< Svd >::operator=().

◆ withU_

template<class Derived>
bool STK::ISvd< Derived >::withU_
protected

Compute U_ ?

Definition at line 195 of file STK_ISvd.h.

Referenced by STK::ISvd< Svd >::operator=().

◆ withV_

template<class Derived>
bool STK::ISvd< Derived >::withV_
protected

Compute V_ ?

Definition at line 197 of file STK_ISvd.h.

Referenced by STK::ISvd< Svd >::operator=().


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