2009-09-01 10:26:15 +08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
// for linear algebra.
|
|
|
|
//
|
2014-09-19 21:25:48 +08:00
|
|
|
// Copyright (C) 2008-2014 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>
|
|
|
|
|
2014-09-19 21:25:48 +08:00
|
|
|
#define SVD_DEFAULT(M) JacobiSVD<M>
|
|
|
|
#define SVD_FOR_MIN_NORM(M) JacobiSVD<M,ColPivHouseholderQRPreconditioner>
|
|
|
|
#include "svd_common.h"
|
2010-10-08 22:42:40 +08:00
|
|
|
|
2014-09-19 21:25:48 +08:00
|
|
|
// Check all variants of JacobiSVD
|
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)
|
2014-09-19 21:25:48 +08:00
|
|
|
svd_fill_random(m);
|
2010-10-12 03:36:04 +08:00
|
|
|
|
2014-09-19 21:25:48 +08:00
|
|
|
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) ));
|
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
|
|
|
{
|
2014-09-19 21:25:48 +08:00
|
|
|
svd_verify_assert<JacobiSVD<MatrixType> >(m);
|
2010-10-12 03:36:04 +08:00
|
|
|
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))
|
|
|
|
}
|
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);
|
2019-01-17 08:17:39 +08:00
|
|
|
VERIFY_IS_APPROX(m.jacobiSvd(ComputeFullU|ComputeFullV).transpose().solve(m), m);
|
|
|
|
VERIFY_IS_APPROX(m.jacobiSvd(ComputeFullU|ComputeFullV).adjoint().solve(m), m);
|
2010-10-17 21:40:52 +08:00
|
|
|
}
|
|
|
|
|
2018-11-21 22:59:47 +08:00
|
|
|
namespace Foo {
|
2018-11-23 21:24:22 +08:00
|
|
|
// older compiler require a default constructor for Bar
|
|
|
|
// cf: https://stackoverflow.com/questions/7411515/
|
|
|
|
class Bar {public: Bar() {}};
|
2018-11-21 22:59:47 +08:00
|
|
|
bool operator<(const Bar&, const Bar&) { return true; }
|
|
|
|
}
|
|
|
|
// regression test for a very strange MSVC issue for which simply
|
|
|
|
// including SVDBase.h messes up with std::max and custom scalar type
|
|
|
|
void msvc_workaround()
|
|
|
|
{
|
|
|
|
const Foo::Bar a;
|
|
|
|
const Foo::Bar b;
|
|
|
|
std::max EIGEN_NOT_A_MACRO (a,b);
|
|
|
|
}
|
|
|
|
|
2018-07-17 20:46:15 +08:00
|
|
|
EIGEN_DECLARE_TEST(jacobisvd)
|
2009-09-01 10:26:15 +08:00
|
|
|
{
|
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)) ));
|
2014-09-19 21:25:48 +08:00
|
|
|
|
2014-10-30 00:46:54 +08:00
|
|
|
CALL_SUBTEST_11(svd_all_trivial_2x2(jacobisvd<Matrix2cd>));
|
|
|
|
CALL_SUBTEST_12(svd_all_trivial_2x2(jacobisvd<Matrix2d>));
|
2010-10-12 03:36:04 +08:00
|
|
|
|
2009-09-01 10:26:15 +08:00
|
|
|
for(int i = 0; i < g_repeat; i++) {
|
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
|
2014-09-19 21:25:48 +08:00
|
|
|
CALL_SUBTEST_7( (svd_inf_nan<JacobiSVD<MatrixXf>, MatrixXf>()) );
|
|
|
|
CALL_SUBTEST_10( (svd_inf_nan<JacobiSVD<MatrixXd>, MatrixXd>()) );
|
2017-02-20 20:44:37 +08:00
|
|
|
|
|
|
|
// 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)) ));
|
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
|
2015-06-22 15:58:06 +08:00
|
|
|
CALL_SUBTEST_9( svd_preallocate<void>() );
|
2011-09-27 21:25:02 +08:00
|
|
|
|
2015-06-22 15:58:06 +08:00
|
|
|
CALL_SUBTEST_2( svd_underoverflow<void>() );
|
2018-11-21 22:59:47 +08:00
|
|
|
|
|
|
|
msvc_workaround();
|
2009-09-01 10:26:15 +08:00
|
|
|
}
|