array_for_matrix.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-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 
12 template<typename MatrixType> void array_for_matrix(const MatrixType& m)
13 {
14  typedef typename MatrixType::Scalar Scalar;
17 
18  Index rows = m.rows();
19  Index cols = m.cols();
20 
21  MatrixType m1 = MatrixType::Random(rows, cols),
22  m2 = MatrixType::Random(rows, cols),
23  m3(rows, cols);
24 
25  ColVectorType cv1 = ColVectorType::Random(rows);
26  RowVectorType rv1 = RowVectorType::Random(cols);
27 
28  Scalar s1 = internal::random<Scalar>(),
29  s2 = internal::random<Scalar>();
30 
31  // scalar addition
32  VERIFY_IS_APPROX(m1.array() + s1, s1 + m1.array());
33  VERIFY_IS_APPROX((m1.array() + s1).matrix(), MatrixType::Constant(rows,cols,s1) + m1);
34  VERIFY_IS_APPROX(((m1*Scalar(2)).array() - s2).matrix(), (m1+m1) - MatrixType::Constant(rows,cols,s2) );
35  m3 = m1;
36  m3.array() += s2;
37  VERIFY_IS_APPROX(m3, (m1.array() + s2).matrix());
38  m3 = m1;
39  m3.array() -= s1;
40  VERIFY_IS_APPROX(m3, (m1.array() - s1).matrix());
41 
42  // reductions
43  VERIFY_IS_MUCH_SMALLER_THAN(m1.colwise().sum().sum() - m1.sum(), m1.squaredNorm());
44  VERIFY_IS_MUCH_SMALLER_THAN(m1.rowwise().sum().sum() - m1.sum(), m1.squaredNorm());
45  VERIFY_IS_MUCH_SMALLER_THAN(m1.colwise().sum() + m2.colwise().sum() - (m1+m2).colwise().sum(), (m1+m2).squaredNorm());
46  VERIFY_IS_MUCH_SMALLER_THAN(m1.rowwise().sum() - m2.rowwise().sum() - (m1-m2).rowwise().sum(), (m1-m2).squaredNorm());
47  VERIFY_IS_APPROX(m1.colwise().sum(), m1.colwise().redux(internal::scalar_sum_op<Scalar,Scalar>()));
48 
49  // vector-wise ops
50  m3 = m1;
51  VERIFY_IS_APPROX(m3.colwise() += cv1, m1.colwise() + cv1);
52  m3 = m1;
53  VERIFY_IS_APPROX(m3.colwise() -= cv1, m1.colwise() - cv1);
54  m3 = m1;
55  VERIFY_IS_APPROX(m3.rowwise() += rv1, m1.rowwise() + rv1);
56  m3 = m1;
57  VERIFY_IS_APPROX(m3.rowwise() -= rv1, m1.rowwise() - rv1);
58 
59  // empty objects
60  VERIFY_IS_APPROX((m1.template block<0,Dynamic>(0,0,0,cols).colwise().sum()), RowVectorType::Zero(cols));
61  VERIFY_IS_APPROX((m1.template block<Dynamic,0>(0,0,rows,0).rowwise().sum()), ColVectorType::Zero(rows));
62  VERIFY_IS_APPROX((m1.template block<0,Dynamic>(0,0,0,cols).colwise().prod()), RowVectorType::Ones(cols));
63  VERIFY_IS_APPROX((m1.template block<Dynamic,0>(0,0,rows,0).rowwise().prod()), ColVectorType::Ones(rows));
64 
65  VERIFY_IS_APPROX(m1.block(0,0,0,cols).colwise().sum(), RowVectorType::Zero(cols));
66  VERIFY_IS_APPROX(m1.block(0,0,rows,0).rowwise().sum(), ColVectorType::Zero(rows));
67  VERIFY_IS_APPROX(m1.block(0,0,0,cols).colwise().prod(), RowVectorType::Ones(cols));
68  VERIFY_IS_APPROX(m1.block(0,0,rows,0).rowwise().prod(), ColVectorType::Ones(rows));
69 
70  // verify the const accessors exist
71  const Scalar& ref_m1 = m.matrix().array().coeffRef(0);
72  const Scalar& ref_m2 = m.matrix().array().coeffRef(0,0);
73  const Scalar& ref_a1 = m.array().matrix().coeffRef(0);
74  const Scalar& ref_a2 = m.array().matrix().coeffRef(0,0);
75  VERIFY(&ref_a1 == &ref_m1);
76  VERIFY(&ref_a2 == &ref_m2);
77 
78  // Check write accessors:
79  m1.array().coeffRef(0,0) = 1;
80  VERIFY_IS_APPROX(m1(0,0),Scalar(1));
81  m1.array()(0,0) = 2;
82  VERIFY_IS_APPROX(m1(0,0),Scalar(2));
83  m1.array().matrix().coeffRef(0,0) = 3;
84  VERIFY_IS_APPROX(m1(0,0),Scalar(3));
85  m1.array().matrix()(0,0) = 4;
86  VERIFY_IS_APPROX(m1(0,0),Scalar(4));
87 }
88 
89 template<typename MatrixType> void comparisons(const MatrixType& m)
90 {
91  using std::abs;
92  typedef typename MatrixType::Scalar Scalar;
93  typedef typename NumTraits<Scalar>::Real RealScalar;
94 
95  Index rows = m.rows();
96  Index cols = m.cols();
97 
98  Index r = internal::random<Index>(0, rows-1),
99  c = internal::random<Index>(0, cols-1);
100 
101  MatrixType m1 = MatrixType::Random(rows, cols),
102  m2 = MatrixType::Random(rows, cols),
103  m3(rows, cols);
104 
105  VERIFY(((m1.array() + Scalar(1)) > m1.array()).all());
106  VERIFY(((m1.array() - Scalar(1)) < m1.array()).all());
107  if (rows*cols>1)
108  {
109  m3 = m1;
110  m3(r,c) += 1;
111  VERIFY(! (m1.array() < m3.array()).all() );
112  VERIFY(! (m1.array() > m3.array()).all() );
113  }
114 
115  // comparisons to scalar
116  VERIFY( (m1.array() != (m1(r,c)+1) ).any() );
117  VERIFY( (m1.array() > (m1(r,c)-1) ).any() );
118  VERIFY( (m1.array() < (m1(r,c)+1) ).any() );
119  VERIFY( (m1.array() == m1(r,c) ).any() );
120  VERIFY( m1.cwiseEqual(m1(r,c)).any() );
121 
122  // test Select
123  VERIFY_IS_APPROX( (m1.array()<m2.array()).select(m1,m2), m1.cwiseMin(m2) );
124  VERIFY_IS_APPROX( (m1.array()>m2.array()).select(m1,m2), m1.cwiseMax(m2) );
125  Scalar mid = (m1.cwiseAbs().minCoeff() + m1.cwiseAbs().maxCoeff())/Scalar(2);
126  for (int j=0; j<cols; ++j)
127  for (int i=0; i<rows; ++i)
128  m3(i,j) = abs(m1(i,j))<mid ? 0 : m1(i,j);
129  VERIFY_IS_APPROX( (m1.array().abs()<MatrixType::Constant(rows,cols,mid).array())
130  .select(MatrixType::Zero(rows,cols),m1), m3);
131  // shorter versions:
132  VERIFY_IS_APPROX( (m1.array().abs()<MatrixType::Constant(rows,cols,mid).array())
133  .select(0,m1), m3);
134  VERIFY_IS_APPROX( (m1.array().abs()>=MatrixType::Constant(rows,cols,mid).array())
135  .select(m1,0), m3);
136  // even shorter version:
137  VERIFY_IS_APPROX( (m1.array().abs()<mid).select(0,m1), m3);
138 
139  // count
140  VERIFY(((m1.array().abs()+1)>RealScalar(0.1)).count() == rows*cols);
141 
142  // and/or
143  VERIFY( ((m1.array()<RealScalar(0)).matrix() && (m1.array()>RealScalar(0)).matrix()).count() == 0);
144  VERIFY( ((m1.array()<RealScalar(0)).matrix() || (m1.array()>=RealScalar(0)).matrix()).count() == rows*cols);
145  RealScalar a = m1.cwiseAbs().mean();
146  VERIFY( ((m1.array()<-a).matrix() || (m1.array()>a).matrix()).count() == (m1.cwiseAbs().array()>a).count());
147 
148  typedef Matrix<Index, Dynamic, 1> VectorOfIndices;
149 
150  // TODO allows colwise/rowwise for array
151  VERIFY_IS_APPROX(((m1.array().abs()+1)>RealScalar(0.1)).matrix().colwise().count(), VectorOfIndices::Constant(cols,rows).transpose());
152  VERIFY_IS_APPROX(((m1.array().abs()+1)>RealScalar(0.1)).matrix().rowwise().count(), VectorOfIndices::Constant(rows, cols));
153 }
154 
155 template<typename VectorType> void lpNorm(const VectorType& v)
156 {
157  using std::sqrt;
158  typedef typename VectorType::RealScalar RealScalar;
159  VectorType u = VectorType::Random(v.size());
160 
161  if(v.size()==0)
162  {
163  VERIFY_IS_APPROX(u.template lpNorm<Infinity>(), RealScalar(0));
164  VERIFY_IS_APPROX(u.template lpNorm<1>(), RealScalar(0));
165  VERIFY_IS_APPROX(u.template lpNorm<2>(), RealScalar(0));
166  VERIFY_IS_APPROX(u.template lpNorm<5>(), RealScalar(0));
167  }
168  else
169  {
170  VERIFY_IS_APPROX(u.template lpNorm<Infinity>(), u.cwiseAbs().maxCoeff());
171  }
172 
173  VERIFY_IS_APPROX(u.template lpNorm<1>(), u.cwiseAbs().sum());
174  VERIFY_IS_APPROX(u.template lpNorm<2>(), sqrt(u.array().abs().square().sum()));
175  VERIFY_IS_APPROX(numext::pow(u.template lpNorm<5>(), typename VectorType::RealScalar(5)), u.array().abs().pow(5).sum());
176 }
177 
178 template<typename MatrixType> void cwise_min_max(const MatrixType& m)
179 {
180  typedef typename MatrixType::Scalar Scalar;
181 
182  Index rows = m.rows();
183  Index cols = m.cols();
184 
185  MatrixType m1 = MatrixType::Random(rows, cols);
186 
187  // min/max with array
188  Scalar maxM1 = m1.maxCoeff();
189  Scalar minM1 = m1.minCoeff();
190 
191  VERIFY_IS_APPROX(MatrixType::Constant(rows,cols, minM1), m1.cwiseMin(MatrixType::Constant(rows,cols, minM1)));
192  VERIFY_IS_APPROX(m1, m1.cwiseMin(MatrixType::Constant(rows,cols, maxM1)));
193 
194  VERIFY_IS_APPROX(MatrixType::Constant(rows,cols, maxM1), m1.cwiseMax(MatrixType::Constant(rows,cols, maxM1)));
195  VERIFY_IS_APPROX(m1, m1.cwiseMax(MatrixType::Constant(rows,cols, minM1)));
196 
197  // min/max with scalar input
198  VERIFY_IS_APPROX(MatrixType::Constant(rows,cols, minM1), m1.cwiseMin( minM1));
199  VERIFY_IS_APPROX(m1, m1.cwiseMin(maxM1));
200  VERIFY_IS_APPROX(-m1, (-m1).cwiseMin(-minM1));
201  VERIFY_IS_APPROX(-m1.array(), ((-m1).array().min)( -minM1));
202 
203  VERIFY_IS_APPROX(MatrixType::Constant(rows,cols, maxM1), m1.cwiseMax( maxM1));
204  VERIFY_IS_APPROX(m1, m1.cwiseMax(minM1));
205  VERIFY_IS_APPROX(-m1, (-m1).cwiseMax(-maxM1));
206  VERIFY_IS_APPROX(-m1.array(), ((-m1).array().max)(-maxM1));
207 
208  VERIFY_IS_APPROX(MatrixType::Constant(rows,cols, minM1).array(), (m1.array().min)( minM1));
209  VERIFY_IS_APPROX(m1.array(), (m1.array().min)( maxM1));
210 
211  VERIFY_IS_APPROX(MatrixType::Constant(rows,cols, maxM1).array(), (m1.array().max)( maxM1));
212  VERIFY_IS_APPROX(m1.array(), (m1.array().max)( minM1));
213 
214 }
215 
216 template<typename MatrixTraits> void resize(const MatrixTraits& t)
217 {
218  typedef typename MatrixTraits::Scalar Scalar;
220  typedef Array<Scalar,Dynamic,Dynamic> Array2DType;
222  typedef Array<Scalar,Dynamic,1> Array1DType;
223 
224  Index rows = t.rows(), cols = t.cols();
225 
226  MatrixType m(rows,cols);
227  VectorType v(rows);
228  Array2DType a2(rows,cols);
229  Array1DType a1(rows);
230 
231  m.array().resize(rows+1,cols+1);
232  VERIFY(m.rows()==rows+1 && m.cols()==cols+1);
233  a2.matrix().resize(rows+1,cols+1);
234  VERIFY(a2.rows()==rows+1 && a2.cols()==cols+1);
235  v.array().resize(cols);
236  VERIFY(v.size()==cols);
237  a1.matrix().resize(cols);
238  VERIFY(a1.size()==cols);
239 }
240 
241 template<int>
243 {
244  ArrayXf a = RowVectorXf(3);
245  VectorXf v = Array<float,1,Dynamic>(3);
246 }
247 
248 // Check propagation of LvalueBit through Array/Matrix-Wrapper
249 template<int>
251 {
252  const Matrix4i M;
253  const Array4i A;
255  MA.row(0);
257  AM.row(0);
258 
259  VERIFY((internal::traits<ArrayWrapper<const Matrix4i> >::Flags&LvalueBit)==0);
260  VERIFY((internal::traits<MatrixWrapper<const Array4i> >::Flags&LvalueBit)==0);
261 
262  VERIFY((internal::traits<ArrayWrapper<Matrix4i> >::Flags&LvalueBit)==LvalueBit);
263  VERIFY((internal::traits<MatrixWrapper<Array4i> >::Flags&LvalueBit)==LvalueBit);
264 }
265 
267 {
268  for(int i = 0; i < g_repeat; i++) {
270  CALL_SUBTEST_2( array_for_matrix(Matrix2f()) );
271  CALL_SUBTEST_3( array_for_matrix(Matrix4d()) );
272  CALL_SUBTEST_4( array_for_matrix(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
273  CALL_SUBTEST_5( array_for_matrix(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
274  CALL_SUBTEST_6( array_for_matrix(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
275  }
276  for(int i = 0; i < g_repeat; i++) {
278  CALL_SUBTEST_2( comparisons(Matrix2f()) );
279  CALL_SUBTEST_3( comparisons(Matrix4d()) );
280  CALL_SUBTEST_5( comparisons(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
281  CALL_SUBTEST_6( comparisons(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
282  }
283  for(int i = 0; i < g_repeat; i++) {
285  CALL_SUBTEST_2( cwise_min_max(Matrix2f()) );
286  CALL_SUBTEST_3( cwise_min_max(Matrix4d()) );
287  CALL_SUBTEST_5( cwise_min_max(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
288  CALL_SUBTEST_6( cwise_min_max(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
289  }
290  for(int i = 0; i < g_repeat; i++) {
292  CALL_SUBTEST_2( lpNorm(Vector2f()) );
293  CALL_SUBTEST_7( lpNorm(Vector3d()) );
294  CALL_SUBTEST_8( lpNorm(Vector4f()) );
295  CALL_SUBTEST_5( lpNorm(VectorXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
296  CALL_SUBTEST_4( lpNorm(VectorXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
297  }
298  CALL_SUBTEST_5( lpNorm(VectorXf(0)) );
299  CALL_SUBTEST_4( lpNorm(VectorXcf(0)) );
300  for(int i = 0; i < g_repeat; i++) {
301  CALL_SUBTEST_4( resize(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
302  CALL_SUBTEST_5( resize(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
303  CALL_SUBTEST_6( resize(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
304  }
305  CALL_SUBTEST_6( regression_bug_654<0>() );
306  CALL_SUBTEST_6( regrrssion_bug_1410<0>() );
307 }
Point2 a1
Definition: testPose2.cpp:769
Matrix3f m
int array[24]
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const CwiseBinaryOp< internal::scalar_max_op< Scalar, Scalar >, const Derived, const OtherDerived > cwiseMax(const EIGEN_CURRENT_STORAGE_BASE_CLASS< OtherDerived > &other) const
SCALAR Scalar
Definition: bench_gemm.cpp:46
#define CALL_SUBTEST_6(FUNC)
#define CALL_SUBTEST_4(FUNC)
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:51
static const Eigen::internal::all_t all
Expression of a mathematical vector or matrix as an array object.
Definition: ArrayWrapper.h:42
#define CALL_SUBTEST_3(FUNC)
MatrixType m2(n_dims)
void regrrssion_bug_1410()
#define CALL_SUBTEST_7(FUNC)
const unsigned int LvalueBit
Definition: Constants.h:144
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
EIGEN_DEVICE_FUNC ArrayBase< Derived > & array()
Definition: ArrayBase.h:141
MatrixXf MatrixType
EIGEN_DEVICE_FUNC MatrixBase< Derived > & matrix()
Definition: MatrixBase.h:314
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:232
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:48
Expression of an array as a mathematical vector or matrix.
Definition: ArrayBase.h:15
#define VERIFY_IS_APPROX(a, b)
void lpNorm(const VectorType &v)
#define CALL_SUBTEST_1(FUNC)
Matrix3d m1
Definition: IOFormat.cpp:2
void resize(const MatrixTraits &t)
static int g_repeat
Definition: main.h:169
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
Array< int, Dynamic, 1 > v
#define CALL_SUBTEST_8(FUNC)
void regression_bug_654()
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:47
#define VERIFY_IS_MUCH_SMALLER_THAN(a, b)
Definition: main.h:390
Point2 a2
Definition: testPose2.cpp:770
#define CALL_SUBTEST_5(FUNC)
#define VERIFY(a)
Definition: main.h:380
#define EIGEN_TEST_MAX_SIZE
void array_for_matrix(const MatrixType &m)
General-purpose arrays with easy API for coefficient-wise operations.
Definition: Array.h:45
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE RowXpr row(Index i)
Definition: DenseBase.h:1119
EIGEN_DECLARE_TEST(array_for_matrix)
#define CALL_SUBTEST_2(FUNC)
void cwise_min_max(const MatrixType &m)
Jet< T, N > sqrt(const Jet< T, N > &f)
Definition: jet.h:418
Jet< T, N > pow(const Jet< T, N > &f, double g)
Definition: jet.h:570
Map< Matrix< T, Dynamic, Dynamic, ColMajor >, 0, OuterStride<> > matrix(T *data, int rows, int cols, int stride)
The matrix class, also used for vectors and row-vectors.
#define abs(x)
Definition: datatypes.h:17
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const CwiseBinaryOp< internal::scalar_min_op< Scalar, Scalar >, const Derived, const OtherDerived > cwiseMin(const EIGEN_CURRENT_STORAGE_BASE_CLASS< OtherDerived > &other) const
std::ptrdiff_t j
Point2 t(10, 10)
void comparisons(const MatrixType &m)


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:33:54