jacobisvd.cpp
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // 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 
17 #define SVD_DEFAULT(M) JacobiSVD<M>
18 #define SVD_FOR_MIN_NORM(M) JacobiSVD<M,ColPivHouseholderQRPreconditioner>
19 #include "svd_common.h"
20 
21 // Check all variants of JacobiSVD
22 template<typename MatrixType>
23 void jacobisvd(const MatrixType& a = MatrixType(), bool pickrandom = true)
24 {
25  MatrixType m = a;
26  if(pickrandom)
28 
32  if(m.rows()==m.cols())
34 }
35 
36 template<typename MatrixType> void jacobisvd_verify_assert(const MatrixType& m)
37 {
38  svd_verify_assert<JacobiSVD<MatrixType> >(m);
39  svd_verify_assert<JacobiSVD<MatrixType, FullPivHouseholderQRPreconditioner> >(m, true);
40  svd_verify_assert<JacobiSVD<MatrixType, ColPivHouseholderQRPreconditioner> >(m);
41  svd_verify_assert<JacobiSVD<MatrixType, HouseholderQRPreconditioner> >(m);
42  Index rows = m.rows();
43  Index cols = m.cols();
44 
45  enum {
46  ColsAtCompileTime = MatrixType::ColsAtCompileTime
47  };
48 
49 
50  MatrixType a = MatrixType::Zero(rows, cols);
51  a.setZero();
52 
53  if (ColsAtCompileTime == Dynamic)
54  {
59  }
60 }
61 
62 template<typename MatrixType>
64 {
65  enum { Size = MatrixType::RowsAtCompileTime };
66  typedef typename MatrixType::RealScalar RealScalar;
67  typedef Matrix<RealScalar, Size, 1> RealVecType;
68  MatrixType m = MatrixType::Identity();
69  VERIFY_IS_APPROX(m.jacobiSvd().singularValues(), RealVecType::Ones());
70  VERIFY_RAISES_ASSERT(m.jacobiSvd().matrixU());
71  VERIFY_RAISES_ASSERT(m.jacobiSvd().matrixV());
72  VERIFY_IS_APPROX(m.jacobiSvd(ComputeFullU|ComputeFullV).solve(m), m);
73  VERIFY_IS_APPROX(m.jacobiSvd(ComputeFullU|ComputeFullV).transpose().solve(m), m);
74  VERIFY_IS_APPROX(m.jacobiSvd(ComputeFullU|ComputeFullV).adjoint().solve(m), m);
75 }
76 
77 namespace Foo {
78 // older compiler require a default constructor for Bar
79 // cf: https://stackoverflow.com/questions/7411515/
80 class Bar {public: Bar() {}};
81 bool operator<(const Bar&, const Bar&) { return true; }
82 }
83 // regression test for a very strange MSVC issue for which simply
84 // including SVDBase.h messes up with std::max and custom scalar type
86 {
87  const Foo::Bar a;
88  const Foo::Bar b;
90 }
91 
93 {
94  CALL_SUBTEST_3(( jacobisvd_verify_assert(Matrix3f()) ));
95  CALL_SUBTEST_4(( jacobisvd_verify_assert(Matrix4d()) ));
96  CALL_SUBTEST_7(( jacobisvd_verify_assert(MatrixXf(10,12)) ));
97  CALL_SUBTEST_8(( jacobisvd_verify_assert(MatrixXcd(7,5)) ));
98 
99  CALL_SUBTEST_11(svd_all_trivial_2x2(jacobisvd<Matrix2cd>));
100  CALL_SUBTEST_12(svd_all_trivial_2x2(jacobisvd<Matrix2d>));
101 
102  for(int i = 0; i < g_repeat; i++) {
103  CALL_SUBTEST_3(( jacobisvd<Matrix3f>() ));
104  CALL_SUBTEST_4(( jacobisvd<Matrix4d>() ));
107 
108  int r = internal::random<int>(1, 30),
109  c = internal::random<int>(1, 30);
110 
113 
114  CALL_SUBTEST_10(( jacobisvd<MatrixXd>(MatrixXd(r,c)) ));
115  CALL_SUBTEST_7(( jacobisvd<MatrixXf>(MatrixXf(r,c)) ));
116  CALL_SUBTEST_8(( jacobisvd<MatrixXcd>(MatrixXcd(r,c)) ));
117  (void) r;
118  (void) c;
119 
120  // Test on inf/nan matrix
123 
124  // bug1395 test compile-time vectors as input
129  }
130 
131  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  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 
134  // test matrixbase method
135  CALL_SUBTEST_1(( jacobisvd_method<Matrix2cd>() ));
136  CALL_SUBTEST_3(( jacobisvd_method<Matrix3f>() ));
137 
138  // Test problem size constructors
140 
141  // Check that preallocation avoids subsequent mallocs
142  CALL_SUBTEST_9( svd_preallocate<void>() );
143 
144  CALL_SUBTEST_2( svd_underoverflow<void>() );
145 
146  msvc_workaround();
147 }
jacobisvd_method
void jacobisvd_method()
Definition: jacobisvd.cpp:63
Eigen::ComputeFullV
@ ComputeFullV
Definition: Constants.h:397
Foo::Bar::Bar
Bar()
Definition: jacobisvd.cpp:80
MatrixType
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
b
Scalar * b
Definition: benchVecAdd.cpp:17
Foo
Definition: jacobisvd.cpp:77
Eigen::ComputeFullU
@ ComputeFullU
Definition: Constants.h:393
Foo::operator<
bool operator<(const Bar &, const Bar &)
Definition: jacobisvd.cpp:81
svd_inf_nan
void svd_inf_nan()
Definition: svd_common.h:304
CALL_SUBTEST_11
#define CALL_SUBTEST_11(FUNC)
Definition: split_test_helper.h:64
CALL_SUBTEST_9
#define CALL_SUBTEST_9(FUNC)
Definition: split_test_helper.h:52
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
VERIFY_RAISES_ASSERT
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:340
CALL_SUBTEST_4
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22
Eigen::JacobiSVD::compute
JacobiSVD & compute(const MatrixType &matrix, unsigned int computationOptions)
Method performing the decomposition of given matrix using custom options.
Definition: JacobiSVD.h:666
CALL_SUBTEST_3
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
CALL_SUBTEST_1
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
svd_fill_random
void svd_fill_random(MatrixType &m, int Option=0)
Definition: svd_fill.h:21
CALL_SUBTEST_13
#define CALL_SUBTEST_13(FUNC)
Definition: split_test_helper.h:76
Eigen::ComputeThinU
@ ComputeThinU
Definition: Constants.h:395
svd_all_trivial_2x2
void svd_all_trivial_2x2(void(*cb)(const MatrixType &, bool))
Definition: svd_common.h:401
svd_common.h
Eigen::Dynamic
const int Dynamic
Definition: Constants.h:22
CALL_SUBTEST_10
#define CALL_SUBTEST_10(FUNC)
Definition: split_test_helper.h:58
jacobisvd_verify_assert
void jacobisvd_verify_assert(const MatrixType &m)
Definition: jacobisvd.cpp:36
CALL_SUBTEST_5
#define CALL_SUBTEST_5(FUNC)
Definition: split_test_helper.h:28
Eigen::g_repeat
static int g_repeat
Definition: main.h:169
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
Foo::Bar
Definition: jacobisvd.cpp:80
CALL_SUBTEST_6
#define CALL_SUBTEST_6(FUNC)
Definition: split_test_helper.h:34
CALL_SUBTEST_2
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
jacobisvd
void jacobisvd(const MatrixType &a=MatrixType(), bool pickrandom=true)
Definition: jacobisvd.cpp:23
VERIFY_IS_APPROX
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:15
RealScalar
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:47
Eigen::JacobiSVD
Two-sided Jacobi SVD decomposition of a rectangular matrix.
Definition: ForwardDeclarations.h:278
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
Eigen::ComputeThinV
@ ComputeThinV
Definition: Constants.h:399
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(jacobisvd)
Definition: jacobisvd.cpp:92
EIGEN_NOT_A_MACRO
#define EIGEN_NOT_A_MACRO
Definition: Macros.h:896
main.h
EIGEN_TEST_MAX_SIZE
#define EIGEN_TEST_MAX_SIZE
Definition: boostmultiprec.cpp:16
CALL_SUBTEST_12
#define CALL_SUBTEST_12(FUNC)
Definition: split_test_helper.h:70
svd_test_all_computation_options
void svd_test_all_computation_options(const MatrixType &m, bool full_only)
Definition: svd_common.h:244
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
msvc_workaround
void msvc_workaround()
Definition: jacobisvd.cpp:85
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
CALL_SUBTEST_7
#define CALL_SUBTEST_7(FUNC)
Definition: split_test_helper.h:40
CALL_SUBTEST_8
#define CALL_SUBTEST_8(FUNC)
Definition: split_test_helper.h:46
max
#define max(a, b)
Definition: datatypes.h:20
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
TEST_SET_BUT_UNUSED_VARIABLE
#define TEST_SET_BUT_UNUSED_VARIABLE(X)
Definition: main.h:121
CALL_SUBTEST
#define CALL_SUBTEST(FUNC)
Definition: main.h:399
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:02:34