#ifndef EIGEN_SVDBASE_H
#define EIGEN_SVDBASE_H
#include "./InternalHeaderCheck.h"
namespace Eigen {
namespace internal {
enum OptionsMasks {
QRPreconditionerBits = NoQRPreconditioner | HouseholderQRPreconditioner | ColPivHouseholderQRPreconditioner |
FullPivHouseholderQRPreconditioner,
ComputationOptionsBits = ComputeThinU | ComputeFullU | ComputeThinV | ComputeFullV
};
constexpr int get_qr_preconditioner(int options) { return options & QRPreconditionerBits; }
constexpr int get_computation_options(int options) { return options & ComputationOptionsBits; }
constexpr bool should_svd_compute_thin_u(int options) { return (options & ComputeThinU) != 0; }
constexpr bool should_svd_compute_full_u(int options) { return (options & ComputeFullU) != 0; }
constexpr bool should_svd_compute_thin_v(int options) { return (options & ComputeThinV) != 0; }
constexpr bool should_svd_compute_full_v(int options) { return (options & ComputeFullV) != 0; }
template <typename MatrixType, int Options>
void check_svd_options_assertions(unsigned int computationOptions, Index rows, Index cols) {
EIGEN_STATIC_ASSERT((Options & ComputationOptionsBits) == 0,
"SVDBase: Cannot request U or V using both static and runtime options, even if they match. "
"Requesting unitaries at runtime is DEPRECATED: "
"Prefer requesting unitaries statically, using the Options template parameter.");
eigen_assert(
!(should_svd_compute_thin_u(computationOptions) && cols < rows && MatrixType::RowsAtCompileTime != Dynamic) &&
!(should_svd_compute_thin_v(computationOptions) && rows < cols && MatrixType::ColsAtCompileTime != Dynamic) &&
"SVDBase: If thin U is requested at runtime, your matrix must have more rows than columns or a dynamic number of "
"rows."
"Similarly, if thin V is requested at runtime, you matrix must have more columns than rows or a dynamic number "
"of columns.");
(void)computationOptions;
(void)rows;
(void)cols;
}
template <typename Derived>
struct traits<SVDBase<Derived> > : traits<Derived> {
typedef MatrixXpr XprKind;
typedef SolverStorage StorageKind;
typedef int StorageIndex;
enum { Flags = 0 };
};
template <typename MatrixType, int Options_>
struct svd_traits : traits<MatrixType> {
static constexpr int Options = Options_;
static constexpr bool ShouldComputeFullU = internal::should_svd_compute_full_u(Options);
static constexpr bool ShouldComputeThinU = internal::should_svd_compute_thin_u(Options);
static constexpr bool ShouldComputeFullV = internal::should_svd_compute_full_v(Options);
static constexpr bool ShouldComputeThinV = internal::should_svd_compute_thin_v(Options);
enum {
DiagSizeAtCompileTime =
internal::min_size_prefer_dynamic(MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime),
MaxDiagSizeAtCompileTime =
internal::min_size_prefer_dynamic(MatrixType::MaxRowsAtCompileTime, MatrixType::MaxColsAtCompileTime),
MatrixUColsAtCompileTime = ShouldComputeThinU ? DiagSizeAtCompileTime : MatrixType::RowsAtCompileTime,
MatrixVColsAtCompileTime = ShouldComputeThinV ? DiagSizeAtCompileTime : MatrixType::ColsAtCompileTime,
MatrixUMaxColsAtCompileTime = ShouldComputeThinU ? MaxDiagSizeAtCompileTime : MatrixType::MaxRowsAtCompileTime,
MatrixVMaxColsAtCompileTime = ShouldComputeThinV ? MaxDiagSizeAtCompileTime : MatrixType::MaxColsAtCompileTime
};
};
}
template <typename Derived>
class SVDBase : public SolverBase<SVDBase<Derived> > {
public:
template <typename Derived_>
friend struct internal::solve_assertion;
typedef typename internal::traits<Derived>::MatrixType MatrixType;
typedef typename MatrixType::Scalar Scalar;
typedef typename NumTraits<typename MatrixType::Scalar>::Real RealScalar;
typedef typename Eigen::internal::traits<SVDBase>::StorageIndex StorageIndex;
static constexpr bool ShouldComputeFullU = internal::traits<Derived>::ShouldComputeFullU;
static constexpr bool ShouldComputeThinU = internal::traits<Derived>::ShouldComputeThinU;
static constexpr bool ShouldComputeFullV = internal::traits<Derived>::ShouldComputeFullV;
static constexpr bool ShouldComputeThinV = internal::traits<Derived>::ShouldComputeThinV;
enum {
RowsAtCompileTime = MatrixType::RowsAtCompileTime,
ColsAtCompileTime = MatrixType::ColsAtCompileTime,
DiagSizeAtCompileTime = internal::min_size_prefer_dynamic(RowsAtCompileTime, ColsAtCompileTime),
MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
MaxDiagSizeAtCompileTime = internal::min_size_prefer_fixed(MaxRowsAtCompileTime, MaxColsAtCompileTime),
MatrixOptions = internal::traits<MatrixType>::Options,
MatrixUColsAtCompileTime = internal::traits<Derived>::MatrixUColsAtCompileTime,
MatrixVColsAtCompileTime = internal::traits<Derived>::MatrixVColsAtCompileTime,
MatrixUMaxColsAtCompileTime = internal::traits<Derived>::MatrixUMaxColsAtCompileTime,
MatrixVMaxColsAtCompileTime = internal::traits<Derived>::MatrixVMaxColsAtCompileTime
};
EIGEN_STATIC_ASSERT(!(ShouldComputeFullU && ShouldComputeThinU), "SVDBase: Cannot request both full and thin U")
EIGEN_STATIC_ASSERT(!(ShouldComputeFullV && ShouldComputeThinV), "SVDBase: Cannot request both full and thin V")
typedef
typename internal::make_proper_matrix_type<Scalar, RowsAtCompileTime, MatrixUColsAtCompileTime, MatrixOptions,
MaxRowsAtCompileTime, MatrixUMaxColsAtCompileTime>::type MatrixUType;
typedef
typename internal::make_proper_matrix_type<Scalar, ColsAtCompileTime, MatrixVColsAtCompileTime, MatrixOptions,
MaxColsAtCompileTime, MatrixVMaxColsAtCompileTime>::type MatrixVType;
typedef typename internal::plain_diag_type<MatrixType, RealScalar>::type SingularValuesType;
Derived& derived() { return *static_cast<Derived*>(this); }
const Derived& derived() const { return *static_cast<const Derived*>(this); }
const MatrixUType& matrixU() const {
_check_compute_assertions();
eigen_assert(computeU() && "This SVD decomposition didn't compute U. Did you ask for it?");
return m_matrixU;
}
const MatrixVType& matrixV() const {
_check_compute_assertions();
eigen_assert(computeV() && "This SVD decomposition didn't compute V. Did you ask for it?");
return m_matrixV;
}
const SingularValuesType& singularValues() const {
_check_compute_assertions();
return m_singularValues;
}
Index nonzeroSingularValues() const {
_check_compute_assertions();
return m_nonzeroSingularValues;
}
inline Index rank() const {
using std::abs;
_check_compute_assertions();
if (m_singularValues.size() == 0) return 0;
RealScalar premultiplied_threshold =
numext::maxi<RealScalar>(m_singularValues.coeff(0) * threshold(), (std::numeric_limits<RealScalar>::min)());
Index i = m_nonzeroSingularValues - 1;
while (i >= 0 && m_singularValues.coeff(i) < premultiplied_threshold) --i;
return i + 1;
}
Derived& setThreshold(const RealScalar& threshold) {
m_usePrescribedThreshold = true;
m_prescribedThreshold = threshold;
return derived();
}
Derived& setThreshold(Default_t) {
m_usePrescribedThreshold = false;
return derived();
}
RealScalar threshold() const {
eigen_assert(m_isInitialized || m_usePrescribedThreshold);
Index diagSize = (std::max<Index>)(1, m_diagSize);
return m_usePrescribedThreshold ? m_prescribedThreshold : RealScalar(diagSize) * NumTraits<Scalar>::epsilon();
}
inline bool computeU() const { return m_computeFullU || m_computeThinU; }
inline bool computeV() const { return m_computeFullV || m_computeThinV; }
inline Index rows() const { return m_rows.value(); }
inline Index cols() const { return m_cols.value(); }
inline Index diagSize() const { return m_diagSize.value(); }
#ifdef EIGEN_PARSED_BY_DOXYGEN
template <typename Rhs>
inline const Solve<Derived, Rhs> solve(const MatrixBase<Rhs>& b) const;
#endif
EIGEN_DEVICE_FUNC ComputationInfo info() const {
eigen_assert(m_isInitialized && "SVD is not initialized.");
return m_info;
}
#ifndef EIGEN_PARSED_BY_DOXYGEN
template <typename RhsType, typename DstType>
void _solve_impl(const RhsType& rhs, DstType& dst) const;
template <bool Conjugate, typename RhsType, typename DstType>
void _solve_impl_transposed(const RhsType& rhs, DstType& dst) const;
#endif
protected:
EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
void _check_compute_assertions() const { eigen_assert(m_isInitialized && "SVD is not initialized."); }
template <bool Transpose_, typename Rhs>
void _check_solve_assertion(const Rhs& b) const {
EIGEN_ONLY_USED_FOR_DEBUG(b);
_check_compute_assertions();
eigen_assert(computeU() && computeV() &&
"SVDBase::solve(): Both unitaries U and V are required to be computed (thin unitaries suffice).");
eigen_assert((Transpose_ ? cols() : rows()) == b.rows() &&
"SVDBase::solve(): invalid number of rows of the right hand side matrix b");
}
bool allocate(Index rows, Index cols, unsigned int computationOptions);
MatrixUType m_matrixU;
MatrixVType m_matrixV;
SingularValuesType m_singularValues;
ComputationInfo m_info;
bool m_isInitialized, m_isAllocated, m_usePrescribedThreshold;
bool m_computeFullU, m_computeThinU;
bool m_computeFullV, m_computeThinV;
unsigned int m_computationOptions;
Index m_nonzeroSingularValues;
internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows;
internal::variable_if_dynamic<Index, ColsAtCompileTime> m_cols;
internal::variable_if_dynamic<Index, DiagSizeAtCompileTime> m_diagSize;
RealScalar m_prescribedThreshold;
SVDBase()
: m_matrixU(MatrixUType()),
m_matrixV(MatrixVType()),
m_singularValues(SingularValuesType()),
m_info(Success),
m_isInitialized(false),
m_isAllocated(false),
m_usePrescribedThreshold(false),
m_computeFullU(ShouldComputeFullU),
m_computeThinU(ShouldComputeThinU),
m_computeFullV(ShouldComputeFullV),
m_computeThinV(ShouldComputeThinV),
m_computationOptions(internal::traits<Derived>::Options),
m_nonzeroSingularValues(0),
m_rows(RowsAtCompileTime),
m_cols(ColsAtCompileTime),
m_diagSize(DiagSizeAtCompileTime),
m_prescribedThreshold(0) {}
};
#ifndef EIGEN_PARSED_BY_DOXYGEN
template <typename Derived>
template <typename RhsType, typename DstType>
void SVDBase<Derived>::_solve_impl(const RhsType& rhs, DstType& dst) const {
Matrix<typename RhsType::Scalar, Dynamic, RhsType::ColsAtCompileTime, 0, MatrixType::MaxRowsAtCompileTime,
RhsType::MaxColsAtCompileTime>
tmp;
Index l_rank = rank();
tmp.noalias() = m_matrixU.leftCols(l_rank).adjoint() * rhs;
tmp = m_singularValues.head(l_rank).asDiagonal().inverse() * tmp;
dst.noalias() = m_matrixV.leftCols(l_rank) * tmp;
}
template <typename Derived>
template <bool Conjugate, typename RhsType, typename DstType>
void SVDBase<Derived>::_solve_impl_transposed(const RhsType& rhs, DstType& dst) const {
Matrix<typename RhsType::Scalar, Dynamic, RhsType::ColsAtCompileTime, 0, MatrixType::MaxRowsAtCompileTime,
RhsType::MaxColsAtCompileTime>
tmp;
Index l_rank = rank();
tmp.noalias() = m_matrixV.leftCols(l_rank).transpose().template conjugateIf<Conjugate>() * rhs;
tmp = m_singularValues.head(l_rank).asDiagonal().inverse() * tmp;
dst = m_matrixU.template conjugateIf<!Conjugate>().leftCols(l_rank) * tmp;
}
#endif
template <typename Derived>
bool SVDBase<Derived>::allocate(Index rows, Index cols, unsigned int computationOptions) {
eigen_assert(rows >= 0 && cols >= 0);
if (m_isAllocated && rows == m_rows.value() && cols == m_cols.value() && computationOptions == m_computationOptions) {
return true;
}
m_rows.setValue(rows);
m_cols.setValue(cols);
m_info = Success;
m_isInitialized = false;
m_isAllocated = true;
m_computationOptions = computationOptions;
m_computeFullU = ShouldComputeFullU || internal::should_svd_compute_full_u(computationOptions);
m_computeThinU = ShouldComputeThinU || internal::should_svd_compute_thin_u(computationOptions);
m_computeFullV = ShouldComputeFullV || internal::should_svd_compute_full_v(computationOptions);
m_computeThinV = ShouldComputeThinV || internal::should_svd_compute_thin_v(computationOptions);
eigen_assert(!(m_computeFullU && m_computeThinU) && "SVDBase: you can't ask for both full and thin U");
eigen_assert(!(m_computeFullV && m_computeThinV) && "SVDBase: you can't ask for both full and thin V");
m_diagSize.setValue(numext::mini(m_rows.value(), m_cols.value()));
m_singularValues.resize(m_diagSize.value());
if (RowsAtCompileTime == Dynamic)
m_matrixU.resize(m_rows.value(), m_computeFullU ? m_rows.value() : m_computeThinU ? m_diagSize.value() : 0);
if (ColsAtCompileTime == Dynamic)
m_matrixV.resize(m_cols.value(), m_computeFullV ? m_cols.value() : m_computeThinV ? m_diagSize.value() : 0);
return false;
}
}
#endif