// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2008-2014 Gael Guennebaud // Copyright (C) 2009 Benoit Jacob // // 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/. // discard stack allocation as that too bypasses malloc #define EIGEN_STACK_ALLOCATION_LIMIT 0 #define EIGEN_RUNTIME_NO_MALLOC #include "main.h" #include #define SVD_DEFAULT(M) JacobiSVD #define SVD_FOR_MIN_NORM(M) JacobiSVD #include "svd_common.h" // Check all variants of JacobiSVD template 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 >(m, true) )); // check full only CALL_SUBTEST(( svd_test_all_computation_options >(m, false) )); CALL_SUBTEST(( svd_test_all_computation_options >(m, false) )); if(m.rows()==m.cols()) CALL_SUBTEST(( svd_test_all_computation_options >(m, false) )); } template void jacobisvd_verify_assert(const MatrixType& m) { svd_verify_assert >(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 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 void jacobisvd_method() { enum { Size = MatrixType::RowsAtCompileTime }; typedef typename MatrixType::RealScalar RealScalar; typedef Matrix 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); VERIFY_IS_APPROX(m.jacobiSvd(ComputeFullU|ComputeFullV).transpose().solve(m), m); VERIFY_IS_APPROX(m.jacobiSvd(ComputeFullU|ComputeFullV).adjoint().solve(m), m); } namespace Foo { // older compiler require a default constructor for Bar // cf: https://stackoverflow.com/questions/7411515/ class Bar {public: Bar() {}}; 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); } EIGEN_DECLARE_TEST(jacobisvd) { 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)); CALL_SUBTEST_12(svd_all_trivial_2x2(jacobisvd)); for(int i = 0; i < g_repeat; i++) { CALL_SUBTEST_3(( jacobisvd() )); CALL_SUBTEST_4(( jacobisvd() )); CALL_SUBTEST_5(( jacobisvd >() )); CALL_SUBTEST_6(( jacobisvd >(Matrix(10,2)) )); int r = internal::random(1, 30), c = internal::random(1, 30); TEST_SET_BUT_UNUSED_VARIABLE(r) TEST_SET_BUT_UNUSED_VARIABLE(c) CALL_SUBTEST_10(( jacobisvd(MatrixXd(r,c)) )); CALL_SUBTEST_7(( jacobisvd(MatrixXf(r,c)) )); CALL_SUBTEST_8(( jacobisvd(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>()) ); // bug1395 test compile-time vectors as input CALL_SUBTEST_13(( jacobisvd_verify_assert(Matrix()) )); CALL_SUBTEST_13(( jacobisvd_verify_assert(Matrix()) )); CALL_SUBTEST_13(( jacobisvd_verify_assert(Matrix(r)) )); CALL_SUBTEST_13(( jacobisvd_verify_assert(Matrix(c)) )); } CALL_SUBTEST_7(( jacobisvd(MatrixXf(internal::random(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE/2), internal::random(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE/2))) )); CALL_SUBTEST_8(( jacobisvd(MatrixXcd(internal::random(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE/3), internal::random(EIGEN_TEST_MAX_SIZE/4, EIGEN_TEST_MAX_SIZE/3))) )); // test matrixbase method CALL_SUBTEST_1(( jacobisvd_method() )); CALL_SUBTEST_3(( jacobisvd_method() )); // Test problem size constructors CALL_SUBTEST_7( JacobiSVD(10,10) ); // Check that preallocation avoids subsequent mallocs CALL_SUBTEST_9( svd_preallocate() ); CALL_SUBTEST_2( svd_underoverflow() ); msvc_workaround(); }