xref: /aosp_15_r20/external/eigen/test/jacobisvd.cpp (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
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) 2008-2014 Gael Guennebaud <[email protected]>
5*bf2c3715SXin Li // Copyright (C) 2009 Benoit Jacob <[email protected]>
6*bf2c3715SXin Li //
7*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
8*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
9*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10*bf2c3715SXin Li 
11*bf2c3715SXin Li // discard stack allocation as that too bypasses malloc
12*bf2c3715SXin Li #define EIGEN_STACK_ALLOCATION_LIMIT 0
13*bf2c3715SXin Li #define EIGEN_RUNTIME_NO_MALLOC
14*bf2c3715SXin Li #include "main.h"
15*bf2c3715SXin Li #include <Eigen/SVD>
16*bf2c3715SXin Li 
17*bf2c3715SXin Li #define SVD_DEFAULT(M) JacobiSVD<M>
18*bf2c3715SXin Li #define SVD_FOR_MIN_NORM(M) JacobiSVD<M,ColPivHouseholderQRPreconditioner>
19*bf2c3715SXin Li #include "svd_common.h"
20*bf2c3715SXin Li 
21*bf2c3715SXin Li // Check all variants of JacobiSVD
22*bf2c3715SXin Li template<typename MatrixType>
jacobisvd(const MatrixType & a=MatrixType (),bool pickrandom=true)23*bf2c3715SXin Li void jacobisvd(const MatrixType& a = MatrixType(), bool pickrandom = true)
24*bf2c3715SXin Li {
25*bf2c3715SXin Li   MatrixType m = a;
26*bf2c3715SXin Li   if(pickrandom)
27*bf2c3715SXin Li     svd_fill_random(m);
28*bf2c3715SXin Li 
29*bf2c3715SXin Li   CALL_SUBTEST(( svd_test_all_computation_options<JacobiSVD<MatrixType, FullPivHouseholderQRPreconditioner> >(m, true)  )); // check full only
30*bf2c3715SXin Li   CALL_SUBTEST(( svd_test_all_computation_options<JacobiSVD<MatrixType, ColPivHouseholderQRPreconditioner>  >(m, false) ));
31*bf2c3715SXin Li   CALL_SUBTEST(( svd_test_all_computation_options<JacobiSVD<MatrixType, HouseholderQRPreconditioner>        >(m, false) ));
32*bf2c3715SXin Li   if(m.rows()==m.cols())
33*bf2c3715SXin Li     CALL_SUBTEST(( svd_test_all_computation_options<JacobiSVD<MatrixType, NoQRPreconditioner>               >(m, false) ));
34*bf2c3715SXin Li }
35*bf2c3715SXin Li 
jacobisvd_verify_assert(const MatrixType & m)36*bf2c3715SXin Li template<typename MatrixType> void jacobisvd_verify_assert(const MatrixType& m)
37*bf2c3715SXin Li {
38*bf2c3715SXin Li   svd_verify_assert<JacobiSVD<MatrixType> >(m);
39*bf2c3715SXin Li   svd_verify_assert<JacobiSVD<MatrixType, FullPivHouseholderQRPreconditioner> >(m, true);
40*bf2c3715SXin Li   svd_verify_assert<JacobiSVD<MatrixType, ColPivHouseholderQRPreconditioner> >(m);
41*bf2c3715SXin Li   svd_verify_assert<JacobiSVD<MatrixType, HouseholderQRPreconditioner> >(m);
42*bf2c3715SXin Li   Index rows = m.rows();
43*bf2c3715SXin Li   Index cols = m.cols();
44*bf2c3715SXin Li 
45*bf2c3715SXin Li   enum {
46*bf2c3715SXin Li     ColsAtCompileTime = MatrixType::ColsAtCompileTime
47*bf2c3715SXin Li   };
48*bf2c3715SXin Li 
49*bf2c3715SXin Li 
50*bf2c3715SXin Li   MatrixType a = MatrixType::Zero(rows, cols);
51*bf2c3715SXin Li   a.setZero();
52*bf2c3715SXin Li 
53*bf2c3715SXin Li   if (ColsAtCompileTime == Dynamic)
54*bf2c3715SXin Li   {
55*bf2c3715SXin Li     JacobiSVD<MatrixType, FullPivHouseholderQRPreconditioner> svd_fullqr;
56*bf2c3715SXin Li     VERIFY_RAISES_ASSERT(svd_fullqr.compute(a, ComputeFullU|ComputeThinV))
57*bf2c3715SXin Li     VERIFY_RAISES_ASSERT(svd_fullqr.compute(a, ComputeThinU|ComputeThinV))
58*bf2c3715SXin Li     VERIFY_RAISES_ASSERT(svd_fullqr.compute(a, ComputeThinU|ComputeFullV))
59*bf2c3715SXin Li   }
60*bf2c3715SXin Li }
61*bf2c3715SXin Li 
62*bf2c3715SXin Li template<typename MatrixType>
jacobisvd_method()63*bf2c3715SXin Li void jacobisvd_method()
64*bf2c3715SXin Li {
65*bf2c3715SXin Li   enum { Size = MatrixType::RowsAtCompileTime };
66*bf2c3715SXin Li   typedef typename MatrixType::RealScalar RealScalar;
67*bf2c3715SXin Li   typedef Matrix<RealScalar, Size, 1> RealVecType;
68*bf2c3715SXin Li   MatrixType m = MatrixType::Identity();
69*bf2c3715SXin Li   VERIFY_IS_APPROX(m.jacobiSvd().singularValues(), RealVecType::Ones());
70*bf2c3715SXin Li   VERIFY_RAISES_ASSERT(m.jacobiSvd().matrixU());
71*bf2c3715SXin Li   VERIFY_RAISES_ASSERT(m.jacobiSvd().matrixV());
72*bf2c3715SXin Li   VERIFY_IS_APPROX(m.jacobiSvd(ComputeFullU|ComputeFullV).solve(m), m);
73*bf2c3715SXin Li   VERIFY_IS_APPROX(m.jacobiSvd(ComputeFullU|ComputeFullV).transpose().solve(m), m);
74*bf2c3715SXin Li   VERIFY_IS_APPROX(m.jacobiSvd(ComputeFullU|ComputeFullV).adjoint().solve(m), m);
75*bf2c3715SXin Li }
76*bf2c3715SXin Li 
77*bf2c3715SXin Li namespace Foo {
78*bf2c3715SXin Li // older compiler require a default constructor for Bar
79*bf2c3715SXin Li // cf: https://stackoverflow.com/questions/7411515/
Bar()80*bf2c3715SXin Li class Bar {public: Bar() {}};
operator <(const Bar &,const Bar &)81*bf2c3715SXin Li bool operator<(const Bar&, const Bar&) { return true; }
82*bf2c3715SXin Li }
83*bf2c3715SXin Li // regression test for a very strange MSVC issue for which simply
84*bf2c3715SXin Li // including SVDBase.h messes up with std::max and custom scalar type
msvc_workaround()85*bf2c3715SXin Li void msvc_workaround()
86*bf2c3715SXin Li {
87*bf2c3715SXin Li   const Foo::Bar a;
88*bf2c3715SXin Li   const Foo::Bar b;
89*bf2c3715SXin Li   std::max EIGEN_NOT_A_MACRO (a,b);
90*bf2c3715SXin Li }
91*bf2c3715SXin Li 
EIGEN_DECLARE_TEST(jacobisvd)92*bf2c3715SXin Li EIGEN_DECLARE_TEST(jacobisvd)
93*bf2c3715SXin Li {
94*bf2c3715SXin Li   CALL_SUBTEST_3(( jacobisvd_verify_assert(Matrix3f()) ));
95*bf2c3715SXin Li   CALL_SUBTEST_4(( jacobisvd_verify_assert(Matrix4d()) ));
96*bf2c3715SXin Li   CALL_SUBTEST_7(( jacobisvd_verify_assert(MatrixXf(10,12)) ));
97*bf2c3715SXin Li   CALL_SUBTEST_8(( jacobisvd_verify_assert(MatrixXcd(7,5)) ));
98*bf2c3715SXin Li 
99*bf2c3715SXin Li   CALL_SUBTEST_11(svd_all_trivial_2x2(jacobisvd<Matrix2cd>));
100*bf2c3715SXin Li   CALL_SUBTEST_12(svd_all_trivial_2x2(jacobisvd<Matrix2d>));
101*bf2c3715SXin Li 
102*bf2c3715SXin Li   for(int i = 0; i < g_repeat; i++) {
103*bf2c3715SXin Li     CALL_SUBTEST_3(( jacobisvd<Matrix3f>() ));
104*bf2c3715SXin Li     CALL_SUBTEST_4(( jacobisvd<Matrix4d>() ));
105*bf2c3715SXin Li     CALL_SUBTEST_5(( jacobisvd<Matrix<float,3,5> >() ));
106*bf2c3715SXin Li     CALL_SUBTEST_6(( jacobisvd<Matrix<double,Dynamic,2> >(Matrix<double,Dynamic,2>(10,2)) ));
107*bf2c3715SXin Li 
108*bf2c3715SXin Li     int r = internal::random<int>(1, 30),
109*bf2c3715SXin Li         c = internal::random<int>(1, 30);
110*bf2c3715SXin Li 
111*bf2c3715SXin Li     TEST_SET_BUT_UNUSED_VARIABLE(r)
112*bf2c3715SXin Li     TEST_SET_BUT_UNUSED_VARIABLE(c)
113*bf2c3715SXin Li 
114*bf2c3715SXin Li     CALL_SUBTEST_10(( jacobisvd<MatrixXd>(MatrixXd(r,c)) ));
115*bf2c3715SXin Li     CALL_SUBTEST_7(( jacobisvd<MatrixXf>(MatrixXf(r,c)) ));
116*bf2c3715SXin Li     CALL_SUBTEST_8(( jacobisvd<MatrixXcd>(MatrixXcd(r,c)) ));
117*bf2c3715SXin Li     (void) r;
118*bf2c3715SXin Li     (void) c;
119*bf2c3715SXin Li 
120*bf2c3715SXin Li     // Test on inf/nan matrix
121*bf2c3715SXin Li     CALL_SUBTEST_7(  (svd_inf_nan<JacobiSVD<MatrixXf>, MatrixXf>()) );
122*bf2c3715SXin Li     CALL_SUBTEST_10( (svd_inf_nan<JacobiSVD<MatrixXd>, MatrixXd>()) );
123*bf2c3715SXin Li 
124*bf2c3715SXin Li     // bug1395 test compile-time vectors as input
125*bf2c3715SXin Li     CALL_SUBTEST_13(( jacobisvd_verify_assert(Matrix<double,6,1>()) ));
126*bf2c3715SXin Li     CALL_SUBTEST_13(( jacobisvd_verify_assert(Matrix<double,1,6>()) ));
127*bf2c3715SXin Li     CALL_SUBTEST_13(( jacobisvd_verify_assert(Matrix<double,Dynamic,1>(r)) ));
128*bf2c3715SXin Li     CALL_SUBTEST_13(( jacobisvd_verify_assert(Matrix<double,1,Dynamic>(c)) ));
129*bf2c3715SXin Li   }
130*bf2c3715SXin Li 
131*bf2c3715SXin Li   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))) ));
132*bf2c3715SXin Li   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))) ));
133*bf2c3715SXin Li 
134*bf2c3715SXin Li   // test matrixbase method
135*bf2c3715SXin Li   CALL_SUBTEST_1(( jacobisvd_method<Matrix2cd>() ));
136*bf2c3715SXin Li   CALL_SUBTEST_3(( jacobisvd_method<Matrix3f>() ));
137*bf2c3715SXin Li 
138*bf2c3715SXin Li   // Test problem size constructors
139*bf2c3715SXin Li   CALL_SUBTEST_7( JacobiSVD<MatrixXf>(10,10) );
140*bf2c3715SXin Li 
141*bf2c3715SXin Li   // Check that preallocation avoids subsequent mallocs
142*bf2c3715SXin Li   CALL_SUBTEST_9( svd_preallocate<void>() );
143*bf2c3715SXin Li 
144*bf2c3715SXin Li   CALL_SUBTEST_2( svd_underoverflow<void>() );
145*bf2c3715SXin Li 
146*bf2c3715SXin Li   msvc_workaround();
147*bf2c3715SXin Li }
148