1 // This file is part of Eigen, a lightweight C++ template library
4 // Copyright (C) 2008-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 // discard stack allocation as that too bypasses malloc
12 #define EIGEN_STACK_ALLOCATION_LIMIT 0
13 #define EIGEN_RUNTIME_NO_MALLOC
17 #define SVD_DEFAULT(M) JacobiSVD<M>
18 #define SVD_FOR_MIN_NORM(M) JacobiSVD<M,ColPivHouseholderQRPreconditioner>
19 #include "svd_common.h"
21 // Check all variants of JacobiSVD
22 template<typename MatrixType
>
23 void jacobisvd(const MatrixType
& a
= MatrixType(), bool pickrandom
= true)
29 CALL_SUBTEST(( svd_test_all_computation_options
<JacobiSVD
<MatrixType
, FullPivHouseholderQRPreconditioner
> >(m
, true) )); // check full only
30 CALL_SUBTEST(( svd_test_all_computation_options
<JacobiSVD
<MatrixType
, ColPivHouseholderQRPreconditioner
> >(m
, false) ));
31 CALL_SUBTEST(( svd_test_all_computation_options
<JacobiSVD
<MatrixType
, HouseholderQRPreconditioner
> >(m
, false) ));
32 if(m
.rows()==m
.cols())
33 CALL_SUBTEST(( svd_test_all_computation_options
<JacobiSVD
<MatrixType
, NoQRPreconditioner
> >(m
, false) ));
36 template<typename MatrixType
> void jacobisvd_verify_assert(const MatrixType
& m
)
38 svd_verify_assert
<JacobiSVD
<MatrixType
> >(m
);
39 typedef typename
MatrixType::Index Index
;
40 Index rows
= m
.rows();
41 Index cols
= m
.cols();
44 ColsAtCompileTime
= MatrixType::ColsAtCompileTime
48 MatrixType a
= MatrixType::Zero(rows
, cols
);
51 if (ColsAtCompileTime
== Dynamic
)
53 JacobiSVD
<MatrixType
, FullPivHouseholderQRPreconditioner
> svd_fullqr
;
54 VERIFY_RAISES_ASSERT(svd_fullqr
.compute(a
, ComputeFullU
|ComputeThinV
))
55 VERIFY_RAISES_ASSERT(svd_fullqr
.compute(a
, ComputeThinU
|ComputeThinV
))
56 VERIFY_RAISES_ASSERT(svd_fullqr
.compute(a
, ComputeThinU
|ComputeFullV
))
60 template<typename MatrixType
>
61 void jacobisvd_method()
63 enum { Size
= MatrixType::RowsAtCompileTime
};
64 typedef typename
MatrixType::RealScalar RealScalar
;
65 typedef Matrix
<RealScalar
, Size
, 1> RealVecType
;
66 MatrixType m
= MatrixType::Identity();
67 VERIFY_IS_APPROX(m
.jacobiSvd().singularValues(), RealVecType::Ones());
68 VERIFY_RAISES_ASSERT(m
.jacobiSvd().matrixU());
69 VERIFY_RAISES_ASSERT(m
.jacobiSvd().matrixV());
70 VERIFY_IS_APPROX(m
.jacobiSvd(ComputeFullU
|ComputeFullV
).solve(m
), m
);
75 CALL_SUBTEST_3(( jacobisvd_verify_assert(Matrix3f()) ));
76 CALL_SUBTEST_4(( jacobisvd_verify_assert(Matrix4d()) ));
77 CALL_SUBTEST_7(( jacobisvd_verify_assert(MatrixXf(10,12)) ));
78 CALL_SUBTEST_8(( jacobisvd_verify_assert(MatrixXcd(7,5)) ));
80 CALL_SUBTEST_11(svd_all_trivial_2x2(jacobisvd
<Matrix2cd
>));
81 CALL_SUBTEST_12(svd_all_trivial_2x2(jacobisvd
<Matrix2d
>));
83 for(int i
= 0; i
< g_repeat
; i
++) {
84 CALL_SUBTEST_3(( jacobisvd
<Matrix3f
>() ));
85 CALL_SUBTEST_4(( jacobisvd
<Matrix4d
>() ));
86 CALL_SUBTEST_5(( jacobisvd
<Matrix
<float,3,5> >() ));
87 CALL_SUBTEST_6(( jacobisvd
<Matrix
<double,Dynamic
,2> >(Matrix
<double,Dynamic
,2>(10,2)) ));
89 int r
= internal::random
<int>(1, 30),
90 c
= internal::random
<int>(1, 30);
92 TEST_SET_BUT_UNUSED_VARIABLE(r
)
93 TEST_SET_BUT_UNUSED_VARIABLE(c
)
95 CALL_SUBTEST_10(( jacobisvd
<MatrixXd
>(MatrixXd(r
,c
)) ));
96 CALL_SUBTEST_7(( jacobisvd
<MatrixXf
>(MatrixXf(r
,c
)) ));
97 CALL_SUBTEST_8(( jacobisvd
<MatrixXcd
>(MatrixXcd(r
,c
)) ));
101 // Test on inf/nan matrix
102 CALL_SUBTEST_7( (svd_inf_nan
<JacobiSVD
<MatrixXf
>, MatrixXf
>()) );
103 CALL_SUBTEST_10( (svd_inf_nan
<JacobiSVD
<MatrixXd
>, MatrixXd
>()) );
105 // bug1395 test compile-time vectors as input
106 CALL_SUBTEST_13(( jacobisvd_verify_assert(Matrix
<double,6,1>()) ));
107 CALL_SUBTEST_13(( jacobisvd_verify_assert(Matrix
<double,1,6>()) ));
108 CALL_SUBTEST_13(( jacobisvd_verify_assert(Matrix
<double,Dynamic
,1>(r
)) ));
109 CALL_SUBTEST_13(( jacobisvd_verify_assert(Matrix
<double,1,Dynamic
>(c
)) ));
112 CALL_SUBTEST_7(( jacobisvd
<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))) ));
113 CALL_SUBTEST_8(( jacobisvd
<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))) ));
115 // test matrixbase method
116 CALL_SUBTEST_1(( jacobisvd_method
<Matrix2cd
>() ));
117 CALL_SUBTEST_3(( jacobisvd_method
<Matrix3f
>() ));
119 // Test problem size constructors
120 CALL_SUBTEST_7( JacobiSVD
<MatrixXf
>(10,10) );
122 // Check that preallocation avoids subsequent mallocs
123 CALL_SUBTEST_9( svd_preallocate
<void>() );
125 CALL_SUBTEST_2( svd_underoverflow
<void>() );