1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
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
array_for_matrix(const MatrixType & m)12 template<typename MatrixType> void array_for_matrix(const MatrixType& m)
13 {
14 typedef typename MatrixType::Index Index;
15 typedef typename MatrixType::Scalar Scalar;
16 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> ColVectorType;
17 typedef Matrix<Scalar, 1, MatrixType::ColsAtCompileTime> RowVectorType;
18
19 Index rows = m.rows();
20 Index cols = m.cols();
21
22 MatrixType m1 = MatrixType::Random(rows, cols),
23 m2 = MatrixType::Random(rows, cols),
24 m3(rows, cols);
25
26 ColVectorType cv1 = ColVectorType::Random(rows);
27 RowVectorType rv1 = RowVectorType::Random(cols);
28
29 Scalar s1 = internal::random<Scalar>(),
30 s2 = internal::random<Scalar>();
31
32 // scalar addition
33 VERIFY_IS_APPROX(m1.array() + s1, s1 + m1.array());
34 VERIFY_IS_APPROX((m1.array() + s1).matrix(), MatrixType::Constant(rows,cols,s1) + m1);
35 VERIFY_IS_APPROX(((m1*Scalar(2)).array() - s2).matrix(), (m1+m1) - MatrixType::Constant(rows,cols,s2) );
36 m3 = m1;
37 m3.array() += s2;
38 VERIFY_IS_APPROX(m3, (m1.array() + s2).matrix());
39 m3 = m1;
40 m3.array() -= s1;
41 VERIFY_IS_APPROX(m3, (m1.array() - s1).matrix());
42
43 // reductions
44 VERIFY_IS_MUCH_SMALLER_THAN(m1.colwise().sum().sum() - m1.sum(), m1.squaredNorm());
45 VERIFY_IS_MUCH_SMALLER_THAN(m1.rowwise().sum().sum() - m1.sum(), m1.squaredNorm());
46 VERIFY_IS_MUCH_SMALLER_THAN(m1.colwise().sum() + m2.colwise().sum() - (m1+m2).colwise().sum(), (m1+m2).squaredNorm());
47 VERIFY_IS_MUCH_SMALLER_THAN(m1.rowwise().sum() - m2.rowwise().sum() - (m1-m2).rowwise().sum(), (m1-m2).squaredNorm());
48 VERIFY_IS_APPROX(m1.colwise().sum(), m1.colwise().redux(internal::scalar_sum_op<Scalar>()));
49
50 // vector-wise ops
51 m3 = m1;
52 VERIFY_IS_APPROX(m3.colwise() += cv1, m1.colwise() + cv1);
53 m3 = m1;
54 VERIFY_IS_APPROX(m3.colwise() -= cv1, m1.colwise() - cv1);
55 m3 = m1;
56 VERIFY_IS_APPROX(m3.rowwise() += rv1, m1.rowwise() + rv1);
57 m3 = m1;
58 VERIFY_IS_APPROX(m3.rowwise() -= rv1, m1.rowwise() - rv1);
59
60 // empty objects
61 VERIFY_IS_APPROX(m1.block(0,0,0,cols).colwise().sum(), RowVectorType::Zero(cols));
62 VERIFY_IS_APPROX(m1.block(0,0,rows,0).rowwise().prod(), ColVectorType::Ones(rows));
63
64 // verify the const accessors exist
65 const Scalar& ref_m1 = m.matrix().array().coeffRef(0);
66 const Scalar& ref_m2 = m.matrix().array().coeffRef(0,0);
67 const Scalar& ref_a1 = m.array().matrix().coeffRef(0);
68 const Scalar& ref_a2 = m.array().matrix().coeffRef(0,0);
69 VERIFY(&ref_a1 == &ref_m1);
70 VERIFY(&ref_a2 == &ref_m2);
71 }
72
comparisons(const MatrixType & m)73 template<typename MatrixType> void comparisons(const MatrixType& m)
74 {
75 using std::abs;
76 typedef typename MatrixType::Index Index;
77 typedef typename MatrixType::Scalar Scalar;
78 typedef typename NumTraits<Scalar>::Real RealScalar;
79
80 Index rows = m.rows();
81 Index cols = m.cols();
82
83 Index r = internal::random<Index>(0, rows-1),
84 c = internal::random<Index>(0, cols-1);
85
86 MatrixType m1 = MatrixType::Random(rows, cols),
87 m2 = MatrixType::Random(rows, cols),
88 m3(rows, cols);
89
90 VERIFY(((m1.array() + Scalar(1)) > m1.array()).all());
91 VERIFY(((m1.array() - Scalar(1)) < m1.array()).all());
92 if (rows*cols>1)
93 {
94 m3 = m1;
95 m3(r,c) += 1;
96 VERIFY(! (m1.array() < m3.array()).all() );
97 VERIFY(! (m1.array() > m3.array()).all() );
98 }
99
100 // comparisons to scalar
101 VERIFY( (m1.array() != (m1(r,c)+1) ).any() );
102 VERIFY( (m1.array() > (m1(r,c)-1) ).any() );
103 VERIFY( (m1.array() < (m1(r,c)+1) ).any() );
104 VERIFY( (m1.array() == m1(r,c) ).any() );
105
106 // test Select
107 VERIFY_IS_APPROX( (m1.array()<m2.array()).select(m1,m2), m1.cwiseMin(m2) );
108 VERIFY_IS_APPROX( (m1.array()>m2.array()).select(m1,m2), m1.cwiseMax(m2) );
109 Scalar mid = (m1.cwiseAbs().minCoeff() + m1.cwiseAbs().maxCoeff())/Scalar(2);
110 for (int j=0; j<cols; ++j)
111 for (int i=0; i<rows; ++i)
112 m3(i,j) = abs(m1(i,j))<mid ? 0 : m1(i,j);
113 VERIFY_IS_APPROX( (m1.array().abs()<MatrixType::Constant(rows,cols,mid).array())
114 .select(MatrixType::Zero(rows,cols),m1), m3);
115 // shorter versions:
116 VERIFY_IS_APPROX( (m1.array().abs()<MatrixType::Constant(rows,cols,mid).array())
117 .select(0,m1), m3);
118 VERIFY_IS_APPROX( (m1.array().abs()>=MatrixType::Constant(rows,cols,mid).array())
119 .select(m1,0), m3);
120 // even shorter version:
121 VERIFY_IS_APPROX( (m1.array().abs()<mid).select(0,m1), m3);
122
123 // count
124 VERIFY(((m1.array().abs()+1)>RealScalar(0.1)).count() == rows*cols);
125
126 typedef Matrix<typename MatrixType::Index, Dynamic, 1> VectorOfIndices;
127
128 // TODO allows colwise/rowwise for array
129 VERIFY_IS_APPROX(((m1.array().abs()+1)>RealScalar(0.1)).matrix().colwise().count(), VectorOfIndices::Constant(cols,rows).transpose());
130 VERIFY_IS_APPROX(((m1.array().abs()+1)>RealScalar(0.1)).matrix().rowwise().count(), VectorOfIndices::Constant(rows, cols));
131 }
132
lpNorm(const VectorType & v)133 template<typename VectorType> void lpNorm(const VectorType& v)
134 {
135 using std::sqrt;
136 VectorType u = VectorType::Random(v.size());
137
138 VERIFY_IS_APPROX(u.template lpNorm<Infinity>(), u.cwiseAbs().maxCoeff());
139 VERIFY_IS_APPROX(u.template lpNorm<1>(), u.cwiseAbs().sum());
140 VERIFY_IS_APPROX(u.template lpNorm<2>(), sqrt(u.array().abs().square().sum()));
141 VERIFY_IS_APPROX(numext::pow(u.template lpNorm<5>(), typename VectorType::RealScalar(5)), u.array().abs().pow(5).sum());
142 }
143
cwise_min_max(const MatrixType & m)144 template<typename MatrixType> void cwise_min_max(const MatrixType& m)
145 {
146 typedef typename MatrixType::Index Index;
147 typedef typename MatrixType::Scalar Scalar;
148
149 Index rows = m.rows();
150 Index cols = m.cols();
151
152 MatrixType m1 = MatrixType::Random(rows, cols);
153
154 // min/max with array
155 Scalar maxM1 = m1.maxCoeff();
156 Scalar minM1 = m1.minCoeff();
157
158 VERIFY_IS_APPROX(MatrixType::Constant(rows,cols, minM1), m1.cwiseMin(MatrixType::Constant(rows,cols, minM1)));
159 VERIFY_IS_APPROX(m1, m1.cwiseMin(MatrixType::Constant(rows,cols, maxM1)));
160
161 VERIFY_IS_APPROX(MatrixType::Constant(rows,cols, maxM1), m1.cwiseMax(MatrixType::Constant(rows,cols, maxM1)));
162 VERIFY_IS_APPROX(m1, m1.cwiseMax(MatrixType::Constant(rows,cols, minM1)));
163
164 // min/max with scalar input
165 VERIFY_IS_APPROX(MatrixType::Constant(rows,cols, minM1), m1.cwiseMin( minM1));
166 VERIFY_IS_APPROX(m1, m1.cwiseMin(maxM1));
167 VERIFY_IS_APPROX(-m1, (-m1).cwiseMin(-minM1));
168 VERIFY_IS_APPROX(-m1.array(), ((-m1).array().min)( -minM1));
169
170 VERIFY_IS_APPROX(MatrixType::Constant(rows,cols, maxM1), m1.cwiseMax( maxM1));
171 VERIFY_IS_APPROX(m1, m1.cwiseMax(minM1));
172 VERIFY_IS_APPROX(-m1, (-m1).cwiseMax(-maxM1));
173 VERIFY_IS_APPROX(-m1.array(), ((-m1).array().max)(-maxM1));
174
175 VERIFY_IS_APPROX(MatrixType::Constant(rows,cols, minM1).array(), (m1.array().min)( minM1));
176 VERIFY_IS_APPROX(m1.array(), (m1.array().min)( maxM1));
177
178 VERIFY_IS_APPROX(MatrixType::Constant(rows,cols, maxM1).array(), (m1.array().max)( maxM1));
179 VERIFY_IS_APPROX(m1.array(), (m1.array().max)( minM1));
180
181 }
182
resize(const MatrixTraits & t)183 template<typename MatrixTraits> void resize(const MatrixTraits& t)
184 {
185 typedef typename MatrixTraits::Index Index;
186 typedef typename MatrixTraits::Scalar Scalar;
187 typedef Matrix<Scalar,Dynamic,Dynamic> MatrixType;
188 typedef Array<Scalar,Dynamic,Dynamic> Array2DType;
189 typedef Matrix<Scalar,Dynamic,1> VectorType;
190 typedef Array<Scalar,Dynamic,1> Array1DType;
191
192 Index rows = t.rows(), cols = t.cols();
193
194 MatrixType m(rows,cols);
195 VectorType v(rows);
196 Array2DType a2(rows,cols);
197 Array1DType a1(rows);
198
199 m.array().resize(rows+1,cols+1);
200 VERIFY(m.rows()==rows+1 && m.cols()==cols+1);
201 a2.matrix().resize(rows+1,cols+1);
202 VERIFY(a2.rows()==rows+1 && a2.cols()==cols+1);
203 v.array().resize(cols);
204 VERIFY(v.size()==cols);
205 a1.matrix().resize(cols);
206 VERIFY(a1.size()==cols);
207 }
208
regression_bug_654()209 void regression_bug_654()
210 {
211 ArrayXf a = RowVectorXf(3);
212 VectorXf v = Array<float,1,Dynamic>(3);
213 }
214
test_array_for_matrix()215 void test_array_for_matrix()
216 {
217 for(int i = 0; i < g_repeat; i++) {
218 CALL_SUBTEST_1( array_for_matrix(Matrix<float, 1, 1>()) );
219 CALL_SUBTEST_2( array_for_matrix(Matrix2f()) );
220 CALL_SUBTEST_3( array_for_matrix(Matrix4d()) );
221 CALL_SUBTEST_4( array_for_matrix(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
222 CALL_SUBTEST_5( array_for_matrix(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
223 CALL_SUBTEST_6( array_for_matrix(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
224 }
225 for(int i = 0; i < g_repeat; i++) {
226 CALL_SUBTEST_1( comparisons(Matrix<float, 1, 1>()) );
227 CALL_SUBTEST_2( comparisons(Matrix2f()) );
228 CALL_SUBTEST_3( comparisons(Matrix4d()) );
229 CALL_SUBTEST_5( comparisons(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
230 CALL_SUBTEST_6( comparisons(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
231 }
232 for(int i = 0; i < g_repeat; i++) {
233 CALL_SUBTEST_1( cwise_min_max(Matrix<float, 1, 1>()) );
234 CALL_SUBTEST_2( cwise_min_max(Matrix2f()) );
235 CALL_SUBTEST_3( cwise_min_max(Matrix4d()) );
236 CALL_SUBTEST_5( cwise_min_max(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
237 CALL_SUBTEST_6( cwise_min_max(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
238 }
239 for(int i = 0; i < g_repeat; i++) {
240 CALL_SUBTEST_1( lpNorm(Matrix<float, 1, 1>()) );
241 CALL_SUBTEST_2( lpNorm(Vector2f()) );
242 CALL_SUBTEST_7( lpNorm(Vector3d()) );
243 CALL_SUBTEST_8( lpNorm(Vector4f()) );
244 CALL_SUBTEST_5( lpNorm(VectorXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
245 CALL_SUBTEST_4( lpNorm(VectorXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
246 }
247 for(int i = 0; i < g_repeat; i++) {
248 CALL_SUBTEST_4( resize(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
249 CALL_SUBTEST_5( resize(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
250 CALL_SUBTEST_6( resize(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
251 }
252 CALL_SUBTEST_6( regression_bug_654() );
253 }
254