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 #include "./InternalHeaderCheck.h"
20 
21 namespace Eigen {
22 
23 namespace internal {
24 
29 };
30 
31 constexpr int get_qr_preconditioner(int options) { return options & QRPreconditionerBits; }
32 
33 constexpr int get_computation_options(int options) { return options & ComputationOptionsBits; }
34 
35 constexpr bool should_svd_compute_thin_u(int options) { return (options & ComputeThinU) != 0; }
36 constexpr bool should_svd_compute_full_u(int options) { return (options & ComputeFullU) != 0; }
37 constexpr bool should_svd_compute_thin_v(int options) { return (options & ComputeThinV) != 0; }
38 constexpr bool should_svd_compute_full_v(int options) { return (options & ComputeFullV) != 0; }
39 
40 template<typename MatrixType, int Options>
41 void check_svd_options_assertions(unsigned int computationOptions, Index rows, Index cols) {
43  "SVDBase: Cannot request U or V using both static and runtime options, even if they match. "
44  "Requesting unitaries at runtime is DEPRECATED: "
45  "Prefer requesting unitaries statically, using the Options template parameter.");
46  eigen_assert(!(should_svd_compute_thin_u(computationOptions) && cols < rows && MatrixType::RowsAtCompileTime != Dynamic) &&
47  !(should_svd_compute_thin_v(computationOptions) && rows < cols && MatrixType::ColsAtCompileTime != Dynamic) &&
48  "SVDBase: If thin U is requested at runtime, your matrix must have more rows than columns or a dynamic number of rows."
49  "Similarly, if thin V is requested at runtime, you matrix must have more columns than rows or a dynamic number of columns.");
50  (void)computationOptions;
51  (void)rows;
52  (void)cols;
53 }
54 
55 template<typename Derived> struct traits<SVDBase<Derived> >
56  : traits<Derived>
57 {
58  typedef MatrixXpr XprKind;
59  typedef SolverStorage StorageKind;
60  typedef int StorageIndex;
61  enum { Flags = 0 };
62 };
63 
64 template <typename MatrixType, int Options_>
65 struct svd_traits : traits<MatrixType> {
66  static constexpr int Options = Options_;
67  static constexpr bool ShouldComputeFullU = internal::should_svd_compute_full_u(Options);
68  static constexpr bool ShouldComputeThinU = internal::should_svd_compute_thin_u(Options);
69  static constexpr bool ShouldComputeFullV = internal::should_svd_compute_full_v(Options);
70  static constexpr bool ShouldComputeThinV = internal::should_svd_compute_thin_v(Options);
71  enum {
72  DiagSizeAtCompileTime =
73  internal::min_size_prefer_dynamic(MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime),
74  MaxDiagSizeAtCompileTime =
75  internal::min_size_prefer_dynamic(MatrixType::MaxRowsAtCompileTime, MatrixType::MaxColsAtCompileTime),
76  MatrixUColsAtCompileTime = ShouldComputeThinU ? DiagSizeAtCompileTime
77  : MatrixType::RowsAtCompileTime,
78  MatrixVColsAtCompileTime = ShouldComputeThinV ? DiagSizeAtCompileTime
79  : MatrixType::ColsAtCompileTime,
80  MatrixUMaxColsAtCompileTime = ShouldComputeThinU ? MaxDiagSizeAtCompileTime
81  : MatrixType::MaxRowsAtCompileTime,
82  MatrixVMaxColsAtCompileTime = ShouldComputeThinV ? MaxDiagSizeAtCompileTime
83  : MatrixType::MaxColsAtCompileTime
84  };
85 };
86 }
87 
118 template<typename Derived> class SVDBase
119  : public SolverBase<SVDBase<Derived> >
120 {
121 public:
122 
123  template<typename Derived_>
124  friend struct internal::solve_assertion;
125 
127  typedef typename MatrixType::Scalar Scalar;
129  typedef typename Eigen::internal::traits<SVDBase>::StorageIndex StorageIndex;
130  typedef Eigen::Index Index;
131 
132  static constexpr bool ShouldComputeFullU = internal::traits<Derived>::ShouldComputeFullU;
133  static constexpr bool ShouldComputeThinU = internal::traits<Derived>::ShouldComputeThinU;
134  static constexpr bool ShouldComputeFullV = internal::traits<Derived>::ShouldComputeFullV;
135  static constexpr bool ShouldComputeThinV = internal::traits<Derived>::ShouldComputeThinV;
136 
137  enum {
138  RowsAtCompileTime = MatrixType::RowsAtCompileTime,
139  ColsAtCompileTime = MatrixType::ColsAtCompileTime,
141  MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
142  MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
144  MatrixOptions = MatrixType::Options,
145  MatrixUColsAtCompileTime = internal::traits<Derived>::MatrixUColsAtCompileTime,
146  MatrixVColsAtCompileTime = internal::traits<Derived>::MatrixVColsAtCompileTime,
147  MatrixUMaxColsAtCompileTime = internal::traits<Derived>::MatrixUMaxColsAtCompileTime,
148  MatrixVMaxColsAtCompileTime = internal::traits<Derived>::MatrixVMaxColsAtCompileTime
149  };
150 
151  EIGEN_STATIC_ASSERT(!(ShouldComputeFullU && ShouldComputeThinU), "SVDBase: Cannot request both full and thin U")
152  EIGEN_STATIC_ASSERT(!(ShouldComputeFullV && ShouldComputeThinV), "SVDBase: Cannot request both full and thin V")
153 
154  typedef
155  typename internal::make_proper_matrix_type<Scalar, RowsAtCompileTime, MatrixUColsAtCompileTime, MatrixOptions,
157  typedef
158  typename internal::make_proper_matrix_type<Scalar, ColsAtCompileTime, MatrixVColsAtCompileTime, MatrixOptions,
160 
161  typedef typename internal::plain_diag_type<MatrixType, RealScalar>::type SingularValuesType;
162 
163  Derived& derived() { return *static_cast<Derived*>(this); }
164  const Derived& derived() const { return *static_cast<const Derived*>(this); }
165 
175  const MatrixUType& matrixU() const
176  {
178  eigen_assert(computeU() && "This SVD decomposition didn't compute U. Did you ask for it?");
179  return m_matrixU;
180  }
181 
191  const MatrixVType& matrixV() const
192  {
194  eigen_assert(computeV() && "This SVD decomposition didn't compute V. Did you ask for it?");
195  return m_matrixV;
196  }
197 
204  {
206  return m_singularValues;
207  }
208 
211  {
214  }
215 
222  inline Index rank() const
223  {
224  using std::abs;
226  if(m_singularValues.size()==0) return 0;
227  RealScalar premultiplied_threshold = numext::maxi<RealScalar>(m_singularValues.coeff(0) * threshold(), (std::numeric_limits<RealScalar>::min)());
229  while(i>=0 && m_singularValues.coeff(i) < premultiplied_threshold) --i;
230  return i+1;
231  }
232 
248  {
251  return derived();
252  }
253 
263  {
264  m_usePrescribedThreshold = false;
265  return derived();
266  }
267 
273  {
275  // this temporary is needed to workaround a MSVC issue
276  Index diagSize = (std::max<Index>)(1,m_diagSize);
279  }
280 
282  inline bool computeU() const { return m_computeFullU || m_computeThinU; }
284  inline bool computeV() const { return m_computeFullV || m_computeThinV; }
285 
286  inline Index rows() const { return m_rows; }
287  inline Index cols() const { return m_cols; }
288 
289  #ifdef EIGEN_PARSED_BY_DOXYGEN
299  template<typename Rhs>
300  inline const Solve<Derived, Rhs>
301  solve(const MatrixBase<Rhs>& b) const;
302  #endif
303 
304 
311  {
312  eigen_assert(m_isInitialized && "SVD is not initialized.");
313  return m_info;
314  }
315 
316  #ifndef EIGEN_PARSED_BY_DOXYGEN
317  template<typename RhsType, typename DstType>
318  void _solve_impl(const RhsType &rhs, DstType &dst) const;
319 
320  template<bool Conjugate, typename RhsType, typename DstType>
321  void _solve_impl_transposed(const RhsType &rhs, DstType &dst) const;
322  #endif
323 
324 protected:
325 
327 
329  eigen_assert(m_isInitialized && "SVD is not initialized.");
330  }
331 
332  template<bool Transpose_, typename Rhs>
333  void _check_solve_assertion(const Rhs& b) const {
336  eigen_assert(computeU() && computeV() && "SVDBase::solve(): Both unitaries U and V are required to be computed (thin unitaries suffice).");
337  eigen_assert((Transpose_?cols():rows())==b.rows() && "SVDBase::solve(): invalid number of rows of the right hand side matrix b");
338  }
339 
340  // return true if already allocated
341  bool allocate(Index rows, Index cols, unsigned int computationOptions);
342 
350  unsigned int m_computationOptions;
353 
359  : m_matrixU(MatrixUType()),
362  m_info(Success),
363  m_isInitialized(false),
364  m_isAllocated(false),
366  m_computeFullU(false),
367  m_computeThinU(false),
368  m_computeFullV(false),
369  m_computeThinV(false),
372  m_rows(-1),
373  m_cols(-1),
374  m_diagSize(0),
376 };
377 
378 #ifndef EIGEN_PARSED_BY_DOXYGEN
379 template<typename Derived>
380 template<typename RhsType, typename DstType>
381 void SVDBase<Derived>::_solve_impl(const RhsType &rhs, DstType &dst) const
382 {
383  // A = U S V^*
384  // So A^{-1} = V S^{-1} U^*
385 
386  Matrix<typename RhsType::Scalar, Dynamic, RhsType::ColsAtCompileTime, 0, MatrixType::MaxRowsAtCompileTime, RhsType::MaxColsAtCompileTime> tmp;
387  Index l_rank = rank();
388  tmp.noalias() = m_matrixU.leftCols(l_rank).adjoint() * rhs;
389  tmp = m_singularValues.head(l_rank).asDiagonal().inverse() * tmp;
390  dst = m_matrixV.leftCols(l_rank) * tmp;
391 }
392 
393 template<typename Derived>
394 template<bool Conjugate, typename RhsType, typename DstType>
395 void SVDBase<Derived>::_solve_impl_transposed(const RhsType &rhs, DstType &dst) const
396 {
397  // A = U S V^*
398  // So A^{-*} = U S^{-1} V^*
399  // And A^{-T} = U_conj S^{-1} V^T
400  Matrix<typename RhsType::Scalar, Dynamic, RhsType::ColsAtCompileTime, 0, MatrixType::MaxRowsAtCompileTime, RhsType::MaxColsAtCompileTime> tmp;
401  Index l_rank = rank();
402 
403  tmp.noalias() = m_matrixV.leftCols(l_rank).transpose().template conjugateIf<Conjugate>() * rhs;
404  tmp = m_singularValues.head(l_rank).asDiagonal().inverse() * tmp;
405  dst = m_matrixU.template conjugateIf<!Conjugate>().leftCols(l_rank) * tmp;
406 }
407 #endif
408 
409 template <typename Derived>
410 bool SVDBase<Derived>::allocate(Index rows, Index cols, unsigned int computationOptions) {
411  eigen_assert(rows >= 0 && cols >= 0);
412 
413  if (m_isAllocated &&
414  rows == m_rows &&
415  cols == m_cols &&
416  computationOptions == m_computationOptions)
417  {
418  return true;
419  }
420 
421  m_rows = rows;
422  m_cols = cols;
423  m_info = Success;
424  m_isInitialized = false;
425  m_isAllocated = true;
426  m_computationOptions = computationOptions;
427  m_computeFullU = ShouldComputeFullU || internal::should_svd_compute_full_u(computationOptions);
428  m_computeThinU = ShouldComputeThinU || internal::should_svd_compute_thin_u(computationOptions);
429  m_computeFullV = ShouldComputeFullV || internal::should_svd_compute_full_v(computationOptions);
430  m_computeThinV = ShouldComputeThinV || internal::should_svd_compute_thin_v(computationOptions);
431 
432  eigen_assert(!(m_computeFullU && m_computeThinU) && "SVDBase: you can't ask for both full and thin U");
433  eigen_assert(!(m_computeFullV && m_computeThinV) && "SVDBase: you can't ask for both full and thin V");
434 
435  m_diagSize = (std::min)(m_rows, m_cols);
436  m_singularValues.resize(m_diagSize);
437  if(RowsAtCompileTime==Dynamic)
438  m_matrixU.resize(m_rows, m_computeFullU ? m_rows : m_computeThinU ? m_diagSize : 0);
439  if(ColsAtCompileTime==Dynamic)
440  m_matrixV.resize(m_cols, m_computeFullV ? m_cols : m_computeThinV ? m_diagSize : 0);
441 
442  return false;
443 }
444 
445 }// end namespace
446 
447 #endif // EIGEN_SVDBASE_H
const AbsReturnType abs() const
Array< int, 3, 1 > b
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:883
#define EIGEN_ONLY_USED_FOR_DEBUG(x)
Definition: Macros.h:914
#define eigen_assert(x)
Definition: Macros.h:902
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
#define EIGEN_STATIC_ASSERT_NON_INTEGER(TYPE)
Definition: StaticAssert.h:81
Matrix< float, 1, Dynamic > MatrixType
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:182
Base class of SVD algorithms.
Definition: SVDBase.h:120
void _check_solve_assertion(const Rhs &b) const
Definition: SVDBase.h:333
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: SVDBase.h:310
Index cols() const
Definition: SVDBase.h:287
static constexpr bool ShouldComputeFullV
Definition: SVDBase.h:134
Derived & setThreshold(const RealScalar &threshold)
Definition: SVDBase.h:247
bool m_usePrescribedThreshold
Definition: SVDBase.h:347
bool m_computeFullV
Definition: SVDBase.h:349
const MatrixVType & matrixV() const
Definition: SVDBase.h:191
Derived & setThreshold(Default_t)
Definition: SVDBase.h:262
static constexpr bool ShouldComputeThinV
Definition: SVDBase.h:135
Index rank() const
Definition: SVDBase.h:222
ComputationInfo m_info
Definition: SVDBase.h:346
bool m_computeThinU
Definition: SVDBase.h:348
const SingularValuesType & singularValues() const
Definition: SVDBase.h:203
NumTraits< typename MatrixType::Scalar >::Real RealScalar
Definition: SVDBase.h:128
bool computeV() const
Definition: SVDBase.h:284
Eigen::Index Index
Definition: SVDBase.h:130
bool m_isInitialized
Definition: SVDBase.h:347
Index m_cols
Definition: SVDBase.h:351
unsigned int m_computationOptions
Definition: SVDBase.h:350
MatrixVType m_matrixV
Definition: SVDBase.h:344
Index m_diagSize
Definition: SVDBase.h:351
bool computeU() const
Definition: SVDBase.h:282
internal::plain_diag_type< MatrixType, RealScalar >::type SingularValuesType
Definition: SVDBase.h:161
RealScalar threshold() const
Definition: SVDBase.h:272
MatrixType::Scalar Scalar
Definition: SVDBase.h:127
Index m_rows
Definition: SVDBase.h:351
Index m_nonzeroSingularValues
Definition: SVDBase.h:351
static constexpr bool ShouldComputeFullU
Definition: SVDBase.h:132
Index rows() const
Definition: SVDBase.h:286
const Solve< Derived, Rhs > solve(const MatrixBase< Rhs > &b) const
Derived & derived()
Definition: SVDBase.h:163
SingularValuesType m_singularValues
Definition: SVDBase.h:345
RealScalar m_prescribedThreshold
Definition: SVDBase.h:352
SVDBase()
Default Constructor.
Definition: SVDBase.h:358
static constexpr bool ShouldComputeThinU
Definition: SVDBase.h:133
bool m_computeThinV
Definition: SVDBase.h:349
bool m_computeFullU
Definition: SVDBase.h:348
bool allocate(Index rows, Index cols, unsigned int computationOptions)
Definition: SVDBase.h:410
@ MatrixVColsAtCompileTime
Definition: SVDBase.h:146
@ MatrixVMaxColsAtCompileTime
Definition: SVDBase.h:148
@ ColsAtCompileTime
Definition: SVDBase.h:139
@ DiagSizeAtCompileTime
Definition: SVDBase.h:140
@ MaxRowsAtCompileTime
Definition: SVDBase.h:141
@ MaxDiagSizeAtCompileTime
Definition: SVDBase.h:143
@ MatrixUColsAtCompileTime
Definition: SVDBase.h:145
@ MaxColsAtCompileTime
Definition: SVDBase.h:142
@ RowsAtCompileTime
Definition: SVDBase.h:138
@ MatrixUMaxColsAtCompileTime
Definition: SVDBase.h:147
const Derived & derived() const
Definition: SVDBase.h:164
Eigen::internal::traits< SVDBase >::StorageIndex StorageIndex
Definition: SVDBase.h:129
internal::traits< Derived >::MatrixType MatrixType
Definition: SVDBase.h:126
MatrixUType m_matrixU
Definition: SVDBase.h:343
void _check_compute_assertions() const
Definition: SVDBase.h:328
bool m_isAllocated
Definition: SVDBase.h:347
const MatrixUType & matrixU() const
Definition: SVDBase.h:175
Index nonzeroSingularValues() const
Definition: SVDBase.h:210
Pseudo expression representing a solving operation.
Definition: Solve.h:65
A base class for matrix decomposition and solvers.
Definition: SolverBase.h:71
ComputationInfo
Definition: Constants.h:444
@ NoQRPreconditioner
Definition: Constants.h:429
@ HouseholderQRPreconditioner
Definition: Constants.h:431
@ ColPivHouseholderQRPreconditioner
Definition: Constants.h:427
@ FullPivHouseholderQRPreconditioner
Definition: Constants.h:433
@ Success
Definition: Constants.h:446
@ ComputeFullV
Definition: Constants.h:399
@ ComputeThinV
Definition: Constants.h:401
@ ComputeFullU
Definition: Constants.h:395
@ ComputeThinU
Definition: Constants.h:397
bfloat16() min(const bfloat16 &a, const bfloat16 &b)
Definition: BFloat16.h:684
constexpr bool should_svd_compute_full_v(int options)
Definition: SVDBase.h:38
constexpr bool should_svd_compute_thin_u(int options)
Definition: SVDBase.h:35
constexpr int get_qr_preconditioner(int options)
Definition: SVDBase.h:31
constexpr bool should_svd_compute_thin_v(int options)
Definition: SVDBase.h:37
constexpr int min_size_prefer_fixed(A a, B b)
Definition: Meta.h:553
@ QRPreconditionerBits
Definition: SVDBase.h:26
@ ComputationOptionsBits
Definition: SVDBase.h:28
constexpr int get_computation_options(int options)
Definition: SVDBase.h:33
void check_svd_options_assertions(unsigned int computationOptions, Index rows, Index cols)
Definition: SVDBase.h:41
constexpr bool should_svd_compute_full_u(int options)
Definition: SVDBase.h:36
constexpr int min_size_prefer_dynamic(A a, B b)
Definition: Meta.h:537
: InteropHeaders
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
Default_t
Definition: Constants.h:364
const int Dynamic
Definition: Constants.h:24
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:41
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:231