mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-07-16 10:01:49 +08:00
add polar decomposition on both sides, in SVD, with test
This commit is contained in:
parent
32754d806d
commit
876b1fb842
@ -79,6 +79,9 @@ template<typename MatrixType> class SVD
|
|||||||
void compute(const MatrixType& matrix);
|
void compute(const MatrixType& matrix);
|
||||||
SVD& sort();
|
SVD& sort();
|
||||||
|
|
||||||
|
void computeUnitaryPositive(MatrixUType *unitary, MatrixType *positive) const;
|
||||||
|
void computePositiveUnitary(MatrixType *positive, MatrixVType *unitary) const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/** \internal */
|
/** \internal */
|
||||||
MatrixUType m_matU;
|
MatrixUType m_matU;
|
||||||
@ -534,6 +537,36 @@ bool SVD<MatrixType>::solve(const MatrixBase<OtherDerived> &b, ResultType* resul
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Computes the polar decomposition of the matrix, as a product unitary x positive.
|
||||||
|
*
|
||||||
|
* If either pointer is zero, the corresponding computation is skipped.
|
||||||
|
*
|
||||||
|
* Only for square matrices.
|
||||||
|
*/
|
||||||
|
template<typename MatrixType>
|
||||||
|
void SVD<MatrixType>::computeUnitaryPositive(typename SVD<MatrixType>::MatrixUType *unitary,
|
||||||
|
MatrixType *positive) const
|
||||||
|
{
|
||||||
|
ei_assert(m_matU.cols() == m_matV.cols() && "Polar decomposition is only for square matrices");
|
||||||
|
if(unitary) *unitary = m_matU * m_matV.adjoint();
|
||||||
|
if(positive) *positive = m_matV * m_sigma.asDiagonal() * m_matV.adjoint();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Computes the polar decomposition of the matrix, as a product positive x unitary.
|
||||||
|
*
|
||||||
|
* If either pointer is zero, the corresponding computation is skipped.
|
||||||
|
*
|
||||||
|
* Only for square matrices.
|
||||||
|
*/
|
||||||
|
template<typename MatrixType>
|
||||||
|
void SVD<MatrixType>::computePositiveUnitary(MatrixType *positive,
|
||||||
|
typename SVD<MatrixType>::MatrixVType *unitary) const
|
||||||
|
{
|
||||||
|
ei_assert(m_matU.rows() == m_matV.rows() && "Polar decomposition is only for square matrices");
|
||||||
|
if(unitary) *unitary = m_matU * m_matV.adjoint();
|
||||||
|
if(positive) *positive = m_matU * m_sigma.asDiagonal() * m_matU.adjoint();
|
||||||
|
}
|
||||||
|
|
||||||
/** \svd_module
|
/** \svd_module
|
||||||
* \returns the SVD decomposition of \c *this
|
* \returns the SVD decomposition of \c *this
|
||||||
*/
|
*/
|
||||||
|
32
test/svd.cpp
32
test/svd.cpp
@ -44,13 +44,15 @@ template<typename MatrixType> void svd(const MatrixType& m)
|
|||||||
if (ei_is_same_type<RealScalar,float>::ret)
|
if (ei_is_same_type<RealScalar,float>::ret)
|
||||||
largerEps = 1e-3f;
|
largerEps = 1e-3f;
|
||||||
|
|
||||||
SVD<MatrixType> svd(a);
|
{
|
||||||
MatrixType sigma = MatrixType::Zero(rows,cols);
|
SVD<MatrixType> svd(a);
|
||||||
MatrixType matU = MatrixType::Zero(rows,rows);
|
MatrixType sigma = MatrixType::Zero(rows,cols);
|
||||||
sigma.block(0,0,cols,cols) = svd.singularValues().asDiagonal();
|
MatrixType matU = MatrixType::Zero(rows,rows);
|
||||||
matU.block(0,0,rows,cols) = svd.matrixU();
|
sigma.block(0,0,cols,cols) = svd.singularValues().asDiagonal();
|
||||||
|
matU.block(0,0,rows,cols) = svd.matrixU();
|
||||||
|
VERIFY_IS_APPROX(a, matU * sigma * svd.matrixV().transpose());
|
||||||
|
}
|
||||||
|
|
||||||
VERIFY_IS_APPROX(a, matU * sigma * svd.matrixV().transpose());
|
|
||||||
|
|
||||||
if (rows==cols)
|
if (rows==cols)
|
||||||
{
|
{
|
||||||
@ -63,6 +65,24 @@ template<typename MatrixType> void svd(const MatrixType& m)
|
|||||||
svd.solve(b, &x);
|
svd.solve(b, &x);
|
||||||
VERIFY_IS_APPROX(a * x,b);
|
VERIFY_IS_APPROX(a * x,b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if(rows==cols)
|
||||||
|
{
|
||||||
|
SVD<MatrixType> svd(a);
|
||||||
|
MatrixType unitary, positive;
|
||||||
|
svd.computeUnitaryPositive(&unitary, &positive);
|
||||||
|
VERIFY_IS_APPROX(unitary * unitary.adjoint(), MatrixType::Identity(unitary.rows(),unitary.rows()));
|
||||||
|
VERIFY_IS_APPROX(positive, positive.adjoint());
|
||||||
|
for(int i = 0; i < rows; i++) VERIFY(positive.diagonal()[i] >= 0); // cheap necessary (not sufficient) condition for positivity
|
||||||
|
VERIFY_IS_APPROX(unitary*positive, a);
|
||||||
|
|
||||||
|
svd.computePositiveUnitary(&positive, &unitary);
|
||||||
|
VERIFY_IS_APPROX(unitary * unitary.adjoint(), MatrixType::Identity(unitary.rows(),unitary.rows()));
|
||||||
|
VERIFY_IS_APPROX(positive, positive.adjoint());
|
||||||
|
for(int i = 0; i < rows; i++) VERIFY(positive.diagonal()[i] >= 0); // cheap necessary (not sufficient) condition for positivity
|
||||||
|
VERIFY_IS_APPROX(positive*unitary, a);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_svd()
|
void test_svd()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user