integer_types.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) 2010 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"
13 
14 #undef VERIFY_IS_APPROX
15 #define VERIFY_IS_APPROX(a, b) VERIFY((a)==(b));
16 #undef VERIFY_IS_NOT_APPROX
17 #define VERIFY_IS_NOT_APPROX(a, b) VERIFY((a)!=(b));
18 
19 template<typename MatrixType> void signed_integer_type_tests(const MatrixType& m)
20 {
21  typedef typename MatrixType::Scalar Scalar;
22 
23  enum { is_signed = (Scalar(-1) > Scalar(0)) ? 0 : 1 };
24  VERIFY(is_signed == 1);
25 
26  Index rows = m.rows();
27  Index cols = m.cols();
28 
30  m2 = MatrixType::Random(rows, cols),
31  mzero = MatrixType::Zero(rows, cols);
32 
33  do {
34  m1 = MatrixType::Random(rows, cols);
35  } while(m1 == mzero || m1 == m2);
36 
37  // check linear structure
38 
39  Scalar s1;
40  do {
41  s1 = internal::random<Scalar>();
42  } while(s1 == 0);
43 
44  VERIFY_IS_EQUAL(-(-m1), m1);
46  VERIFY_IS_EQUAL((-m1+m2)*s1, -s1*m1+s1*m2);
47 }
48 
49 template<typename MatrixType> void integer_type_tests(const MatrixType& m)
50 {
51  typedef typename MatrixType::Scalar Scalar;
52 
54  enum { is_signed = (Scalar(-1) > Scalar(0)) ? 0 : 1 };
55  VERIFY(int(NumTraits<Scalar>::IsSigned) == is_signed);
56 
58 
59  Index rows = m.rows();
60  Index cols = m.cols();
61 
62  // this test relies a lot on Random.h, and there's not much more that we can do
63  // to test it, hence I consider that we will have tested Random.h
65  m2 = MatrixType::Random(rows, cols),
66  m3(rows, cols),
67  mzero = MatrixType::Zero(rows, cols);
68 
70  SquareMatrixType identity = SquareMatrixType::Identity(rows, rows),
71  square = SquareMatrixType::Random(rows, rows);
73  v2 = VectorType::Random(rows),
74  vzero = VectorType::Zero(rows);
75 
76  do {
77  m1 = MatrixType::Random(rows, cols);
78  } while(m1 == mzero || m1 == m2);
79 
80  do {
81  v1 = VectorType::Random(rows);
82  } while(v1 == vzero || v1 == v2);
83 
86  VERIFY_IS_APPROX( vzero, v1-v1);
89  VERIFY_IS_APPROX( mzero, m1-m1);
90 
92  MatrixType m4;
93  VERIFY_IS_APPROX(m4 = m1,m1);
94 
95  m3.real() = m1.real();
96  VERIFY_IS_APPROX(static_cast<const MatrixType&>(m3).real(), static_cast<const MatrixType&>(m1).real());
97  VERIFY_IS_APPROX(static_cast<const MatrixType&>(m3).real(), m1.real());
98 
99  // check == / != operators
100  VERIFY(m1==m1);
101  VERIFY(m1!=m2);
102  VERIFY(!(m1==m2));
103  VERIFY(!(m1!=m1));
104  m1 = m2;
105  VERIFY(m1==m2);
106  VERIFY(!(m1!=m2));
107 
108  // check linear structure
109 
110  Scalar s1;
111  do {
112  s1 = internal::random<Scalar>();
113  } while(s1 == 0);
114 
115  VERIFY_IS_EQUAL(m1+m1, 2*m1);
117  VERIFY_IS_EQUAL(m1*s1, s1*m1);
118  VERIFY_IS_EQUAL((m1+m2)*s1, s1*m1+s1*m2);
119  m3 = m2; m3 += m1;
121  m3 = m2; m3 -= m1;
123  m3 = m2; m3 *= s1;
124  VERIFY_IS_EQUAL(m3, s1*m2);
125 
126  // check matrix product.
127 
128  VERIFY_IS_APPROX(identity * m1, m1);
129  VERIFY_IS_APPROX(square * (m1 + m2), square * m1 + square * m2);
130  VERIFY_IS_APPROX((m1 + m2).transpose() * square, m1.transpose() * square + m2.transpose() * square);
131  VERIFY_IS_APPROX((m1 * m2.transpose()) * m1, m1 * (m2.transpose() * m1));
132 }
133 
134 template<int>
136 {
139  if(sizeof(long)>sizeof(int)) {
142  }
143 }
144 
145 EIGEN_DECLARE_TEST(integer_types)
146 {
147  for(int i = 0; i < g_repeat; i++) {
150 
153 
156 
159 
162 
164 
165 #if EIGEN_HAS_CXX11
168 
170 #endif
171  }
172  CALL_SUBTEST_9( integer_types_extra<0>() );
173 }
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
MatrixType
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
VERIFY_IS_EQUAL
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
m1
Matrix3d m1
Definition: IOFormat.cpp:2
real
float real
Definition: datatypes.h:10
integer_type_tests
void integer_type_tests(const MatrixType &m)
Definition: integer_types.cpp:49
CALL_SUBTEST_9
#define CALL_SUBTEST_9(FUNC)
Definition: split_test_helper.h:52
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
CALL_SUBTEST_4
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22
VERIFY_IS_NOT_APPROX
#define VERIFY_IS_NOT_APPROX(a, b)
Definition: integer_types.cpp:17
m2
MatrixType m2(n_dims)
CALL_SUBTEST_3
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
signed_integer_type_tests
void signed_integer_type_tests(const MatrixType &m)
Definition: integer_types.cpp:19
CALL_SUBTEST_1
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
square
const EIGEN_DEVICE_FUNC SquareReturnType square() const
Definition: ArrayCwiseUnaryOps.h:425
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(integer_types)
Definition: integer_types.cpp:145
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
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
VERIFY_IS_APPROX
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:15
m3
static const DiscreteKey m3(M(3), 2)
main.h
v2
Vector v2
Definition: testSerializationBase.cpp:39
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
VectorType
Definition: FFTW.cpp:65
integer_types_extra
void integer_types_extra()
Definition: integer_types.cpp:135
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 Fri Nov 1 2024 03:32:46