Eigen::JacobiSVD< MatrixType_, Options_ > Class Template Reference

Two-sided Jacobi SVD decomposition of a rectangular matrix. More...

#include <JacobiSVD.h>

+ Inheritance diagram for Eigen::JacobiSVD< MatrixType_, Options_ >:

Public Types

enum  : int {
  Options = Options_ , QRPreconditioner = internal::get_qr_preconditioner(Options) , RowsAtCompileTime = Base::RowsAtCompileTime , ColsAtCompileTime = Base::ColsAtCompileTime ,
  DiagSizeAtCompileTime = Base::DiagSizeAtCompileTime , MaxRowsAtCompileTime = Base::MaxRowsAtCompileTime , MaxColsAtCompileTime = Base::MaxColsAtCompileTime , MaxDiagSizeAtCompileTime = Base::MaxDiagSizeAtCompileTime ,
  MatrixOptions = Base::MatrixOptions
}
 
typedef MatrixType_ MatrixType
 
typedef Base::Scalar Scalar
 
typedef Base::RealScalar RealScalar
 
typedef Base::MatrixUType MatrixUType
 
typedef Base::MatrixVType MatrixVType
 
typedef Base::SingularValuesType SingularValuesType
 
typedef Matrix< Scalar, DiagSizeAtCompileTime, DiagSizeAtCompileTime, MatrixOptions, MaxDiagSizeAtCompileTime, MaxDiagSizeAtCompileTimeWorkMatrixType
 
- Public Types inherited from Eigen::SVDBase< JacobiSVD< MatrixType_, Options_ > >
enum  
 
typedef internal::traits< JacobiSVD< MatrixType_, Options_ > >::MatrixType MatrixType
 
typedef MatrixType::Scalar Scalar
 
typedef NumTraits< typename MatrixType::Scalar >::Real RealScalar
 
typedef Eigen::internal::traits< SVDBase >::StorageIndex StorageIndex
 
typedef internal::make_proper_matrix_type< Scalar, RowsAtCompileTime, MatrixUColsAtCompileTime, MatrixOptions, MaxRowsAtCompileTime, MatrixUMaxColsAtCompileTime >::type MatrixUType
 
typedef internal::make_proper_matrix_type< Scalar, ColsAtCompileTime, MatrixVColsAtCompileTime, MatrixOptions, MaxColsAtCompileTime, MatrixVMaxColsAtCompileTime >::type MatrixVType
 
typedef internal::plain_diag_type< MatrixType, RealScalar >::type SingularValuesType
 
- Public Types inherited from Eigen::SolverBase< Derived >
enum  {
  RowsAtCompileTime = internal::traits<Derived>::RowsAtCompileTime , ColsAtCompileTime = internal::traits<Derived>::ColsAtCompileTime , SizeAtCompileTime = (internal::size_of_xpr_at_compile_time<Derived>::ret) , MaxRowsAtCompileTime = internal::traits<Derived>::MaxRowsAtCompileTime ,
  MaxColsAtCompileTime = internal::traits<Derived>::MaxColsAtCompileTime , MaxSizeAtCompileTime , IsVectorAtCompileTime , NumDimensions
}
 
typedef EigenBase< Derived > Base
 
typedef internal::traits< Derived >::Scalar Scalar
 
typedef Scalar CoeffReturnType
 
typedef Transpose< const Derived > ConstTransposeReturnType
 
typedef std::conditional_t< NumTraits< Scalar >::IsComplex, CwiseUnaryOp< internal::scalar_conjugate_op< Scalar >, const ConstTransposeReturnType >, const ConstTransposeReturnTypeAdjointReturnType
 
- Public Types inherited from Eigen::EigenBase< Derived >
typedef Eigen::Index Index
 The interface type of indices. More...
 
typedef internal::traits< Derived >::StorageKind StorageKind
 

Public Member Functions

 JacobiSVD ()
 Default Constructor. More...
 
 JacobiSVD (Index rows, Index cols)
 Default Constructor with memory preallocation. More...
 
EIGEN_DEPRECATED JacobiSVD (Index rows, Index cols, unsigned int computationOptions)
 Default Constructor with memory preallocation. More...
 
 JacobiSVD (const MatrixType &matrix)
 Constructor performing the decomposition of given matrix, using the custom options specified with the Options template parameter. More...
 
 JacobiSVD (const MatrixType &matrix, unsigned int computationOptions)
 Constructor performing the decomposition of given matrix using specified options for computing unitaries. More...
 
JacobiSVDcompute (const MatrixType &matrix)
 Method performing the decomposition of given matrix. Computes Thin/Full unitaries U/V if specified using the Options template parameter or the class constructor. More...
 
EIGEN_DEPRECATED JacobiSVDcompute (const MatrixType &matrix, unsigned int computationOptions)
 Method performing the decomposition of given matrix, as specified by the computationOptions parameter. More...
 
void allocate (Index rows_, Index cols_, unsigned int computationOptions)
 
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index cols () const EIGEN_NOEXCEPT
 
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index rows () const EIGEN_NOEXCEPT
 
- Public Member Functions inherited from Eigen::SVDBase< JacobiSVD< MatrixType_, Options_ > >
JacobiSVD< MatrixType_, Options_ > & derived ()
 
const JacobiSVD< MatrixType_, Options_ > & derived () const
 
const MatrixUTypematrixU () const
 
const MatrixVTypematrixV () const
 
const SingularValuesTypesingularValues () const
 
Index nonzeroSingularValues () const
 
Index rank () const
 
JacobiSVD< MatrixType_, Options_ > & setThreshold (const RealScalar &threshold)
 
JacobiSVD< MatrixType_, Options_ > & setThreshold (Default_t)
 
RealScalar threshold () const
 
bool computeU () const
 
bool computeV () const
 
Index rows () const
 
Index cols () const
 
Index diagSize () const
 
EIGEN_DEVICE_FUNC ComputationInfo info () const
 Reports whether previous computation was successful. More...
 
void _solve_impl (const RhsType &rhs, DstType &dst) const
 
void _solve_impl_transposed (const RhsType &rhs, DstType &dst) const
 
- Public Member Functions inherited from Eigen::SolverBase< Derived >
 SolverBase ()
 
 ~SolverBase ()
 
template<typename Rhs >
const Solve< Derived, Rhs > solve (const MatrixBase< Rhs > &b) const
 
const ConstTransposeReturnType transpose () const
 
const AdjointReturnType adjoint () const
 
constexpr EIGEN_DEVICE_FUNC Derived & derived ()
 
constexpr EIGEN_DEVICE_FUNC const Derived & derived () const
 
- Public Member Functions inherited from Eigen::EigenBase< Derived >
constexpr EIGEN_DEVICE_FUNC Derived & derived ()
 
constexpr EIGEN_DEVICE_FUNC const Derived & derived () const
 
EIGEN_DEVICE_FUNC Derived & const_cast_derived () const
 
EIGEN_DEVICE_FUNC const Derived & const_derived () const
 
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index rows () const EIGEN_NOEXCEPT
 
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index cols () const EIGEN_NOEXCEPT
 
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index size () const EIGEN_NOEXCEPT
 
template<typename Dest >
EIGEN_DEVICE_FUNC void evalTo (Dest &dst) const
 
template<typename Dest >
EIGEN_DEVICE_FUNC void addTo (Dest &dst) const
 
template<typename Dest >
EIGEN_DEVICE_FUNC void subTo (Dest &dst) const
 
template<typename Dest >
EIGEN_DEVICE_FUNC void applyThisOnTheRight (Dest &dst) const
 
template<typename Dest >
EIGEN_DEVICE_FUNC void applyThisOnTheLeft (Dest &dst) const
 
template<typename Device >
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DeviceWrapper< Derived, Device > device (Device &device)
 
template<typename Device >
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DeviceWrapper< const Derived, Device > device (Device &device) const
 

Protected Member Functions

 EIGEN_STATIC_ASSERT (!(ShouldComputeThinU &&int(QRPreconditioner)==int(FullPivHouseholderQRPreconditioner)) &&!(ShouldComputeThinU &&int(QRPreconditioner)==int(FullPivHouseholderQRPreconditioner)), "JacobiSVD: can't compute thin U or thin V with the FullPivHouseholderQR preconditioner. " "Use the ColPivHouseholderQR preconditioner instead.") template< typename MatrixType__
 
- Protected Member Functions inherited from Eigen::SVDBase< JacobiSVD< MatrixType_, Options_ > >
void _check_compute_assertions () const
 
void _check_solve_assertion (const Rhs &b) const
 
bool allocate (Index rows, Index cols, unsigned int computationOptions)
 
 SVDBase ()
 Default Constructor. More...
 
- Protected Member Functions inherited from Eigen::SolverBase< Derived >
template<bool Transpose_, typename Rhs >
void _check_solve_assertion (const Rhs &b) const
 

Protected Attributes

int Options__
 
internal::qr_preconditioner_impl< MatrixType, Options, QRPreconditioner, internal::PreconditionIfMoreColsThanRowsm_qr_precond_morecols
 
internal::qr_preconditioner_impl< MatrixType, Options, QRPreconditioner, internal::PreconditionIfMoreRowsThanColsm_qr_precond_morerows
 
WorkMatrixType m_workMatrix
 
- Protected Attributes inherited from Eigen::SVDBase< JacobiSVD< MatrixType_, Options_ > >
MatrixUType m_matrixU
 
MatrixVType m_matrixV
 
SingularValuesType m_singularValues
 
ComputationInfo m_info
 
bool m_isInitialized
 
bool m_isAllocated
 
bool m_usePrescribedThreshold
 
bool m_computeFullU
 
bool m_computeThinU
 
bool m_computeFullV
 
bool m_computeThinV
 
unsigned int m_computationOptions
 
Index m_nonzeroSingularValues
 
internal::variable_if_dynamic< Index, RowsAtCompileTimem_rows
 
internal::variable_if_dynamic< Index, ColsAtCompileTimem_cols
 
internal::variable_if_dynamic< Index, DiagSizeAtCompileTimem_diagSize
 
RealScalar m_prescribedThreshold
 

Private Types

typedef SVDBase< JacobiSVDBase
 

Private Member Functions

JacobiSVDcompute_impl (const MatrixType &matrix, unsigned int computationOptions)
 

Friends

template<typename MatrixType__ , int Options__, int QRPreconditioner_, int Case_, bool DoAnything_>
struct internal::qr_preconditioner_impl
 

Additional Inherited Members

- Static Public Attributes inherited from Eigen::SVDBase< JacobiSVD< MatrixType_, Options_ > >
static constexpr bool ShouldComputeFullU
 
static constexpr bool ShouldComputeThinU
 
static constexpr bool ShouldComputeFullV
 
static constexpr bool ShouldComputeThinV
 

Detailed Description

template<typename MatrixType_, int Options_>
class Eigen::JacobiSVD< MatrixType_, Options_ >

Two-sided Jacobi SVD decomposition of a rectangular matrix.

Template Parameters
MatrixType_the type of the matrix of which we are computing the SVD decomposition
Optionsthis optional parameter allows one to specify the type of QR decomposition that will be used internally for the R-SVD step for non-square matrices. Additionally, it allows one to specify whether to compute thin or full unitaries U and V. See discussion of possible values below.

SVD decomposition consists in decomposing any n-by-p matrix A as a product

\[ A = U S V^* \]

where U is a n-by-n unitary, V is a p-by-p unitary, and S is a n-by-p real positive matrix which is zero outside of its main diagonal; the diagonal entries of S are known as the singular values of A and the columns of U and V are known as the left and right singular vectors of A respectively.

Singular values are always sorted in decreasing order.

This JacobiSVD decomposition computes only the singular values by default. If you want U or V, you need to ask for them explicitly.

You can ask for only thin U or V to be computed, meaning the following. In case of a rectangular n-by-p matrix, letting m be the smaller value among n and p, there are only m singular vectors; the remaining columns of U and V do not correspond to actual singular vectors. Asking for thin U or V means asking for only their m first columns to be formed. So U is then a n-by-m matrix, and V is then a p-by-m matrix. Notice that thin U and V are all you need for (least squares) solving.

Here's an example demonstrating basic usage:

MatrixXf m = MatrixXf::Random(3, 2);
cout << "Here is the matrix m:" << endl << m << endl;
JacobiSVD<MatrixXf, ComputeThinU | ComputeThinV> svd(m);
cout << "Its singular values are:" << endl << svd.singularValues() << endl;
cout << "Its left singular vectors are the columns of the thin U matrix:" << endl << svd.matrixU() << endl;
cout << "Its right singular vectors are the columns of the thin V matrix:" << endl << svd.matrixV() << endl;
Vector3f rhs(1, 0, 0);
cout << "Now consider this rhs vector:" << endl << rhs << endl;
cout << "A least-squares solution of m*x = rhs is:" << endl << svd.solve(rhs) << endl;
cout<< "Here is the matrix m:"<< endl<< m<< endl;JacobiSVD< MatrixXf, ComputeThinU|ComputeThinV > svd(m)
int * m
Definition: level2_cplx_impl.h:294

Output:

This JacobiSVD class is a two-sided Jacobi R-SVD decomposition, ensuring optimal reliability and accuracy. The downside is that it's slower than bidiagonalizing SVD algorithms for large square matrices; however its complexity is still \( O(n^2p) \) where n is the smaller dimension and p is the greater dimension, meaning that it is still of the same order of complexity as the faster bidiagonalizing R-SVD algorithms. In particular, like any R-SVD, it takes advantage of non-squareness in that its complexity is only linear in the greater dimension.

If the input matrix has inf or nan coefficients, the result of the computation is undefined, but the computation is guaranteed to terminate in finite (and reasonable) time.

The possible QR preconditioners that can be set with Options template parameter are:

  • ColPivHouseholderQRPreconditioner is the default. In practice it's very safe. It uses column-pivoting QR.
  • FullPivHouseholderQRPreconditioner, is the safest and slowest. It uses full-pivoting QR. Contrary to other QRs, it doesn't allow computing thin unitaries.
  • HouseholderQRPreconditioner is the fastest, and less safe and accurate than the pivoting variants. It uses non-pivoting QR. This is very similar in safety and accuracy to the bidiagonalization process used by bidiagonalizing SVD algorithms (since bidiagonalization is inherently non-pivoting). However the resulting SVD is still more reliable than bidiagonalizing SVDs because the Jacobi-based iterarive process is more reliable than the optimized bidiagonal SVD iterations.
  • NoQRPreconditioner allows not to use a QR preconditioner at all. This is useful if you know that you will only be computing JacobiSVD decompositions of square matrices. Non-square matrices require a QR preconditioner. Using this option will result in faster compilation and smaller executable code. It won't significantly speed up computation, since JacobiSVD is always checking if QR preconditioning is needed before applying it anyway.

One may also use the Options template parameter to specify how the unitaries should be computed. The options are ComputeThinU, ComputeThinV, ComputeFullU, ComputeFullV. It is not possible to request both the thin and full versions of a unitary. By default, unitaries will not be computed.

You can set the QRPreconditioner and unitary options together: JacobiSVD<MatrixType, ColPivHouseholderQRPreconditioner | ComputeThinU | ComputeFullV>

See also
MatrixBase::jacobiSvd()

Member Typedef Documentation

◆ Base

template<typename MatrixType_ , int Options_>
typedef SVDBase<JacobiSVD> Eigen::JacobiSVD< MatrixType_, Options_ >::Base
private

◆ MatrixType

template<typename MatrixType_ , int Options_>
typedef MatrixType_ Eigen::JacobiSVD< MatrixType_, Options_ >::MatrixType

◆ MatrixUType

template<typename MatrixType_ , int Options_>
typedef Base::MatrixUType Eigen::JacobiSVD< MatrixType_, Options_ >::MatrixUType

◆ MatrixVType

template<typename MatrixType_ , int Options_>
typedef Base::MatrixVType Eigen::JacobiSVD< MatrixType_, Options_ >::MatrixVType

◆ RealScalar

template<typename MatrixType_ , int Options_>
typedef Base::RealScalar Eigen::JacobiSVD< MatrixType_, Options_ >::RealScalar

◆ Scalar

template<typename MatrixType_ , int Options_>
typedef Base::Scalar Eigen::JacobiSVD< MatrixType_, Options_ >::Scalar

◆ SingularValuesType

template<typename MatrixType_ , int Options_>
typedef Base::SingularValuesType Eigen::JacobiSVD< MatrixType_, Options_ >::SingularValuesType

◆ WorkMatrixType

Member Enumeration Documentation

◆ anonymous enum

template<typename MatrixType_ , int Options_>
anonymous enum : int
Enumerator
Options 
QRPreconditioner 
RowsAtCompileTime 
ColsAtCompileTime 
DiagSizeAtCompileTime 
MaxRowsAtCompileTime 
MaxColsAtCompileTime 
MaxDiagSizeAtCompileTime 
MatrixOptions 
507  : int {
508  Options = Options_,
517  };
@ ColsAtCompileTime
Definition: JacobiSVD.h:511
@ MaxColsAtCompileTime
Definition: JacobiSVD.h:514
@ Options
Definition: JacobiSVD.h:508
@ MaxRowsAtCompileTime
Definition: JacobiSVD.h:513
@ DiagSizeAtCompileTime
Definition: JacobiSVD.h:512
@ MatrixOptions
Definition: JacobiSVD.h:516
@ RowsAtCompileTime
Definition: JacobiSVD.h:510
@ MaxDiagSizeAtCompileTime
Definition: JacobiSVD.h:515
@ QRPreconditioner
Definition: JacobiSVD.h:509
@ MatrixOptions
Definition: SVDBase.h:141
@ ColsAtCompileTime
Definition: SVDBase.h:136
@ DiagSizeAtCompileTime
Definition: SVDBase.h:137
@ MaxRowsAtCompileTime
Definition: SVDBase.h:138
@ MaxDiagSizeAtCompileTime
Definition: SVDBase.h:140
@ MaxColsAtCompileTime
Definition: SVDBase.h:139
@ RowsAtCompileTime
Definition: SVDBase.h:135
constexpr int get_qr_preconditioner(int options)
Definition: SVDBase.h:32

Constructor & Destructor Documentation

◆ JacobiSVD() [1/5]

template<typename MatrixType_ , int Options_>
Eigen::JacobiSVD< MatrixType_, Options_ >::JacobiSVD ( )
inline

Default Constructor.

The default constructor is useful in cases in which the user intends to perform decompositions via JacobiSVD::compute(const MatrixType&).

531 {}

◆ JacobiSVD() [2/5]

template<typename MatrixType_ , int Options_>
Eigen::JacobiSVD< MatrixType_, Options_ >::JacobiSVD ( Index  rows,
Index  cols 
)
inline

Default Constructor with memory preallocation.

Like the default constructor but with preallocation of the internal data according to the specified problem size and Options template parameter.

See also
JacobiSVD()
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: EigenBase.h:61
void allocate(Index rows_, Index cols_, unsigned int computationOptions)
Definition: JacobiSVD.h:614
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: EigenBase.h:59
constexpr int get_computation_options(int options)
Definition: SVDBase.h:34

References Eigen::JacobiSVD< MatrixType_, Options_ >::allocate(), Eigen::JacobiSVD< MatrixType_, Options_ >::cols(), Eigen::internal::get_computation_options(), Eigen::JacobiSVD< MatrixType_, Options_ >::Options, and Eigen::JacobiSVD< MatrixType_, Options_ >::rows().

◆ JacobiSVD() [3/5]

template<typename MatrixType_ , int Options_>
EIGEN_DEPRECATED Eigen::JacobiSVD< MatrixType_, Options_ >::JacobiSVD ( Index  rows,
Index  cols,
unsigned int  computationOptions 
)
inline

Default Constructor with memory preallocation.

Like the default constructor but with preallocation of the internal data according to the specified problem size.

One cannot request unitaries using both the Options template parameter and the constructor. If possible, prefer using the Options template parameter.

Parameters
computationOptionsspecify whether to compute Thin/Full unitaries U/V
See also
JacobiSVD()
Deprecated:
Will be removed in the next major Eigen version. Options should be specified in the Options template parameter.
556  {
557  internal::check_svd_options_assertions<MatrixType, Options>(computationOptions, rows, cols);
558  allocate(rows, cols, computationOptions);
559  }

References Eigen::JacobiSVD< MatrixType_, Options_ >::allocate(), Eigen::JacobiSVD< MatrixType_, Options_ >::cols(), and Eigen::JacobiSVD< MatrixType_, Options_ >::rows().

◆ JacobiSVD() [4/5]

template<typename MatrixType_ , int Options_>
Eigen::JacobiSVD< MatrixType_, Options_ >::JacobiSVD ( const MatrixType matrix)
inlineexplicit

Constructor performing the decomposition of given matrix, using the custom options specified with the Options template parameter.

Parameters
matrixthe matrix to decompose
JacobiSVD & compute_impl(const MatrixType &matrix, unsigned int computationOptions)
Definition: JacobiSVD.h:665
Eigen::Map< Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor >, 0, Eigen::OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: common.h:85

References Eigen::JacobiSVD< MatrixType_, Options_ >::compute_impl(), Eigen::internal::get_computation_options(), matrix(), and Eigen::JacobiSVD< MatrixType_, Options_ >::Options.

◆ JacobiSVD() [5/5]

template<typename MatrixType_ , int Options_>
Eigen::JacobiSVD< MatrixType_, Options_ >::JacobiSVD ( const MatrixType matrix,
unsigned int  computationOptions 
)
inline

Constructor performing the decomposition of given matrix using specified options for computing unitaries.

One cannot request unitiaries using both the Options template parameter and the constructor. If possible, prefer using the Options template parameter.

Parameters
matrixthe matrix to decompose
computationOptionsspecify whether to compute Thin/Full unitaries U/V
Deprecated:
Will be removed in the next major Eigen version. Options should be specified in the Options template parameter.
581  {
582  internal::check_svd_options_assertions<MatrixType, Options>(computationOptions, matrix.rows(), matrix.cols());
583  compute_impl(matrix, computationOptions);
584  }

References Eigen::JacobiSVD< MatrixType_, Options_ >::compute_impl(), and matrix().

Member Function Documentation

◆ allocate()

template<typename MatrixType_ , int Options_>
void Eigen::JacobiSVD< MatrixType_, Options_ >::allocate ( Index  rows_,
Index  cols_,
unsigned int  computationOptions 
)
inline
614  {
615  if (Base::allocate(rows_, cols_, computationOptions)) return;
618  "JacobiSVD: can't compute thin U or thin V with the FullPivHouseholderQR preconditioner. "
619  "Use the ColPivHouseholderQR preconditioner instead.");
620 
622  if (cols() > rows()) m_qr_precond_morecols.allocate(*this);
623  if (rows() > cols()) m_qr_precond_morerows.allocate(*this);
624  }
#define eigen_assert(x)
Definition: Macros.h:910
internal::qr_preconditioner_impl< MatrixType, Options, QRPreconditioner, internal::PreconditionIfMoreColsThanRows > m_qr_precond_morecols
Definition: JacobiSVD.h:658
internal::qr_preconditioner_impl< MatrixType, Options, QRPreconditioner, internal::PreconditionIfMoreRowsThanCols > m_qr_precond_morerows
Definition: JacobiSVD.h:660
WorkMatrixType m_workMatrix
Definition: JacobiSVD.h:661
EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE void resize(Index rows, Index cols)
Definition: PlainObjectBase.h:294
static constexpr bool ShouldComputeThinU
Definition: SVDBase.h:130
bool allocate(Index rows, Index cols, unsigned int computationOptions)
Definition: SVDBase.h:403
Index diagSize() const
Definition: SVDBase.h:279
@ FullPivHouseholderQRPreconditioner
Definition: Constants.h:427

References Eigen::SVDBase< Derived >::allocate(), Eigen::JacobiSVD< MatrixType_, Options_ >::cols(), Eigen::SVDBase< JacobiSVD< MatrixType_, Options_ > >::diagSize(), eigen_assert, Eigen::FullPivHouseholderQRPreconditioner, Eigen::JacobiSVD< MatrixType_, Options_ >::m_qr_precond_morecols, Eigen::JacobiSVD< MatrixType_, Options_ >::m_qr_precond_morerows, Eigen::JacobiSVD< MatrixType_, Options_ >::m_workMatrix, Eigen::JacobiSVD< MatrixType_, Options_ >::QRPreconditioner, Eigen::PlainObjectBase< Derived >::resize(), Eigen::JacobiSVD< MatrixType_, Options_ >::rows(), and Eigen::SVDBase< JacobiSVD< MatrixType_, Options_ > >::ShouldComputeThinU.

Referenced by Eigen::JacobiSVD< MatrixType_, Options_ >::compute_impl(), and Eigen::JacobiSVD< MatrixType_, Options_ >::JacobiSVD().

◆ cols()

◆ compute() [1/2]

template<typename MatrixType_ , int Options_>
JacobiSVD& Eigen::JacobiSVD< MatrixType_, Options_ >::compute ( const MatrixType matrix)
inline

Method performing the decomposition of given matrix. Computes Thin/Full unitaries U/V if specified using the Options template parameter or the class constructor.

Parameters
matrixthe matrix to decompose
unsigned int m_computationOptions
Definition: SVDBase.h:338

References Eigen::JacobiSVD< MatrixType_, Options_ >::compute_impl(), Eigen::SVDBase< JacobiSVD< MatrixType_, Options_ > >::m_computationOptions, and matrix().

Referenced by bench(), and ctms_decompositions().

◆ compute() [2/2]

template<typename MatrixType_ , int Options_>
EIGEN_DEPRECATED JacobiSVD& Eigen::JacobiSVD< MatrixType_, Options_ >::compute ( const MatrixType matrix,
unsigned int  computationOptions 
)
inline

Method performing the decomposition of given matrix, as specified by the computationOptions parameter.

Parameters
matrixthe matrix to decompose
computationOptionsspecify whether to compute Thin/Full unitaries U/V
Deprecated:
Will be removed in the next major Eigen version. Options should be specified in the Options template parameter.
602  {
603  internal::check_svd_options_assertions<MatrixType, Options>(m_computationOptions, matrix.rows(), matrix.cols());
604  return compute_impl(matrix, computationOptions);
605  }

References Eigen::JacobiSVD< MatrixType_, Options_ >::compute_impl(), Eigen::SVDBase< JacobiSVD< MatrixType_, Options_ > >::m_computationOptions, and matrix().

◆ compute_impl()

template<typename MatrixType , int Options>
JacobiSVD< MatrixType, Options > & Eigen::JacobiSVD< MatrixType, Options >::compute_impl ( const MatrixType matrix,
unsigned int  computationOptions 
)
private
666  {
667  using std::abs;
668 
669  allocate(matrix.rows(), matrix.cols(), computationOptions);
670 
671  // currently we stop when we reach precision 2*epsilon as the last bit of precision can require an unreasonable number
672  // of iterations, only worsening the precision of U and V as we accumulate more rotations
673  const RealScalar precision = RealScalar(2) * NumTraits<Scalar>::epsilon();
674 
675  // limit for denormal numbers to be considered zero in order to avoid infinite loops (see bug 286)
676  const RealScalar considerAsZero = (std::numeric_limits<RealScalar>::min)();
677 
678  // Scaling factor to reduce over/under-flows
679  RealScalar scale = matrix.cwiseAbs().template maxCoeff<PropagateNaN>();
680  if (!(numext::isfinite)(scale)) {
681  m_isInitialized = true;
684  return *this;
685  }
686  if (numext::is_exactly_zero(scale)) scale = RealScalar(1);
687 
688  /*** step 1. The R-SVD step: we use a QR decomposition to reduce to the case of a square matrix */
689 
690  if (rows() != cols()) {
691  m_qr_precond_morecols.run(*this, matrix / scale);
692  m_qr_precond_morerows.run(*this, matrix / scale);
693  } else {
694  m_workMatrix =
695  matrix.template topLeftCorner<DiagSizeAtCompileTime, DiagSizeAtCompileTime>(diagSize(), diagSize()) / scale;
696  if (m_computeFullU) m_matrixU.setIdentity(rows(), rows());
697  if (m_computeThinU) m_matrixU.setIdentity(rows(), diagSize());
698  if (m_computeFullV) m_matrixV.setIdentity(cols(), cols());
699  if (m_computeThinV) m_matrixV.setIdentity(cols(), diagSize());
700  }
701 
702  /*** step 2. The main Jacobi SVD iteration. ***/
703  RealScalar maxDiagEntry = m_workMatrix.cwiseAbs().diagonal().maxCoeff();
704 
705  bool finished = false;
706  while (!finished) {
707  finished = true;
708 
709  // do a sweep: for all index pairs (p,q), perform SVD of the corresponding 2x2 sub-matrix
710 
711  for (Index p = 1; p < diagSize(); ++p) {
712  for (Index q = 0; q < p; ++q) {
713  // if this 2x2 sub-matrix is not diagonal already...
714  // notice that this comparison will evaluate to false if any NaN is involved, ensuring that NaN's don't
715  // keep us iterating forever. Similarly, small denormal numbers are considered zero.
716  RealScalar threshold = numext::maxi<RealScalar>(considerAsZero, precision * maxDiagEntry);
718  finished = false;
719  // perform SVD decomposition of 2x2 sub-matrix corresponding to indices p,q to make it diagonal
720  // the complex to real operation returns true if the updated 2x2 block is not already diagonal
722  maxDiagEntry)) {
723  JacobiRotation<RealScalar> j_left, j_right;
724  internal::real_2x2_jacobi_svd(m_workMatrix, p, q, &j_left, &j_right);
725 
726  // accumulate resulting Jacobi rotations
727  m_workMatrix.applyOnTheLeft(p, q, j_left);
728  if (computeU()) m_matrixU.applyOnTheRight(p, q, j_left.transpose());
729 
730  m_workMatrix.applyOnTheRight(p, q, j_right);
731  if (computeV()) m_matrixV.applyOnTheRight(p, q, j_right);
732 
733  // keep track of the largest diagonal coefficient
734  maxDiagEntry = numext::maxi<RealScalar>(
735  maxDiagEntry, numext::maxi<RealScalar>(abs(m_workMatrix.coeff(p, p)), abs(m_workMatrix.coeff(q, q))));
736  }
737  }
738  }
739  }
740  }
741 
742  /*** step 3. The work matrix is now diagonal, so ensure it's positive so its diagonal entries are the singular values
743  * ***/
744 
745  for (Index i = 0; i < diagSize(); ++i) {
746  // For a complex matrix, some diagonal coefficients might note have been
747  // treated by svd_precondition_2x2_block_to_be_real, and the imaginary part
748  // of some diagonal entry might not be null.
749  if (NumTraits<Scalar>::IsComplex && abs(numext::imag(m_workMatrix.coeff(i, i))) > considerAsZero) {
751  m_singularValues.coeffRef(i) = abs(a);
752  if (computeU()) m_matrixU.col(i) *= m_workMatrix.coeff(i, i) / a;
753  } else {
754  // m_workMatrix.coeff(i,i) is already real, no difficulty:
756  m_singularValues.coeffRef(i) = abs(a);
757  if (computeU() && (a < RealScalar(0))) m_matrixU.col(i) = -m_matrixU.col(i);
758  }
759  }
760 
761  m_singularValues *= scale;
762 
763  /*** step 4. Sort singular values in descending order and compute the number of nonzero singular values ***/
764 
766  for (Index i = 0; i < diagSize(); i++) {
767  Index pos;
768  RealScalar maxRemainingSingularValue = m_singularValues.tail(diagSize() - i).maxCoeff(&pos);
769  if (numext::is_exactly_zero(maxRemainingSingularValue)) {
771  break;
772  }
773  if (pos) {
774  pos += i;
775  std::swap(m_singularValues.coeffRef(i), m_singularValues.coeffRef(pos));
776  if (computeU()) m_matrixU.col(pos).swap(m_matrixU.col(i));
777  if (computeV()) m_matrixV.col(pos).swap(m_matrixV.col(i));
778  }
779  }
780 
781  m_isInitialized = true;
782  return *this;
783 }
AnnoyingScalar abs(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:135
AnnoyingScalar imag(const AnnoyingScalar &)
Definition: AnnoyingScalar.h:132
int i
Definition: BiCGSTAB_step_by_step.cpp:9
float * p
Definition: Tutorial_Map_using.cpp:9
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:46
Base::RealScalar RealScalar
Definition: JacobiSVD.h:506
EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const Scalar & coeff(Index rowId, Index colId) const
Definition: PlainObjectBase.h:198
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void swap(DenseBase< OtherDerived > &other)
Override DenseBase::swap() since for dynamic-sized matrices of same type it is enough to swap the dat...
Definition: PlainObjectBase.h:898
ComputationInfo m_info
Definition: SVDBase.h:334
bool computeV() const
Definition: SVDBase.h:275
MatrixVType m_matrixV
Definition: SVDBase.h:332
bool computeU() const
Definition: SVDBase.h:273
RealScalar threshold() const
Definition: SVDBase.h:265
Index m_nonzeroSingularValues
Definition: SVDBase.h:339
SingularValuesType m_singularValues
Definition: SVDBase.h:333
MatrixUType m_matrixU
Definition: SVDBase.h:331
float real
Definition: datatypes.h:10
#define min(a, b)
Definition: datatypes.h:22
@ InvalidInput
Definition: Constants.h:447
EIGEN_BLAS_FUNC() swap(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Definition: level1_impl.h:117
const Scalar * a
Definition: level2_cplx_impl.h:32
void real_2x2_jacobi_svd(const MatrixType &matrix, Index p, Index q, JacobiRotation< RealScalar > *j_left, JacobiRotation< RealScalar > *j_right)
Definition: RealSvd2x2.h:22
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool() isfinite(const Eigen::bfloat16 &h)
Definition: BFloat16.h:752
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool is_exactly_zero(const X &x)
Definition: Meta.h:592
EIGEN_DEVICE_FUNC const Scalar & q
Definition: SpecialFunctionsImpl.h:2019
bool finished
Definition: MergeRestartFiles.py:79
double epsilon
Definition: osc_ring_sarah_asymptotics.h:43
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:43
@ IsComplex
Definition: NumTraits.h:176
void run(const string &dir_name, LinearSolver *linear_solver_pt, const unsigned nel_1d, bool mess_up_order)
Definition: two_d_poisson_compare_solvers.cc:317

References a, abs(), Eigen::JacobiSVD< MatrixType_, Options_ >::allocate(), Eigen::PlainObjectBase< Derived >::coeff(), Eigen::JacobiSVD< MatrixType_, Options_ >::cols(), Eigen::SVDBase< JacobiSVD< MatrixType_, Options_ > >::computeU(), Eigen::SVDBase< JacobiSVD< MatrixType_, Options_ > >::computeV(), Eigen::SVDBase< JacobiSVD< MatrixType_, Options_ > >::diagSize(), oomph::SarahBL::epsilon, MergeRestartFiles::finished, i, imag(), Eigen::InvalidInput, Eigen::numext::is_exactly_zero(), Eigen::numext::isfinite(), Eigen::SVDBase< JacobiSVD< MatrixType_, Options_ > >::m_computeFullU, Eigen::SVDBase< JacobiSVD< MatrixType_, Options_ > >::m_computeFullV, Eigen::SVDBase< JacobiSVD< MatrixType_, Options_ > >::m_computeThinU, Eigen::SVDBase< JacobiSVD< MatrixType_, Options_ > >::m_computeThinV, Eigen::SVDBase< JacobiSVD< MatrixType_, Options_ > >::m_info, Eigen::SVDBase< JacobiSVD< MatrixType_, Options_ > >::m_isInitialized, Eigen::SVDBase< JacobiSVD< MatrixType_, Options_ > >::m_matrixU, Eigen::SVDBase< JacobiSVD< MatrixType_, Options_ > >::m_matrixV, Eigen::SVDBase< JacobiSVD< MatrixType_, Options_ > >::m_nonzeroSingularValues, Eigen::JacobiSVD< MatrixType_, Options_ >::m_qr_precond_morecols, Eigen::JacobiSVD< MatrixType_, Options_ >::m_qr_precond_morerows, Eigen::SVDBase< JacobiSVD< MatrixType_, Options_ > >::m_singularValues, Eigen::JacobiSVD< MatrixType_, Options_ >::m_workMatrix, matrix(), min, p, Eigen::numext::q, Eigen::internal::real_2x2_jacobi_svd(), Eigen::JacobiSVD< MatrixType_, Options_ >::rows(), Eigen::PlainObjectBase< Derived >::swap(), swap(), Eigen::SVDBase< JacobiSVD< MatrixType_, Options_ > >::threshold(), and Eigen::JacobiRotation< Scalar >::transpose().

Referenced by Eigen::JacobiSVD< MatrixType_, Options_ >::compute(), and Eigen::JacobiSVD< MatrixType_, Options_ >::JacobiSVD().

◆ EIGEN_STATIC_ASSERT()

template<typename MatrixType_ , int Options_>
Eigen::JacobiSVD< MatrixType_, Options_ >::EIGEN_STATIC_ASSERT ( ShouldComputeThinU &&int(QRPreconditioner)==int(FullPivHouseholderQRPreconditioner)) &&!(ShouldComputeThinU &&int(QRPreconditioner)==int(FullPivHouseholderQRPreconditioner),
"JacobiSVD< MatrixType_, Options_ >: can't compute thin U or thin V with the FullPivHouseholderQR preconditioner. " "Use the ColPivHouseholderQR preconditioner instead."   
)
protected

◆ rows()

Friends And Related Function Documentation

◆ internal::qr_preconditioner_impl

template<typename MatrixType_ , int Options_>
template<typename MatrixType__ , int Options__, int QRPreconditioner_, int Case_, bool DoAnything_>
friend struct internal::qr_preconditioner_impl
friend

Member Data Documentation

◆ m_qr_precond_morecols

◆ m_qr_precond_morerows

◆ m_workMatrix

template<typename MatrixType_ , int Options_>
WorkMatrixType Eigen::JacobiSVD< MatrixType_, Options_ >::m_workMatrix
protected

◆ Options__

template<typename MatrixType_ , int Options_>
int Eigen::JacobiSVD< MatrixType_, Options_ >::Options__
protected

The documentation for this class was generated from the following files: