1*bf2c3715SXin Li // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 2013 Gauthier Brun <[email protected]>
5*bf2c3715SXin Li // Copyright (C) 2013 Nicolas Carre <[email protected]>
6*bf2c3715SXin Li // Copyright (C) 2013 Jean Ceccato <[email protected]>
7*bf2c3715SXin Li // Copyright (C) 2013 Pierre Zoppitelli <[email protected]>
8*bf2c3715SXin Li //
9*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
10*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
11*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/
12*bf2c3715SXin Li
13*bf2c3715SXin Li // discard stack allocation as that too bypasses malloc
14*bf2c3715SXin Li #define EIGEN_STACK_ALLOCATION_LIMIT 0
15*bf2c3715SXin Li #define EIGEN_RUNTIME_NO_MALLOC
16*bf2c3715SXin Li
17*bf2c3715SXin Li #include "main.h"
18*bf2c3715SXin Li #include <Eigen/SVD>
19*bf2c3715SXin Li #include <iostream>
20*bf2c3715SXin Li #include <Eigen/LU>
21*bf2c3715SXin Li
22*bf2c3715SXin Li
23*bf2c3715SXin Li #define SVD_DEFAULT(M) BDCSVD<M>
24*bf2c3715SXin Li #define SVD_FOR_MIN_NORM(M) BDCSVD<M>
25*bf2c3715SXin Li #include "svd_common.h"
26*bf2c3715SXin Li
27*bf2c3715SXin Li // Check all variants of JacobiSVD
28*bf2c3715SXin Li template<typename MatrixType>
bdcsvd(const MatrixType & a=MatrixType (),bool pickrandom=true)29*bf2c3715SXin Li void bdcsvd(const MatrixType& a = MatrixType(), bool pickrandom = true)
30*bf2c3715SXin Li {
31*bf2c3715SXin Li MatrixType m;
32*bf2c3715SXin Li if(pickrandom) {
33*bf2c3715SXin Li m.resizeLike(a);
34*bf2c3715SXin Li svd_fill_random(m);
35*bf2c3715SXin Li }
36*bf2c3715SXin Li else
37*bf2c3715SXin Li m = a;
38*bf2c3715SXin Li
39*bf2c3715SXin Li CALL_SUBTEST(( svd_test_all_computation_options<BDCSVD<MatrixType> >(m, false) ));
40*bf2c3715SXin Li }
41*bf2c3715SXin Li
42*bf2c3715SXin Li template<typename MatrixType>
bdcsvd_method()43*bf2c3715SXin Li void bdcsvd_method()
44*bf2c3715SXin Li {
45*bf2c3715SXin Li enum { Size = MatrixType::RowsAtCompileTime };
46*bf2c3715SXin Li typedef typename MatrixType::RealScalar RealScalar;
47*bf2c3715SXin Li typedef Matrix<RealScalar, Size, 1> RealVecType;
48*bf2c3715SXin Li MatrixType m = MatrixType::Identity();
49*bf2c3715SXin Li VERIFY_IS_APPROX(m.bdcSvd().singularValues(), RealVecType::Ones());
50*bf2c3715SXin Li VERIFY_RAISES_ASSERT(m.bdcSvd().matrixU());
51*bf2c3715SXin Li VERIFY_RAISES_ASSERT(m.bdcSvd().matrixV());
52*bf2c3715SXin Li VERIFY_IS_APPROX(m.bdcSvd(ComputeFullU|ComputeFullV).solve(m), m);
53*bf2c3715SXin Li VERIFY_IS_APPROX(m.bdcSvd(ComputeFullU|ComputeFullV).transpose().solve(m), m);
54*bf2c3715SXin Li VERIFY_IS_APPROX(m.bdcSvd(ComputeFullU|ComputeFullV).adjoint().solve(m), m);
55*bf2c3715SXin Li }
56*bf2c3715SXin Li
57*bf2c3715SXin Li // compare the Singular values returned with Jacobi and Bdc
58*bf2c3715SXin Li template<typename MatrixType>
compare_bdc_jacobi(const MatrixType & a=MatrixType (),unsigned int computationOptions=0)59*bf2c3715SXin Li void compare_bdc_jacobi(const MatrixType& a = MatrixType(), unsigned int computationOptions = 0)
60*bf2c3715SXin Li {
61*bf2c3715SXin Li MatrixType m = MatrixType::Random(a.rows(), a.cols());
62*bf2c3715SXin Li BDCSVD<MatrixType> bdc_svd(m);
63*bf2c3715SXin Li JacobiSVD<MatrixType> jacobi_svd(m);
64*bf2c3715SXin Li VERIFY_IS_APPROX(bdc_svd.singularValues(), jacobi_svd.singularValues());
65*bf2c3715SXin Li if(computationOptions & ComputeFullU) VERIFY_IS_APPROX(bdc_svd.matrixU(), jacobi_svd.matrixU());
66*bf2c3715SXin Li if(computationOptions & ComputeThinU) VERIFY_IS_APPROX(bdc_svd.matrixU(), jacobi_svd.matrixU());
67*bf2c3715SXin Li if(computationOptions & ComputeFullV) VERIFY_IS_APPROX(bdc_svd.matrixV(), jacobi_svd.matrixV());
68*bf2c3715SXin Li if(computationOptions & ComputeThinV) VERIFY_IS_APPROX(bdc_svd.matrixV(), jacobi_svd.matrixV());
69*bf2c3715SXin Li }
70*bf2c3715SXin Li
EIGEN_DECLARE_TEST(bdcsvd)71*bf2c3715SXin Li EIGEN_DECLARE_TEST(bdcsvd)
72*bf2c3715SXin Li {
73*bf2c3715SXin Li CALL_SUBTEST_3(( svd_verify_assert<BDCSVD<Matrix3f> >(Matrix3f()) ));
74*bf2c3715SXin Li CALL_SUBTEST_4(( svd_verify_assert<BDCSVD<Matrix4d> >(Matrix4d()) ));
75*bf2c3715SXin Li CALL_SUBTEST_7(( svd_verify_assert<BDCSVD<MatrixXf> >(MatrixXf(10,12)) ));
76*bf2c3715SXin Li CALL_SUBTEST_8(( svd_verify_assert<BDCSVD<MatrixXcd> >(MatrixXcd(7,5)) ));
77*bf2c3715SXin Li
78*bf2c3715SXin Li CALL_SUBTEST_101(( svd_all_trivial_2x2(bdcsvd<Matrix2cd>) ));
79*bf2c3715SXin Li CALL_SUBTEST_102(( svd_all_trivial_2x2(bdcsvd<Matrix2d>) ));
80*bf2c3715SXin Li
81*bf2c3715SXin Li for(int i = 0; i < g_repeat; i++) {
82*bf2c3715SXin Li CALL_SUBTEST_3(( bdcsvd<Matrix3f>() ));
83*bf2c3715SXin Li CALL_SUBTEST_4(( bdcsvd<Matrix4d>() ));
84*bf2c3715SXin Li CALL_SUBTEST_5(( bdcsvd<Matrix<float,3,5> >() ));
85*bf2c3715SXin Li
86*bf2c3715SXin Li int r = internal::random<int>(1, EIGEN_TEST_MAX_SIZE/2),
87*bf2c3715SXin Li c = internal::random<int>(1, EIGEN_TEST_MAX_SIZE/2);
88*bf2c3715SXin Li
89*bf2c3715SXin Li TEST_SET_BUT_UNUSED_VARIABLE(r)
90*bf2c3715SXin Li TEST_SET_BUT_UNUSED_VARIABLE(c)
91*bf2c3715SXin Li
92*bf2c3715SXin Li CALL_SUBTEST_6(( bdcsvd(Matrix<double,Dynamic,2>(r,2)) ));
93*bf2c3715SXin Li CALL_SUBTEST_7(( bdcsvd(MatrixXf(r,c)) ));
94*bf2c3715SXin Li CALL_SUBTEST_7(( compare_bdc_jacobi(MatrixXf(r,c)) ));
95*bf2c3715SXin Li CALL_SUBTEST_10(( bdcsvd(MatrixXd(r,c)) ));
96*bf2c3715SXin Li CALL_SUBTEST_10(( compare_bdc_jacobi(MatrixXd(r,c)) ));
97*bf2c3715SXin Li CALL_SUBTEST_8(( bdcsvd(MatrixXcd(r,c)) ));
98*bf2c3715SXin Li CALL_SUBTEST_8(( compare_bdc_jacobi(MatrixXcd(r,c)) ));
99*bf2c3715SXin Li
100*bf2c3715SXin Li // Test on inf/nan matrix
101*bf2c3715SXin Li CALL_SUBTEST_7( (svd_inf_nan<BDCSVD<MatrixXf>, MatrixXf>()) );
102*bf2c3715SXin Li CALL_SUBTEST_10( (svd_inf_nan<BDCSVD<MatrixXd>, MatrixXd>()) );
103*bf2c3715SXin Li }
104*bf2c3715SXin Li
105*bf2c3715SXin Li // test matrixbase method
106*bf2c3715SXin Li CALL_SUBTEST_1(( bdcsvd_method<Matrix2cd>() ));
107*bf2c3715SXin Li CALL_SUBTEST_3(( bdcsvd_method<Matrix3f>() ));
108*bf2c3715SXin Li
109*bf2c3715SXin Li // Test problem size constructors
110*bf2c3715SXin Li CALL_SUBTEST_7( BDCSVD<MatrixXf>(10,10) );
111*bf2c3715SXin Li
112*bf2c3715SXin Li // Check that preallocation avoids subsequent mallocs
113*bf2c3715SXin Li // Disabled because not supported by BDCSVD
114*bf2c3715SXin Li // CALL_SUBTEST_9( svd_preallocate<void>() );
115*bf2c3715SXin Li
116*bf2c3715SXin Li CALL_SUBTEST_2( svd_underoverflow<void>() );
117*bf2c3715SXin Li }
118*bf2c3715SXin Li
119