mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
Revert "Update SVD Module to allow specifying computation options with a...
This commit is contained in:
committed by
David Tellenbach
parent
4dd126c630
commit
085c2fc5d5
@@ -19,11 +19,26 @@
|
||||
#include <iostream>
|
||||
#include <Eigen/LU>
|
||||
|
||||
|
||||
#define SVD_DEFAULT(M) BDCSVD<M>
|
||||
#define SVD_FOR_MIN_NORM(M) BDCSVD<M>
|
||||
#define SVD_STATIC_OPTIONS(M, O) BDCSVD<M, O>
|
||||
#include "svd_common.h"
|
||||
|
||||
// Check all variants of JacobiSVD
|
||||
template<typename MatrixType>
|
||||
void bdcsvd(const MatrixType& a = MatrixType(), bool pickrandom = true)
|
||||
{
|
||||
MatrixType m;
|
||||
if(pickrandom) {
|
||||
m.resizeLike(a);
|
||||
svd_fill_random(m);
|
||||
}
|
||||
else
|
||||
m = a;
|
||||
|
||||
CALL_SUBTEST(( svd_test_all_computation_options<BDCSVD<MatrixType> >(m, false) ));
|
||||
}
|
||||
|
||||
template<typename MatrixType>
|
||||
void bdcsvd_method()
|
||||
{
|
||||
@@ -34,23 +49,28 @@ void bdcsvd_method()
|
||||
VERIFY_IS_APPROX(m.bdcSvd().singularValues(), RealVecType::Ones());
|
||||
VERIFY_RAISES_ASSERT(m.bdcSvd().matrixU());
|
||||
VERIFY_RAISES_ASSERT(m.bdcSvd().matrixV());
|
||||
VERIFY_IS_APPROX(m.template bdcSvd<ComputeFullU|ComputeFullV>().solve(m), m);
|
||||
VERIFY_IS_APPROX(m.template bdcSvd<ComputeFullU|ComputeFullV>().transpose().solve(m), m);
|
||||
VERIFY_IS_APPROX(m.template bdcSvd<ComputeFullU|ComputeFullV>().adjoint().solve(m), m);
|
||||
VERIFY_IS_APPROX(m.bdcSvd(ComputeFullU|ComputeFullV).solve(m), m);
|
||||
VERIFY_IS_APPROX(m.bdcSvd(ComputeFullU|ComputeFullV).transpose().solve(m), m);
|
||||
VERIFY_IS_APPROX(m.bdcSvd(ComputeFullU|ComputeFullV).adjoint().solve(m), m);
|
||||
}
|
||||
|
||||
// compare the Singular values returned with Jacobi and Bdc
|
||||
// Compare the Singular values returned with Jacobi and Bdc.
|
||||
template<typename MatrixType>
|
||||
void compare_bdc_jacobi(const MatrixType& a = MatrixType(), int algoswap = 16, bool random = true)
|
||||
void compare_bdc_jacobi(const MatrixType& a = MatrixType(), unsigned int computationOptions = 0, int algoswap = 16, bool random = true)
|
||||
{
|
||||
MatrixType m = random ? MatrixType::Random(a.rows(), a.cols()) : a;
|
||||
|
||||
BDCSVD<MatrixType> bdc_svd(m.rows(), m.cols());
|
||||
BDCSVD<MatrixType> bdc_svd(m.rows(), m.cols(), computationOptions);
|
||||
bdc_svd.setSwitchSize(algoswap);
|
||||
bdc_svd.compute(m);
|
||||
|
||||
|
||||
JacobiSVD<MatrixType> jacobi_svd(m);
|
||||
VERIFY_IS_APPROX(bdc_svd.singularValues(), jacobi_svd.singularValues());
|
||||
|
||||
if(computationOptions & ComputeFullU) VERIFY_IS_APPROX(bdc_svd.matrixU(), jacobi_svd.matrixU());
|
||||
if(computationOptions & ComputeThinU) VERIFY_IS_APPROX(bdc_svd.matrixU(), jacobi_svd.matrixU());
|
||||
if(computationOptions & ComputeFullV) VERIFY_IS_APPROX(bdc_svd.matrixV(), jacobi_svd.matrixV());
|
||||
if(computationOptions & ComputeThinV) VERIFY_IS_APPROX(bdc_svd.matrixV(), jacobi_svd.matrixV());
|
||||
}
|
||||
|
||||
// Verifies total deflation is **not** triggered.
|
||||
@@ -71,59 +91,41 @@ void compare_bdc_jacobi_instance(bool structure_as_m, int algoswap = 16)
|
||||
-20.794, 8.68496, -4.83103,
|
||||
-8.4981, -10.5451, 23.9072;
|
||||
}
|
||||
compare_bdc_jacobi(m, algoswap, false);
|
||||
}
|
||||
|
||||
template<typename MatrixType>
|
||||
void bdcsvd_all_options(const MatrixType& input = MatrixType())
|
||||
{
|
||||
MatrixType m = input;
|
||||
svd_fill_random(m);
|
||||
svd_option_checks<MatrixType, 0>(m);
|
||||
compare_bdc_jacobi(m, 0, algoswap, false);
|
||||
}
|
||||
|
||||
EIGEN_DECLARE_TEST(bdcsvd)
|
||||
{
|
||||
CALL_SUBTEST_3(( svd_verify_assert<Matrix3f>() ));
|
||||
CALL_SUBTEST_4(( svd_verify_assert<Matrix4d>() ));
|
||||
CALL_SUBTEST_7(( svd_verify_assert<Matrix<float, 30, 21> >() ));
|
||||
CALL_SUBTEST_7(( svd_verify_assert<Matrix<float, 21, 30> >() ));
|
||||
CALL_SUBTEST_9(( svd_verify_assert<Matrix<std::complex<double>, 20, 27> >() ));
|
||||
CALL_SUBTEST_3(( svd_verify_assert<BDCSVD<Matrix3f> >(Matrix3f()) ));
|
||||
CALL_SUBTEST_4(( svd_verify_assert<BDCSVD<Matrix4d> >(Matrix4d()) ));
|
||||
CALL_SUBTEST_7(( svd_verify_assert<BDCSVD<MatrixXf> >(MatrixXf(10,12)) ));
|
||||
CALL_SUBTEST_8(( svd_verify_assert<BDCSVD<MatrixXcd> >(MatrixXcd(7,5)) ));
|
||||
|
||||
CALL_SUBTEST_101(( svd_all_trivial_2x2(bdcsvd_all_options<Matrix2cd>) ));
|
||||
CALL_SUBTEST_102(( svd_all_trivial_2x2(bdcsvd_all_options<Matrix2d>) ));
|
||||
CALL_SUBTEST_101(( svd_all_trivial_2x2(bdcsvd<Matrix2cd>) ));
|
||||
CALL_SUBTEST_102(( svd_all_trivial_2x2(bdcsvd<Matrix2d>) ));
|
||||
|
||||
for(int i = 0; i < g_repeat; i++) {
|
||||
CALL_SUBTEST_3(( bdcsvd<Matrix3f>() ));
|
||||
CALL_SUBTEST_4(( bdcsvd<Matrix4d>() ));
|
||||
CALL_SUBTEST_5(( bdcsvd<Matrix<float,3,5> >() ));
|
||||
|
||||
int r = internal::random<int>(1, EIGEN_TEST_MAX_SIZE/2),
|
||||
c = internal::random<int>(1, EIGEN_TEST_MAX_SIZE/2);
|
||||
|
||||
TEST_SET_BUT_UNUSED_VARIABLE(r)
|
||||
TEST_SET_BUT_UNUSED_VARIABLE(c)
|
||||
|
||||
CALL_SUBTEST_7(( compare_bdc_jacobi<MatrixXf>(MatrixXf(r,c)) ));
|
||||
CALL_SUBTEST_10(( compare_bdc_jacobi<MatrixXd>(MatrixXd(r,c)) ));
|
||||
CALL_SUBTEST_8(( compare_bdc_jacobi<MatrixXcd>(MatrixXcd(r,c)) ));
|
||||
CALL_SUBTEST_6(( bdcsvd(Matrix<double,Dynamic,2>(r,2)) ));
|
||||
CALL_SUBTEST_7(( bdcsvd(MatrixXf(r,c)) ));
|
||||
CALL_SUBTEST_7(( compare_bdc_jacobi(MatrixXf(r,c)) ));
|
||||
CALL_SUBTEST_10(( bdcsvd(MatrixXd(r,c)) ));
|
||||
CALL_SUBTEST_10(( compare_bdc_jacobi(MatrixXd(r,c)) ));
|
||||
CALL_SUBTEST_8(( bdcsvd(MatrixXcd(r,c)) ));
|
||||
CALL_SUBTEST_8(( compare_bdc_jacobi(MatrixXcd(r,c)) ));
|
||||
|
||||
// Test on inf/nan matrix
|
||||
CALL_SUBTEST_7( (svd_inf_nan<MatrixXf>()) );
|
||||
CALL_SUBTEST_10( (svd_inf_nan<MatrixXd>()) );
|
||||
|
||||
// Verify some computations using all combinations of the Options template parameter.
|
||||
CALL_SUBTEST_3(( bdcsvd_all_options<Matrix3f>() ));
|
||||
CALL_SUBTEST_3(( bdcsvd_all_options<Matrix<float, 2, 3> >() ));
|
||||
CALL_SUBTEST_4(( bdcsvd_all_options<Matrix<double, 20, 17> >() ));
|
||||
CALL_SUBTEST_4(( bdcsvd_all_options<Matrix<double, 17, 20> >() ));
|
||||
CALL_SUBTEST_5(( bdcsvd_all_options<Matrix<double, Dynamic, 30> >(Matrix<double, Dynamic, 30>(r, 30)) ));
|
||||
CALL_SUBTEST_5(( bdcsvd_all_options<Matrix<double, 20, Dynamic> >(Matrix<double, 20, Dynamic>(20, c)) ));
|
||||
CALL_SUBTEST_7(( bdcsvd_all_options<MatrixXf>(MatrixXf(r, c)) ));
|
||||
CALL_SUBTEST_8(( bdcsvd_all_options<MatrixXcd>(MatrixXcd(r, c)) ));
|
||||
CALL_SUBTEST_10(( bdcsvd_all_options<MatrixXd>(MatrixXd(r, c)) ));
|
||||
CALL_SUBTEST_14(( bdcsvd_all_options<Matrix<double, 20, 27, RowMajor>>() ));
|
||||
CALL_SUBTEST_14(( bdcsvd_all_options<Matrix<double, 27, 20, RowMajor>>() ));
|
||||
|
||||
CALL_SUBTEST_15(( svd_check_max_size_matrix<Matrix<float, Dynamic, Dynamic, ColMajor, 20, 35>, ColPivHouseholderQRPreconditioner>(r, c) ));
|
||||
CALL_SUBTEST_15(( svd_check_max_size_matrix<Matrix<float, Dynamic, Dynamic, ColMajor, 35, 20>, HouseholderQRPreconditioner>(r, c) ));
|
||||
CALL_SUBTEST_15(( svd_check_max_size_matrix<Matrix<float, Dynamic, Dynamic, RowMajor, 20, 35>, ColPivHouseholderQRPreconditioner>(r, c) ));
|
||||
CALL_SUBTEST_15(( svd_check_max_size_matrix<Matrix<float, Dynamic, Dynamic, RowMajor, 35, 20>, HouseholderQRPreconditioner>(r, c) ));
|
||||
CALL_SUBTEST_7( (svd_inf_nan<BDCSVD<MatrixXf>, MatrixXf>()) );
|
||||
CALL_SUBTEST_10( (svd_inf_nan<BDCSVD<MatrixXd>, MatrixXd>()) );
|
||||
}
|
||||
|
||||
// test matrixbase method
|
||||
|
||||
@@ -200,8 +200,8 @@ EIGEN_DECLARE_TEST(boostmultiprec)
|
||||
TEST_SET_BUT_UNUSED_VARIABLE(s)
|
||||
}
|
||||
|
||||
CALL_SUBTEST_9(( jacobisvd_all_options(Mat(internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE), internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE/2))) ));
|
||||
CALL_SUBTEST_10(( bdcsvd_all_options(Mat(internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE), internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE/2))) ));
|
||||
CALL_SUBTEST_9(( jacobisvd(Mat(internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE), internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE/2))) ));
|
||||
CALL_SUBTEST_10(( bdcsvd(Mat(internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE), internal::random<int>(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE/2))) ));
|
||||
|
||||
CALL_SUBTEST_11(( test_simplicial_cholesky_T<Real,int,ColMajor>() ));
|
||||
}
|
||||
|
||||
@@ -211,7 +211,7 @@ MatrixType randomRotationMatrix()
|
||||
// https://www.isprs-ann-photogramm-remote-sens-spatial-inf-sci.net/III-7/103/2016/isprs-annals-III-7-103-2016.pdf
|
||||
const MatrixType rand = MatrixType::Random();
|
||||
const MatrixType q = rand.householderQr().householderQ();
|
||||
const JacobiSVD<MatrixType, ComputeFullU | ComputeFullV> svd(q);
|
||||
const JacobiSVD<MatrixType> svd = q.jacobiSvd(ComputeFullU | ComputeFullV);
|
||||
const typename MatrixType::Scalar det = (svd.matrixU() * svd.matrixV().transpose()).determinant();
|
||||
MatrixType diag = rand.Identity();
|
||||
diag(MatrixType::RowsAtCompileTime - 1, MatrixType::ColsAtCompileTime - 1) = det;
|
||||
|
||||
@@ -16,9 +16,49 @@
|
||||
|
||||
#define SVD_DEFAULT(M) JacobiSVD<M>
|
||||
#define SVD_FOR_MIN_NORM(M) JacobiSVD<M,ColPivHouseholderQRPreconditioner>
|
||||
#define SVD_STATIC_OPTIONS(M, O) JacobiSVD<M, O>
|
||||
#include "svd_common.h"
|
||||
|
||||
// Check all variants of JacobiSVD
|
||||
template<typename MatrixType>
|
||||
void jacobisvd(const MatrixType& a = MatrixType(), bool pickrandom = true)
|
||||
{
|
||||
MatrixType m = a;
|
||||
if(pickrandom)
|
||||
svd_fill_random(m);
|
||||
|
||||
CALL_SUBTEST(( svd_test_all_computation_options<JacobiSVD<MatrixType, FullPivHouseholderQRPreconditioner> >(m, true) )); // check full only
|
||||
CALL_SUBTEST(( svd_test_all_computation_options<JacobiSVD<MatrixType, ColPivHouseholderQRPreconditioner> >(m, false) ));
|
||||
CALL_SUBTEST(( svd_test_all_computation_options<JacobiSVD<MatrixType, HouseholderQRPreconditioner> >(m, false) ));
|
||||
if(m.rows()==m.cols())
|
||||
CALL_SUBTEST(( svd_test_all_computation_options<JacobiSVD<MatrixType, NoQRPreconditioner> >(m, false) ));
|
||||
}
|
||||
|
||||
template<typename MatrixType> void jacobisvd_verify_assert(const MatrixType& m)
|
||||
{
|
||||
svd_verify_assert<JacobiSVD<MatrixType> >(m);
|
||||
svd_verify_assert<JacobiSVD<MatrixType, FullPivHouseholderQRPreconditioner> >(m, true);
|
||||
svd_verify_assert<JacobiSVD<MatrixType, ColPivHouseholderQRPreconditioner> >(m);
|
||||
svd_verify_assert<JacobiSVD<MatrixType, HouseholderQRPreconditioner> >(m);
|
||||
Index rows = m.rows();
|
||||
Index cols = m.cols();
|
||||
|
||||
enum {
|
||||
ColsAtCompileTime = MatrixType::ColsAtCompileTime
|
||||
};
|
||||
|
||||
|
||||
MatrixType a = MatrixType::Zero(rows, cols);
|
||||
a.setZero();
|
||||
|
||||
if (ColsAtCompileTime == Dynamic)
|
||||
{
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
template<typename MatrixType>
|
||||
void jacobisvd_method()
|
||||
{
|
||||
@@ -29,47 +69,9 @@ void jacobisvd_method()
|
||||
VERIFY_IS_APPROX(m.jacobiSvd().singularValues(), RealVecType::Ones());
|
||||
VERIFY_RAISES_ASSERT(m.jacobiSvd().matrixU());
|
||||
VERIFY_RAISES_ASSERT(m.jacobiSvd().matrixV());
|
||||
VERIFY_IS_APPROX(m.template jacobiSvd<ComputeFullU|ComputeFullV>().solve(m), m);
|
||||
VERIFY_IS_APPROX(m.template jacobiSvd<ComputeFullU|ComputeFullV>().transpose().solve(m), m);
|
||||
VERIFY_IS_APPROX(m.template jacobiSvd<ComputeFullU|ComputeFullV>().adjoint().solve(m), m);
|
||||
}
|
||||
|
||||
template<typename MatrixType>
|
||||
void jacobisvd_all_options(const MatrixType& input = MatrixType())
|
||||
{
|
||||
MatrixType m = input;
|
||||
svd_fill_random(m);
|
||||
svd_option_checks<MatrixType, 0 /* Default */>(m);
|
||||
svd_option_checks<MatrixType, ColPivHouseholderQRPreconditioner>(m);
|
||||
svd_option_checks<MatrixType, HouseholderQRPreconditioner>(m);
|
||||
svd_option_checks_full_only<MatrixType, FullPivHouseholderQRPreconditioner>(m); // FullPiv only used when computing full unitaries
|
||||
}
|
||||
|
||||
template<typename MatrixType>
|
||||
void jacobisvd_verify_assert(const MatrixType& m = MatrixType())
|
||||
{
|
||||
svd_verify_assert<MatrixType, 0 /* Default */>(m);
|
||||
svd_verify_assert<MatrixType, ColPivHouseholderQRPreconditioner>(m);
|
||||
svd_verify_assert<MatrixType, HouseholderQRPreconditioner>(m);
|
||||
svd_verify_assert_full_only<MatrixType, FullPivHouseholderQRPreconditioner>(m);
|
||||
}
|
||||
|
||||
template<typename MatrixType>
|
||||
void jacobisvd_verify_inputs(const MatrixType& m = MatrixType()) {
|
||||
// check defaults
|
||||
typedef JacobiSVD<MatrixType> DefaultSVD;
|
||||
DefaultSVD defaultSvd(m);
|
||||
VERIFY((int)DefaultSVD::QRPreconditioner == (int)ColPivHouseholderQRPreconditioner);
|
||||
VERIFY(!defaultSvd.computeU());
|
||||
VERIFY(!defaultSvd.computeV());
|
||||
|
||||
// ColPivHouseholderQR is always default in presence of other options.
|
||||
VERIFY(( (int)JacobiSVD<MatrixType, ComputeThinU>::QRPreconditioner == (int)ColPivHouseholderQRPreconditioner ));
|
||||
VERIFY(( (int)JacobiSVD<MatrixType, ComputeThinV>::QRPreconditioner == (int)ColPivHouseholderQRPreconditioner ));
|
||||
VERIFY(( (int)JacobiSVD<MatrixType, ComputeThinU | ComputeThinV>::QRPreconditioner == (int)ColPivHouseholderQRPreconditioner ));
|
||||
VERIFY(( (int)JacobiSVD<MatrixType, ComputeFullU | ComputeFullV>::QRPreconditioner == (int)ColPivHouseholderQRPreconditioner ));
|
||||
VERIFY(( (int)JacobiSVD<MatrixType, ComputeThinU | ComputeFullV>::QRPreconditioner == (int)ColPivHouseholderQRPreconditioner ));
|
||||
VERIFY(( (int)JacobiSVD<MatrixType, ComputeFullU | ComputeThinV>::QRPreconditioner == (int)ColPivHouseholderQRPreconditioner ));
|
||||
VERIFY_IS_APPROX(m.jacobiSvd(ComputeFullU|ComputeFullV).solve(m), m);
|
||||
VERIFY_IS_APPROX(m.jacobiSvd(ComputeFullU|ComputeFullV).transpose().solve(m), m);
|
||||
VERIFY_IS_APPROX(m.jacobiSvd(ComputeFullU|ComputeFullV).adjoint().solve(m), m);
|
||||
}
|
||||
|
||||
namespace Foo {
|
||||
@@ -89,63 +91,45 @@ void msvc_workaround()
|
||||
|
||||
EIGEN_DECLARE_TEST(jacobisvd)
|
||||
{
|
||||
CALL_SUBTEST_4(( jacobisvd_verify_inputs<Matrix4d>() ));
|
||||
CALL_SUBTEST_7(( jacobisvd_verify_inputs(Matrix<float, 10, Dynamic>(10, 12)) ));
|
||||
CALL_SUBTEST_8(( jacobisvd_verify_inputs<Matrix<std::complex<double>, 7, 5> >() ));
|
||||
|
||||
CALL_SUBTEST_3(( jacobisvd_verify_assert<Matrix3f>() ));
|
||||
CALL_SUBTEST_4(( jacobisvd_verify_assert<Matrix4d>() ));
|
||||
CALL_SUBTEST_7(( jacobisvd_verify_assert<Matrix<float, 10, 12>>() ));
|
||||
CALL_SUBTEST_7(( jacobisvd_verify_assert<Matrix<float, 12, 10>>() ));
|
||||
CALL_SUBTEST_7(( jacobisvd_verify_assert<MatrixXf>(MatrixXf(10, 12)) ));
|
||||
CALL_SUBTEST_8(( jacobisvd_verify_assert<MatrixXcd>(MatrixXcd(7, 5)) ));
|
||||
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)) ));
|
||||
|
||||
CALL_SUBTEST_11(svd_all_trivial_2x2(jacobisvd_all_options<Matrix2cd>));
|
||||
CALL_SUBTEST_12(svd_all_trivial_2x2(jacobisvd_all_options<Matrix2d>));
|
||||
CALL_SUBTEST_11(svd_all_trivial_2x2(jacobisvd<Matrix2cd>));
|
||||
CALL_SUBTEST_12(svd_all_trivial_2x2(jacobisvd<Matrix2d>));
|
||||
|
||||
for(int i = 0; i < g_repeat; i++) {
|
||||
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)) ));
|
||||
|
||||
int r = internal::random<int>(1, 30),
|
||||
c = internal::random<int>(1, 30);
|
||||
|
||||
TEST_SET_BUT_UNUSED_VARIABLE(r)
|
||||
TEST_SET_BUT_UNUSED_VARIABLE(c)
|
||||
|
||||
// Verify some computations using all combinations of the Options template parameter.
|
||||
CALL_SUBTEST_3(( jacobisvd_all_options<Matrix3f>() ));
|
||||
CALL_SUBTEST_3(( jacobisvd_all_options<Matrix<float, 2, 3> >() ));
|
||||
CALL_SUBTEST_4(( jacobisvd_all_options<Matrix4d>() ));
|
||||
CALL_SUBTEST_4(( jacobisvd_all_options<Matrix<double, 10, 16> >() ));
|
||||
CALL_SUBTEST_4(( jacobisvd_all_options<Matrix<double, 16, 10> >() ));
|
||||
CALL_SUBTEST_5(( jacobisvd_all_options<Matrix<double, Dynamic, 16> >(Matrix<double, Dynamic, 16>(r, 16)) ));
|
||||
CALL_SUBTEST_5(( jacobisvd_all_options<Matrix<double, 10, Dynamic> >(Matrix<double, 10, Dynamic>(10, c)) ));
|
||||
CALL_SUBTEST_7(( jacobisvd_all_options<MatrixXf>( MatrixXf(r, c)) ));
|
||||
CALL_SUBTEST_8(( jacobisvd_all_options<MatrixXcd>( MatrixXcd(r, c)) ));
|
||||
CALL_SUBTEST_10(( jacobisvd_all_options<MatrixXd>( MatrixXd(r, c)) ));
|
||||
CALL_SUBTEST_14(( jacobisvd_all_options<Matrix<double, 5, 7, RowMajor>>() ));
|
||||
CALL_SUBTEST_14(( jacobisvd_all_options<Matrix<double, 7, 5, RowMajor>>() ));
|
||||
|
||||
MatrixXcd noQRTest = MatrixXcd(r, r);
|
||||
svd_fill_random(noQRTest);
|
||||
CALL_SUBTEST_16(( svd_option_checks<MatrixXcd, NoQRPreconditioner>(noQRTest) ));
|
||||
|
||||
CALL_SUBTEST_15(( svd_check_max_size_matrix<Matrix<float, Dynamic, Dynamic, ColMajor, 13, 15>, ColPivHouseholderQRPreconditioner>(r, c) ));
|
||||
CALL_SUBTEST_15(( svd_check_max_size_matrix<Matrix<float, Dynamic, Dynamic, ColMajor, 15, 13>, HouseholderQRPreconditioner>(r, c) ));
|
||||
CALL_SUBTEST_15(( svd_check_max_size_matrix<Matrix<float, Dynamic, Dynamic, RowMajor, 13, 15>, ColPivHouseholderQRPreconditioner>(r, c) ));
|
||||
CALL_SUBTEST_15(( svd_check_max_size_matrix<Matrix<float, Dynamic, Dynamic, RowMajor, 15, 13>, HouseholderQRPreconditioner>(r, c) ));
|
||||
CALL_SUBTEST_10(( jacobisvd<MatrixXd>(MatrixXd(r,c)) ));
|
||||
CALL_SUBTEST_7(( jacobisvd<MatrixXf>(MatrixXf(r,c)) ));
|
||||
CALL_SUBTEST_8(( jacobisvd<MatrixXcd>(MatrixXcd(r,c)) ));
|
||||
(void) r;
|
||||
(void) c;
|
||||
|
||||
// Test on inf/nan matrix
|
||||
CALL_SUBTEST_7( (svd_inf_nan<MatrixXf>()) );
|
||||
CALL_SUBTEST_10( (svd_inf_nan<MatrixXd>()) );
|
||||
CALL_SUBTEST_7( (svd_inf_nan<JacobiSVD<MatrixXf>, MatrixXf>()) );
|
||||
CALL_SUBTEST_10( (svd_inf_nan<JacobiSVD<MatrixXd>, MatrixXd>()) );
|
||||
|
||||
CALL_SUBTEST_13(( jacobisvd_verify_assert<Matrix<double, 6, 1>>() ));
|
||||
CALL_SUBTEST_13(( jacobisvd_verify_assert<Matrix<double, 1, 6>>() ));
|
||||
CALL_SUBTEST_13(( jacobisvd_verify_assert<Matrix<double, Dynamic, 1>>(Matrix<double, Dynamic, 1>(r)) ));
|
||||
CALL_SUBTEST_13(( jacobisvd_verify_assert<Matrix<double, 1, Dynamic>>(Matrix<double, 1, Dynamic>(c)) ));
|
||||
// bug1395 test compile-time vectors as input
|
||||
CALL_SUBTEST_13(( jacobisvd_verify_assert(Matrix<double,6,1>()) ));
|
||||
CALL_SUBTEST_13(( jacobisvd_verify_assert(Matrix<double,1,6>()) ));
|
||||
CALL_SUBTEST_13(( jacobisvd_verify_assert(Matrix<double,Dynamic,1>(r)) ));
|
||||
CALL_SUBTEST_13(( jacobisvd_verify_assert(Matrix<double,1,Dynamic>(c)) ));
|
||||
}
|
||||
|
||||
CALL_SUBTEST_7(( jacobisvd_all_options<MatrixXd>(MatrixXd(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_all_options<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))) ));
|
||||
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))) ));
|
||||
|
||||
// test matrixbase method
|
||||
CALL_SUBTEST_1(( jacobisvd_method<Matrix2cd>() ));
|
||||
|
||||
@@ -152,7 +152,7 @@ void ctms_decompositions()
|
||||
x = fpQR.solve(b);
|
||||
|
||||
// SVD module
|
||||
Eigen::JacobiSVD<Matrix, ComputeFullU | ComputeFullV> jSVD; jSVD.compute(A);
|
||||
Eigen::JacobiSVD<Matrix> jSVD; jSVD.compute(A, ComputeFullU | ComputeFullV);
|
||||
}
|
||||
|
||||
void test_zerosized() {
|
||||
|
||||
@@ -55,7 +55,7 @@ void cod() {
|
||||
MatrixType exact_solution = MatrixType::Random(cols, cols2);
|
||||
MatrixType rhs = matrix * exact_solution;
|
||||
MatrixType cod_solution = cod.solve(rhs);
|
||||
JacobiSVD<MatrixType, ComputeThinU | ComputeThinV> svd(matrix);
|
||||
JacobiSVD<MatrixType> svd(matrix, ComputeThinU | ComputeThinV);
|
||||
MatrixType svd_solution = svd.solve(rhs);
|
||||
VERIFY_IS_APPROX(cod_solution, svd_solution);
|
||||
|
||||
@@ -88,7 +88,7 @@ void cod_fixedsize() {
|
||||
exact_solution.setRandom(Cols, Cols2);
|
||||
Matrix<Scalar, Rows, Cols2> rhs = matrix * exact_solution;
|
||||
Matrix<Scalar, Cols, Cols2> cod_solution = cod.solve(rhs);
|
||||
JacobiSVD<MatrixType, ComputeFullU | ComputeFullV> svd(matrix);
|
||||
JacobiSVD<MatrixType> svd(matrix, ComputeFullU | ComputeFullV);
|
||||
Matrix<Scalar, Cols, Cols2> svd_solution = svd.solve(rhs);
|
||||
VERIFY_IS_APPROX(cod_solution, svd_solution);
|
||||
|
||||
|
||||
@@ -16,10 +16,6 @@
|
||||
#error a macro SVD_FOR_MIN_NORM(MatrixType) must be defined prior to including svd_common.h
|
||||
#endif
|
||||
|
||||
#ifndef SVD_STATIC_OPTIONS
|
||||
#error a macro SVD_STATIC_OPTIONS(MatrixType, Options) must be defined prior to including svd_common.h
|
||||
#endif
|
||||
|
||||
#include "svd_fill.h"
|
||||
#include "solverbase.h"
|
||||
|
||||
@@ -59,8 +55,9 @@ void svd_check_full(const MatrixType& m, const SvdType& svd)
|
||||
}
|
||||
|
||||
// Compare partial SVD defined by computationOptions to a full SVD referenceSvd
|
||||
template<typename MatrixType, typename SvdType, int Options>
|
||||
template<typename SvdType, typename MatrixType>
|
||||
void svd_compare_to_full(const MatrixType& m,
|
||||
unsigned int computationOptions,
|
||||
const SvdType& referenceSvd)
|
||||
{
|
||||
typedef typename MatrixType::RealScalar RealScalar;
|
||||
@@ -69,18 +66,18 @@ void svd_compare_to_full(const MatrixType& m,
|
||||
Index diagSize = (std::min)(rows, cols);
|
||||
RealScalar prec = test_precision<RealScalar>();
|
||||
|
||||
SVD_STATIC_OPTIONS(MatrixType, Options) svd(m);
|
||||
SvdType svd(m, computationOptions);
|
||||
|
||||
VERIFY_IS_APPROX(svd.singularValues(), referenceSvd.singularValues());
|
||||
|
||||
if(Options & (ComputeFullV|ComputeThinV))
|
||||
if(computationOptions & (ComputeFullV|ComputeThinV))
|
||||
{
|
||||
VERIFY( (svd.matrixV().adjoint()*svd.matrixV()).isIdentity(prec) );
|
||||
VERIFY_IS_APPROX( svd.matrixV().leftCols(diagSize) * svd.singularValues().asDiagonal() * svd.matrixV().leftCols(diagSize).adjoint(),
|
||||
referenceSvd.matrixV().leftCols(diagSize) * referenceSvd.singularValues().asDiagonal() * referenceSvd.matrixV().leftCols(diagSize).adjoint());
|
||||
}
|
||||
|
||||
if(Options & (ComputeFullU|ComputeThinU))
|
||||
if(computationOptions & (ComputeFullU|ComputeThinU))
|
||||
{
|
||||
VERIFY( (svd.matrixU().adjoint()*svd.matrixU()).isIdentity(prec) );
|
||||
VERIFY_IS_APPROX( svd.matrixU().leftCols(diagSize) * svd.singularValues().cwiseAbs2().asDiagonal() * svd.matrixU().leftCols(diagSize).adjoint(),
|
||||
@@ -88,18 +85,19 @@ void svd_compare_to_full(const MatrixType& m,
|
||||
}
|
||||
|
||||
// The following checks are not critical.
|
||||
// For instance, with Dived&Conquer SVD, if only the factor 'V' is computed then different matrix-matrix product implementation will be used
|
||||
// For instance, with Dived&Conquer SVD, if only the factor 'V' is computedt then different matrix-matrix product implementation will be used
|
||||
// and the resulting 'V' factor might be significantly different when the SVD decomposition is not unique, especially with single precision float.
|
||||
++g_test_level;
|
||||
if(Options & ComputeFullU) VERIFY_IS_APPROX(svd.matrixU(), referenceSvd.matrixU());
|
||||
if(Options & ComputeThinU) VERIFY_IS_APPROX(svd.matrixU(), referenceSvd.matrixU().leftCols(diagSize));
|
||||
if(Options & ComputeFullV) VERIFY_IS_APPROX(svd.matrixV().cwiseAbs(), referenceSvd.matrixV().cwiseAbs());
|
||||
if(Options & ComputeThinV) VERIFY_IS_APPROX(svd.matrixV(), referenceSvd.matrixV().leftCols(diagSize));
|
||||
if(computationOptions & ComputeFullU) VERIFY_IS_APPROX(svd.matrixU(), referenceSvd.matrixU());
|
||||
if(computationOptions & ComputeThinU) VERIFY_IS_APPROX(svd.matrixU(), referenceSvd.matrixU().leftCols(diagSize));
|
||||
if(computationOptions & ComputeFullV) VERIFY_IS_APPROX(svd.matrixV().cwiseAbs(), referenceSvd.matrixV().cwiseAbs());
|
||||
if(computationOptions & ComputeThinV) VERIFY_IS_APPROX(svd.matrixV(), referenceSvd.matrixV().leftCols(diagSize));
|
||||
--g_test_level;
|
||||
}
|
||||
|
||||
//
|
||||
template<typename SvdType, typename MatrixType>
|
||||
void svd_least_square(const MatrixType& m)
|
||||
void svd_least_square(const MatrixType& m, unsigned int computationOptions)
|
||||
{
|
||||
typedef typename MatrixType::Scalar Scalar;
|
||||
typedef typename MatrixType::RealScalar RealScalar;
|
||||
@@ -115,7 +113,7 @@ void svd_least_square(const MatrixType& m)
|
||||
typedef Matrix<Scalar, ColsAtCompileTime, Dynamic> SolutionType;
|
||||
|
||||
RhsType rhs = RhsType::Random(rows, internal::random<Index>(1, cols));
|
||||
SvdType svd(m);
|
||||
SvdType svd(m, computationOptions);
|
||||
|
||||
if(internal::is_same<RealScalar,double>::value) svd.setThreshold(1e-8);
|
||||
else if(internal::is_same<RealScalar,float>::value) svd.setThreshold(2e-4);
|
||||
@@ -164,9 +162,9 @@ void svd_least_square(const MatrixType& m)
|
||||
}
|
||||
}
|
||||
|
||||
// check minimal norm solutions, the input matrix m is only used to recover problem size
|
||||
template<typename MatrixType, int Options>
|
||||
void svd_min_norm(const MatrixType& m)
|
||||
// check minimal norm solutions, the inoput matrix m is only used to recover problem size
|
||||
template<typename MatrixType>
|
||||
void svd_min_norm(const MatrixType& m, unsigned int computationOptions)
|
||||
{
|
||||
typedef typename MatrixType::Scalar Scalar;
|
||||
Index cols = m.cols();
|
||||
@@ -201,7 +199,7 @@ void svd_min_norm(const MatrixType& m)
|
||||
tmp.tail(cols-rank).setZero();
|
||||
SolutionType x21 = qr.householderQ() * tmp;
|
||||
// now check with SVD
|
||||
SVD_STATIC_OPTIONS(MatrixType2, Options) svd2(m2);
|
||||
SVD_FOR_MIN_NORM(MatrixType2) svd2(m2, computationOptions);
|
||||
SolutionType x22 = svd2.solve(rhs2);
|
||||
VERIFY_IS_APPROX(m2*x21, rhs2);
|
||||
VERIFY_IS_APPROX(m2*x22, rhs2);
|
||||
@@ -214,7 +212,7 @@ void svd_min_norm(const MatrixType& m)
|
||||
Matrix<Scalar,RowsAtCompileTime3,Dynamic> C = Matrix<Scalar,RowsAtCompileTime3,Dynamic>::Random(rows3,rank);
|
||||
MatrixType3 m3 = C * m2;
|
||||
RhsType3 rhs3 = C * rhs2;
|
||||
SVD_STATIC_OPTIONS(MatrixType3, Options) svd3(m3);
|
||||
SVD_FOR_MIN_NORM(MatrixType3) svd3(m3, computationOptions);
|
||||
SolutionType x3 = svd3.solve(rhs3);
|
||||
VERIFY_IS_APPROX(m3*x3, rhs3);
|
||||
VERIFY_IS_APPROX(m3*x21, rhs3);
|
||||
@@ -241,6 +239,57 @@ void svd_test_solvers(const MatrixType& m, const SolverType& solver) {
|
||||
check_solverbase<CMatrixType, MatrixType>(m, solver, rows, cols, cols2);
|
||||
}
|
||||
|
||||
// Check full, compare_to_full, least_square, and min_norm for all possible compute-options
|
||||
template<typename SvdType, typename MatrixType>
|
||||
void svd_test_all_computation_options(const MatrixType& m, bool full_only)
|
||||
{
|
||||
// if (QRPreconditioner == NoQRPreconditioner && m.rows() != m.cols())
|
||||
// return;
|
||||
STATIC_CHECK(( internal::is_same<typename SvdType::StorageIndex,int>::value ));
|
||||
|
||||
SvdType fullSvd(m, ComputeFullU|ComputeFullV);
|
||||
CALL_SUBTEST(( svd_check_full(m, fullSvd) ));
|
||||
CALL_SUBTEST(( svd_least_square<SvdType>(m, ComputeFullU | ComputeFullV) ));
|
||||
CALL_SUBTEST(( svd_min_norm(m, ComputeFullU | ComputeFullV) ));
|
||||
|
||||
#if defined __INTEL_COMPILER
|
||||
// remark #111: statement is unreachable
|
||||
#pragma warning disable 111
|
||||
#endif
|
||||
|
||||
svd_test_solvers(m, fullSvd);
|
||||
|
||||
if(full_only)
|
||||
return;
|
||||
|
||||
CALL_SUBTEST(( svd_compare_to_full(m, ComputeFullU, fullSvd) ));
|
||||
CALL_SUBTEST(( svd_compare_to_full(m, ComputeFullV, fullSvd) ));
|
||||
CALL_SUBTEST(( svd_compare_to_full(m, 0, fullSvd) ));
|
||||
|
||||
if (MatrixType::ColsAtCompileTime == Dynamic) {
|
||||
// thin U/V are only available with dynamic number of columns
|
||||
CALL_SUBTEST(( svd_compare_to_full(m, ComputeFullU|ComputeThinV, fullSvd) ));
|
||||
CALL_SUBTEST(( svd_compare_to_full(m, ComputeThinV, fullSvd) ));
|
||||
CALL_SUBTEST(( svd_compare_to_full(m, ComputeThinU|ComputeFullV, fullSvd) ));
|
||||
CALL_SUBTEST(( svd_compare_to_full(m, ComputeThinU , fullSvd) ));
|
||||
CALL_SUBTEST(( svd_compare_to_full(m, ComputeThinU|ComputeThinV, fullSvd) ));
|
||||
|
||||
CALL_SUBTEST(( svd_least_square<SvdType>(m, ComputeFullU | ComputeThinV) ));
|
||||
CALL_SUBTEST(( svd_least_square<SvdType>(m, ComputeThinU | ComputeFullV) ));
|
||||
CALL_SUBTEST(( svd_least_square<SvdType>(m, ComputeThinU | ComputeThinV) ));
|
||||
|
||||
CALL_SUBTEST(( svd_min_norm(m, ComputeFullU | ComputeThinV) ));
|
||||
CALL_SUBTEST(( svd_min_norm(m, ComputeThinU | ComputeFullV) ));
|
||||
CALL_SUBTEST(( svd_min_norm(m, ComputeThinU | ComputeThinV) ));
|
||||
|
||||
// test reconstruction
|
||||
Index diagSize = (std::min)(m.rows(), m.cols());
|
||||
SvdType svd(m, ComputeThinU | ComputeThinV);
|
||||
VERIFY_IS_APPROX(m, svd.matrixU().leftCols(diagSize) * svd.singularValues().asDiagonal() * svd.matrixV().leftCols(diagSize).adjoint());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 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>
|
||||
@@ -249,32 +298,31 @@ EIGEN_DONT_INLINE Scalar zero() { return Scalar(0); }
|
||||
// workaround aggressive optimization in ICC
|
||||
template<typename T> EIGEN_DONT_INLINE T sub(T a, T b) { return a - b; }
|
||||
|
||||
|
||||
// This function verifies we don't iterate infinitely on nan/inf values,
|
||||
// and that info() returns InvalidInput.
|
||||
template<typename MatrixType>
|
||||
template<typename SvdType, typename MatrixType>
|
||||
void svd_inf_nan()
|
||||
{
|
||||
SVD_STATIC_OPTIONS(MatrixType, ComputeFullU | ComputeFullV) svd;
|
||||
SvdType svd;
|
||||
typedef typename MatrixType::Scalar Scalar;
|
||||
Scalar some_inf = Scalar(1) / zero<Scalar>();
|
||||
VERIFY(sub(some_inf, some_inf) != sub(some_inf, some_inf));
|
||||
svd.compute(MatrixType::Constant(10,10,some_inf));
|
||||
svd.compute(MatrixType::Constant(10,10,some_inf), ComputeFullU | ComputeFullV);
|
||||
VERIFY(svd.info() == InvalidInput);
|
||||
|
||||
Scalar nan = std::numeric_limits<Scalar>::quiet_NaN();
|
||||
VERIFY(nan != nan);
|
||||
svd.compute(MatrixType::Constant(10,10,nan));
|
||||
svd.compute(MatrixType::Constant(10,10,nan), ComputeFullU | ComputeFullV);
|
||||
VERIFY(svd.info() == InvalidInput);
|
||||
|
||||
MatrixType m = MatrixType::Zero(10,10);
|
||||
m(internal::random<int>(0,9), internal::random<int>(0,9)) = some_inf;
|
||||
svd.compute(m);
|
||||
svd.compute(m, ComputeFullU | ComputeFullV);
|
||||
VERIFY(svd.info() == InvalidInput);
|
||||
|
||||
m = MatrixType::Zero(10,10);
|
||||
m(internal::random<int>(0,9), internal::random<int>(0,9)) = nan;
|
||||
svd.compute(m);
|
||||
svd.compute(m, ComputeFullU | ComputeFullV);
|
||||
VERIFY(svd.info() == InvalidInput);
|
||||
|
||||
// regression test for bug 791
|
||||
@@ -282,7 +330,7 @@ void svd_inf_nan()
|
||||
m << 0, 2*NumTraits<Scalar>::epsilon(), 0.5,
|
||||
0, -0.5, 0,
|
||||
nan, 0, 0;
|
||||
svd.compute(m);
|
||||
svd.compute(m, ComputeFullU | ComputeFullV);
|
||||
VERIFY(svd.info() == InvalidInput);
|
||||
|
||||
m.resize(4,4);
|
||||
@@ -290,7 +338,7 @@ void svd_inf_nan()
|
||||
0, 3, 1, 2e-308,
|
||||
1, 0, 1, nan,
|
||||
0, nan, nan, 0;
|
||||
svd.compute(m);
|
||||
svd.compute(m, ComputeFullU | ComputeFullV);
|
||||
VERIFY(svd.info() == InvalidInput);
|
||||
}
|
||||
|
||||
@@ -307,8 +355,8 @@ void svd_underoverflow()
|
||||
Matrix2d M;
|
||||
M << -7.90884e-313, -4.94e-324,
|
||||
0, 5.60844e-313;
|
||||
SVD_STATIC_OPTIONS(Matrix2d, ComputeFullU | ComputeFullV) svd;
|
||||
svd.compute(M);
|
||||
SVD_DEFAULT(Matrix2d) svd;
|
||||
svd.compute(M,ComputeFullU|ComputeFullV);
|
||||
CALL_SUBTEST( svd_check_full(M,svd) );
|
||||
|
||||
// Check all 2x2 matrices made with the following coefficients:
|
||||
@@ -319,7 +367,7 @@ void svd_underoverflow()
|
||||
do
|
||||
{
|
||||
M << value_set(id(0)), value_set(id(1)), value_set(id(2)), value_set(id(3));
|
||||
svd.compute(M);
|
||||
svd.compute(M,ComputeFullU|ComputeFullV);
|
||||
CALL_SUBTEST( svd_check_full(M,svd) );
|
||||
|
||||
id(k)++;
|
||||
@@ -342,13 +390,15 @@ void svd_underoverflow()
|
||||
3.7841695601406358e+307, 2.4331702789740617e+306, -3.5235707140272905e+307,
|
||||
-8.7190887618028355e+307, -7.3453213709232193e+307, -2.4367363684472105e+307;
|
||||
|
||||
SVD_STATIC_OPTIONS(Matrix3d, ComputeFullU|ComputeFullV) svd3;
|
||||
svd3.compute(M3); // just check we don't loop indefinitely
|
||||
SVD_DEFAULT(Matrix3d) svd3;
|
||||
svd3.compute(M3,ComputeFullU|ComputeFullV); // just check we don't loop indefinitely
|
||||
CALL_SUBTEST( svd_check_full(M3,svd3) );
|
||||
}
|
||||
|
||||
// void jacobisvd(const MatrixType& a = MatrixType(), bool pickrandom = true)
|
||||
|
||||
template<typename MatrixType>
|
||||
void svd_all_trivial_2x2( void (*cb)(const MatrixType&) )
|
||||
void svd_all_trivial_2x2( void (*cb)(const MatrixType&,bool) )
|
||||
{
|
||||
MatrixType M;
|
||||
VectorXd value_set(3);
|
||||
@@ -359,7 +409,7 @@ void svd_all_trivial_2x2( void (*cb)(const MatrixType&) )
|
||||
{
|
||||
M << value_set(id(0)), value_set(id(1)), value_set(id(2)), value_set(id(3));
|
||||
|
||||
cb(M);
|
||||
cb(M,false);
|
||||
|
||||
id(k)++;
|
||||
if(id(k)>=value_set.size())
|
||||
@@ -384,10 +434,22 @@ void svd_preallocate()
|
||||
internal::set_is_malloc_allowed(true);
|
||||
svd.compute(m);
|
||||
VERIFY_IS_APPROX(svd.singularValues(), v);
|
||||
VERIFY_RAISES_ASSERT(svd.matrixU());
|
||||
VERIFY_RAISES_ASSERT(svd.matrixV());
|
||||
|
||||
SVD_STATIC_OPTIONS(MatrixXf, ComputeFullU | ComputeFullV) svd2(3,3);
|
||||
SVD_DEFAULT(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);
|
||||
|
||||
SVD_DEFAULT(MatrixXf) svd3(3,3,ComputeFullU|ComputeFullV);
|
||||
internal::set_is_malloc_allowed(false);
|
||||
svd2.compute(m);
|
||||
internal::set_is_malloc_allowed(true);
|
||||
@@ -395,168 +457,65 @@ void svd_preallocate()
|
||||
VERIFY_IS_APPROX(svd2.matrixU(), Matrix3f::Identity());
|
||||
VERIFY_IS_APPROX(svd2.matrixV(), Matrix3f::Identity());
|
||||
internal::set_is_malloc_allowed(false);
|
||||
svd2.compute(m);
|
||||
svd2.compute(m, ComputeFullU|ComputeFullV);
|
||||
internal::set_is_malloc_allowed(true);
|
||||
}
|
||||
|
||||
template<typename MatrixType, int QRPreconditioner = 0>
|
||||
void svd_verify_assert_full_only(const MatrixType& m = MatrixType())
|
||||
template<typename SvdType,typename MatrixType>
|
||||
void svd_verify_assert(const MatrixType& m, bool fullOnly = false)
|
||||
{
|
||||
enum {
|
||||
RowsAtCompileTime = MatrixType::RowsAtCompileTime
|
||||
};
|
||||
|
||||
typedef Matrix<typename MatrixType::Scalar, RowsAtCompileTime, 1> RhsType;
|
||||
RhsType rhs = RhsType::Zero(m.rows());
|
||||
|
||||
SVD_STATIC_OPTIONS(MatrixType, QRPreconditioner) svd0;
|
||||
VERIFY_RAISES_ASSERT(( svd0.matrixU() ));
|
||||
VERIFY_RAISES_ASSERT(( svd0.singularValues() ));
|
||||
VERIFY_RAISES_ASSERT(( svd0.matrixV() ));
|
||||
VERIFY_RAISES_ASSERT(( svd0.solve(rhs) ));
|
||||
VERIFY_RAISES_ASSERT(( svd0.transpose().solve(rhs) ));
|
||||
VERIFY_RAISES_ASSERT(( svd0.adjoint().solve(rhs) ));
|
||||
|
||||
SVD_STATIC_OPTIONS(MatrixType, QRPreconditioner) svd1(m);
|
||||
VERIFY_RAISES_ASSERT(( svd1.matrixU() ));
|
||||
VERIFY_RAISES_ASSERT(( svd1.matrixV() ));
|
||||
VERIFY_RAISES_ASSERT(( svd1.solve(rhs)));
|
||||
|
||||
SVD_STATIC_OPTIONS(MatrixType, QRPreconditioner | ComputeFullU) svdFullU(m);
|
||||
VERIFY_RAISES_ASSERT(( svdFullU.matrixV() ));
|
||||
VERIFY_RAISES_ASSERT(( svdFullU.solve(rhs)));
|
||||
SVD_STATIC_OPTIONS(MatrixType, QRPreconditioner | ComputeFullV) svdFullV(m);
|
||||
VERIFY_RAISES_ASSERT(( svdFullV.matrixU() ));
|
||||
VERIFY_RAISES_ASSERT(( svdFullV.solve(rhs)));
|
||||
}
|
||||
|
||||
template<typename MatrixType, int QRPreconditioner = 0>
|
||||
void svd_verify_assert(const MatrixType& m = MatrixType())
|
||||
{
|
||||
enum {
|
||||
RowsAtCompileTime = MatrixType::RowsAtCompileTime
|
||||
};
|
||||
|
||||
typedef Matrix<typename MatrixType::Scalar, RowsAtCompileTime, 1> RhsType;
|
||||
RhsType rhs = RhsType::Zero(m.rows());
|
||||
|
||||
SVD_STATIC_OPTIONS(MatrixType, QRPreconditioner | ComputeThinU) svdThinU(m);
|
||||
VERIFY_RAISES_ASSERT(( svdThinU.matrixV() ));
|
||||
VERIFY_RAISES_ASSERT(( svdThinU.solve(rhs)));
|
||||
SVD_STATIC_OPTIONS(MatrixType, QRPreconditioner | ComputeThinV) svdThinV(m);
|
||||
VERIFY_RAISES_ASSERT(( svdThinV.matrixU() ));
|
||||
VERIFY_RAISES_ASSERT(( svdThinV.solve(rhs)));
|
||||
|
||||
svd_verify_assert_full_only<MatrixType, QRPreconditioner>(m);
|
||||
}
|
||||
|
||||
template<typename MatrixType, int Options>
|
||||
void svd_compute_checks(const MatrixType& m)
|
||||
{
|
||||
typedef SVD_STATIC_OPTIONS(MatrixType, Options) SVDType;
|
||||
typedef typename MatrixType::Scalar Scalar;
|
||||
Index rows = m.rows();
|
||||
Index cols = m.cols();
|
||||
|
||||
enum {
|
||||
RowsAtCompileTime = MatrixType::RowsAtCompileTime,
|
||||
ColsAtCompileTime = MatrixType::ColsAtCompileTime,
|
||||
DiagAtCompileTime = EIGEN_SIZE_MIN_PREFER_DYNAMIC(RowsAtCompileTime, ColsAtCompileTime),
|
||||
MatrixURowsAtCompileTime = SVDType::MatrixUType::RowsAtCompileTime,
|
||||
MatrixUColsAtCompileTime = SVDType::MatrixUType::ColsAtCompileTime,
|
||||
MatrixVRowsAtCompileTime = SVDType::MatrixVType::RowsAtCompileTime,
|
||||
MatrixVColsAtCompileTime = SVDType::MatrixVType::ColsAtCompileTime
|
||||
ColsAtCompileTime = MatrixType::ColsAtCompileTime
|
||||
};
|
||||
|
||||
SVDType staticSvd(m);
|
||||
|
||||
VERIFY(MatrixURowsAtCompileTime == RowsAtCompileTime);
|
||||
VERIFY(MatrixVRowsAtCompileTime == ColsAtCompileTime);
|
||||
if (Options & ComputeThinU) VERIFY(MatrixUColsAtCompileTime == DiagAtCompileTime);
|
||||
if (Options & ComputeFullU) VERIFY(MatrixUColsAtCompileTime == RowsAtCompileTime);
|
||||
if (Options & ComputeThinV) VERIFY(MatrixVColsAtCompileTime == DiagAtCompileTime);
|
||||
if (Options & ComputeFullV) VERIFY(MatrixVColsAtCompileTime == ColsAtCompileTime);
|
||||
typedef Matrix<Scalar, RowsAtCompileTime, 1> RhsType;
|
||||
RhsType rhs(rows);
|
||||
SvdType svd;
|
||||
VERIFY_RAISES_ASSERT(svd.matrixU())
|
||||
VERIFY_RAISES_ASSERT(svd.singularValues())
|
||||
VERIFY_RAISES_ASSERT(svd.matrixV())
|
||||
VERIFY_RAISES_ASSERT(svd.solve(rhs))
|
||||
VERIFY_RAISES_ASSERT(svd.transpose().solve(rhs))
|
||||
VERIFY_RAISES_ASSERT(svd.adjoint().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 (Options & (ComputeThinU|ComputeFullU)) VERIFY(staticSvd.computeU());
|
||||
else VERIFY(!staticSvd.computeU());
|
||||
if (Options & (ComputeThinV|ComputeFullV)) VERIFY(staticSvd.computeV());
|
||||
else VERIFY(!staticSvd.computeV());
|
||||
svd.compute(a, ComputeFullU);
|
||||
svd.matrixU();
|
||||
VERIFY_RAISES_ASSERT(svd.matrixV())
|
||||
VERIFY_RAISES_ASSERT(svd.solve(rhs))
|
||||
svd.compute(a, ComputeFullV);
|
||||
svd.matrixV();
|
||||
VERIFY_RAISES_ASSERT(svd.matrixU())
|
||||
VERIFY_RAISES_ASSERT(svd.solve(rhs))
|
||||
|
||||
if (staticSvd.computeU()) VERIFY(staticSvd.matrixU().isUnitary());
|
||||
if (staticSvd.computeV()) VERIFY(staticSvd.matrixV().isUnitary());
|
||||
|
||||
if (staticSvd.computeU() && staticSvd.computeV())
|
||||
if (!fullOnly && ColsAtCompileTime == Dynamic)
|
||||
{
|
||||
svd_test_solvers(m, staticSvd);
|
||||
svd_least_square<SVDType, MatrixType>(m);
|
||||
// svd_min_norm generates non-square matrices so it can't be used with NoQRPreconditioner
|
||||
if ((Options & internal::QRPreconditionerBits) != NoQRPreconditioner)
|
||||
svd_min_norm<MatrixType, Options>(m);
|
||||
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))
|
||||
}
|
||||
else
|
||||
{
|
||||
VERIFY_RAISES_ASSERT(svd.compute(a, ComputeThinU))
|
||||
VERIFY_RAISES_ASSERT(svd.compute(a, ComputeThinV))
|
||||
}
|
||||
}
|
||||
|
||||
template<typename MatrixType, int QRPreconditioner = 0>
|
||||
void svd_option_checks(const MatrixType& m)
|
||||
{
|
||||
// singular values only
|
||||
svd_compute_checks<MatrixType, QRPreconditioner>(m);
|
||||
// Thin only
|
||||
svd_compute_checks<MatrixType, QRPreconditioner | ComputeThinU >(m);
|
||||
svd_compute_checks<MatrixType, QRPreconditioner | ComputeThinV >(m);
|
||||
svd_compute_checks<MatrixType, QRPreconditioner | ComputeThinU | ComputeThinV>(m);
|
||||
// Full only
|
||||
svd_compute_checks<MatrixType, QRPreconditioner | ComputeFullU >(m);
|
||||
svd_compute_checks<MatrixType, QRPreconditioner | ComputeFullV >(m);
|
||||
svd_compute_checks<MatrixType, QRPreconditioner | ComputeFullU | ComputeFullV>(m);
|
||||
// Mixed
|
||||
svd_compute_checks<MatrixType, QRPreconditioner | ComputeThinU | ComputeFullV>(m);
|
||||
svd_compute_checks<MatrixType, QRPreconditioner | ComputeFullU | ComputeThinV>(m);
|
||||
|
||||
typedef SVD_STATIC_OPTIONS(MatrixType, QRPreconditioner | ComputeFullU | ComputeFullV) FullSvdType;
|
||||
FullSvdType fullSvd(m);
|
||||
svd_check_full(m, fullSvd);
|
||||
svd_compare_to_full<MatrixType, FullSvdType, QRPreconditioner | ComputeFullU | ComputeFullV>(m, fullSvd);
|
||||
}
|
||||
|
||||
template<typename MatrixType, int QRPreconditioner = 0>
|
||||
void svd_option_checks_full_only(const MatrixType& m)
|
||||
{
|
||||
svd_compute_checks<MatrixType, QRPreconditioner | ComputeFullU>(m);
|
||||
svd_compute_checks<MatrixType, QRPreconditioner | ComputeFullV>(m);
|
||||
svd_compute_checks<MatrixType, QRPreconditioner | ComputeFullU | ComputeFullV>(m);
|
||||
|
||||
SVD_STATIC_OPTIONS(MatrixType, QRPreconditioner | ComputeFullU | ComputeFullV) fullSvd(m);
|
||||
svd_check_full(m, fullSvd);
|
||||
}
|
||||
|
||||
template<typename MatrixType, int QRPreconditioner = 0>
|
||||
void svd_check_max_size_matrix(int initialRows, int initialCols)
|
||||
{
|
||||
enum {
|
||||
MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
|
||||
MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
|
||||
};
|
||||
|
||||
int rows = MaxRowsAtCompileTime == Dynamic ? initialRows : (std::min)(initialRows, (int)MaxRowsAtCompileTime);
|
||||
int cols = MaxColsAtCompileTime == Dynamic ? initialCols : (std::min)(initialCols, (int)MaxColsAtCompileTime);
|
||||
|
||||
MatrixType m(rows, cols);
|
||||
SVD_STATIC_OPTIONS(MatrixType, QRPreconditioner | ComputeThinU | ComputeThinV) thinSvd(m);
|
||||
SVD_STATIC_OPTIONS(MatrixType, QRPreconditioner | ComputeThinU | ComputeFullV) mixedSvd1(m);
|
||||
SVD_STATIC_OPTIONS(MatrixType, QRPreconditioner | ComputeFullU | ComputeThinV) mixedSvd2(m);
|
||||
SVD_STATIC_OPTIONS(MatrixType, QRPreconditioner | ComputeFullU | ComputeFullV) fullSvd(m);
|
||||
|
||||
MatrixType n(MaxRowsAtCompileTime, MaxColsAtCompileTime);
|
||||
thinSvd.compute(n);
|
||||
mixedSvd1.compute(n);
|
||||
mixedSvd2.compute(n);
|
||||
fullSvd.compute(n);
|
||||
|
||||
MatrixX<typename MatrixType::Scalar> dynamicMatrix(MaxRowsAtCompileTime + 1, MaxColsAtCompileTime + 1);
|
||||
|
||||
VERIFY_RAISES_ASSERT(thinSvd.compute(dynamicMatrix));
|
||||
VERIFY_RAISES_ASSERT(mixedSvd1.compute(dynamicMatrix));
|
||||
VERIFY_RAISES_ASSERT(mixedSvd2.compute(dynamicMatrix));
|
||||
VERIFY_RAISES_ASSERT(fullSvd.compute(dynamicMatrix));
|
||||
}
|
||||
|
||||
#undef SVD_DEFAULT
|
||||
#undef SVD_FOR_MIN_NORM
|
||||
#undef SVD_STATIC_OPTIONS
|
||||
|
||||
Reference in New Issue
Block a user