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.block(0,0,0,cols).colwise().sum(), RowVectorType::Zero(cols));
61  VERIFY_IS_APPROX(m1.block(0,0,rows,0).rowwise().prod(), ColVectorType::Ones(rows));
62 
63  // verify the const accessors exist
64  const Scalar& ref_m1 = m.matrix().array().coeffRef(0);
65  const Scalar& ref_m2 = m.matrix().array().coeffRef(0,0);
66  const Scalar& ref_a1 = m.array().matrix().coeffRef(0);
67  const Scalar& ref_a2 = m.array().matrix().coeffRef(0,0);
68  VERIFY(&ref_a1 == &ref_m1);
69  VERIFY(&ref_a2 == &ref_m2);
70 
71  // Check write accessors:
72  m1.array().coeffRef(0,0) = 1;
73  VERIFY_IS_APPROX(m1(0,0),Scalar(1));
74  m1.array()(0,0) = 2;
75  VERIFY_IS_APPROX(m1(0,0),Scalar(2));
76  m1.array().matrix().coeffRef(0,0) = 3;
77  VERIFY_IS_APPROX(m1(0,0),Scalar(3));
78  m1.array().matrix()(0,0) = 4;
79  VERIFY_IS_APPROX(m1(0,0),Scalar(4));
80 }
81 
82 template<typename MatrixType> void comparisons(const MatrixType& m)
83 {
84  using std::abs;
85  typedef typename MatrixType::Scalar Scalar;
86  typedef typename NumTraits<Scalar>::Real RealScalar;
87 
88  Index rows = m.rows();
89  Index cols = m.cols();
90 
91  Index r = internal::random<Index>(0, rows-1),
92  c = internal::random<Index>(0, cols-1);
93 
94  MatrixType m1 = MatrixType::Random(rows, cols),
95  m2 = MatrixType::Random(rows, cols),
96  m3(rows, cols);
97 
98  VERIFY(((m1.array() + Scalar(1)) > m1.array()).all());
99  VERIFY(((m1.array() - Scalar(1)) < m1.array()).all());
100  if (rows*cols>1)
101  {
102  m3 = m1;
103  m3(r,c) += 1;
104  VERIFY(! (m1.array() < m3.array()).all() );
105  VERIFY(! (m1.array() > m3.array()).all() );
106  }
107 
108  // comparisons to scalar
109  VERIFY( (m1.array() != (m1(r,c)+1) ).any() );
110  VERIFY( (m1.array() > (m1(r,c)-1) ).any() );
111  VERIFY( (m1.array() < (m1(r,c)+1) ).any() );
112  VERIFY( (m1.array() == m1(r,c) ).any() );
113  VERIFY( m1.cwiseEqual(m1(r,c)).any() );
114 
115  // test Select
116  VERIFY_IS_APPROX( (m1.array()<m2.array()).select(m1,m2), m1.cwiseMin(m2) );
117  VERIFY_IS_APPROX( (m1.array()>m2.array()).select(m1,m2), m1.cwiseMax(m2) );
118  Scalar mid = (m1.cwiseAbs().minCoeff() + m1.cwiseAbs().maxCoeff())/Scalar(2);
119  for (int j=0; j<cols; ++j)
120  for (int i=0; i<rows; ++i)
121  m3(i,j) = abs(m1(i,j))<mid ? 0 : m1(i,j);
122  VERIFY_IS_APPROX( (m1.array().abs()<MatrixType::Constant(rows,cols,mid).array())
123  .select(MatrixType::Zero(rows,cols),m1), m3);
124  // shorter versions:
125  VERIFY_IS_APPROX( (m1.array().abs()<MatrixType::Constant(rows,cols,mid).array())
126  .select(0,m1), m3);
127  VERIFY_IS_APPROX( (m1.array().abs()>=MatrixType::Constant(rows,cols,mid).array())
128  .select(m1,0), m3);
129  // even shorter version:
130  VERIFY_IS_APPROX( (m1.array().abs()<mid).select(0,m1), m3);
131 
132  // count
133  VERIFY(((m1.array().abs()+1)>RealScalar(0.1)).count() == rows*cols);
134 
135  // and/or
136  VERIFY( ((m1.array()<RealScalar(0)).matrix() && (m1.array()>RealScalar(0)).matrix()).count() == 0);
137  VERIFY( ((m1.array()<RealScalar(0)).matrix() || (m1.array()>=RealScalar(0)).matrix()).count() == rows*cols);
138  RealScalar a = m1.cwiseAbs().mean();
139  VERIFY( ((m1.array()<-a).matrix() || (m1.array()>a).matrix()).count() == (m1.cwiseAbs().array()>a).count());
140 
141  typedef Matrix<typename MatrixType::Index, Dynamic, 1> VectorOfIndices;
142 
143  // TODO allows colwise/rowwise for array
144  VERIFY_IS_APPROX(((m1.array().abs()+1)>RealScalar(0.1)).matrix().colwise().count(), VectorOfIndices::Constant(cols,rows).transpose());
145  VERIFY_IS_APPROX(((m1.array().abs()+1)>RealScalar(0.1)).matrix().rowwise().count(), VectorOfIndices::Constant(rows, cols));
146 }
147 
148 template<typename VectorType> void lpNorm(const VectorType& v)
149 {
150  using std::sqrt;
151  typedef typename VectorType::RealScalar RealScalar;
152  VectorType u = VectorType::Random(v.size());
153 
154  if(v.size()==0)
155  {
156  VERIFY_IS_APPROX(u.template lpNorm<Infinity>(), RealScalar(0));
157  VERIFY_IS_APPROX(u.template lpNorm<1>(), RealScalar(0));
158  VERIFY_IS_APPROX(u.template lpNorm<2>(), RealScalar(0));
159  VERIFY_IS_APPROX(u.template lpNorm<5>(), RealScalar(0));
160  }
161  else
162  {
163  VERIFY_IS_APPROX(u.template lpNorm<Infinity>(), u.cwiseAbs().maxCoeff());
164  }
165 
166  VERIFY_IS_APPROX(u.template lpNorm<1>(), u.cwiseAbs().sum());
167  VERIFY_IS_APPROX(u.template lpNorm<2>(), sqrt(u.array().abs().square().sum()));
168  VERIFY_IS_APPROX(numext::pow(u.template lpNorm<5>(), typename VectorType::RealScalar(5)), u.array().abs().pow(5).sum());
169 }
170 
171 template<typename MatrixType> void cwise_min_max(const MatrixType& m)
172 {
173  typedef typename MatrixType::Scalar Scalar;
174 
175  Index rows = m.rows();
176  Index cols = m.cols();
177 
178  MatrixType m1 = MatrixType::Random(rows, cols);
179 
180  // min/max with array
181  Scalar maxM1 = m1.maxCoeff();
182  Scalar minM1 = m1.minCoeff();
183 
184  VERIFY_IS_APPROX(MatrixType::Constant(rows,cols, minM1), m1.cwiseMin(MatrixType::Constant(rows,cols, minM1)));
185  VERIFY_IS_APPROX(m1, m1.cwiseMin(MatrixType::Constant(rows,cols, maxM1)));
186 
187  VERIFY_IS_APPROX(MatrixType::Constant(rows,cols, maxM1), m1.cwiseMax(MatrixType::Constant(rows,cols, maxM1)));
188  VERIFY_IS_APPROX(m1, m1.cwiseMax(MatrixType::Constant(rows,cols, minM1)));
189 
190  // min/max with scalar input
191  VERIFY_IS_APPROX(MatrixType::Constant(rows,cols, minM1), m1.cwiseMin( minM1));
192  VERIFY_IS_APPROX(m1, m1.cwiseMin(maxM1));
193  VERIFY_IS_APPROX(-m1, (-m1).cwiseMin(-minM1));
194  VERIFY_IS_APPROX(-m1.array(), ((-m1).array().min)( -minM1));
195 
196  VERIFY_IS_APPROX(MatrixType::Constant(rows,cols, maxM1), m1.cwiseMax( maxM1));
197  VERIFY_IS_APPROX(m1, m1.cwiseMax(minM1));
198  VERIFY_IS_APPROX(-m1, (-m1).cwiseMax(-maxM1));
199  VERIFY_IS_APPROX(-m1.array(), ((-m1).array().max)(-maxM1));
200 
201  VERIFY_IS_APPROX(MatrixType::Constant(rows,cols, minM1).array(), (m1.array().min)( minM1));
202  VERIFY_IS_APPROX(m1.array(), (m1.array().min)( maxM1));
203 
204  VERIFY_IS_APPROX(MatrixType::Constant(rows,cols, maxM1).array(), (m1.array().max)( maxM1));
205  VERIFY_IS_APPROX(m1.array(), (m1.array().max)( minM1));
206 
207 }
208 
209 template<typename MatrixTraits> void resize(const MatrixTraits& t)
210 {
211  typedef typename MatrixTraits::Scalar Scalar;
213  typedef Array<Scalar,Dynamic,Dynamic> Array2DType;
215  typedef Array<Scalar,Dynamic,1> Array1DType;
216 
217  Index rows = t.rows(), cols = t.cols();
218 
219  MatrixType m(rows,cols);
220  VectorType v(rows);
221  Array2DType a2(rows,cols);
222  Array1DType a1(rows);
223 
224  m.array().resize(rows+1,cols+1);
225  VERIFY(m.rows()==rows+1 && m.cols()==cols+1);
226  a2.matrix().resize(rows+1,cols+1);
227  VERIFY(a2.rows()==rows+1 && a2.cols()==cols+1);
228  v.array().resize(cols);
229  VERIFY(v.size()==cols);
230  a1.matrix().resize(cols);
231  VERIFY(a1.size()==cols);
232 }
233 
234 template<int>
236 {
237  ArrayXf a = RowVectorXf(3);
238  VectorXf v = Array<float,1,Dynamic>(3);
239 }
240 
241 // Check propagation of LvalueBit through Array/Matrix-Wrapper
242 template<int>
244 {
245  const Matrix4i M;
246  const Array4i A;
248  MA.row(0);
250  AM.row(0);
251 
252  VERIFY((internal::traits<ArrayWrapper<const Matrix4i> >::Flags&LvalueBit)==0);
253  VERIFY((internal::traits<MatrixWrapper<const Array4i> >::Flags&LvalueBit)==0);
254 
255  VERIFY((internal::traits<ArrayWrapper<Matrix4i> >::Flags&LvalueBit)==LvalueBit);
256  VERIFY((internal::traits<MatrixWrapper<Array4i> >::Flags&LvalueBit)==LvalueBit);
257 }
258 
260 {
261  for(int i = 0; i < g_repeat; i++) {
262  CALL_SUBTEST_1( array_for_matrix(Matrix<float, 1, 1>()) );
263  CALL_SUBTEST_2( array_for_matrix(Matrix2f()) );
264  CALL_SUBTEST_3( array_for_matrix(Matrix4d()) );
265  CALL_SUBTEST_4( array_for_matrix(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
266  CALL_SUBTEST_5( array_for_matrix(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
267  CALL_SUBTEST_6( array_for_matrix(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
268  }
269  for(int i = 0; i < g_repeat; i++) {
270  CALL_SUBTEST_1( comparisons(Matrix<float, 1, 1>()) );
271  CALL_SUBTEST_2( comparisons(Matrix2f()) );
272  CALL_SUBTEST_3( comparisons(Matrix4d()) );
273  CALL_SUBTEST_5( comparisons(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
274  CALL_SUBTEST_6( comparisons(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++) {
277  CALL_SUBTEST_1( cwise_min_max(Matrix<float, 1, 1>()) );
278  CALL_SUBTEST_2( cwise_min_max(Matrix2f()) );
279  CALL_SUBTEST_3( cwise_min_max(Matrix4d()) );
280  CALL_SUBTEST_5( cwise_min_max(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
281  CALL_SUBTEST_6( cwise_min_max(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++) {
284  CALL_SUBTEST_1( lpNorm(Matrix<float, 1, 1>()) );
285  CALL_SUBTEST_2( lpNorm(Vector2f()) );
286  CALL_SUBTEST_7( lpNorm(Vector3d()) );
287  CALL_SUBTEST_8( lpNorm(Vector4f()) );
288  CALL_SUBTEST_5( lpNorm(VectorXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
289  CALL_SUBTEST_4( lpNorm(VectorXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
290  }
291  CALL_SUBTEST_5( lpNorm(VectorXf(0)) );
292  CALL_SUBTEST_4( lpNorm(VectorXcf(0)) );
293  for(int i = 0; i < g_repeat; i++) {
294  CALL_SUBTEST_4( resize(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
295  CALL_SUBTEST_5( resize(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
296  CALL_SUBTEST_6( resize(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
297  }
298  CALL_SUBTEST_6( regression_bug_654<0>() );
299  CALL_SUBTEST_6( regrrssion_bug_1410<0>() );
300 }
Matrix3f m
int array[24]
SCALAR Scalar
Definition: bench_gemm.cpp:33
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:38
Expression of a mathematical vector or matrix as an array object.
Definition: ArrayWrapper.h:42
MatrixType m2(n_dims)
ArrayXcf v
Definition: Cwise_arg.cpp:1
void regrrssion_bug_1410()
const unsigned int LvalueBit
Definition: Constants.h:139
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
EIGEN_DEVICE_FUNC ArrayBase< Derived > & array()
Definition: ArrayBase.h:141
EIGEN_DEVICE_FUNC const SqrtReturnType sqrt() const
MatrixXf MatrixType
EIGEN_DEVICE_FUNC MatrixBase< Derived > & matrix()
Definition: MatrixBase.h:312
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:150
Matrix< SCALARA, Dynamic, Dynamic > A
Definition: bench_gemm.cpp:35
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
Array33i a
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)
Matrix3d m1
Definition: IOFormat.cpp:2
void resize(const MatrixTraits &t)
static int g_repeat
Definition: main.h:144
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
void test_array_for_matrix()
void regression_bug_654()
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:34
#define VERIFY_IS_MUCH_SMALLER_THAN(a, b)
Definition: main.h:335
#define VERIFY(a)
Definition: main.h:325
#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
void cwise_min_max(const MatrixType &m)
EIGEN_DEVICE_FUNC RowXpr row(Index i)
Definition: DenseBase.h:860
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
std::ptrdiff_t j
Point2 t(10, 10)
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
void comparisons(const MatrixType &m)


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:41:37