16 #ifndef EIGEN_SVDBASE_H
17 #define EIGEN_SVDBASE_H
27 typedef int StorageIndex;
64 template<
typename Derived_>
68 typedef typename MatrixType::Scalar Scalar;
73 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
74 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
75 DiagSizeAtCompileTime = EIGEN_SIZE_MIN_PREFER_DYNAMIC(RowsAtCompileTime,ColsAtCompileTime),
76 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
77 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
78 MaxDiagSizeAtCompileTime = EIGEN_SIZE_MIN_PREFER_FIXED(MaxRowsAtCompileTime,MaxColsAtCompileTime),
79 MatrixOptions = MatrixType::Options
86 Derived& derived() {
return *
static_cast<Derived*
>(
this); }
87 const Derived& derived()
const {
return *
static_cast<const Derived*
>(
this); }
100 eigen_assert(m_isInitialized &&
"SVD is not initialized.");
101 eigen_assert(
computeU() &&
"This SVD decomposition didn't compute U. Did you ask for it?");
116 eigen_assert(m_isInitialized &&
"SVD is not initialized.");
117 eigen_assert(
computeV() &&
"This SVD decomposition didn't compute V. Did you ask for it?");
128 eigen_assert(m_isInitialized &&
"SVD is not initialized.");
129 return m_singularValues;
135 eigen_assert(m_isInitialized &&
"SVD is not initialized.");
136 return m_nonzeroSingularValues;
148 eigen_assert(m_isInitialized &&
"JacobiSVD is not initialized.");
149 if(m_singularValues.size()==0)
return 0;
150 RealScalar premultiplied_threshold = numext::maxi<RealScalar>(m_singularValues.coeff(0) *
threshold(), (std::numeric_limits<RealScalar>::min)());
151 Index i = m_nonzeroSingularValues-1;
152 while(i>=0 && m_singularValues.coeff(i) < premultiplied_threshold) --i;
172 m_usePrescribedThreshold =
true;
187 m_usePrescribedThreshold =
false;
197 eigen_assert(m_isInitialized || m_usePrescribedThreshold);
199 Index diagSize = (std::max<Index>)(1,m_diagSize);
200 return m_usePrescribedThreshold ? m_prescribedThreshold
205 inline bool computeU()
const {
return m_computeFullU || m_computeThinU; }
207 inline bool computeV()
const {
return m_computeFullV || m_computeThinV; }
209 inline Index rows()
const {
return m_rows; }
210 inline Index cols()
const {
return m_cols; }
212 #ifdef EIGEN_PARSED_BY_DOXYGEN
222 template<
typename Rhs>
223 inline const Solve<Derived, Rhs>
224 solve(
const MatrixBase<Rhs>& b)
const;
227 #ifndef EIGEN_PARSED_BY_DOXYGEN
228 template<
typename RhsType,
typename DstType>
229 void _solve_impl(
const RhsType &rhs, DstType &dst)
const;
231 template<
bool Conjugate,
typename RhsType,
typename DstType>
232 void _solve_impl_transposed(
const RhsType &rhs, DstType &dst)
const;
237 static void check_template_parameters()
239 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar);
242 template<
bool Transpose_,
typename Rhs>
243 void _check_solve_assertion(
const Rhs& b)
const {
244 EIGEN_ONLY_USED_FOR_DEBUG(b);
245 eigen_assert(m_isInitialized &&
"SVD is not initialized.");
246 eigen_assert(
computeU() &&
computeV() &&
"SVDBase::solve(): Both unitaries U and V are required to be computed (thin unitaries suffice).");
247 eigen_assert((Transpose_?cols():rows())==b.rows() &&
"SVDBase::solve(): invalid number of rows of the right hand side matrix b");
251 bool allocate(
Index rows,
Index cols,
unsigned int computationOptions) ;
253 MatrixUType m_matrixU;
254 MatrixVType m_matrixV;
255 SingularValuesType m_singularValues;
256 bool m_isInitialized, m_isAllocated, m_usePrescribedThreshold;
257 bool m_computeFullU, m_computeThinU;
258 bool m_computeFullV, m_computeThinV;
259 unsigned int m_computationOptions;
260 Index m_nonzeroSingularValues, m_rows, m_cols, m_diagSize;
261 RealScalar m_prescribedThreshold;
268 : m_isInitialized(false),
269 m_isAllocated(false),
270 m_usePrescribedThreshold(false),
271 m_computeFullU(false),
272 m_computeThinU(false),
273 m_computeFullV(false),
274 m_computeThinV(false),
275 m_computationOptions(0),
276 m_rows(-1), m_cols(-1), m_diagSize(0)
278 check_template_parameters();
284 #ifndef EIGEN_PARSED_BY_DOXYGEN
285 template<
typename Derived>
286 template<
typename RhsType,
typename DstType>
287 void SVDBase<Derived>::_solve_impl(
const RhsType &rhs, DstType &dst)
const
292 Matrix<typename RhsType::Scalar, Dynamic, RhsType::ColsAtCompileTime, 0, MatrixType::MaxRowsAtCompileTime, RhsType::MaxColsAtCompileTime> tmp;
293 Index l_rank = rank();
294 tmp.noalias() = m_matrixU.leftCols(l_rank).adjoint() * rhs;
295 tmp = m_singularValues.head(l_rank).asDiagonal().inverse() * tmp;
296 dst = m_matrixV.leftCols(l_rank) * tmp;
299 template<
typename Derived>
300 template<
bool Conjugate,
typename RhsType,
typename DstType>
301 void SVDBase<Derived>::_solve_impl_transposed(
const RhsType &rhs, DstType &dst)
const
306 Matrix<typename RhsType::Scalar, Dynamic, RhsType::ColsAtCompileTime, 0, MatrixType::MaxRowsAtCompileTime, RhsType::MaxColsAtCompileTime> tmp;
307 Index l_rank = rank();
309 tmp.noalias() = m_matrixV.leftCols(l_rank).transpose().template conjugateIf<Conjugate>() * rhs;
310 tmp = m_singularValues.head(l_rank).asDiagonal().inverse() * tmp;
311 dst = m_matrixU.template conjugateIf<!Conjugate>().leftCols(l_rank) * tmp;
315 template<
typename MatrixType>
316 bool SVDBase<MatrixType>::allocate(
Index rows,
Index cols,
unsigned int computationOptions)
318 eigen_assert(rows >= 0 && cols >= 0);
323 computationOptions == m_computationOptions)
330 m_isInitialized =
false;
331 m_isAllocated =
true;
332 m_computationOptions = computationOptions;
333 m_computeFullU = (computationOptions &
ComputeFullU) != 0;
334 m_computeThinU = (computationOptions &
ComputeThinU) != 0;
335 m_computeFullV = (computationOptions &
ComputeFullV) != 0;
336 m_computeThinV = (computationOptions &
ComputeThinV) != 0;
337 eigen_assert(!(m_computeFullU && m_computeThinU) &&
"SVDBase: you can't ask for both full and thin U");
338 eigen_assert(!(m_computeFullV && m_computeThinV) &&
"SVDBase: you can't ask for both full and thin V");
339 eigen_assert(EIGEN_IMPLIES(m_computeThinU || m_computeThinV, MatrixType::ColsAtCompileTime==
Dynamic) &&
340 "SVDBase: thin U and V are only available when your matrix has a dynamic number of columns.");
342 m_diagSize = (std::min)(m_rows, m_cols);
343 m_singularValues.resize(m_diagSize);
345 m_matrixU.resize(m_rows, m_computeFullU ? m_rows : m_computeThinU ? m_diagSize : 0);
347 m_matrixV.resize(m_cols, m_computeFullV ? m_cols : m_computeThinV ? m_diagSize : 0);
354 #endif // EIGEN_SVDBASE_H