// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2008 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 template void jacobisvd_check_full(const MatrixType& m, const JacobiSVD& svd) { typedef typename MatrixType::Index Index; Index rows = m.rows(); Index cols = m.cols(); enum { RowsAtCompileTime = MatrixType::RowsAtCompileTime, ColsAtCompileTime = MatrixType::ColsAtCompileTime }; typedef typename MatrixType::Scalar Scalar; typedef typename NumTraits::Real RealScalar; typedef Matrix MatrixUType; typedef Matrix MatrixVType; typedef Matrix ColVectorType; typedef Matrix InputVectorType; MatrixType sigma = MatrixType::Zero(rows,cols); sigma.diagonal() = svd.singularValues().template cast(); MatrixUType u = svd.matrixU(); MatrixVType v = svd.matrixV(); VERIFY_IS_APPROX(m, u * sigma * v.adjoint()); VERIFY_IS_UNITARY(u); VERIFY_IS_UNITARY(v); } template void jacobisvd_compare_to_full(const MatrixType& m, unsigned int computationOptions, const JacobiSVD& referenceSvd) { typedef typename MatrixType::Index Index; Index rows = m.rows(); Index cols = m.cols(); Index diagSize = (std::min)(rows, cols); JacobiSVD svd(m, computationOptions); VERIFY_IS_APPROX(svd.singularValues(), referenceSvd.singularValues()); 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(), referenceSvd.matrixV()); if(computationOptions & ComputeThinV) VERIFY_IS_APPROX(svd.matrixV(), referenceSvd.matrixV().leftCols(diagSize)); } template void jacobisvd_solve(const MatrixType& m, unsigned int computationOptions) { 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 RhsType; typedef Matrix SolutionType; RhsType rhs = RhsType::Random(rows, internal::random(1, cols)); JacobiSVD svd(m, computationOptions); SolutionType x = svd.solve(rhs); // evaluate normal equation which works also for least-squares solutions VERIFY_IS_APPROX(m.adjoint()*m*x,m.adjoint()*rhs); } template void jacobisvd_test_all_computation_options(const MatrixType& m) { if (QRPreconditioner == NoQRPreconditioner && m.rows() != m.cols()) return; JacobiSVD fullSvd(m, ComputeFullU|ComputeFullV); jacobisvd_check_full(m, fullSvd); jacobisvd_solve(m, ComputeFullU | ComputeFullV); if(QRPreconditioner == FullPivHouseholderQRPreconditioner) return; jacobisvd_compare_to_full(m, ComputeFullU, fullSvd); jacobisvd_compare_to_full(m, ComputeFullV, fullSvd); jacobisvd_compare_to_full(m, 0, fullSvd); if (MatrixType::ColsAtCompileTime == Dynamic) { // thin U/V are only available with dynamic number of columns jacobisvd_compare_to_full(m, ComputeFullU|ComputeThinV, fullSvd); jacobisvd_compare_to_full(m, ComputeThinV, fullSvd); jacobisvd_compare_to_full(m, ComputeThinU|ComputeFullV, fullSvd); jacobisvd_compare_to_full(m, ComputeThinU , fullSvd); jacobisvd_compare_to_full(m, ComputeThinU|ComputeThinV, fullSvd); jacobisvd_solve(m, ComputeFullU | ComputeThinV); jacobisvd_solve(m, ComputeThinU | ComputeFullV); jacobisvd_solve(m, ComputeThinU | ComputeThinV); // test reconstruction typedef typename MatrixType::Index Index; Index diagSize = (std::min)(m.rows(), m.cols()); JacobiSVD svd(m, ComputeThinU | ComputeThinV); VERIFY_IS_APPROX(m, svd.matrixU().leftCols(diagSize) * svd.singularValues().asDiagonal() * svd.matrixV().leftCols(diagSize).adjoint()); } } template void jacobisvd(const MatrixType& a = MatrixType(), bool pickrandom = true) { MatrixType m = pickrandom ? MatrixType::Random(a.rows(), a.cols()) : a; jacobisvd_test_all_computation_options(m); jacobisvd_test_all_computation_options(m); jacobisvd_test_all_computation_options(m); jacobisvd_test_all_computation_options(m); } template void jacobisvd_verify_assert(const MatrixType& m) { 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 RhsType; RhsType rhs(rows); JacobiSVD svd; VERIFY_RAISES_ASSERT(svd.matrixU()) VERIFY_RAISES_ASSERT(svd.singularValues()) VERIFY_RAISES_ASSERT(svd.matrixV()) 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 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)) } } 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); } // 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 EIGEN_DONT_INLINE Scalar zero() { return Scalar(0); } // workaround aggressive optimization in ICC template EIGEN_DONT_INLINE T sub(T a, T b) { return a - b; } template void jacobisvd_inf_nan() { // all this function does is verify we don't iterate infinitely on nan/inf values JacobiSVD svd; typedef typename MatrixType::Scalar Scalar; Scalar some_inf = Scalar(1) / zero(); VERIFY(sub(some_inf, some_inf) != sub(some_inf, some_inf)); svd.compute(MatrixType::Constant(10,10,some_inf), ComputeFullU | ComputeFullV); Scalar some_nan = zero() / zero(); VERIFY(some_nan != some_nan); svd.compute(MatrixType::Constant(10,10,some_nan), ComputeFullU | ComputeFullV); MatrixType m = MatrixType::Zero(10,10); m(internal::random(0,9), internal::random(0,9)) = some_inf; svd.compute(m, ComputeFullU | ComputeFullV); m = MatrixType::Zero(10,10); m(internal::random(0,9), internal::random(0,9)) = some_nan; svd.compute(m, ComputeFullU | ComputeFullV); } // Regression test for bug 286: JacobiSVD loops indefinitely with some // matrices containing denormal numbers. void jacobisvd_bug286() { #if defined __INTEL_COMPILER // shut up warning #239: floating point underflow #pragma warning push #pragma warning disable 239 #endif Matrix2d M; M << -7.90884e-313, -4.94e-324, 0, 5.60844e-313; #if defined __INTEL_COMPILER #pragma warning pop #endif JacobiSVD svd; svd.compute(M); // just check we don't loop indefinitely } void jacobisvd_preallocate() { Vector3f v(3.f, 2.f, 1.f); MatrixXf m = v.asDiagonal(); internal::set_is_malloc_allowed(false); VERIFY_RAISES_ASSERT(VectorXf v(10);) JacobiSVD svd; internal::set_is_malloc_allowed(true); svd.compute(m); VERIFY_IS_APPROX(svd.singularValues(), v); JacobiSVD 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 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); } void 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)) )); for(int i = 0; i < g_repeat; i++) { Matrix2cd m; m << 0, 1, 0, 1; CALL_SUBTEST_1(( jacobisvd(m, false) )); m << 1, 0, 1, 0; CALL_SUBTEST_1(( jacobisvd(m, false) )); Matrix2d n; n << 0, 0, 0, 0; CALL_SUBTEST_2(( jacobisvd(n, false) )); n << 0, 0, 0, 1; CALL_SUBTEST_2(( jacobisvd(n, false) )); 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); 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( jacobisvd_inf_nan() ); } 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( jacobisvd_preallocate() ); // Regression check for bug 286 CALL_SUBTEST_2( jacobisvd_bug286() ); }