1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra. Eigen itself is part of the KDE project.
3 //
4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10 #include "main.h"
11
triangular(const MatrixType & m)12 template<typename MatrixType> void triangular(const MatrixType& m)
13 {
14 typedef typename MatrixType::Scalar Scalar;
15 typedef typename NumTraits<Scalar>::Real RealScalar;
16 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
17
18 RealScalar largerEps = 10*test_precision<RealScalar>();
19
20 int rows = m.rows();
21 int cols = m.cols();
22
23 MatrixType m1 = MatrixType::Random(rows, cols),
24 m2 = MatrixType::Random(rows, cols),
25 m3(rows, cols),
26 m4(rows, cols),
27 r1(rows, cols),
28 r2(rows, cols),
29 mzero = MatrixType::Zero(rows, cols),
30 mones = MatrixType::Ones(rows, cols),
31 identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
32 ::Identity(rows, rows),
33 square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
34 ::Random(rows, rows);
35 VectorType v1 = VectorType::Random(rows),
36 v2 = VectorType::Random(rows),
37 vzero = VectorType::Zero(rows);
38
39 MatrixType m1up = m1.template part<Eigen::UpperTriangular>();
40 MatrixType m2up = m2.template part<Eigen::UpperTriangular>();
41
42 if (rows*cols>1)
43 {
44 VERIFY(m1up.isUpperTriangular());
45 VERIFY(m2up.transpose().isLowerTriangular());
46 VERIFY(!m2.isLowerTriangular());
47 }
48
49 // VERIFY_IS_APPROX(m1up.transpose() * m2, m1.upper().transpose().lower() * m2);
50
51 // test overloaded operator+=
52 r1.setZero();
53 r2.setZero();
54 r1.template part<Eigen::UpperTriangular>() += m1;
55 r2 += m1up;
56 VERIFY_IS_APPROX(r1,r2);
57
58 // test overloaded operator=
59 m1.setZero();
60 m1.template part<Eigen::UpperTriangular>() = (m2.transpose() * m2).lazy();
61 m3 = m2.transpose() * m2;
62 VERIFY_IS_APPROX(m3.template part<Eigen::LowerTriangular>().transpose(), m1);
63
64 // test overloaded operator=
65 m1.setZero();
66 m1.template part<Eigen::LowerTriangular>() = (m2.transpose() * m2).lazy();
67 VERIFY_IS_APPROX(m3.template part<Eigen::LowerTriangular>(), m1);
68
69 VERIFY_IS_APPROX(m3.template part<Diagonal>(), m3.diagonal().asDiagonal());
70
71 m1 = MatrixType::Random(rows, cols);
72 for (int i=0; i<rows; ++i)
73 while (ei_abs2(m1(i,i))<1e-3) m1(i,i) = ei_random<Scalar>();
74
75 Transpose<MatrixType> trm4(m4);
76 // test back and forward subsitution
77 m3 = m1.template part<Eigen::LowerTriangular>();
78 VERIFY(m3.template marked<Eigen::LowerTriangular>().solveTriangular(m3).cwise().abs().isIdentity(test_precision<RealScalar>()));
79 VERIFY(m3.transpose().template marked<Eigen::UpperTriangular>()
80 .solveTriangular(m3.transpose()).cwise().abs().isIdentity(test_precision<RealScalar>()));
81 // check M * inv(L) using in place API
82 m4 = m3;
83 m3.transpose().template marked<Eigen::UpperTriangular>().solveTriangularInPlace(trm4);
84 VERIFY(m4.cwise().abs().isIdentity(test_precision<RealScalar>()));
85
86 m3 = m1.template part<Eigen::UpperTriangular>();
87 VERIFY(m3.template marked<Eigen::UpperTriangular>().solveTriangular(m3).cwise().abs().isIdentity(test_precision<RealScalar>()));
88 VERIFY(m3.transpose().template marked<Eigen::LowerTriangular>()
89 .solveTriangular(m3.transpose()).cwise().abs().isIdentity(test_precision<RealScalar>()));
90 // check M * inv(U) using in place API
91 m4 = m3;
92 m3.transpose().template marked<Eigen::LowerTriangular>().solveTriangularInPlace(trm4);
93 VERIFY(m4.cwise().abs().isIdentity(test_precision<RealScalar>()));
94
95 m3 = m1.template part<Eigen::UpperTriangular>();
96 VERIFY(m2.isApprox(m3 * (m3.template marked<Eigen::UpperTriangular>().solveTriangular(m2)), largerEps));
97 m3 = m1.template part<Eigen::LowerTriangular>();
98 VERIFY(m2.isApprox(m3 * (m3.template marked<Eigen::LowerTriangular>().solveTriangular(m2)), largerEps));
99
100 VERIFY((m1.template part<Eigen::UpperTriangular>() * m2.template part<Eigen::UpperTriangular>()).isUpperTriangular());
101
102 // test swap
103 m1.setOnes();
104 m2.setZero();
105 m2.template part<Eigen::UpperTriangular>().swap(m1);
106 m3.setZero();
107 m3.template part<Eigen::UpperTriangular>().setOnes();
108 VERIFY_IS_APPROX(m2,m3);
109
110 }
111
selfadjoint()112 void selfadjoint()
113 {
114 Matrix2i m;
115 m << 1, 2,
116 3, 4;
117
118 Matrix2i m1 = Matrix2i::Zero();
119 m1.part<SelfAdjoint>() = m;
120 Matrix2i ref1;
121 ref1 << 1, 2,
122 2, 4;
123 VERIFY(m1 == ref1);
124
125 Matrix2i m2 = Matrix2i::Zero();
126 m2.part<SelfAdjoint>() = m.part<UpperTriangular>();
127 Matrix2i ref2;
128 ref2 << 1, 2,
129 2, 4;
130 VERIFY(m2 == ref2);
131
132 Matrix2i m3 = Matrix2i::Zero();
133 m3.part<SelfAdjoint>() = m.part<LowerTriangular>();
134 Matrix2i ref3;
135 ref3 << 1, 0,
136 0, 4;
137 VERIFY(m3 == ref3);
138
139 // example inspired from bug 159
140 int array[] = {1, 2, 3, 4};
141 Matrix2i::Map(array).part<SelfAdjoint>() = Matrix2i::Random().part<LowerTriangular>();
142
143 std::cout << "hello\n" << array << std::endl;
144 }
145
test_eigen2_triangular()146 void test_eigen2_triangular()
147 {
148 CALL_SUBTEST_8( selfadjoint() );
149 for(int i = 0; i < g_repeat ; i++) {
150 CALL_SUBTEST_1( triangular(Matrix<float, 1, 1>()) );
151 CALL_SUBTEST_2( triangular(Matrix<float, 2, 2>()) );
152 CALL_SUBTEST_3( triangular(Matrix3d()) );
153 CALL_SUBTEST_4( triangular(MatrixXcf(4, 4)) );
154 CALL_SUBTEST_5( triangular(Matrix<std::complex<float>,8, 8>()) );
155 CALL_SUBTEST_6( triangular(MatrixXd(17,17)) );
156 CALL_SUBTEST_7( triangular(Matrix<float,Dynamic,Dynamic,RowMajor>(5, 5)) );
157 }
158 }
159