1 // This file is part of Eigen, a lightweight C++ template library
4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2006-2008 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 // this hack is needed to make this file compiles with -pedantic (gcc)
16 #ifdef __INTEL_COMPILER
17 // disable "warning #76: argument to macro is empty" produced by the above hack
18 #pragma warning disable 76
21 // discard stack allocation as that too bypasses malloc
22 #define EIGEN_STACK_ALLOCATION_LIMIT 0
23 // any heap allocation will raise an assert
24 #define EIGEN_NO_MALLOC
27 #include <Eigen/Cholesky>
28 #include <Eigen/Eigenvalues>
33 template<typename MatrixType
> void nomalloc(const MatrixType
& m
)
35 /* this test check no dynamic memory allocation are issued with fixed-size matrices
37 typedef typename
MatrixType::Index Index
;
38 typedef typename
MatrixType::Scalar Scalar
;
40 Index rows
= m
.rows();
41 Index cols
= m
.cols();
43 MatrixType m1
= MatrixType::Random(rows
, cols
),
44 m2
= MatrixType::Random(rows
, cols
),
47 Scalar s1
= internal::random
<Scalar
>();
49 Index r
= internal::random
<Index
>(0, rows
-1),
50 c
= internal::random
<Index
>(0, cols
-1);
52 VERIFY_IS_APPROX((m1
+m2
)*s1
, s1
*m1
+s1
*m2
);
53 VERIFY_IS_APPROX((m1
+m2
)(r
,c
), (m1(r
,c
))+(m2(r
,c
)));
54 VERIFY_IS_APPROX(m1
.cwiseProduct(m1
.block(0,0,rows
,cols
)), (m1
.array()*m1
.array()).matrix());
55 VERIFY_IS_APPROX((m1
*m1
.transpose())*m2
, m1
*(m1
.transpose()*m2
));
57 m2
.col(0).noalias() = m1
* m1
.col(0);
58 m2
.col(0).noalias() -= m1
.adjoint() * m1
.col(0);
59 m2
.col(0).noalias() -= m1
* m1
.row(0).adjoint();
60 m2
.col(0).noalias() -= m1
.adjoint() * m1
.row(0).adjoint();
62 m2
.row(0).noalias() = m1
.row(0) * m1
;
63 m2
.row(0).noalias() -= m1
.row(0) * m1
.adjoint();
64 m2
.row(0).noalias() -= m1
.col(0).adjoint() * m1
;
65 m2
.row(0).noalias() -= m1
.col(0).adjoint() * m1
.adjoint();
66 VERIFY_IS_APPROX(m2
,m2
);
68 m2
.col(0).noalias() = m1
.template triangularView
<Upper
>() * m1
.col(0);
69 m2
.col(0).noalias() -= m1
.adjoint().template triangularView
<Upper
>() * m1
.col(0);
70 m2
.col(0).noalias() -= m1
.template triangularView
<Upper
>() * m1
.row(0).adjoint();
71 m2
.col(0).noalias() -= m1
.adjoint().template triangularView
<Upper
>() * m1
.row(0).adjoint();
73 m2
.row(0).noalias() = m1
.row(0) * m1
.template triangularView
<Upper
>();
74 m2
.row(0).noalias() -= m1
.row(0) * m1
.adjoint().template triangularView
<Upper
>();
75 m2
.row(0).noalias() -= m1
.col(0).adjoint() * m1
.template triangularView
<Upper
>();
76 m2
.row(0).noalias() -= m1
.col(0).adjoint() * m1
.adjoint().template triangularView
<Upper
>();
77 VERIFY_IS_APPROX(m2
,m2
);
79 m2
.col(0).noalias() = m1
.template selfadjointView
<Upper
>() * m1
.col(0);
80 m2
.col(0).noalias() -= m1
.adjoint().template selfadjointView
<Upper
>() * m1
.col(0);
81 m2
.col(0).noalias() -= m1
.template selfadjointView
<Upper
>() * m1
.row(0).adjoint();
82 m2
.col(0).noalias() -= m1
.adjoint().template selfadjointView
<Upper
>() * m1
.row(0).adjoint();
84 m2
.row(0).noalias() = m1
.row(0) * m1
.template selfadjointView
<Upper
>();
85 m2
.row(0).noalias() -= m1
.row(0) * m1
.adjoint().template selfadjointView
<Upper
>();
86 m2
.row(0).noalias() -= m1
.col(0).adjoint() * m1
.template selfadjointView
<Upper
>();
87 m2
.row(0).noalias() -= m1
.col(0).adjoint() * m1
.adjoint().template selfadjointView
<Upper
>();
88 VERIFY_IS_APPROX(m2
,m2
);
90 m2
.template selfadjointView
<Lower
>().rankUpdate(m1
.col(0),-1);
91 m2
.template selfadjointView
<Lower
>().rankUpdate(m1
.row(0),-1);
93 // The following fancy matrix-matrix products are not safe yet regarding static allocation
94 // m1 += m1.template triangularView<Upper>() * m2.col(;
95 // m1.template selfadjointView<Lower>().rankUpdate(m2);
96 // m1 += m1.template triangularView<Upper>() * m2;
97 // m1 += m1.template selfadjointView<Lower>() * m2;
98 // VERIFY_IS_APPROX(m1,m1);
101 template<typename Scalar
>
102 void ctms_decompositions()
104 const int maxSize
= 16;
107 typedef Eigen::Matrix
<Scalar
,
108 Eigen::Dynamic
, Eigen::Dynamic
,
110 maxSize
, maxSize
> Matrix
;
112 typedef Eigen::Matrix
<Scalar
,
117 typedef Eigen::Matrix
<std::complex<Scalar
>,
118 Eigen::Dynamic
, Eigen::Dynamic
,
120 maxSize
, maxSize
> ComplexMatrix
;
122 const Matrix
A(Matrix::Random(size
, size
)), B(Matrix::Random(size
, size
));
124 const ComplexMatrix
complexA(ComplexMatrix::Random(size
, size
));
125 const Matrix saA
= A
.adjoint() * A
;
126 const Vector
b(Vector::Random(size
));
130 Eigen::LLT
<Matrix
> LLT
; LLT
.compute(A
);
133 Eigen::LDLT
<Matrix
> LDLT
; LDLT
.compute(A
);
137 // Eigenvalues module
138 Eigen::HessenbergDecomposition
<ComplexMatrix
> hessDecomp
; hessDecomp
.compute(complexA
);
139 Eigen::ComplexSchur
<ComplexMatrix
> cSchur(size
); cSchur
.compute(complexA
);
140 Eigen::ComplexEigenSolver
<ComplexMatrix
> cEigSolver
; cEigSolver
.compute(complexA
);
141 Eigen::EigenSolver
<Matrix
> eigSolver
; eigSolver
.compute(A
);
142 Eigen::SelfAdjointEigenSolver
<Matrix
> saEigSolver(size
); saEigSolver
.compute(saA
);
143 Eigen::Tridiagonalization
<Matrix
> tridiag
; tridiag
.compute(saA
);
146 Eigen::PartialPivLU
<Matrix
> ppLU
; ppLU
.compute(A
);
149 Eigen::FullPivLU
<Matrix
> fpLU
; fpLU
.compute(A
);
154 Eigen::HouseholderQR
<Matrix
> hQR
; hQR
.compute(A
);
157 Eigen::ColPivHouseholderQR
<Matrix
> cpQR
; cpQR
.compute(A
);
160 Eigen::FullPivHouseholderQR
<Matrix
> fpQR
; fpQR
.compute(A
);
161 // FIXME X = fpQR.solve(B);
165 Eigen::JacobiSVD
<Matrix
> jSVD
; jSVD
.compute(A
, ComputeFullU
| ComputeFullV
);
168 void test_zerosized() {
169 // default constructors:
172 // explicit zero-sized:
173 Eigen::ArrayXXd
A0(0,0);
174 Eigen::ArrayXd
v0(std::ptrdiff_t(0)); // FIXME ArrayXd(0) is ambiguous
176 // assigning empty objects to each other:
181 template<typename MatrixType
> void test_reference(const MatrixType
& m
) {
182 typedef typename
MatrixType::Scalar Scalar
;
183 enum { Flag
= MatrixType::IsRowMajor
? Eigen::RowMajor
: Eigen::ColMajor
};
184 enum { TransposeFlag
= !MatrixType::IsRowMajor
? Eigen::RowMajor
: Eigen::ColMajor
};
185 typename
MatrixType::Index rows
= m
.rows(), cols
=m
.cols();
186 // Dynamic reference:
187 typedef Eigen::Ref
<const Eigen::Matrix
<Scalar
, Eigen::Dynamic
, Eigen::Dynamic
, Flag
> > Ref
;
188 typedef Eigen::Ref
<const Eigen::Matrix
<Scalar
, Eigen::Dynamic
, Eigen::Dynamic
, TransposeFlag
> > RefT
;
191 Ref
r2(m
.block(rows
/3, cols
/4, rows
/2, cols
/2));
192 RefT
r3(m
.transpose());
193 RefT
r4(m
.topLeftCorner(rows
/2, cols
/2).transpose());
195 VERIFY_RAISES_ASSERT(RefT
r5(m
));
196 VERIFY_RAISES_ASSERT(Ref
r6(m
.transpose()));
197 VERIFY_RAISES_ASSERT(Ref
r7(Scalar(2) * m
));
202 // check that our operator new is indeed called:
203 VERIFY_RAISES_ASSERT(MatrixXd
dummy(MatrixXd::Random(3,3)));
204 CALL_SUBTEST_1(nomalloc(Matrix
<float, 1, 1>()) );
205 CALL_SUBTEST_2(nomalloc(Matrix4d()) );
206 CALL_SUBTEST_3(nomalloc(Matrix
<float,32,32>()) );
208 // Check decomposition modules with dynamic matrices that have a known compile-time max size (ctms)
209 CALL_SUBTEST_4(ctms_decompositions
<float>());
210 CALL_SUBTEST_5(test_zerosized());
211 CALL_SUBTEST_6(test_reference(Matrix
<float,32,32>()));