STK++ 0.9.13
STK_ISvd.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 Interface ISvd Class.
28 * Author: Serge Iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 *
30 **/
31
36#ifndef STK_ISVD_H
37#define STK_ISVD_H
38
39#include <STKernel.h>
40
41#include "STK_Algebra_Util.h"
42
43namespace STK
44{
59template<class Derived>
60class ISvd: public IRunnerBase, public IRecursiveTemplate<Derived>
61{
62 protected:
66 typedef typename ArrayU::Type Type;
67
74 inline ISvd( ArrayU const& A, bool ref, bool withU = true, bool withV = true)
75 : U_(A, ref), V_(), D_()
76 , withU_(withU), withV_(withV), norm_(0), rank_(0), trace_(0), det_(0)
77 {}
83 template<class OtherDerived>
84 inline ISvd( ArrayBase<OtherDerived> const& A, bool withU = true, bool withV = true)
85 : U_(A), V_(), D_()
86 , withU_(withU), withV_(withV), norm_(0), rank_(0), trace_(0), det_(0)
87 {}
91 inline ISvd( ISvd const& S)
92 : U_(S.U_, S.U_.isRef()), V_(S.V_), D_(S.D_)
95 {}
97 inline virtual ~ISvd() {}
102 {
103 U_ = S.U_; V_ = S.V_; D_ = S.D_;
104 withU_ = S.withU_; withV_ = S.withV_;
105 norm_ = S.norm_; rank_ = S.rank_; trace_ = S.trace_; det_ = S.det_;
106 return *this;
107 }
111 virtual void finalize()
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 }
136
137 public:
138 // getters
140 inline Type det() const { return det_;}
142 inline Type trace() const { return trace_;}
144 inline Type norm() const { return norm_;}
146 inline int rank() const { return rank_;}
148 inline ArrayU const& U() const { return U_;}
150 inline ArrayV const& V() const { return V_;}
152 inline ArrayD const& D() const { return D_;}
153
155 virtual bool run()
156 {
157 if (U_.empty()) { return true;}
158 // compute Svd decomposition
159 if (!this->asDerived().runImpl()) return false;
160 finalize();
161 return true;
162 }
172 template<class OtherArray>
173 void setData( OtherArray const& A, bool withU = true, bool withV = true)
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 }
184 template<class OtherArray>
186
187 protected:
195 bool withU_;
197 bool withV_;
201 int rank_;
206
208 inline int nrowU() const { return U_.sizeRows();}
210 inline int ncolU() const { return U_.sizeCols();}
212 inline int nrowD() const { return D_.sizeRows();}
214 inline int ncolD() const { return D_.sizeCols();}
216 inline int nrowV() const { return V_.sizeRows();}
218 inline int ncolV() const { return V_.sizeCols();}
219};
220
221/* Compute the generalized inverse of the matrix and put the result in res.
222 * @param res array with the result
223 * @return the result
224 */
225template<class Derived>
226template<class OtherArray>
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}
245
246} // namespace STK
247
248#endif /* STK_ISVD_H */
In this file we define and implement utilies class and method for the Algebra project.
This file include all the header files of the project STKernel.
Interface base class for all classes implementing the curious recursive template paradigm.
Derived & asDerived()
static cast : return a reference of this with a cast to the derived class.
Abstract base class for all classes having a.
Definition STK_IRunner.h:65
Compute the Singular Value Decomposition of an array.
Definition STK_ISvd.h:61
ArrayU::Type Type
Definition STK_ISvd.h:66
bool withU_
Compute U_ ?
Definition STK_ISvd.h:195
Type norm() const
Definition STK_ISvd.h:144
virtual bool run()
implement the run method
Definition STK_ISvd.h:155
Type norm_
trace norm
Definition STK_ISvd.h:199
ArrayD const & D() const
Definition STK_ISvd.h:152
int nrowU() const
Definition STK_ISvd.h:208
hidden::AlgebraTraits< Derived >::ArrayV ArrayV
Definition STK_ISvd.h:65
Type trace() const
Definition STK_ISvd.h:142
ISvd(ArrayBase< OtherDerived > const &A, bool withU=true, bool withV=true)
constructor with other kind of array/expression
Definition STK_ISvd.h:84
virtual void finalize()
Finalize any operations that have to be done after the computation of the decomposition.
Definition STK_ISvd.h:111
Type det() const
Definition STK_ISvd.h:140
ArrayD D_
Diagonal array of the singular values.
Definition STK_ISvd.h:193
int rank() const
Definition STK_ISvd.h:146
int nrowV() const
Definition STK_ISvd.h:216
OtherArray & ginv(OtherArray &res) const
Compute the generalized inverse of the matrix and put the result in res.
Definition STK_ISvd.h:227
ISvd & operator=(const ISvd &S)
Operator = : overwrite the ISvd with S.
Definition STK_ISvd.h:101
ArrayV V_
V_ matrix.
Definition STK_ISvd.h:191
virtual ~ISvd()
destructor.
Definition STK_ISvd.h:97
ArrayU U_
U_ matrix.
Definition STK_ISvd.h:189
int ncolV() const
Definition STK_ISvd.h:218
Type trace_
trace norm
Definition STK_ISvd.h:203
int nrowD() const
Definition STK_ISvd.h:212
ArrayU const & U() const
Definition STK_ISvd.h:148
hidden::AlgebraTraits< Derived >::ArrayU ArrayU
Definition STK_ISvd.h:63
int rank_
rank
Definition STK_ISvd.h:201
ISvd(ArrayU const &A, bool ref, bool withU=true, bool withV=true)
Default constructor.
Definition STK_ISvd.h:74
bool withV_
Compute V_ ?
Definition STK_ISvd.h:197
void setData(OtherArray const &A, bool withU=true, bool withV=true)
Set a new data set to ISvd class.
Definition STK_ISvd.h:173
ArrayV const & V() const
Definition STK_ISvd.h:150
int ncolU() const
Definition STK_ISvd.h:210
hidden::AlgebraTraits< Derived >::ArrayD ArrayD
Definition STK_ISvd.h:64
Type det_
determinant
Definition STK_ISvd.h:205
ISvd(ISvd const &S)
Copy Constructor.
Definition STK_ISvd.h:91
int ncolD() const
Definition STK_ISvd.h:214
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Type sign(Type const &x, Type const &y=Type(1))
template sign value sign(x) * y: Type should be an integral type
Definition STK_Misc.h:53
The namespace STK is the main domain space of the Statistical ToolKit project.
Arithmetic properties of STK fundamental types.
traits class for the algebra methods.