// 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 #define SVD_STATIC_OPTIONS(M, O) JacobiSVD #include "svd_common.h" 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.template jacobiSvd().solve(m), m); VERIFY_IS_APPROX(m.template jacobiSvd().transpose().solve(m), m); VERIFY_IS_APPROX(m.template jacobiSvd().adjoint().solve(m), m); } template void jacobisvd_all_options(const MatrixType& input = MatrixType()) { MatrixType m = input; svd_fill_random(m); svd_option_checks(m); svd_option_checks(m); svd_option_checks(m); svd_option_checks_full_only(m); // FullPiv only used when computing full unitaries } template void jacobisvd_verify_assert(const MatrixType& m = MatrixType()) { svd_verify_assert(m); svd_verify_assert(m); svd_verify_assert(m); svd_verify_assert_full_only(m); } template void jacobisvd_verify_inputs(const MatrixType& m = MatrixType()) { // check defaults typedef JacobiSVD 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::QRPreconditioner == (int)ColPivHouseholderQRPreconditioner )); VERIFY(( (int)JacobiSVD::QRPreconditioner == (int)ColPivHouseholderQRPreconditioner )); VERIFY(( (int)JacobiSVD::QRPreconditioner == (int)ColPivHouseholderQRPreconditioner )); VERIFY(( (int)JacobiSVD::QRPreconditioner == (int)ColPivHouseholderQRPreconditioner )); VERIFY(( (int)JacobiSVD::QRPreconditioner == (int)ColPivHouseholderQRPreconditioner )); VERIFY(( (int)JacobiSVD::QRPreconditioner == (int)ColPivHouseholderQRPreconditioner )); } 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_4(( jacobisvd_verify_inputs() )); CALL_SUBTEST_7(( jacobisvd_verify_inputs(Matrix(10, 12)) )); CALL_SUBTEST_8(( jacobisvd_verify_inputs, 7, 5> >() )); CALL_SUBTEST_3(( jacobisvd_verify_assert() )); CALL_SUBTEST_4(( jacobisvd_verify_assert() )); CALL_SUBTEST_7(( jacobisvd_verify_assert>() )); CALL_SUBTEST_7(( jacobisvd_verify_assert>() )); 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)); CALL_SUBTEST_12(svd_all_trivial_2x2(jacobisvd_all_options)); for(int i = 0; i < g_repeat; i++) { int r = internal::random(1, 30), c = internal::random(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() )); CALL_SUBTEST_3(( jacobisvd_all_options >() )); CALL_SUBTEST_4(( jacobisvd_all_options() )); CALL_SUBTEST_4(( jacobisvd_all_options >() )); CALL_SUBTEST_4(( jacobisvd_all_options >() )); CALL_SUBTEST_5(( jacobisvd_all_options >(Matrix(r, 16)) )); CALL_SUBTEST_5(( jacobisvd_all_options >(Matrix(10, c)) )); CALL_SUBTEST_7(( jacobisvd_all_options( MatrixXf(r, c)) )); CALL_SUBTEST_8(( jacobisvd_all_options( MatrixXcd(r, c)) )); CALL_SUBTEST_10(( jacobisvd_all_options( MatrixXd(r, c)) )); CALL_SUBTEST_14(( jacobisvd_all_options>() )); CALL_SUBTEST_14(( jacobisvd_all_options>() )); MatrixXcd noQRTest = MatrixXcd(r, r); svd_fill_random(noQRTest); CALL_SUBTEST_16(( svd_option_checks(noQRTest) )); CALL_SUBTEST_15(( svd_check_max_size_matrix, ColPivHouseholderQRPreconditioner>(r, c) )); CALL_SUBTEST_15(( svd_check_max_size_matrix, HouseholderQRPreconditioner>(r, c) )); CALL_SUBTEST_15(( svd_check_max_size_matrix, ColPivHouseholderQRPreconditioner>(r, c) )); CALL_SUBTEST_15(( svd_check_max_size_matrix, HouseholderQRPreconditioner>(r, c) )); // Test on inf/nan matrix CALL_SUBTEST_7( (svd_inf_nan()) ); CALL_SUBTEST_10( (svd_inf_nan()) ); CALL_SUBTEST_13(( jacobisvd_verify_assert>() )); CALL_SUBTEST_13(( jacobisvd_verify_assert>() )); CALL_SUBTEST_13(( jacobisvd_verify_assert>(Matrix(r)) )); CALL_SUBTEST_13(( jacobisvd_verify_assert>(Matrix(c)) )); } CALL_SUBTEST_7(( jacobisvd_all_options(MatrixXd(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_all_options(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(); }