void test_bdcsvd() { 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<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_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<BDCSVD<MatrixXf>, MatrixXf>()) ); CALL_SUBTEST_10( (svd_inf_nan<BDCSVD<MatrixXd>, MatrixXd>()) ); } // test matrixbase method CALL_SUBTEST_1(( bdcsvd_method<Matrix2cd>() )); CALL_SUBTEST_3(( bdcsvd_method<Matrix3f>() )); // Test problem size constructors CALL_SUBTEST_7( BDCSVD<MatrixXf>(10,10) ); // Check that preallocation avoids subsequent mallocs // Disbaled because not supported by BDCSVD // CALL_SUBTEST_9( svd_preallocate<void>() ); CALL_SUBTEST_2( svd_underoverflow<void>() ); }
void test_boostmultiprec() { typedef Matrix<Real,Dynamic,Dynamic> Mat; typedef Matrix<std::complex<Real>,Dynamic,Dynamic> MatC; std::cout << "NumTraits<Real>::epsilon() = " << NumTraits<Real>::epsilon() << std::endl; std::cout << "NumTraits<Real>::dummy_precision() = " << NumTraits<Real>::dummy_precision() << std::endl; std::cout << "NumTraits<Real>::lowest() = " << NumTraits<Real>::lowest() << std::endl; std::cout << "NumTraits<Real>::highest() = " << NumTraits<Real>::highest() << std::endl; std::cout << "NumTraits<Real>::digits10() = " << NumTraits<Real>::digits10() << std::endl; // chekc stream output { Mat A(10,10); A.setRandom(); std::stringstream ss; ss << A; } { MatC A(10,10); A.setRandom(); std::stringstream ss; ss << A; } for(int i = 0; i < g_repeat; i++) { int s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE); CALL_SUBTEST_1( cholesky(Mat(s,s)) ); CALL_SUBTEST_2( lu_non_invertible<Mat>() ); CALL_SUBTEST_2( lu_invertible<Mat>() ); CALL_SUBTEST_2( lu_non_invertible<MatC>() ); CALL_SUBTEST_2( lu_invertible<MatC>() ); CALL_SUBTEST_3( qr(Mat(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) ); CALL_SUBTEST_3( qr_invertible<Mat>() ); CALL_SUBTEST_4( qr<Mat>() ); CALL_SUBTEST_4( cod<Mat>() ); CALL_SUBTEST_4( qr_invertible<Mat>() ); CALL_SUBTEST_5( qr<Mat>() ); CALL_SUBTEST_5( qr_invertible<Mat>() ); CALL_SUBTEST_6( selfadjointeigensolver(Mat(s,s)) ); CALL_SUBTEST_7( eigensolver(Mat(s,s)) ); CALL_SUBTEST_8( generalized_eigensolver_real(Mat(s,s)) ); TEST_SET_BUT_UNUSED_VARIABLE(s) } 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 the tests void test_bdcsvd() { // test of Dynamic defined Matrix (42, 42) of float CALL_SUBTEST_11(( bdcsvd_verify_assert<Matrix<float,Dynamic,Dynamic> > (Matrix<float,Dynamic,Dynamic>(42,42)) )); CALL_SUBTEST_11(( compare_bdc_jacobi<Matrix<float,Dynamic,Dynamic> > (Matrix<float,Dynamic,Dynamic>(42,42), 0) )); CALL_SUBTEST_11(( bdcsvd<Matrix<float,Dynamic,Dynamic> > (Matrix<float,Dynamic,Dynamic>(42,42)) )); // test of Dynamic defined Matrix (50, 50) of double CALL_SUBTEST_13(( bdcsvd_verify_assert<Matrix<double,Dynamic,Dynamic> > (Matrix<double,Dynamic,Dynamic>(50,50)) )); CALL_SUBTEST_13(( compare_bdc_jacobi<Matrix<double,Dynamic,Dynamic> > (Matrix<double,Dynamic,Dynamic>(50,50), 0) )); CALL_SUBTEST_13(( bdcsvd<Matrix<double,Dynamic,Dynamic> > (Matrix<double,Dynamic,Dynamic>(50, 50)) )); // test of Dynamic defined Matrix (22, 22) of complex double CALL_SUBTEST_14(( bdcsvd_verify_assert<Matrix<std::complex<double>,Dynamic,Dynamic> > (Matrix<std::complex<double>,Dynamic,Dynamic>(22,22)) )); CALL_SUBTEST_14(( compare_bdc_jacobi<Matrix<std::complex<double>,Dynamic,Dynamic> > (Matrix<std::complex<double>, Dynamic, Dynamic> (22,22), 0) )); CALL_SUBTEST_14(( bdcsvd<Matrix<std::complex<double>,Dynamic,Dynamic> > (Matrix<std::complex<double>,Dynamic,Dynamic>(22, 22)) )); // test of Dynamic defined Matrix (10, 10) of int //CALL_SUBTEST_15(( bdcsvd_verify_assert<Matrix<int,Dynamic,Dynamic> > // (Matrix<int,Dynamic,Dynamic>(10,10)) )); //CALL_SUBTEST_15(( compare_bdc_jacobi<Matrix<int,Dynamic,Dynamic> > // (Matrix<int,Dynamic,Dynamic>(10,10), 0) )); //CALL_SUBTEST_15(( bdcsvd<Matrix<int,Dynamic,Dynamic> > // (Matrix<int,Dynamic,Dynamic>(10, 10)) )); // test of Dynamic defined Matrix (8, 6) of double CALL_SUBTEST_16(( bdcsvd_verify_assert<Matrix<double,Dynamic,Dynamic> > (Matrix<double,Dynamic,Dynamic>(8,6)) )); CALL_SUBTEST_16(( compare_bdc_jacobi<Matrix<double,Dynamic,Dynamic> > (Matrix<double,Dynamic,Dynamic>(8, 6), 0) )); CALL_SUBTEST_16(( bdcsvd<Matrix<double,Dynamic,Dynamic> > (Matrix<double,Dynamic,Dynamic>(8, 6)) )); // test of Dynamic defined Matrix (36, 12) of float CALL_SUBTEST_17(( compare_bdc_jacobi<Matrix<float,Dynamic,Dynamic> > (Matrix<float,Dynamic,Dynamic>(36, 12), 0) )); CALL_SUBTEST_17(( bdcsvd<Matrix<float,Dynamic,Dynamic> > (Matrix<float,Dynamic,Dynamic>(36, 12)) )); // test of Dynamic defined Matrix (5, 8) of double CALL_SUBTEST_18(( compare_bdc_jacobi<Matrix<double,Dynamic,Dynamic> > (Matrix<double,Dynamic,Dynamic>(5, 8), 0) )); CALL_SUBTEST_18(( bdcsvd<Matrix<double,Dynamic,Dynamic> > (Matrix<double,Dynamic,Dynamic>(5, 8)) )); // non regression tests CALL_SUBTEST_3(( bdcsvd_verify_assert(Matrix3f()) )); CALL_SUBTEST_4(( bdcsvd_verify_assert(Matrix4d()) )); CALL_SUBTEST_7(( bdcsvd_verify_assert(MatrixXf(10,12)) )); CALL_SUBTEST_8(( bdcsvd_verify_assert(MatrixXcd(7,5)) )); // SUBTESTS 1 and 2 on specifics matrix for(int i = 0; i < g_repeat; i++) { Matrix2cd m; m << 0, 1, 0, 1; CALL_SUBTEST_1(( bdcsvd(m, false) )); m << 1, 0, 1, 0; CALL_SUBTEST_1(( bdcsvd(m, false) )); Matrix2d n; n << 0, 0, 0, 0; CALL_SUBTEST_2(( bdcsvd(n, false) )); n << 0, 0, 0, 1; CALL_SUBTEST_2(( bdcsvd(n, false) )); // Statics matrix don't work with BDSVD yet // bdc algo on a random 3x3 float matrix // CALL_SUBTEST_3(( bdcsvd<Matrix3f>() )); // bdc algo on a random 4x4 double matrix // CALL_SUBTEST_4(( bdcsvd<Matrix4d>() )); // bdc algo on a random 3x5 float matrix // CALL_SUBTEST_5(( bdcsvd<Matrix<float,3,5> >() )); int r = internal::random<int>(1, 30), c = internal::random<int>(1, 30); CALL_SUBTEST_7(( bdcsvd<MatrixXf>(MatrixXf(r,c)) )); CALL_SUBTEST_8(( bdcsvd<MatrixXcd>(MatrixXcd(r,c)) )); (void) r; (void) c; // Test on inf/nan matrix CALL_SUBTEST_7( bdcsvd_inf_nan<MatrixXf>() ); } CALL_SUBTEST_7(( bdcsvd<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(( bdcsvd<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 problem size constructors CALL_SUBTEST_7( BDCSVD<MatrixXf>(10,10) ); } // end test_bdcsvd
typename GaussianProcess<TScalarType>::MatrixType GaussianProcess<TScalarType>::InvertKernelMatrix(const typename GaussianProcess<TScalarType>::MatrixType &K, typename GaussianProcess<TScalarType>::InversionMethod inv_method = GaussianProcess<TScalarType>::FullPivotLU, bool stable) const{ // compute core matrix if(debug){ std::cout << "GaussianProcess::InvertKernelMatrix: inverting kernel matrix... "; std::cout.flush(); } typename GaussianProcess<TScalarType>::MatrixType core; switch(inv_method){ // standard method: fast but not that accurate // Uses the LU decomposition with full pivoting for the inversion case FullPivotLU:{ if(debug) std::cout << " (inversion method: FullPivotLU) " << std::flush; try{ if(stable){ core = K.inverse(); } else{ if(debug) std::cout << " (using lapack) " << std::flush; core = lapack::lu_invert<TScalarType>(K); } } catch(lapack::LAPACKException& e){ core = K.inverse(); } } break; // very accurate and very slow method, use it for small problems // Uses the two-sided Jacobi SVD decomposition case JacobiSVD:{ if(debug) std::cout << " (inversion method: JacobiSVD) " << std::flush; Eigen::JacobiSVD<MatrixType> jacobisvd(K, Eigen::ComputeThinU | Eigen::ComputeThinV); if((jacobisvd.singularValues().real().array() < 0).any() && debug){ std::cout << "GaussianProcess::InvertKernelMatrix: warning: there are negative eigenvalues."; std::cout.flush(); } core = jacobisvd.matrixV() * VectorType(1/jacobisvd.singularValues().array()).asDiagonal() * jacobisvd.matrixU().transpose(); } break; // accurate method and faster than Jacobi SVD. // Uses the bidiagonal divide and conquer SVD case BDCSVD:{ if(debug) std::cout << " (inversion method: BDCSVD) " << std::flush; #ifdef EIGEN_BDCSVD_H Eigen::BDCSVD<MatrixType> bdcsvd(K, Eigen::ComputeThinU | Eigen::ComputeThinV); if((bdcsvd.singularValues().real().array() < 0).any() && debug){ std::cout << "GaussianProcess::InvertKernelMatrix: warning: there are negative eigenvalues."; std::cout.flush(); } core = bdcsvd.matrixV() * VectorType(1/bdcsvd.singularValues().array()).asDiagonal() * bdcsvd.matrixU().transpose(); #else // this is checked, since BDCSVD is currently not in the newest release throw std::string("GaussianProcess::InvertKernelMatrix: BDCSVD is not supported by the provided Eigen library."); #endif } break; // faster than the SVD method but less stable // computes the eigenvalues/eigenvectors of selfadjoint matrices case SelfAdjointEigenSolver:{ if(debug) std::cout << " (inversion method: SelfAdjointEigenSolver) " << std::flush; try{ core = lapack::chol_invert<TScalarType>(K); } catch(lapack::LAPACKException& e){ Eigen::SelfAdjointEigenSolver<MatrixType> es; es.compute(K); VectorType eigenValues = es.eigenvalues().reverse(); MatrixType eigenVectors = es.eigenvectors().rowwise().reverse(); if((eigenValues.real().array() < 0).any() && debug){ std::cout << "GaussianProcess::InvertKernelMatrix: warning: there are negative eigenvalues."; std::cout.flush(); } core = eigenVectors * VectorType(1/eigenValues.array()).asDiagonal() * eigenVectors.transpose(); } } break; } if(debug) std::cout << "[done]" << std::endl; return core; }