SVDBase.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. #ifndef EIGEN_SVDBASE_H
  16. #define EIGEN_SVDBASE_H
  17. namespace Eigen {
  18. /** \ingroup SVD_Module
  19. *
  20. *
  21. * \class SVDBase
  22. *
  23. * \brief Base class of SVD algorithms
  24. *
  25. * \tparam Derived the type of the actual SVD decomposition
  26. *
  27. * SVD decomposition consists in decomposing any n-by-p matrix \a A as a product
  28. * \f[ A = U S V^* \f]
  29. * where \a U is a n-by-n unitary, \a V is a p-by-p unitary, and \a S is a n-by-p real positive matrix which is zero outside of its main diagonal;
  30. * the diagonal entries of S are known as the \em singular \em values of \a A and the columns of \a U and \a V are known as the left
  31. * and right \em singular \em vectors of \a A respectively.
  32. *
  33. * Singular values are always sorted in decreasing order.
  34. *
  35. *
  36. * You can ask for only \em thin \a U or \a V to be computed, meaning the following. In case of a rectangular n-by-p matrix, letting \a m be the
  37. * smaller value among \a n and \a p, there are only \a m singular vectors; the remaining columns of \a U and \a V do not correspond to actual
  38. * singular vectors. Asking for \em thin \a U or \a V means asking for only their \a m first columns to be formed. So \a U is then a n-by-m matrix,
  39. * and \a V is then a p-by-m matrix. Notice that thin \a U and \a V are all you need for (least squares) solving.
  40. *
  41. * If the input matrix has inf or nan coefficients, the result of the computation is undefined, but the computation is guaranteed to
  42. * terminate in finite (and reasonable) time.
  43. * \sa class BDCSVD, class JacobiSVD
  44. */
  45. template<typename Derived>
  46. class SVDBase
  47. {
  48. public:
  49. typedef typename internal::traits<Derived>::MatrixType MatrixType;
  50. typedef typename MatrixType::Scalar Scalar;
  51. typedef typename NumTraits<typename MatrixType::Scalar>::Real RealScalar;
  52. typedef typename MatrixType::StorageIndex StorageIndex;
  53. typedef Eigen::Index Index; ///< \deprecated since Eigen 3.3
  54. enum {
  55. RowsAtCompileTime = MatrixType::RowsAtCompileTime,
  56. ColsAtCompileTime = MatrixType::ColsAtCompileTime,
  57. DiagSizeAtCompileTime = EIGEN_SIZE_MIN_PREFER_DYNAMIC(RowsAtCompileTime,ColsAtCompileTime),
  58. MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
  59. MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
  60. MaxDiagSizeAtCompileTime = EIGEN_SIZE_MIN_PREFER_FIXED(MaxRowsAtCompileTime,MaxColsAtCompileTime),
  61. MatrixOptions = MatrixType::Options
  62. };
  63. typedef Matrix<Scalar, RowsAtCompileTime, RowsAtCompileTime, MatrixOptions, MaxRowsAtCompileTime, MaxRowsAtCompileTime> MatrixUType;
  64. typedef Matrix<Scalar, ColsAtCompileTime, ColsAtCompileTime, MatrixOptions, MaxColsAtCompileTime, MaxColsAtCompileTime> MatrixVType;
  65. typedef typename internal::plain_diag_type<MatrixType, RealScalar>::type SingularValuesType;
  66. Derived& derived() { return *static_cast<Derived*>(this); }
  67. const Derived& derived() const { return *static_cast<const Derived*>(this); }
  68. /** \returns the \a U matrix.
  69. *
  70. * For the SVD decomposition of a n-by-p matrix, letting \a m be the minimum of \a n and \a p,
  71. * the U matrix is n-by-n if you asked for \link Eigen::ComputeFullU ComputeFullU \endlink, and is n-by-m if you asked for \link Eigen::ComputeThinU ComputeThinU \endlink.
  72. *
  73. * The \a m first columns of \a U are the left singular vectors of the matrix being decomposed.
  74. *
  75. * This method asserts that you asked for \a U to be computed.
  76. */
  77. const MatrixUType& matrixU() const
  78. {
  79. eigen_assert(m_isInitialized && "SVD is not initialized.");
  80. eigen_assert(computeU() && "This SVD decomposition didn't compute U. Did you ask for it?");
  81. return m_matrixU;
  82. }
  83. /** \returns the \a V matrix.
  84. *
  85. * For the SVD decomposition of a n-by-p matrix, letting \a m be the minimum of \a n and \a p,
  86. * the V matrix is p-by-p if you asked for \link Eigen::ComputeFullV ComputeFullV \endlink, and is p-by-m if you asked for \link Eigen::ComputeThinV ComputeThinV \endlink.
  87. *
  88. * The \a m first columns of \a V are the right singular vectors of the matrix being decomposed.
  89. *
  90. * This method asserts that you asked for \a V to be computed.
  91. */
  92. const MatrixVType& matrixV() const
  93. {
  94. eigen_assert(m_isInitialized && "SVD is not initialized.");
  95. eigen_assert(computeV() && "This SVD decomposition didn't compute V. Did you ask for it?");
  96. return m_matrixV;
  97. }
  98. /** \returns the vector of singular values.
  99. *
  100. * For the SVD decomposition of a n-by-p matrix, letting \a m be the minimum of \a n and \a p, the
  101. * returned vector has size \a m. Singular values are always sorted in decreasing order.
  102. */
  103. const SingularValuesType& singularValues() const
  104. {
  105. eigen_assert(m_isInitialized && "SVD is not initialized.");
  106. return m_singularValues;
  107. }
  108. /** \returns the number of singular values that are not exactly 0 */
  109. Index nonzeroSingularValues() const
  110. {
  111. eigen_assert(m_isInitialized && "SVD is not initialized.");
  112. return m_nonzeroSingularValues;
  113. }
  114. /** \returns the rank of the matrix of which \c *this is the SVD.
  115. *
  116. * \note This method has to determine which singular values should be considered nonzero.
  117. * For that, it uses the threshold value that you can control by calling
  118. * setThreshold(const RealScalar&).
  119. */
  120. inline Index rank() const
  121. {
  122. using std::abs;
  123. eigen_assert(m_isInitialized && "JacobiSVD is not initialized.");
  124. if(m_singularValues.size()==0) return 0;
  125. RealScalar premultiplied_threshold = numext::maxi<RealScalar>(m_singularValues.coeff(0) * threshold(), (std::numeric_limits<RealScalar>::min)());
  126. Index i = m_nonzeroSingularValues-1;
  127. while(i>=0 && m_singularValues.coeff(i) < premultiplied_threshold) --i;
  128. return i+1;
  129. }
  130. /** Allows to prescribe a threshold to be used by certain methods, such as rank() and solve(),
  131. * which need to determine when singular values are to be considered nonzero.
  132. * This is not used for the SVD decomposition itself.
  133. *
  134. * When it needs to get the threshold value, Eigen calls threshold().
  135. * The default is \c NumTraits<Scalar>::epsilon()
  136. *
  137. * \param threshold The new value to use as the threshold.
  138. *
  139. * A singular value will be considered nonzero if its value is strictly greater than
  140. * \f$ \vert singular value \vert \leqslant threshold \times \vert max singular value \vert \f$.
  141. *
  142. * If you want to come back to the default behavior, call setThreshold(Default_t)
  143. */
  144. Derived& setThreshold(const RealScalar& threshold)
  145. {
  146. m_usePrescribedThreshold = true;
  147. m_prescribedThreshold = threshold;
  148. return derived();
  149. }
  150. /** Allows to come back to the default behavior, letting Eigen use its default formula for
  151. * determining the threshold.
  152. *
  153. * You should pass the special object Eigen::Default as parameter here.
  154. * \code svd.setThreshold(Eigen::Default); \endcode
  155. *
  156. * See the documentation of setThreshold(const RealScalar&).
  157. */
  158. Derived& setThreshold(Default_t)
  159. {
  160. m_usePrescribedThreshold = false;
  161. return derived();
  162. }
  163. /** Returns the threshold that will be used by certain methods such as rank().
  164. *
  165. * See the documentation of setThreshold(const RealScalar&).
  166. */
  167. RealScalar threshold() const
  168. {
  169. eigen_assert(m_isInitialized || m_usePrescribedThreshold);
  170. // this temporary is needed to workaround a MSVC issue
  171. Index diagSize = (std::max<Index>)(1,m_diagSize);
  172. return m_usePrescribedThreshold ? m_prescribedThreshold
  173. : RealScalar(diagSize)*NumTraits<Scalar>::epsilon();
  174. }
  175. /** \returns true if \a U (full or thin) is asked for in this SVD decomposition */
  176. inline bool computeU() const { return m_computeFullU || m_computeThinU; }
  177. /** \returns true if \a V (full or thin) is asked for in this SVD decomposition */
  178. inline bool computeV() const { return m_computeFullV || m_computeThinV; }
  179. inline Index rows() const { return m_rows; }
  180. inline Index cols() const { return m_cols; }
  181. /** \returns a (least squares) solution of \f$ A x = b \f$ using the current SVD decomposition of A.
  182. *
  183. * \param b the right-hand-side of the equation to solve.
  184. *
  185. * \note Solving requires both U and V to be computed. Thin U and V are enough, there is no need for full U or V.
  186. *
  187. * \note SVD solving is implicitly least-squares. Thus, this method serves both purposes of exact solving and least-squares solving.
  188. * In other words, the returned solution is guaranteed to minimize the Euclidean norm \f$ \Vert A x - b \Vert \f$.
  189. */
  190. template<typename Rhs>
  191. inline const Solve<Derived, Rhs>
  192. solve(const MatrixBase<Rhs>& b) const
  193. {
  194. eigen_assert(m_isInitialized && "SVD is not initialized.");
  195. eigen_assert(computeU() && computeV() && "SVD::solve() requires both unitaries U and V to be computed (thin unitaries suffice).");
  196. return Solve<Derived, Rhs>(derived(), b.derived());
  197. }
  198. #ifndef EIGEN_PARSED_BY_DOXYGEN
  199. template<typename RhsType, typename DstType>
  200. EIGEN_DEVICE_FUNC
  201. void _solve_impl(const RhsType &rhs, DstType &dst) const;
  202. #endif
  203. protected:
  204. static void check_template_parameters()
  205. {
  206. EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar);
  207. }
  208. // return true if already allocated
  209. bool allocate(Index rows, Index cols, unsigned int computationOptions) ;
  210. MatrixUType m_matrixU;
  211. MatrixVType m_matrixV;
  212. SingularValuesType m_singularValues;
  213. bool m_isInitialized, m_isAllocated, m_usePrescribedThreshold;
  214. bool m_computeFullU, m_computeThinU;
  215. bool m_computeFullV, m_computeThinV;
  216. unsigned int m_computationOptions;
  217. Index m_nonzeroSingularValues, m_rows, m_cols, m_diagSize;
  218. RealScalar m_prescribedThreshold;
  219. /** \brief Default Constructor.
  220. *
  221. * Default constructor of SVDBase
  222. */
  223. SVDBase()
  224. : m_isInitialized(false),
  225. m_isAllocated(false),
  226. m_usePrescribedThreshold(false),
  227. m_computationOptions(0),
  228. m_rows(-1), m_cols(-1), m_diagSize(0)
  229. {
  230. check_template_parameters();
  231. }
  232. };
  233. #ifndef EIGEN_PARSED_BY_DOXYGEN
  234. template<typename Derived>
  235. template<typename RhsType, typename DstType>
  236. void SVDBase<Derived>::_solve_impl(const RhsType &rhs, DstType &dst) const
  237. {
  238. eigen_assert(rhs.rows() == rows());
  239. // A = U S V^*
  240. // So A^{-1} = V S^{-1} U^*
  241. Matrix<Scalar, Dynamic, RhsType::ColsAtCompileTime, 0, MatrixType::MaxRowsAtCompileTime, RhsType::MaxColsAtCompileTime> tmp;
  242. Index l_rank = rank();
  243. tmp.noalias() = m_matrixU.leftCols(l_rank).adjoint() * rhs;
  244. tmp = m_singularValues.head(l_rank).asDiagonal().inverse() * tmp;
  245. dst = m_matrixV.leftCols(l_rank) * tmp;
  246. }
  247. #endif
  248. template<typename MatrixType>
  249. bool SVDBase<MatrixType>::allocate(Index rows, Index cols, unsigned int computationOptions)
  250. {
  251. eigen_assert(rows >= 0 && cols >= 0);
  252. if (m_isAllocated &&
  253. rows == m_rows &&
  254. cols == m_cols &&
  255. computationOptions == m_computationOptions)
  256. {
  257. return true;
  258. }
  259. m_rows = rows;
  260. m_cols = cols;
  261. m_isInitialized = false;
  262. m_isAllocated = true;
  263. m_computationOptions = computationOptions;
  264. m_computeFullU = (computationOptions & ComputeFullU) != 0;
  265. m_computeThinU = (computationOptions & ComputeThinU) != 0;
  266. m_computeFullV = (computationOptions & ComputeFullV) != 0;
  267. m_computeThinV = (computationOptions & ComputeThinV) != 0;
  268. eigen_assert(!(m_computeFullU && m_computeThinU) && "SVDBase: you can't ask for both full and thin U");
  269. eigen_assert(!(m_computeFullV && m_computeThinV) && "SVDBase: you can't ask for both full and thin V");
  270. eigen_assert(EIGEN_IMPLIES(m_computeThinU || m_computeThinV, MatrixType::ColsAtCompileTime==Dynamic) &&
  271. "SVDBase: thin U and V are only available when your matrix has a dynamic number of columns.");
  272. m_diagSize = (std::min)(m_rows, m_cols);
  273. m_singularValues.resize(m_diagSize);
  274. if(RowsAtCompileTime==Dynamic)
  275. m_matrixU.resize(m_rows, m_computeFullU ? m_rows : m_computeThinU ? m_diagSize : 0);
  276. if(ColsAtCompileTime==Dynamic)
  277. m_matrixV.resize(m_cols, m_computeFullV ? m_cols : m_computeThinV ? m_diagSize : 0);
  278. return false;
  279. }
  280. }// end namespace
  281. #endif // EIGEN_SVDBASE_H