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 
29  MatrixType m1(rows, cols),
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);
45  VERIFY_IS_EQUAL(-m2+m1+m2, 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
64  MatrixType m1(rows, cols),
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);
72  VectorType v1(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 
91  VERIFY_IS_APPROX(m3 = m1,m1);
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);
116  VERIFY_IS_EQUAL(m1+m2-m1, m2);
117  VERIFY_IS_EQUAL(m1*s1, s1*m1);
118  VERIFY_IS_EQUAL((m1+m2)*s1, s1*m1+s1*m2);
119  m3 = m2; m3 += m1;
120  VERIFY_IS_EQUAL(m3, m1+m2);
121  m3 = m2; m3 -= m1;
122  VERIFY_IS_EQUAL(m3, m2-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 }
Matrix3f m
SCALAR Scalar
Definition: bench_gemm.cpp:46
#define CALL_SUBTEST_9(FUNC)
#define CALL_SUBTEST_6(FUNC)
#define CALL_SUBTEST_4(FUNC)
float real
Definition: datatypes.h:10
EIGEN_DECLARE_TEST(integer_types)
Vector v2
Vector v1
#define CALL_SUBTEST_3(FUNC)
MatrixType m2(n_dims)
#define CALL_SUBTEST_7(FUNC)
MatrixXf MatrixType
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:232
#define VERIFY_IS_APPROX(a, b)
EIGEN_DEVICE_FUNC const SquareReturnType square() const
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
#define CALL_SUBTEST_1(FUNC)
Matrix3d m1
Definition: IOFormat.cpp:2
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
#define CALL_SUBTEST_8(FUNC)
void integer_types_extra()
#define CALL_SUBTEST_5(FUNC)
#define VERIFY(a)
Definition: main.h:380
void signed_integer_type_tests(const MatrixType &m)
void integer_type_tests(const MatrixType &m)
#define VERIFY_IS_NOT_APPROX(a, b)
#define CALL_SUBTEST_2(FUNC)
The matrix class, also used for vectors and row-vectors.


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:23