basicstuff.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) 2006-2008 Benoit Jacob <jacob.benoit.1@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 #define EIGEN_NO_STATIC_ASSERT
11 
12 #include "main.h"
14 
15 template<typename MatrixType> void basicStuff(const MatrixType& m)
16 {
17  typedef typename MatrixType::Scalar Scalar;
20 
21  Index rows = m.rows();
22  Index cols = m.cols();
23 
24  // this test relies a lot on Random.h, and there's not much more that we can do
25  // to test it, hence I consider that we will have tested Random.h
26  MatrixType m1 = MatrixType::Random(rows, cols),
27  m2 = MatrixType::Random(rows, cols),
28  m3(rows, cols),
29  mzero = MatrixType::Zero(rows, cols),
31  VectorType v1 = VectorType::Random(rows),
32  vzero = VectorType::Zero(rows);
33  SquareMatrixType sm1 = SquareMatrixType::Random(rows,rows), sm2(rows,rows);
34 
35  Scalar x = 0;
36  while(x == Scalar(0)) x = internal::random<Scalar>();
37 
38  Index r = internal::random<Index>(0, rows-1),
39  c = internal::random<Index>(0, cols-1);
40 
41  m1.coeffRef(r,c) = x;
42  VERIFY_IS_APPROX(x, m1.coeff(r,c));
43  m1(r,c) = x;
44  VERIFY_IS_APPROX(x, m1(r,c));
45  v1.coeffRef(r) = x;
46  VERIFY_IS_APPROX(x, v1.coeff(r));
47  v1(r) = x;
48  VERIFY_IS_APPROX(x, v1(r));
49  v1[r] = x;
50  VERIFY_IS_APPROX(x, v1[r]);
51 
52  // test fetching with various index types.
53  Index r1 = internal::random<Index>(0, numext::mini(Index(127),rows-1));
54  x = v1(static_cast<char>(r1));
55  x = v1(static_cast<signed char>(r1));
56  x = v1(static_cast<unsigned char>(r1));
57  x = v1(static_cast<signed short>(r1));
58  x = v1(static_cast<unsigned short>(r1));
59  x = v1(static_cast<signed int>(r1));
60  x = v1(static_cast<unsigned int>(r1));
61  x = v1(static_cast<signed long>(r1));
62  x = v1(static_cast<unsigned long>(r1));
63 #if EIGEN_HAS_CXX11
64  x = v1(static_cast<long long int>(r1));
65  x = v1(static_cast<unsigned long long int>(r1));
66 #endif
67 
71  VERIFY_IS_MUCH_SMALLER_THAN( vzero, v1.squaredNorm());
73  VERIFY_IS_APPROX( vzero, v1-v1);
78  VERIFY_IS_APPROX( mzero, m1-m1);
79 
80  // always test operator() on each read-only expression class,
81  // in order to check const-qualifiers.
82  // indeed, if an expression class (here Zero) is meant to be read-only,
83  // hence has no _write() method, the corresponding MatrixBase method (here zero())
84  // should return a const-qualified object so that it is the const-qualified
85  // operator() that gets called, which in turn calls _read().
86  VERIFY_IS_MUCH_SMALLER_THAN(MatrixType::Zero(rows,cols)(r,c), static_cast<Scalar>(1));
87 
88  // now test copying a row-vector into a (column-)vector and conversely.
89  square.col(r) = square.row(r).eval();
92  rv = square.row(r);
93  cv = square.col(r);
94 
95  VERIFY_IS_APPROX(rv, cv.transpose());
96 
97  if(cols!=1 && rows!=1 && MatrixType::SizeAtCompileTime!=Dynamic)
98  {
99  VERIFY_RAISES_ASSERT(m1 = (m2.block(0,0, rows-1, cols-1)));
100  }
101 
102  if(cols!=1 && rows!=1)
103  {
105  VERIFY_RAISES_ASSERT((m1+m1)[0]);
106  }
107 
109  MatrixType m4;
110  VERIFY_IS_APPROX(m4 = m1,m1);
111 
112  m3.real() = m1.real();
113  VERIFY_IS_APPROX(static_cast<const MatrixType&>(m3).real(), static_cast<const MatrixType&>(m1).real());
114  VERIFY_IS_APPROX(static_cast<const MatrixType&>(m3).real(), m1.real());
115 
116  // check == / != operators
117  VERIFY(m1==m1);
118  VERIFY(m1!=m2);
119  VERIFY(!(m1==m2));
120  VERIFY(!(m1!=m1));
121  m1 = m2;
122  VERIFY(m1==m2);
123  VERIFY(!(m1!=m2));
124 
125  // check automatic transposition
126  sm2.setZero();
127  for(Index i=0;i<rows;++i)
128  sm2.col(i) = sm1.row(i);
129  VERIFY_IS_APPROX(sm2,sm1.transpose());
130 
131  sm2.setZero();
132  for(Index i=0;i<rows;++i)
133  sm2.col(i).noalias() = sm1.row(i);
134  VERIFY_IS_APPROX(sm2,sm1.transpose());
135 
136  sm2.setZero();
137  for(Index i=0;i<rows;++i)
138  sm2.col(i).noalias() += sm1.row(i);
139  VERIFY_IS_APPROX(sm2,sm1.transpose());
140 
141  sm2.setZero();
142  for(Index i=0;i<rows;++i)
143  sm2.col(i).noalias() -= sm1.row(i);
144  VERIFY_IS_APPROX(sm2,-sm1.transpose());
145 
146  // check ternary usage
147  {
148  bool b = internal::random<int>(0,10)>5;
149  m3 = b ? m1 : m2;
150  if(b) VERIFY_IS_APPROX(m3,m1);
151  else VERIFY_IS_APPROX(m3,m2);
152  m3 = b ? -m1 : m2;
153  if(b) VERIFY_IS_APPROX(m3,-m1);
154  else VERIFY_IS_APPROX(m3,m2);
155  m3 = b ? m1 : -m2;
156  if(b) VERIFY_IS_APPROX(m3,m1);
157  else VERIFY_IS_APPROX(m3,-m2);
158  }
159 }
160 
161 template<typename MatrixType> void basicStuffComplex(const MatrixType& m)
162 {
163  typedef typename MatrixType::Scalar Scalar;
164  typedef typename NumTraits<Scalar>::Real RealScalar;
166 
167  Index rows = m.rows();
168  Index cols = m.cols();
169 
170  Scalar s1 = internal::random<Scalar>(),
171  s2 = internal::random<Scalar>();
172 
175  numext::real_ref(s1) = numext::real(s2);
176  numext::imag_ref(s1) = numext::imag(s2);
178  // extended precision in Intel FPUs means that s1 == s2 in the line above is not guaranteed.
179 
180  RealMatrixType rm1 = RealMatrixType::Random(rows,cols),
181  rm2 = RealMatrixType::Random(rows,cols);
183  cm.real() = rm1;
184  cm.imag() = rm2;
185  VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).real(), rm1);
186  VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).imag(), rm2);
187  rm1.setZero();
188  rm2.setZero();
189  rm1 = cm.real();
190  rm2 = cm.imag();
191  VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).real(), rm1);
192  VERIFY_IS_APPROX(static_cast<const MatrixType&>(cm).imag(), rm2);
193  cm.real().setZero();
194  VERIFY(static_cast<const MatrixType&>(cm).real().isZero());
195  VERIFY(!static_cast<const MatrixType&>(cm).imag().isZero());
196 }
197 
198 template<typename SrcScalar, typename TgtScalar>
199 struct casting_test {
200  static void run() {
202  for (int i=0; i<m.rows(); ++i) {
203  for (int j=0; j<m.cols(); ++j) {
205  }
206  }
207  Matrix<TgtScalar,4,4> n = m.template cast<TgtScalar>();
208  for (int i=0; i<m.rows(); ++i) {
209  for (int j=0; j<m.cols(); ++j) {
210  VERIFY_IS_APPROX(n(i, j), (internal::cast<SrcScalar,TgtScalar>(m(i, j))));
211  }
212  }
213  }
214 };
215 
216 template<typename SrcScalar, typename EnableIf = void>
218  static void run() {
226 #if EIGEN_HAS_CXX11
229 #endif
236  }
237 };
238 
239 template<typename SrcScalar>
240 struct casting_test_runner<SrcScalar, typename internal::enable_if<(NumTraits<SrcScalar>::IsComplex)>::type>
241 {
242  static void run() {
243  // Only a few casts from std::complex<T> are defined.
248  }
249 };
250 
251 void casting_all() {
259 #if EIGEN_HAS_CXX11
262 #endif
269 }
270 
271 template <typename Scalar>
273 {
274  Scalar raw[4];
275  for(int k=0; k<4; ++k)
276  raw[k] = internal::random<Scalar>();
277 
278  {
279  Matrix<Scalar,4,1> m(raw);
280  Array<Scalar,4,1> a(raw);
281  for(int k=0; k<4; ++k) VERIFY(m(k) == raw[k]);
282  for(int k=0; k<4; ++k) VERIFY(a(k) == raw[k]);
283  VERIFY_IS_EQUAL(m,(Matrix<Scalar,4,1>(raw[0],raw[1],raw[2],raw[3])));
284  VERIFY((a==(Array<Scalar,4,1>(raw[0],raw[1],raw[2],raw[3]))).all());
285  }
286  {
287  Matrix<Scalar,3,1> m(raw);
288  Array<Scalar,3,1> a(raw);
289  for(int k=0; k<3; ++k) VERIFY(m(k) == raw[k]);
290  for(int k=0; k<3; ++k) VERIFY(a(k) == raw[k]);
291  VERIFY_IS_EQUAL(m,(Matrix<Scalar,3,1>(raw[0],raw[1],raw[2])));
292  VERIFY((a==Array<Scalar,3,1>(raw[0],raw[1],raw[2])).all());
293  }
294  {
295  Matrix<Scalar,2,1> m(raw), m2( (DenseIndex(raw[0])), (DenseIndex(raw[1])) );
296  Array<Scalar,2,1> a(raw), a2( (DenseIndex(raw[0])), (DenseIndex(raw[1])) );
297  for(int k=0; k<2; ++k) VERIFY(m(k) == raw[k]);
298  for(int k=0; k<2; ++k) VERIFY(a(k) == raw[k]);
299  VERIFY_IS_EQUAL(m,(Matrix<Scalar,2,1>(raw[0],raw[1])));
300  VERIFY((a==Array<Scalar,2,1>(raw[0],raw[1])).all());
301  for(int k=0; k<2; ++k) VERIFY(m2(k) == DenseIndex(raw[k]));
302  for(int k=0; k<2; ++k) VERIFY(a2(k) == DenseIndex(raw[k]));
303  }
304  {
305  Matrix<Scalar,1,2> m(raw),
306  m2( (DenseIndex(raw[0])), (DenseIndex(raw[1])) ),
307  m3( (int(raw[0])), (int(raw[1])) ),
308  m4( (float(raw[0])), (float(raw[1])) );
309  Array<Scalar,1,2> a(raw), a2( (DenseIndex(raw[0])), (DenseIndex(raw[1])) );
310  for(int k=0; k<2; ++k) VERIFY(m(k) == raw[k]);
311  for(int k=0; k<2; ++k) VERIFY(a(k) == raw[k]);
312  VERIFY_IS_EQUAL(m,(Matrix<Scalar,1,2>(raw[0],raw[1])));
313  VERIFY((a==Array<Scalar,1,2>(raw[0],raw[1])).all());
314  for(int k=0; k<2; ++k) VERIFY(m2(k) == DenseIndex(raw[k]));
315  for(int k=0; k<2; ++k) VERIFY(a2(k) == DenseIndex(raw[k]));
316  for(int k=0; k<2; ++k) VERIFY(m3(k) == int(raw[k]));
317  for(int k=0; k<2; ++k) VERIFY((m4(k)) == Scalar(float(raw[k])));
318  }
319  {
320  Matrix<Scalar,1,1> m(raw), m1(raw[0]), m2( (DenseIndex(raw[0])) ), m3( (int(raw[0])) );
321  Array<Scalar,1,1> a(raw), a1(raw[0]), a2( (DenseIndex(raw[0])) );
322  VERIFY(m(0) == raw[0]);
323  VERIFY(a(0) == raw[0]);
324  VERIFY(m1(0) == raw[0]);
325  VERIFY(a1(0) == raw[0]);
326  VERIFY(m2(0) == DenseIndex(raw[0]));
327  VERIFY(a2(0) == DenseIndex(raw[0]));
328  VERIFY(m3(0) == int(raw[0]));
330  VERIFY((a==Array<Scalar,1,1>(raw[0])).all());
331  }
332 }
333 
335 {
336  for(int i = 0; i < g_repeat; i++) {
338  CALL_SUBTEST_2( basicStuff(Matrix4d()) );
339  CALL_SUBTEST_3( basicStuff(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
340  CALL_SUBTEST_4( basicStuff(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
341  CALL_SUBTEST_5( basicStuff(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
343  CALL_SUBTEST_7( basicStuff(Matrix<long double,Dynamic,Dynamic>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
345 
346  CALL_SUBTEST_3( basicStuffComplex(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
347  CALL_SUBTEST_5( basicStuffComplex(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
348  }
349 
350  CALL_SUBTEST_1(fixedSizeMatrixConstruction<unsigned char>());
351  CALL_SUBTEST_1(fixedSizeMatrixConstruction<float>());
352  CALL_SUBTEST_1(fixedSizeMatrixConstruction<double>());
353  CALL_SUBTEST_1(fixedSizeMatrixConstruction<int>());
354  CALL_SUBTEST_1(fixedSizeMatrixConstruction<long int>());
355  CALL_SUBTEST_1(fixedSizeMatrixConstruction<std::ptrdiff_t>());
356 }
basicStuff
void basicStuff(const MatrixType &m)
Definition: basicstuff.cpp:15
VERIFY_IS_MUCH_SMALLER_THAN
#define VERIFY_IS_MUCH_SMALLER_THAN(a, b)
Definition: main.h:390
gtsam.examples.DogLegOptimizerExample.type
type
Definition: DogLegOptimizerExample.py:111
Eigen::numext::imag_ref
EIGEN_DEVICE_FUNC internal::add_const_on_value_type< EIGEN_MATHFUNC_RETVAL(imag_ref, Scalar) >::type imag_ref(const Scalar &x)
Definition: Eigen/src/Core/MathFunctions.h:1267
MatrixType
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
VERIFY_IS_EQUAL
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
b
Scalar * b
Definition: benchVecAdd.cpp:17
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
m1
Matrix3d m1
Definition: IOFormat.cpp:2
Eigen::Array
General-purpose arrays with easy API for coefficient-wise operations.
Definition: Array.h:45
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(basicstuff)
Definition: basicstuff.cpp:334
real
float real
Definition: datatypes.h:10
Eigen::internal::isApprox
EIGEN_DEVICE_FUNC bool isApprox(const Scalar &x, const Scalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
Definition: Eigen/src/Core/MathFunctions.h:1947
casting_test
Definition: basicstuff.cpp:199
r1
static const double r1
Definition: testSmartRangeFactor.cpp:32
casting_test_runner::run
static void run()
Definition: basicstuff.cpp:218
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
VERIFY_RAISES_ASSERT
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:340
basicStuffComplex
void basicStuffComplex(const MatrixType &m)
Definition: basicstuff.cpp:161
casting_test::run
static void run()
Definition: basicstuff.cpp:200
CALL_SUBTEST_4
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22
n
int n
Definition: BiCGSTAB_simple.cpp:1
VERIFY_IS_NOT_APPROX
#define VERIFY_IS_NOT_APPROX(a, b)
Definition: integer_types.cpp:17
align_3::a1
Point2 a1
Definition: testPose2.cpp:769
m2
MatrixType m2(n_dims)
CALL_SUBTEST_3
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
CALL_SUBTEST_1
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
square
const EIGEN_DEVICE_FUNC SquareReturnType square() const
Definition: ArrayCwiseUnaryOps.h:425
Eigen::Dynamic
const int Dynamic
Definition: Constants.h:22
Eigen::all
static const Eigen::internal::all_t all
Definition: IndexedViewHelper.h:171
Eigen::numext::real_ref
EIGEN_DEVICE_FUNC internal::add_const_on_value_type< EIGEN_MATHFUNC_RETVAL(real_ref, Scalar) >::type real_ref(const Scalar &x)
Definition: Eigen/src/Core/MathFunctions.h:1239
Eigen::numext::mini
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T mini(const T &x, const T &y)
Definition: Eigen/src/Core/MathFunctions.h:1085
VERIFY_IS_NOT_MUCH_SMALLER_THAN
#define VERIFY_IS_NOT_MUCH_SMALLER_THAN(a, b)
Definition: main.h:391
casting_all
void casting_all()
Definition: basicstuff.cpp:251
CALL_SUBTEST_5
#define CALL_SUBTEST_5(FUNC)
Definition: split_test_helper.h:28
Eigen::g_repeat
static int g_repeat
Definition: main.h:169
imag
const EIGEN_DEVICE_FUNC ImagReturnType imag() const
Definition: CommonCwiseUnaryOps.h:109
gtsam.examples.DogLegOptimizerExample.run
def run(args)
Definition: DogLegOptimizerExample.py:21
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
CALL_SUBTEST_6
#define CALL_SUBTEST_6(FUNC)
Definition: split_test_helper.h:34
CALL_SUBTEST_2
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
fixedSizeMatrixConstruction
void fixedSizeMatrixConstruction()
Definition: basicstuff.cpp:272
VERIFY_IS_APPROX
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:15
casting_test_runner< SrcScalar, typename internal::enable_if<(NumTraits< SrcScalar >::IsComplex)>::type >::run
static void run()
Definition: basicstuff.cpp:242
RealScalar
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:47
m3
static const DiscreteKey m3(M(3), 2)
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
main.h
Eigen::DenseIndex
EIGEN_DEFAULT_DENSE_INDEX_TYPE DenseIndex
Definition: Meta.h:66
random_without_cast_overflow.h
align_3::a2
Point2 a2
Definition: testPose2.cpp:770
EIGEN_TEST_MAX_SIZE
#define EIGEN_TEST_MAX_SIZE
Definition: boostmultiprec.cpp:16
cm
static const double cm
Definition: TimeOfArrivalExample.cpp:34
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
internal
Definition: BandTriangularSolver.h:13
VectorType
Definition: FFTW.cpp:65
casting_test_runner
Definition: basicstuff.cpp:217
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
CALL_SUBTEST_7
#define CALL_SUBTEST_7(FUNC)
Definition: split_test_helper.h:40
CALL_SUBTEST_8
#define CALL_SUBTEST_8(FUNC)
Definition: split_test_helper.h:46
Eigen::NumTraits
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:232
test_callbacks.value
value
Definition: test_callbacks.py:160
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
v1
Vector v1
Definition: testSerializationBase.cpp:38
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
VERIFY
#define VERIFY(a)
Definition: main.h:380
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:01:53