blob: 64b8663580ee3e6f96050f076118c6dbcdd2c256 [file] [log] [blame]
Brian Silverman72890c22015-09-19 14:37:37 -04001// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
Austin Schuh189376f2018-12-20 22:11:15 +11004// Copyright (C) 2008-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
Brian Silverman72890c22015-09-19 14:37:37 -04005// Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
6//
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/.
10
11// discard stack allocation as that too bypasses malloc
12#define EIGEN_STACK_ALLOCATION_LIMIT 0
13#define EIGEN_RUNTIME_NO_MALLOC
14#include "main.h"
15#include <Eigen/SVD>
16
Austin Schuh189376f2018-12-20 22:11:15 +110017#define SVD_DEFAULT(M) JacobiSVD<M>
18#define SVD_FOR_MIN_NORM(M) JacobiSVD<M,ColPivHouseholderQRPreconditioner>
19#include "svd_common.h"
Brian Silverman72890c22015-09-19 14:37:37 -040020
Austin Schuh189376f2018-12-20 22:11:15 +110021// Check all variants of JacobiSVD
Brian Silverman72890c22015-09-19 14:37:37 -040022template<typename MatrixType>
23void jacobisvd(const MatrixType& a = MatrixType(), bool pickrandom = true)
24{
25 MatrixType m = a;
26 if(pickrandom)
Austin Schuh189376f2018-12-20 22:11:15 +110027 svd_fill_random(m);
Brian Silverman72890c22015-09-19 14:37:37 -040028
Austin Schuh189376f2018-12-20 22:11:15 +110029 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) ));
Brian Silverman72890c22015-09-19 14:37:37 -040034}
35
36template<typename MatrixType> void jacobisvd_verify_assert(const MatrixType& m)
37{
Austin Schuh189376f2018-12-20 22:11:15 +110038 svd_verify_assert<JacobiSVD<MatrixType> >(m);
Brian Silverman72890c22015-09-19 14:37:37 -040039 Index rows = m.rows();
40 Index cols = m.cols();
41
42 enum {
Brian Silverman72890c22015-09-19 14:37:37 -040043 ColsAtCompileTime = MatrixType::ColsAtCompileTime
44 };
45
Brian Silverman72890c22015-09-19 14:37:37 -040046
47 MatrixType a = MatrixType::Zero(rows, cols);
48 a.setZero();
Brian Silverman72890c22015-09-19 14:37:37 -040049
50 if (ColsAtCompileTime == Dynamic)
51 {
Brian Silverman72890c22015-09-19 14:37:37 -040052 JacobiSVD<MatrixType, FullPivHouseholderQRPreconditioner> svd_fullqr;
53 VERIFY_RAISES_ASSERT(svd_fullqr.compute(a, ComputeFullU|ComputeThinV))
54 VERIFY_RAISES_ASSERT(svd_fullqr.compute(a, ComputeThinU|ComputeThinV))
55 VERIFY_RAISES_ASSERT(svd_fullqr.compute(a, ComputeThinU|ComputeFullV))
56 }
Brian Silverman72890c22015-09-19 14:37:37 -040057}
58
59template<typename MatrixType>
60void jacobisvd_method()
61{
62 enum { Size = MatrixType::RowsAtCompileTime };
63 typedef typename MatrixType::RealScalar RealScalar;
64 typedef Matrix<RealScalar, Size, 1> RealVecType;
65 MatrixType m = MatrixType::Identity();
66 VERIFY_IS_APPROX(m.jacobiSvd().singularValues(), RealVecType::Ones());
67 VERIFY_RAISES_ASSERT(m.jacobiSvd().matrixU());
68 VERIFY_RAISES_ASSERT(m.jacobiSvd().matrixV());
69 VERIFY_IS_APPROX(m.jacobiSvd(ComputeFullU|ComputeFullV).solve(m), m);
70}
71
Austin Schuh189376f2018-12-20 22:11:15 +110072namespace Foo {
73// older compiler require a default constructor for Bar
74// cf: https://stackoverflow.com/questions/7411515/
75class Bar {public: Bar() {}};
76bool operator<(const Bar&, const Bar&) { return true; }
Brian Silverman72890c22015-09-19 14:37:37 -040077}
Austin Schuh189376f2018-12-20 22:11:15 +110078// regression test for a very strange MSVC issue for which simply
79// including SVDBase.h messes up with std::max and custom scalar type
80void msvc_workaround()
Brian Silverman72890c22015-09-19 14:37:37 -040081{
Austin Schuh189376f2018-12-20 22:11:15 +110082 const Foo::Bar a;
83 const Foo::Bar b;
84 std::max EIGEN_NOT_A_MACRO (a,b);
Brian Silverman72890c22015-09-19 14:37:37 -040085}
86
87void test_jacobisvd()
88{
89 CALL_SUBTEST_3(( jacobisvd_verify_assert(Matrix3f()) ));
90 CALL_SUBTEST_4(( jacobisvd_verify_assert(Matrix4d()) ));
91 CALL_SUBTEST_7(( jacobisvd_verify_assert(MatrixXf(10,12)) ));
92 CALL_SUBTEST_8(( jacobisvd_verify_assert(MatrixXcd(7,5)) ));
Austin Schuh189376f2018-12-20 22:11:15 +110093
94 CALL_SUBTEST_11(svd_all_trivial_2x2(jacobisvd<Matrix2cd>));
95 CALL_SUBTEST_12(svd_all_trivial_2x2(jacobisvd<Matrix2d>));
Brian Silverman72890c22015-09-19 14:37:37 -040096
97 for(int i = 0; i < g_repeat; i++) {
Brian Silverman72890c22015-09-19 14:37:37 -040098 CALL_SUBTEST_3(( jacobisvd<Matrix3f>() ));
99 CALL_SUBTEST_4(( jacobisvd<Matrix4d>() ));
100 CALL_SUBTEST_5(( jacobisvd<Matrix<float,3,5> >() ));
101 CALL_SUBTEST_6(( jacobisvd<Matrix<double,Dynamic,2> >(Matrix<double,Dynamic,2>(10,2)) ));
102
103 int r = internal::random<int>(1, 30),
104 c = internal::random<int>(1, 30);
105
106 TEST_SET_BUT_UNUSED_VARIABLE(r)
107 TEST_SET_BUT_UNUSED_VARIABLE(c)
108
109 CALL_SUBTEST_10(( jacobisvd<MatrixXd>(MatrixXd(r,c)) ));
110 CALL_SUBTEST_7(( jacobisvd<MatrixXf>(MatrixXf(r,c)) ));
111 CALL_SUBTEST_8(( jacobisvd<MatrixXcd>(MatrixXcd(r,c)) ));
112 (void) r;
113 (void) c;
114
115 // Test on inf/nan matrix
Austin Schuh189376f2018-12-20 22:11:15 +1100116 CALL_SUBTEST_7( (svd_inf_nan<JacobiSVD<MatrixXf>, MatrixXf>()) );
117 CALL_SUBTEST_10( (svd_inf_nan<JacobiSVD<MatrixXd>, MatrixXd>()) );
118
119 // bug1395 test compile-time vectors as input
120 CALL_SUBTEST_13(( jacobisvd_verify_assert(Matrix<double,6,1>()) ));
121 CALL_SUBTEST_13(( jacobisvd_verify_assert(Matrix<double,1,6>()) ));
122 CALL_SUBTEST_13(( jacobisvd_verify_assert(Matrix<double,Dynamic,1>(r)) ));
123 CALL_SUBTEST_13(( jacobisvd_verify_assert(Matrix<double,1,Dynamic>(c)) ));
Brian Silverman72890c22015-09-19 14:37:37 -0400124 }
125
126 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))) ));
127 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))) ));
128
129 // test matrixbase method
130 CALL_SUBTEST_1(( jacobisvd_method<Matrix2cd>() ));
131 CALL_SUBTEST_3(( jacobisvd_method<Matrix3f>() ));
132
133 // Test problem size constructors
134 CALL_SUBTEST_7( JacobiSVD<MatrixXf>(10,10) );
135
136 // Check that preallocation avoids subsequent mallocs
Austin Schuh189376f2018-12-20 22:11:15 +1100137 CALL_SUBTEST_9( svd_preallocate<void>() );
Brian Silverman72890c22015-09-19 14:37:37 -0400138
Austin Schuh189376f2018-12-20 22:11:15 +1100139 CALL_SUBTEST_2( svd_underoverflow<void>() );
140
141 msvc_workaround();
Brian Silverman72890c22015-09-19 14:37:37 -0400142}