2009-09-01 10:26:15 +08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
// for linear algebra.
|
|
|
|
//
|
2010-06-25 05:21:58 +08:00
|
|
|
// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
|
2009-09-01 10:26:15 +08:00
|
|
|
// Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
|
|
|
|
//
|
2012-07-14 02:42:47 +08:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2009-09-01 10:26:15 +08:00
|
|
|
|
2011-03-07 09:59:25 +08:00
|
|
|
// discard stack allocation as that too bypasses malloc
|
|
|
|
#define EIGEN_STACK_ALLOCATION_LIMIT 0
|
|
|
|
#define EIGEN_RUNTIME_NO_MALLOC
|
2009-09-01 10:26:15 +08:00
|
|
|
#include "main.h"
|
|
|
|
#include <Eigen/SVD>
|
|
|
|
|
2010-10-08 22:42:32 +08:00
|
|
|
template<typename MatrixType, int QRPreconditioner>
|
2010-10-08 22:42:40 +08:00
|
|
|
void jacobisvd_check_full(const MatrixType& m, const JacobiSVD<MatrixType, QRPreconditioner>& svd)
|
2009-09-01 10:26:15 +08:00
|
|
|
{
|
2010-06-20 23:37:56 +08:00
|
|
|
typedef typename MatrixType::Index Index;
|
|
|
|
Index rows = m.rows();
|
|
|
|
Index cols = m.cols();
|
2009-09-01 10:26:15 +08:00
|
|
|
|
|
|
|
enum {
|
|
|
|
RowsAtCompileTime = MatrixType::RowsAtCompileTime,
|
|
|
|
ColsAtCompileTime = MatrixType::ColsAtCompileTime
|
|
|
|
};
|
2009-09-16 20:35:42 +08:00
|
|
|
|
2009-09-01 10:26:15 +08:00
|
|
|
typedef typename MatrixType::Scalar Scalar;
|
|
|
|
typedef Matrix<Scalar, RowsAtCompileTime, RowsAtCompileTime> MatrixUType;
|
|
|
|
typedef Matrix<Scalar, ColsAtCompileTime, ColsAtCompileTime> MatrixVType;
|
2009-09-16 20:35:42 +08:00
|
|
|
|
2009-09-01 10:26:15 +08:00
|
|
|
MatrixType sigma = MatrixType::Zero(rows,cols);
|
|
|
|
sigma.diagonal() = svd.singularValues().template cast<Scalar>();
|
|
|
|
MatrixUType u = svd.matrixU();
|
|
|
|
MatrixVType v = svd.matrixV();
|
2009-09-16 20:35:42 +08:00
|
|
|
|
2010-10-08 22:42:40 +08:00
|
|
|
VERIFY_IS_APPROX(m, u * sigma * v.adjoint());
|
2009-09-01 10:26:15 +08:00
|
|
|
VERIFY_IS_UNITARY(u);
|
|
|
|
VERIFY_IS_UNITARY(v);
|
|
|
|
}
|
|
|
|
|
2010-10-08 22:42:40 +08:00
|
|
|
template<typename MatrixType, int QRPreconditioner>
|
|
|
|
void jacobisvd_compare_to_full(const MatrixType& m,
|
|
|
|
unsigned int computationOptions,
|
|
|
|
const JacobiSVD<MatrixType, QRPreconditioner>& referenceSvd)
|
|
|
|
{
|
|
|
|
typedef typename MatrixType::Index Index;
|
|
|
|
Index rows = m.rows();
|
|
|
|
Index cols = m.cols();
|
2011-08-19 20:18:05 +08:00
|
|
|
Index diagSize = (std::min)(rows, cols);
|
2010-10-08 22:42:40 +08:00
|
|
|
|
|
|
|
JacobiSVD<MatrixType, QRPreconditioner> svd(m, computationOptions);
|
|
|
|
|
2010-10-13 21:40:57 +08:00
|
|
|
VERIFY_IS_APPROX(svd.singularValues(), referenceSvd.singularValues());
|
2010-10-08 22:42:40 +08:00
|
|
|
if(computationOptions & ComputeFullU)
|
2010-10-13 21:40:57 +08:00
|
|
|
VERIFY_IS_APPROX(svd.matrixU(), referenceSvd.matrixU());
|
2010-10-08 22:42:40 +08:00
|
|
|
if(computationOptions & ComputeThinU)
|
2010-10-13 21:40:57 +08:00
|
|
|
VERIFY_IS_APPROX(svd.matrixU(), referenceSvd.matrixU().leftCols(diagSize));
|
2010-10-08 22:42:40 +08:00
|
|
|
if(computationOptions & ComputeFullV)
|
2010-10-13 21:40:57 +08:00
|
|
|
VERIFY_IS_APPROX(svd.matrixV(), referenceSvd.matrixV());
|
2010-10-08 22:42:40 +08:00
|
|
|
if(computationOptions & ComputeThinV)
|
2010-10-13 21:40:57 +08:00
|
|
|
VERIFY_IS_APPROX(svd.matrixV(), referenceSvd.matrixV().leftCols(diagSize));
|
2010-10-08 22:42:40 +08:00
|
|
|
}
|
|
|
|
|
2010-10-12 03:36:04 +08:00
|
|
|
template<typename MatrixType, int QRPreconditioner>
|
|
|
|
void jacobisvd_solve(const MatrixType& m, unsigned int computationOptions)
|
|
|
|
{
|
|
|
|
typedef typename MatrixType::Scalar Scalar;
|
2014-07-17 23:09:15 +08:00
|
|
|
typedef typename MatrixType::RealScalar RealScalar;
|
2010-10-12 03:36:04 +08:00
|
|
|
typedef typename MatrixType::Index Index;
|
|
|
|
Index rows = m.rows();
|
|
|
|
Index cols = m.cols();
|
|
|
|
|
|
|
|
enum {
|
|
|
|
RowsAtCompileTime = MatrixType::RowsAtCompileTime,
|
|
|
|
ColsAtCompileTime = MatrixType::ColsAtCompileTime
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef Matrix<Scalar, RowsAtCompileTime, Dynamic> RhsType;
|
|
|
|
typedef Matrix<Scalar, ColsAtCompileTime, Dynamic> SolutionType;
|
|
|
|
|
2010-10-25 22:15:22 +08:00
|
|
|
RhsType rhs = RhsType::Random(rows, internal::random<Index>(1, cols));
|
2010-10-12 03:36:04 +08:00
|
|
|
JacobiSVD<MatrixType, QRPreconditioner> svd(m, computationOptions);
|
2014-07-17 23:09:15 +08:00
|
|
|
|
|
|
|
if(internal::is_same<RealScalar,double>::value) svd.setThreshold(1e-8);
|
|
|
|
else if(internal::is_same<RealScalar,float>::value) svd.setThreshold(1e-4);
|
|
|
|
|
2010-10-12 03:36:04 +08:00
|
|
|
SolutionType x = svd.solve(rhs);
|
2014-07-17 23:09:15 +08:00
|
|
|
|
|
|
|
RealScalar residual = (m*x-rhs).norm();
|
|
|
|
// Check that there is no significantly better solution in the neighborhood of x
|
|
|
|
if(!test_isMuchSmallerThan(residual,rhs.norm()))
|
|
|
|
{
|
|
|
|
// If the residual is very small, then we have an exact solution, so we are already good.
|
|
|
|
for(int k=0;k<x.rows();++k)
|
|
|
|
{
|
|
|
|
SolutionType y(x);
|
|
|
|
y.row(k).array() += 2*NumTraits<RealScalar>::epsilon();
|
|
|
|
RealScalar residual_y = (m*y-rhs).norm();
|
|
|
|
VERIFY( test_isApprox(residual_y,residual) || residual < residual_y );
|
|
|
|
|
|
|
|
y.row(k) = x.row(k).array() - 2*NumTraits<RealScalar>::epsilon();
|
|
|
|
residual_y = (m*y-rhs).norm();
|
|
|
|
VERIFY( test_isApprox(residual_y,residual) || residual < residual_y );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-12 03:36:04 +08:00
|
|
|
// evaluate normal equation which works also for least-squares solutions
|
2014-07-17 23:09:15 +08:00
|
|
|
if(internal::is_same<RealScalar,double>::value)
|
|
|
|
{
|
|
|
|
// This test is not stable with single precision.
|
|
|
|
// This is probably because squaring m signicantly affects the precision.
|
|
|
|
VERIFY_IS_APPROX(m.adjoint()*m*x,m.adjoint()*rhs);
|
|
|
|
}
|
2013-11-03 20:18:56 +08:00
|
|
|
|
|
|
|
// check minimal norm solutions
|
|
|
|
{
|
|
|
|
// generate a full-rank m x n problem with m<n
|
|
|
|
enum {
|
|
|
|
RankAtCompileTime2 = ColsAtCompileTime==Dynamic ? Dynamic : (ColsAtCompileTime)/2+1,
|
|
|
|
RowsAtCompileTime3 = ColsAtCompileTime==Dynamic ? Dynamic : ColsAtCompileTime+1
|
|
|
|
};
|
|
|
|
typedef Matrix<Scalar, RankAtCompileTime2, ColsAtCompileTime> MatrixType2;
|
|
|
|
typedef Matrix<Scalar, RankAtCompileTime2, 1> RhsType2;
|
|
|
|
typedef Matrix<Scalar, ColsAtCompileTime, RankAtCompileTime2> MatrixType2T;
|
|
|
|
Index rank = RankAtCompileTime2==Dynamic ? internal::random<Index>(1,cols) : Index(RankAtCompileTime2);
|
|
|
|
MatrixType2 m2(rank,cols);
|
|
|
|
int guard = 0;
|
|
|
|
do {
|
|
|
|
m2.setRandom();
|
|
|
|
} while(m2.jacobiSvd().setThreshold(test_precision<Scalar>()).rank()!=rank && (++guard)<10);
|
|
|
|
VERIFY(guard<10);
|
|
|
|
RhsType2 rhs2 = RhsType2::Random(rank);
|
|
|
|
// use QR to find a reference minimal norm solution
|
|
|
|
HouseholderQR<MatrixType2T> qr(m2.adjoint());
|
|
|
|
Matrix<Scalar,Dynamic,1> tmp = qr.matrixQR().topLeftCorner(rank,rank).template triangularView<Upper>().adjoint().solve(rhs2);
|
|
|
|
tmp.conservativeResize(cols);
|
|
|
|
tmp.tail(cols-rank).setZero();
|
|
|
|
SolutionType x21 = qr.householderQ() * tmp;
|
|
|
|
// now check with SVD
|
|
|
|
JacobiSVD<MatrixType2, ColPivHouseholderQRPreconditioner> svd2(m2, computationOptions);
|
|
|
|
SolutionType x22 = svd2.solve(rhs2);
|
|
|
|
VERIFY_IS_APPROX(m2*x21, rhs2);
|
|
|
|
VERIFY_IS_APPROX(m2*x22, rhs2);
|
|
|
|
VERIFY_IS_APPROX(x21, x22);
|
|
|
|
|
|
|
|
// Now check with a rank deficient matrix
|
|
|
|
typedef Matrix<Scalar, RowsAtCompileTime3, ColsAtCompileTime> MatrixType3;
|
|
|
|
typedef Matrix<Scalar, RowsAtCompileTime3, 1> RhsType3;
|
|
|
|
Index rows3 = RowsAtCompileTime3==Dynamic ? internal::random<Index>(rank+1,2*cols) : Index(RowsAtCompileTime3);
|
|
|
|
Matrix<Scalar,RowsAtCompileTime3,Dynamic> C = Matrix<Scalar,RowsAtCompileTime3,Dynamic>::Random(rows3,rank);
|
|
|
|
MatrixType3 m3 = C * m2;
|
|
|
|
RhsType3 rhs3 = C * rhs2;
|
|
|
|
JacobiSVD<MatrixType3, ColPivHouseholderQRPreconditioner> svd3(m3, computationOptions);
|
|
|
|
SolutionType x3 = svd3.solve(rhs3);
|
2013-11-03 20:19:55 +08:00
|
|
|
VERIFY_IS_APPROX(m3*x3, rhs3);
|
2013-11-03 20:18:56 +08:00
|
|
|
VERIFY_IS_APPROX(m3*x21, rhs3);
|
|
|
|
VERIFY_IS_APPROX(m2*x3, rhs2);
|
|
|
|
|
|
|
|
VERIFY_IS_APPROX(x21, x3);
|
|
|
|
}
|
2010-10-12 03:36:04 +08:00
|
|
|
}
|
|
|
|
|
2010-10-08 22:42:40 +08:00
|
|
|
template<typename MatrixType, int QRPreconditioner>
|
|
|
|
void jacobisvd_test_all_computation_options(const MatrixType& m)
|
|
|
|
{
|
|
|
|
if (QRPreconditioner == NoQRPreconditioner && m.rows() != m.cols())
|
|
|
|
return;
|
|
|
|
JacobiSVD<MatrixType, QRPreconditioner> fullSvd(m, ComputeFullU|ComputeFullV);
|
2014-07-17 23:09:15 +08:00
|
|
|
CALL_SUBTEST(( jacobisvd_check_full(m, fullSvd) ));
|
|
|
|
CALL_SUBTEST(( jacobisvd_solve<MatrixType, QRPreconditioner>(m, ComputeFullU | ComputeFullV) ));
|
|
|
|
|
2013-06-24 01:11:32 +08:00
|
|
|
#if defined __INTEL_COMPILER
|
|
|
|
// remark #111: statement is unreachable
|
|
|
|
#pragma warning disable 111
|
|
|
|
#endif
|
2010-10-08 22:42:40 +08:00
|
|
|
if(QRPreconditioner == FullPivHouseholderQRPreconditioner)
|
|
|
|
return;
|
|
|
|
|
2014-07-17 23:09:15 +08:00
|
|
|
CALL_SUBTEST(( jacobisvd_compare_to_full(m, ComputeFullU, fullSvd) ));
|
|
|
|
CALL_SUBTEST(( jacobisvd_compare_to_full(m, ComputeFullV, fullSvd) ));
|
|
|
|
CALL_SUBTEST(( jacobisvd_compare_to_full(m, 0, fullSvd) ));
|
2010-10-08 22:42:40 +08:00
|
|
|
|
|
|
|
if (MatrixType::ColsAtCompileTime == Dynamic) {
|
|
|
|
// thin U/V are only available with dynamic number of columns
|
2014-07-17 23:09:15 +08:00
|
|
|
CALL_SUBTEST(( jacobisvd_compare_to_full(m, ComputeFullU|ComputeThinV, fullSvd) ));
|
|
|
|
CALL_SUBTEST(( jacobisvd_compare_to_full(m, ComputeThinV, fullSvd) ));
|
|
|
|
CALL_SUBTEST(( jacobisvd_compare_to_full(m, ComputeThinU|ComputeFullV, fullSvd) ));
|
|
|
|
CALL_SUBTEST(( jacobisvd_compare_to_full(m, ComputeThinU , fullSvd) ));
|
|
|
|
CALL_SUBTEST(( jacobisvd_compare_to_full(m, ComputeThinU|ComputeThinV, fullSvd) ));
|
|
|
|
CALL_SUBTEST(( jacobisvd_solve<MatrixType, QRPreconditioner>(m, ComputeFullU | ComputeThinV) ));
|
|
|
|
CALL_SUBTEST(( jacobisvd_solve<MatrixType, QRPreconditioner>(m, ComputeThinU | ComputeFullV) ));
|
|
|
|
CALL_SUBTEST(( jacobisvd_solve<MatrixType, QRPreconditioner>(m, ComputeThinU | ComputeThinV) ));
|
2011-12-24 06:15:08 +08:00
|
|
|
|
2011-12-24 06:22:31 +08:00
|
|
|
// test reconstruction
|
|
|
|
typedef typename MatrixType::Index Index;
|
|
|
|
Index diagSize = (std::min)(m.rows(), m.cols());
|
2011-12-24 06:15:08 +08:00
|
|
|
JacobiSVD<MatrixType, QRPreconditioner> svd(m, ComputeThinU | ComputeThinV);
|
2011-12-24 06:22:31 +08:00
|
|
|
VERIFY_IS_APPROX(m, svd.matrixU().leftCols(diagSize) * svd.singularValues().asDiagonal() * svd.matrixV().leftCols(diagSize).adjoint());
|
2011-12-24 06:15:08 +08:00
|
|
|
}
|
2010-10-08 22:42:40 +08:00
|
|
|
}
|
|
|
|
|
2010-10-08 22:42:32 +08:00
|
|
|
template<typename MatrixType>
|
2010-10-08 22:42:40 +08:00
|
|
|
void jacobisvd(const MatrixType& a = MatrixType(), bool pickrandom = true)
|
2010-10-08 22:42:32 +08:00
|
|
|
{
|
2014-07-17 23:09:15 +08:00
|
|
|
MatrixType m = a;
|
|
|
|
if(pickrandom)
|
|
|
|
{
|
|
|
|
typedef typename MatrixType::Scalar Scalar;
|
|
|
|
typedef typename MatrixType::RealScalar RealScalar;
|
|
|
|
typedef typename MatrixType::Index Index;
|
|
|
|
Index diagSize = (std::min)(a.rows(), a.cols());
|
|
|
|
RealScalar s = std::numeric_limits<RealScalar>::max_exponent10/4;
|
|
|
|
s = internal::random<RealScalar>(1,s);
|
|
|
|
Matrix<RealScalar,Dynamic,1> d = Matrix<RealScalar,Dynamic,1>::Random(diagSize);
|
|
|
|
for(Index k=0; k<diagSize; ++k)
|
|
|
|
d(k) = d(k)*std::pow(RealScalar(10),internal::random<RealScalar>(-s,s));
|
|
|
|
m = Matrix<Scalar,Dynamic,Dynamic>::Random(a.rows(),diagSize) * d.asDiagonal() * Matrix<Scalar,Dynamic,Dynamic>::Random(diagSize,a.cols());
|
|
|
|
// cancel some coeffs
|
|
|
|
Index n = internal::random<Index>(0,m.size()-1);
|
|
|
|
for(Index i=0; i<n; ++i)
|
|
|
|
m(internal::random<Index>(0,m.rows()-1), internal::random<Index>(0,m.cols()-1)) = Scalar(0);
|
|
|
|
}
|
2010-10-12 03:36:04 +08:00
|
|
|
|
2014-07-17 23:09:15 +08:00
|
|
|
CALL_SUBTEST(( jacobisvd_test_all_computation_options<MatrixType, FullPivHouseholderQRPreconditioner>(m) ));
|
|
|
|
CALL_SUBTEST(( jacobisvd_test_all_computation_options<MatrixType, ColPivHouseholderQRPreconditioner>(m) ));
|
|
|
|
CALL_SUBTEST(( jacobisvd_test_all_computation_options<MatrixType, HouseholderQRPreconditioner>(m) ));
|
|
|
|
CALL_SUBTEST(( jacobisvd_test_all_computation_options<MatrixType, NoQRPreconditioner>(m) ));
|
2010-10-08 22:42:32 +08:00
|
|
|
}
|
|
|
|
|
2010-10-12 03:36:04 +08:00
|
|
|
template<typename MatrixType> void jacobisvd_verify_assert(const MatrixType& m)
|
2009-09-01 10:26:15 +08:00
|
|
|
{
|
2010-10-12 03:36:04 +08:00
|
|
|
typedef typename MatrixType::Scalar Scalar;
|
|
|
|
typedef typename MatrixType::Index Index;
|
|
|
|
Index rows = m.rows();
|
|
|
|
Index cols = m.cols();
|
|
|
|
|
|
|
|
enum {
|
|
|
|
RowsAtCompileTime = MatrixType::RowsAtCompileTime,
|
|
|
|
ColsAtCompileTime = MatrixType::ColsAtCompileTime
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef Matrix<Scalar, RowsAtCompileTime, 1> RhsType;
|
2009-09-01 10:26:15 +08:00
|
|
|
|
2010-10-12 03:36:04 +08:00
|
|
|
RhsType rhs(rows);
|
|
|
|
|
2010-10-12 09:00:42 +08:00
|
|
|
JacobiSVD<MatrixType> svd;
|
2009-09-01 10:26:15 +08:00
|
|
|
VERIFY_RAISES_ASSERT(svd.matrixU())
|
|
|
|
VERIFY_RAISES_ASSERT(svd.singularValues())
|
|
|
|
VERIFY_RAISES_ASSERT(svd.matrixV())
|
2010-10-12 03:36:04 +08:00
|
|
|
VERIFY_RAISES_ASSERT(svd.solve(rhs))
|
|
|
|
|
|
|
|
MatrixType a = MatrixType::Zero(rows, cols);
|
|
|
|
a.setZero();
|
|
|
|
svd.compute(a, 0);
|
|
|
|
VERIFY_RAISES_ASSERT(svd.matrixU())
|
|
|
|
VERIFY_RAISES_ASSERT(svd.matrixV())
|
|
|
|
svd.singularValues();
|
|
|
|
VERIFY_RAISES_ASSERT(svd.solve(rhs))
|
|
|
|
|
|
|
|
if (ColsAtCompileTime == Dynamic)
|
|
|
|
{
|
|
|
|
svd.compute(a, ComputeThinU);
|
|
|
|
svd.matrixU();
|
|
|
|
VERIFY_RAISES_ASSERT(svd.matrixV())
|
|
|
|
VERIFY_RAISES_ASSERT(svd.solve(rhs))
|
|
|
|
|
|
|
|
svd.compute(a, ComputeThinV);
|
|
|
|
svd.matrixV();
|
|
|
|
VERIFY_RAISES_ASSERT(svd.matrixU())
|
|
|
|
VERIFY_RAISES_ASSERT(svd.solve(rhs))
|
|
|
|
|
|
|
|
JacobiSVD<MatrixType, FullPivHouseholderQRPreconditioner> svd_fullqr;
|
|
|
|
VERIFY_RAISES_ASSERT(svd_fullqr.compute(a, ComputeFullU|ComputeThinV))
|
|
|
|
VERIFY_RAISES_ASSERT(svd_fullqr.compute(a, ComputeThinU|ComputeThinV))
|
|
|
|
VERIFY_RAISES_ASSERT(svd_fullqr.compute(a, ComputeThinU|ComputeFullV))
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
VERIFY_RAISES_ASSERT(svd.compute(a, ComputeThinU))
|
|
|
|
VERIFY_RAISES_ASSERT(svd.compute(a, ComputeThinV))
|
|
|
|
}
|
2009-09-01 10:26:15 +08:00
|
|
|
}
|
|
|
|
|
2010-10-17 21:40:52 +08:00
|
|
|
template<typename MatrixType>
|
|
|
|
void jacobisvd_method()
|
|
|
|
{
|
|
|
|
enum { Size = MatrixType::RowsAtCompileTime };
|
|
|
|
typedef typename MatrixType::RealScalar RealScalar;
|
|
|
|
typedef Matrix<RealScalar, Size, 1> RealVecType;
|
|
|
|
MatrixType m = MatrixType::Identity();
|
|
|
|
VERIFY_IS_APPROX(m.jacobiSvd().singularValues(), RealVecType::Ones());
|
|
|
|
VERIFY_RAISES_ASSERT(m.jacobiSvd().matrixU());
|
|
|
|
VERIFY_RAISES_ASSERT(m.jacobiSvd().matrixV());
|
|
|
|
VERIFY_IS_APPROX(m.jacobiSvd(ComputeFullU|ComputeFullV).solve(m), m);
|
|
|
|
}
|
|
|
|
|
2010-10-20 09:56:11 +08:00
|
|
|
// work around stupid msvc error when constructing at compile time an expression that involves
|
|
|
|
// a division by zero, even if the numeric type has floating point
|
|
|
|
template<typename Scalar>
|
|
|
|
EIGEN_DONT_INLINE Scalar zero() { return Scalar(0); }
|
|
|
|
|
2011-02-21 18:00:07 +08:00
|
|
|
// workaround aggressive optimization in ICC
|
|
|
|
template<typename T> EIGEN_DONT_INLINE T sub(T a, T b) { return a - b; }
|
|
|
|
|
2010-10-14 22:14:43 +08:00
|
|
|
template<typename MatrixType>
|
|
|
|
void jacobisvd_inf_nan()
|
|
|
|
{
|
2010-10-20 09:56:11 +08:00
|
|
|
// all this function does is verify we don't iterate infinitely on nan/inf values
|
|
|
|
|
2010-10-14 22:14:43 +08:00
|
|
|
JacobiSVD<MatrixType> svd;
|
|
|
|
typedef typename MatrixType::Scalar Scalar;
|
2010-10-20 09:56:11 +08:00
|
|
|
Scalar some_inf = Scalar(1) / zero<Scalar>();
|
2011-02-21 18:00:07 +08:00
|
|
|
VERIFY(sub(some_inf, some_inf) != sub(some_inf, some_inf));
|
2010-10-14 22:14:43 +08:00
|
|
|
svd.compute(MatrixType::Constant(10,10,some_inf), ComputeFullU | ComputeFullV);
|
2010-10-20 09:56:11 +08:00
|
|
|
|
2014-09-10 17:54:20 +08:00
|
|
|
Scalar nan = std::numeric_limits<Scalar>::quiet_NaN();
|
|
|
|
VERIFY(nan != nan);
|
|
|
|
svd.compute(MatrixType::Constant(10,10,nan), ComputeFullU | ComputeFullV);
|
2010-10-20 09:56:11 +08:00
|
|
|
|
2010-10-14 22:14:43 +08:00
|
|
|
MatrixType m = MatrixType::Zero(10,10);
|
2010-10-25 22:15:22 +08:00
|
|
|
m(internal::random<int>(0,9), internal::random<int>(0,9)) = some_inf;
|
2010-10-14 22:14:43 +08:00
|
|
|
svd.compute(m, ComputeFullU | ComputeFullV);
|
2010-10-20 09:56:11 +08:00
|
|
|
|
2010-10-14 22:14:43 +08:00
|
|
|
m = MatrixType::Zero(10,10);
|
2014-09-10 17:54:20 +08:00
|
|
|
m(internal::random<int>(0,9), internal::random<int>(0,9)) = nan;
|
|
|
|
svd.compute(m, ComputeFullU | ComputeFullV);
|
|
|
|
|
|
|
|
// regression test for bug 791
|
|
|
|
m.resize(3,3);
|
|
|
|
m << 0, 2*NumTraits<Scalar>::epsilon(), 0.5,
|
|
|
|
0, -0.5, 0,
|
|
|
|
nan, 0, 0;
|
2010-10-14 22:14:43 +08:00
|
|
|
svd.compute(m, ComputeFullU | ComputeFullV);
|
2014-09-10 17:59:45 +08:00
|
|
|
|
|
|
|
m.resize(4,4);
|
|
|
|
m << 1, 0, 0, 0,
|
|
|
|
0, 3, 1, 2e-308,
|
|
|
|
1, 0, 1, nan,
|
|
|
|
0, nan, nan, 0;
|
|
|
|
svd.compute(m, ComputeFullU | ComputeFullV);
|
2010-10-14 22:14:43 +08:00
|
|
|
}
|
|
|
|
|
2011-09-27 21:25:02 +08:00
|
|
|
// Regression test for bug 286: JacobiSVD loops indefinitely with some
|
|
|
|
// matrices containing denormal numbers.
|
2013-11-19 18:53:48 +08:00
|
|
|
void jacobisvd_underoverflow()
|
2011-09-27 21:25:02 +08:00
|
|
|
{
|
2012-02-01 06:18:17 +08:00
|
|
|
#if defined __INTEL_COMPILER
|
|
|
|
// shut up warning #239: floating point underflow
|
|
|
|
#pragma warning push
|
|
|
|
#pragma warning disable 239
|
|
|
|
#endif
|
2011-09-27 21:25:02 +08:00
|
|
|
Matrix2d M;
|
|
|
|
M << -7.90884e-313, -4.94e-324,
|
|
|
|
0, 5.60844e-313;
|
2014-09-11 05:11:58 +08:00
|
|
|
JacobiSVD<Matrix2d> svd;
|
|
|
|
svd.compute(M,ComputeFullU|ComputeFullV);
|
|
|
|
jacobisvd_check_full(M,svd);
|
|
|
|
|
|
|
|
VectorXd value_set(9);
|
|
|
|
value_set << 0, 1, -1, 5.60844e-313, -5.60844e-313, 4.94e-324, -4.94e-324, -4.94e-223, 4.94e-223;
|
|
|
|
Array4i id(0,0,0,0);
|
|
|
|
int k = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
M << value_set(id(0)), value_set(id(1)), value_set(id(2)), value_set(id(3));
|
|
|
|
svd.compute(M,ComputeFullU|ComputeFullV);
|
|
|
|
jacobisvd_check_full(M,svd);
|
|
|
|
|
|
|
|
id(k)++;
|
|
|
|
if(id(k)>=value_set.size())
|
|
|
|
{
|
|
|
|
while(k<3 && id(k)>=value_set.size()) id(++k)++;
|
|
|
|
id.head(k).setZero();
|
|
|
|
k=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while((id<int(value_set.size())).all());
|
|
|
|
|
2012-02-01 06:18:17 +08:00
|
|
|
#if defined __INTEL_COMPILER
|
|
|
|
#pragma warning pop
|
|
|
|
#endif
|
2013-11-19 18:53:48 +08:00
|
|
|
|
|
|
|
// Check for overflow:
|
|
|
|
Matrix3d M3;
|
|
|
|
M3 << 4.4331978442502944e+307, -5.8585363752028680e+307, 6.4527017443412964e+307,
|
|
|
|
3.7841695601406358e+307, 2.4331702789740617e+306, -3.5235707140272905e+307,
|
|
|
|
-8.7190887618028355e+307, -7.3453213709232193e+307, -2.4367363684472105e+307;
|
|
|
|
|
|
|
|
JacobiSVD<Matrix3d> svd3;
|
2014-09-11 05:11:58 +08:00
|
|
|
svd3.compute(M3,ComputeFullU|ComputeFullV); // just check we don't loop indefinitely
|
|
|
|
jacobisvd_check_full(M3,svd3);
|
2011-09-27 21:25:02 +08:00
|
|
|
}
|
|
|
|
|
2011-03-07 09:59:25 +08:00
|
|
|
void jacobisvd_preallocate()
|
|
|
|
{
|
|
|
|
Vector3f v(3.f, 2.f, 1.f);
|
|
|
|
MatrixXf m = v.asDiagonal();
|
|
|
|
|
|
|
|
internal::set_is_malloc_allowed(false);
|
2013-06-24 01:11:32 +08:00
|
|
|
VERIFY_RAISES_ASSERT(VectorXf tmp(10);)
|
2011-03-07 09:59:25 +08:00
|
|
|
JacobiSVD<MatrixXf> svd;
|
|
|
|
internal::set_is_malloc_allowed(true);
|
|
|
|
svd.compute(m);
|
|
|
|
VERIFY_IS_APPROX(svd.singularValues(), v);
|
|
|
|
|
|
|
|
JacobiSVD<MatrixXf> svd2(3,3);
|
|
|
|
internal::set_is_malloc_allowed(false);
|
|
|
|
svd2.compute(m);
|
|
|
|
internal::set_is_malloc_allowed(true);
|
|
|
|
VERIFY_IS_APPROX(svd2.singularValues(), v);
|
|
|
|
VERIFY_RAISES_ASSERT(svd2.matrixU());
|
|
|
|
VERIFY_RAISES_ASSERT(svd2.matrixV());
|
|
|
|
svd2.compute(m, ComputeFullU | ComputeFullV);
|
|
|
|
VERIFY_IS_APPROX(svd2.matrixU(), Matrix3f::Identity());
|
|
|
|
VERIFY_IS_APPROX(svd2.matrixV(), Matrix3f::Identity());
|
|
|
|
internal::set_is_malloc_allowed(false);
|
|
|
|
svd2.compute(m);
|
|
|
|
internal::set_is_malloc_allowed(true);
|
|
|
|
|
|
|
|
JacobiSVD<MatrixXf> svd3(3,3,ComputeFullU|ComputeFullV);
|
|
|
|
internal::set_is_malloc_allowed(false);
|
|
|
|
svd2.compute(m);
|
|
|
|
internal::set_is_malloc_allowed(true);
|
|
|
|
VERIFY_IS_APPROX(svd2.singularValues(), v);
|
|
|
|
VERIFY_IS_APPROX(svd2.matrixU(), Matrix3f::Identity());
|
|
|
|
VERIFY_IS_APPROX(svd2.matrixV(), Matrix3f::Identity());
|
|
|
|
internal::set_is_malloc_allowed(false);
|
|
|
|
svd2.compute(m, ComputeFullU|ComputeFullV);
|
|
|
|
internal::set_is_malloc_allowed(true);
|
|
|
|
}
|
|
|
|
|
2009-09-01 10:26:15 +08:00
|
|
|
void test_jacobisvd()
|
|
|
|
{
|
2010-10-12 03:36:04 +08:00
|
|
|
CALL_SUBTEST_3(( jacobisvd_verify_assert(Matrix3f()) ));
|
|
|
|
CALL_SUBTEST_4(( jacobisvd_verify_assert(Matrix4d()) ));
|
|
|
|
CALL_SUBTEST_7(( jacobisvd_verify_assert(MatrixXf(10,12)) ));
|
|
|
|
CALL_SUBTEST_8(( jacobisvd_verify_assert(MatrixXcd(7,5)) ));
|
|
|
|
|
2009-09-01 10:26:15 +08:00
|
|
|
for(int i = 0; i < g_repeat; i++) {
|
|
|
|
Matrix2cd m;
|
|
|
|
m << 0, 1,
|
|
|
|
0, 1;
|
2010-10-08 22:42:40 +08:00
|
|
|
CALL_SUBTEST_1(( jacobisvd(m, false) ));
|
2009-09-01 10:26:15 +08:00
|
|
|
m << 1, 0,
|
|
|
|
1, 0;
|
2010-10-08 22:42:40 +08:00
|
|
|
CALL_SUBTEST_1(( jacobisvd(m, false) ));
|
2010-10-14 22:14:43 +08:00
|
|
|
|
2009-09-01 10:26:15 +08:00
|
|
|
Matrix2d n;
|
2010-10-14 22:14:43 +08:00
|
|
|
n << 0, 0,
|
|
|
|
0, 0;
|
|
|
|
CALL_SUBTEST_2(( jacobisvd(n, false) ));
|
|
|
|
n << 0, 0,
|
|
|
|
0, 1;
|
2010-10-08 22:42:40 +08:00
|
|
|
CALL_SUBTEST_2(( jacobisvd(n, false) ));
|
2010-10-14 22:14:43 +08:00
|
|
|
|
2010-10-08 22:42:40 +08:00
|
|
|
CALL_SUBTEST_3(( jacobisvd<Matrix3f>() ));
|
|
|
|
CALL_SUBTEST_4(( jacobisvd<Matrix4d>() ));
|
|
|
|
CALL_SUBTEST_5(( jacobisvd<Matrix<float,3,5> >() ));
|
|
|
|
CALL_SUBTEST_6(( jacobisvd<Matrix<double,Dynamic,2> >(Matrix<double,Dynamic,2>(10,2)) ));
|
|
|
|
|
2010-10-25 22:15:22 +08:00
|
|
|
int r = internal::random<int>(1, 30),
|
|
|
|
c = internal::random<int>(1, 30);
|
2013-06-25 17:42:04 +08:00
|
|
|
|
|
|
|
TEST_SET_BUT_UNUSED_VARIABLE(r)
|
|
|
|
TEST_SET_BUT_UNUSED_VARIABLE(c)
|
|
|
|
|
2014-07-17 23:09:15 +08:00
|
|
|
CALL_SUBTEST_10(( jacobisvd<MatrixXd>(MatrixXd(r,c)) ));
|
2010-10-12 03:36:04 +08:00
|
|
|
CALL_SUBTEST_7(( jacobisvd<MatrixXf>(MatrixXf(r,c)) ));
|
|
|
|
CALL_SUBTEST_8(( jacobisvd<MatrixXcd>(MatrixXcd(r,c)) ));
|
|
|
|
(void) r;
|
|
|
|
(void) c;
|
2010-10-14 22:14:43 +08:00
|
|
|
|
|
|
|
// Test on inf/nan matrix
|
|
|
|
CALL_SUBTEST_7( jacobisvd_inf_nan<MatrixXf>() );
|
2014-09-10 17:54:20 +08:00
|
|
|
CALL_SUBTEST_10( jacobisvd_inf_nan<MatrixXd>() );
|
2009-09-01 10:26:15 +08:00
|
|
|
}
|
2010-04-21 23:15:57 +08:00
|
|
|
|
2011-07-12 20:41:00 +08:00
|
|
|
CALL_SUBTEST_7(( jacobisvd<MatrixXf>(MatrixXf(internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE/2), internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE/2))) ));
|
|
|
|
CALL_SUBTEST_8(( jacobisvd<MatrixXcd>(MatrixXcd(internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE/3), internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE/3))) ));
|
2010-10-14 22:14:43 +08:00
|
|
|
|
2010-10-17 21:40:52 +08:00
|
|
|
// test matrixbase method
|
|
|
|
CALL_SUBTEST_1(( jacobisvd_method<Matrix2cd>() ));
|
|
|
|
CALL_SUBTEST_3(( jacobisvd_method<Matrix3f>() ));
|
|
|
|
|
2010-10-14 22:14:43 +08:00
|
|
|
// Test problem size constructors
|
|
|
|
CALL_SUBTEST_7( JacobiSVD<MatrixXf>(10,10) );
|
2011-03-07 09:59:25 +08:00
|
|
|
|
|
|
|
// Check that preallocation avoids subsequent mallocs
|
|
|
|
CALL_SUBTEST_9( jacobisvd_preallocate() );
|
2011-09-27 21:25:02 +08:00
|
|
|
|
|
|
|
// Regression check for bug 286
|
2013-11-19 18:53:48 +08:00
|
|
|
CALL_SUBTEST_2( jacobisvd_underoverflow() );
|
2009-09-01 10:26:15 +08:00
|
|
|
}
|