SVDBase.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2009-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
5 // Copyright (C) 2014 Gael Guennebaud <gael.guennebaud@inria.fr>
6 //
7 // Copyright (C) 2013 Gauthier Brun <brun.gauthier@gmail.com>
8 // Copyright (C) 2013 Nicolas Carre <nicolas.carre@ensimag.fr>
9 // Copyright (C) 2013 Jean Ceccato <jean.ceccato@ensimag.fr>
10 // Copyright (C) 2013 Pierre Zoppitelli <pierre.zoppitelli@ensimag.fr>
11 //
12 // This Source Code Form is subject to the terms of the Mozilla
13 // Public License v. 2.0. If a copy of the MPL was not distributed
14 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 
16 #ifndef EIGEN_SVDBASE_H
17 #define EIGEN_SVDBASE_H
18 
19 namespace Eigen {
47 template<typename Derived>
48 class SVDBase
49 {
50 
51 public:
53  typedef typename MatrixType::Scalar Scalar;
55  typedef typename MatrixType::StorageIndex StorageIndex;
56  typedef Eigen::Index Index;
57  enum {
58  RowsAtCompileTime = MatrixType::RowsAtCompileTime,
59  ColsAtCompileTime = MatrixType::ColsAtCompileTime,
61  MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
62  MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
64  MatrixOptions = MatrixType::Options
65  };
66 
70 
71  Derived& derived() { return *static_cast<Derived*>(this); }
72  const Derived& derived() const { return *static_cast<const Derived*>(this); }
73 
83  const MatrixUType& matrixU() const
84  {
85  eigen_assert(m_isInitialized && "SVD is not initialized.");
86  eigen_assert(computeU() && "This SVD decomposition didn't compute U. Did you ask for it?");
87  return m_matrixU;
88  }
89 
99  const MatrixVType& matrixV() const
100  {
101  eigen_assert(m_isInitialized && "SVD is not initialized.");
102  eigen_assert(computeV() && "This SVD decomposition didn't compute V. Did you ask for it?");
103  return m_matrixV;
104  }
105 
111  const SingularValuesType& singularValues() const
112  {
113  eigen_assert(m_isInitialized && "SVD is not initialized.");
114  return m_singularValues;
115  }
116 
118  Index nonzeroSingularValues() const
119  {
120  eigen_assert(m_isInitialized && "SVD is not initialized.");
122  }
123 
130  inline Index rank() const
131  {
132  using std::abs;
133  eigen_assert(m_isInitialized && "JacobiSVD is not initialized.");
134  if(m_singularValues.size()==0) return 0;
135  RealScalar premultiplied_threshold = numext::maxi<RealScalar>(m_singularValues.coeff(0) * threshold(), (std::numeric_limits<RealScalar>::min)());
136  Index i = m_nonzeroSingularValues-1;
137  while(i>=0 && m_singularValues.coeff(i) < premultiplied_threshold) --i;
138  return i+1;
139  }
140 
155  Derived& setThreshold(const RealScalar& threshold)
156  {
159  return derived();
160  }
161 
171  {
172  m_usePrescribedThreshold = false;
173  return derived();
174  }
175 
180  RealScalar threshold() const
181  {
183  // this temporary is needed to workaround a MSVC issue
184  Index diagSize = (std::max<Index>)(1,m_diagSize);
186  : diagSize*NumTraits<Scalar>::epsilon();
187  }
188 
190  inline bool computeU() const { return m_computeFullU || m_computeThinU; }
192  inline bool computeV() const { return m_computeFullV || m_computeThinV; }
193 
194  inline Index rows() const { return m_rows; }
195  inline Index cols() const { return m_cols; }
196 
206  template<typename Rhs>
207  inline const Solve<Derived, Rhs>
208  solve(const MatrixBase<Rhs>& b) const
209  {
210  eigen_assert(m_isInitialized && "SVD is not initialized.");
211  eigen_assert(computeU() && computeV() && "SVD::solve() requires both unitaries U and V to be computed (thin unitaries suffice).");
212  return Solve<Derived, Rhs>(derived(), b.derived());
213  }
214 
215  #ifndef EIGEN_PARSED_BY_DOXYGEN
216  template<typename RhsType, typename DstType>
217  EIGEN_DEVICE_FUNC
218  void _solve_impl(const RhsType &rhs, DstType &dst) const;
219  #endif
220 
221 protected:
222 
224  {
226  }
227 
228  // return true if already allocated
229  bool allocate(Index rows, Index cols, unsigned int computationOptions) ;
230 
231  MatrixUType m_matrixU;
232  MatrixVType m_matrixV;
233  SingularValuesType m_singularValues;
237  unsigned int m_computationOptions;
240 
246  : m_isInitialized(false),
247  m_isAllocated(false),
248  m_usePrescribedThreshold(false),
249  m_computationOptions(0),
250  m_rows(-1), m_cols(-1), m_diagSize(0)
251  {
253  }
254 
255 
256 };
257 
258 #ifndef EIGEN_PARSED_BY_DOXYGEN
259 template<typename Derived>
260 template<typename RhsType, typename DstType>
261 void SVDBase<Derived>::_solve_impl(const RhsType &rhs, DstType &dst) const
262 {
263  eigen_assert(rhs.rows() == rows());
264 
265  // A = U S V^*
266  // So A^{-1} = V S^{-1} U^*
267 
269  Index l_rank = rank();
270  tmp.noalias() = m_matrixU.leftCols(l_rank).adjoint() * rhs;
271  tmp = m_singularValues.head(l_rank).asDiagonal().inverse() * tmp;
272  dst = m_matrixV.leftCols(l_rank) * tmp;
273 }
274 #endif
275 
276 template<typename MatrixType>
277 bool SVDBase<MatrixType>::allocate(Index rows, Index cols, unsigned int computationOptions)
278 {
279  eigen_assert(rows >= 0 && cols >= 0);
280 
281  if (m_isAllocated &&
282  rows == m_rows &&
283  cols == m_cols &&
284  computationOptions == m_computationOptions)
285  {
286  return true;
287  }
288 
289  m_rows = rows;
290  m_cols = cols;
291  m_isInitialized = false;
292  m_isAllocated = true;
293  m_computationOptions = computationOptions;
294  m_computeFullU = (computationOptions & ComputeFullU) != 0;
295  m_computeThinU = (computationOptions & ComputeThinU) != 0;
296  m_computeFullV = (computationOptions & ComputeFullV) != 0;
297  m_computeThinV = (computationOptions & ComputeThinV) != 0;
298  eigen_assert(!(m_computeFullU && m_computeThinU) && "SVDBase: you can't ask for both full and thin U");
299  eigen_assert(!(m_computeFullV && m_computeThinV) && "SVDBase: you can't ask for both full and thin V");
300  eigen_assert(EIGEN_IMPLIES(m_computeThinU || m_computeThinV, MatrixType::ColsAtCompileTime==Dynamic) &&
301  "SVDBase: thin U and V are only available when your matrix has a dynamic number of columns.");
302 
309 
310  return false;
311 }
312 
313 }// end namespace
314 
315 #endif // EIGEN_SVDBASE_H
SCALAR Scalar
Definition: bench_gemm.cpp:33
Derived & derived()
Definition: SVDBase.h:71
Derived & setThreshold(Default_t)
Definition: SVDBase.h:170
Scalar * b
Definition: benchVecAdd.cpp:17
unsigned int m_computationOptions
Definition: SVDBase.h:237
Matrix< Scalar, ColsAtCompileTime, ColsAtCompileTime, MatrixOptions, MaxColsAtCompileTime, MaxColsAtCompileTime > MatrixVType
Definition: SVDBase.h:68
const SingularValuesType & singularValues() const
Definition: SVDBase.h:111
#define min(a, b)
Definition: datatypes.h:19
bool m_computeFullV
Definition: SVDBase.h:236
Eigen::Index Index
Definition: SVDBase.h:56
MatrixType::StorageIndex StorageIndex
Definition: SVDBase.h:55
RealScalar threshold() const
Definition: SVDBase.h:180
MatrixUType m_matrixU
Definition: SVDBase.h:231
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:150
Default_t
Definition: Constants.h:352
Index rank() const
Definition: SVDBase.h:130
NumTraits< typename MatrixType::Scalar >::Real RealScalar
Definition: SVDBase.h:54
EIGEN_DEVICE_FUNC void _solve_impl(const RhsType &rhs, DstType &dst) const
#define EIGEN_IMPLIES(a, b)
Definition: Macros.h:902
#define EIGEN_SIZE_MIN_PREFER_FIXED(a, b)
Definition: Macros.h:889
bool allocate(Index rows, Index cols, unsigned int computationOptions)
Definition: SVDBase.h:277
Index rows() const
Definition: SVDBase.h:194
static double epsilon
Definition: testRot3.cpp:39
virtual EIGEN_DEVICE_FUNC const Scalar coeff(DenseIndex index) const
Definition: TensorRef.h:63
bool computeV() const
Definition: SVDBase.h:192
bool m_isInitialized
Definition: SVDBase.h:234
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resize(Index rows, Index cols)
bool m_usePrescribedThreshold
Definition: SVDBase.h:234
Base class of SVD algorithms.
Definition: SVDBase.h:48
MatrixVType m_matrixV
Definition: SVDBase.h:232
bool m_computeThinV
Definition: SVDBase.h:236
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
Index m_cols
Definition: SVDBase.h:238
SVDBase()
Default Constructor.
Definition: SVDBase.h:245
#define eigen_assert(x)
Definition: Macros.h:579
Matrix< Scalar, RowsAtCompileTime, RowsAtCompileTime, MatrixOptions, MaxRowsAtCompileTime, MaxRowsAtCompileTime > MatrixUType
Definition: SVDBase.h:67
SingularValuesType m_singularValues
Definition: SVDBase.h:233
#define EIGEN_STATIC_ASSERT_NON_INTEGER(TYPE)
Definition: StaticAssert.h:184
const MatrixVType & matrixV() const
Definition: SVDBase.h:99
Index m_rows
Definition: SVDBase.h:238
RealScalar m_prescribedThreshold
Definition: SVDBase.h:239
const MatrixUType & matrixU() const
Definition: SVDBase.h:83
Derived & setThreshold(const RealScalar &threshold)
Definition: SVDBase.h:155
const Derived & derived() const
Definition: SVDBase.h:72
internal::plain_diag_type< MatrixType, RealScalar >::type SingularValuesType
Definition: SVDBase.h:69
static void check_template_parameters()
Definition: SVDBase.h:223
bool computeU() const
Definition: SVDBase.h:190
Index nonzeroSingularValues() const
Definition: SVDBase.h:118
Index m_nonzeroSingularValues
Definition: SVDBase.h:238
bool m_computeFullU
Definition: SVDBase.h:235
Index m_diagSize
Definition: SVDBase.h:238
bool m_computeThinU
Definition: SVDBase.h:235
const int Dynamic
Definition: Constants.h:21
Pseudo expression representing a solving operation.
Definition: Solve.h:62
#define EIGEN_SIZE_MIN_PREFER_DYNAMIC(a, b)
Definition: Macros.h:881
#define abs(x)
Definition: datatypes.h:17
internal::traits< Derived >::MatrixType MatrixType
Definition: SVDBase.h:52
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
const Solve< Derived, Rhs > solve(const MatrixBase< Rhs > &b) const
Definition: SVDBase.h:208
Index cols() const
Definition: SVDBase.h:195
bool m_isAllocated
Definition: SVDBase.h:234
MatrixType::Scalar Scalar
Definition: SVDBase.h:53


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:45:02