cxx11_tensor_fixed_size.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) 2014 Benoit Steiner <benoit.steiner.goog@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 #include "main.h"
11 
12 #include <Eigen/CXX11/Tensor>
13 
14 using Eigen::Tensor;
15 using Eigen::RowMajor;
16 
17 
18 static void test_0d()
19 {
22  VERIFY_IS_EQUAL(scalar1.rank(), 0);
23  VERIFY_IS_EQUAL(scalar1.size(), 1);
25 
26  scalar1() = 7.0;
27  scalar2() = 13.0;
28 
29  // Test against shallow copy.
31  VERIFY_IS_NOT_EQUAL(scalar1.data(), copy.data());
32  VERIFY_IS_APPROX(scalar1(), copy());
33  copy = scalar1;
34  VERIFY_IS_NOT_EQUAL(scalar1.data(), copy.data());
35  VERIFY_IS_APPROX(scalar1(), copy());
36 
37  TensorFixedSize<float, Sizes<> > scalar3 = scalar1.sqrt();
38  TensorFixedSize<float, Sizes<>, RowMajor> scalar4 = scalar2.sqrt();
39  VERIFY_IS_EQUAL(scalar3.rank(), 0);
40  VERIFY_IS_APPROX(scalar3(), sqrtf(7.0));
41  VERIFY_IS_APPROX(scalar4(), sqrtf(13.0));
42 
43  scalar3 = scalar1 + scalar2;
44  VERIFY_IS_APPROX(scalar3(), 7.0f + 13.0f);
45 }
46 
47 static void test_1d()
48 {
51 
52  VERIFY_IS_EQUAL((vec1.size()), 6);
53  // VERIFY_IS_EQUAL((vec1.dimensions()[0]), 6);
54  // VERIFY_IS_EQUAL((vec1.dimension(0)), 6);
55 
56  vec1(0) = 4.0; vec2(0) = 0.0;
57  vec1(1) = 8.0; vec2(1) = 1.0;
58  vec1(2) = 15.0; vec2(2) = 2.0;
59  vec1(3) = 16.0; vec2(3) = 3.0;
60  vec1(4) = 23.0; vec2(4) = 4.0;
61  vec1(5) = 42.0; vec2(5) = 5.0;
62 
63  // Test against shallow copy.
65  VERIFY_IS_NOT_EQUAL(vec1.data(), copy.data());
66  for (int i = 0; i < 6; ++i) {
68  }
69  copy = vec1;
70  VERIFY_IS_NOT_EQUAL(vec1.data(), copy.data());
71  for (int i = 0; i < 6; ++i) {
73  }
74 
77 
78  VERIFY_IS_EQUAL((vec3.size()), 6);
79  VERIFY_IS_EQUAL(vec3.rank(), 1);
80  // VERIFY_IS_EQUAL((vec3.dimensions()[0]), 6);
81  // VERIFY_IS_EQUAL((vec3.dimension(0)), 6);
82 
83  VERIFY_IS_APPROX(vec3(0), sqrtf(4.0));
84  VERIFY_IS_APPROX(vec3(1), sqrtf(8.0));
85  VERIFY_IS_APPROX(vec3(2), sqrtf(15.0));
86  VERIFY_IS_APPROX(vec3(3), sqrtf(16.0));
87  VERIFY_IS_APPROX(vec3(4), sqrtf(23.0));
88  VERIFY_IS_APPROX(vec3(5), sqrtf(42.0));
89 
90  VERIFY_IS_APPROX(vec4(0), sqrtf(0.0));
91  VERIFY_IS_APPROX(vec4(1), sqrtf(1.0));
92  VERIFY_IS_APPROX(vec4(2), sqrtf(2.0));
93  VERIFY_IS_APPROX(vec4(3), sqrtf(3.0));
94  VERIFY_IS_APPROX(vec4(4), sqrtf(4.0));
95  VERIFY_IS_APPROX(vec4(5), sqrtf(5.0));
96 
97  vec3 = vec1 + vec2;
98  VERIFY_IS_APPROX(vec3(0), 4.0f + 0.0f);
99  VERIFY_IS_APPROX(vec3(1), 8.0f + 1.0f);
100  VERIFY_IS_APPROX(vec3(2), 15.0f + 2.0f);
101  VERIFY_IS_APPROX(vec3(3), 16.0f + 3.0f);
102  VERIFY_IS_APPROX(vec3(4), 23.0f + 4.0f);
103  VERIFY_IS_APPROX(vec3(5), 42.0f + 5.0f);
104 }
105 
106 static void test_tensor_map()
107 {
110 
111  vec1(0) = 4.0; vec2(0) = 0.0;
112  vec1(1) = 8.0; vec2(1) = 1.0;
113  vec1(2) = 15.0; vec2(2) = 2.0;
114  vec1(3) = 16.0; vec2(3) = 3.0;
115  vec1(4) = 23.0; vec2(4) = 4.0;
116  vec1(5) = 42.0; vec2(5) = 5.0;
117 
118  float data3[6];
120  vec3 = vec1.sqrt() + vec2;
121 
122  VERIFY_IS_APPROX(vec3(0), sqrtf(4.0));
123  VERIFY_IS_APPROX(vec3(1), sqrtf(8.0) + 1.0f);
124  VERIFY_IS_APPROX(vec3(2), sqrtf(15.0) + 2.0f);
125  VERIFY_IS_APPROX(vec3(3), sqrtf(16.0) + 3.0f);
126  VERIFY_IS_APPROX(vec3(4), sqrtf(23.0) + 4.0f);
127  VERIFY_IS_APPROX(vec3(5), sqrtf(42.0) + 5.0f);
128 }
129 
130 static void test_2d()
131 {
132  float data1[6];
134  float data2[6];
136 
137  VERIFY_IS_EQUAL((mat1.size()), 2*3);
138  VERIFY_IS_EQUAL(mat1.rank(), 2);
139  // VERIFY_IS_EQUAL((mat1.dimension(0)), 2);
140  // VERIFY_IS_EQUAL((mat1.dimension(1)), 3);
141 
142  mat1(0,0) = 0.0;
143  mat1(0,1) = 1.0;
144  mat1(0,2) = 2.0;
145  mat1(1,0) = 3.0;
146  mat1(1,1) = 4.0;
147  mat1(1,2) = 5.0;
148 
149  mat2(0,0) = -0.0;
150  mat2(0,1) = -1.0;
151  mat2(0,2) = -2.0;
152  mat2(1,0) = -3.0;
153  mat2(1,1) = -4.0;
154  mat2(1,2) = -5.0;
155 
158  mat3 = mat1.abs();
159  mat4 = mat2.abs();
160 
161  VERIFY_IS_EQUAL((mat3.size()), 2*3);
162  // VERIFY_IS_EQUAL((mat3.dimension(0)), 2);
163  // VERIFY_IS_EQUAL((mat3.dimension(1)), 3);
164 
165  VERIFY_IS_APPROX(mat3(0,0), 0.0f);
166  VERIFY_IS_APPROX(mat3(0,1), 1.0f);
167  VERIFY_IS_APPROX(mat3(0,2), 2.0f);
168  VERIFY_IS_APPROX(mat3(1,0), 3.0f);
169  VERIFY_IS_APPROX(mat3(1,1), 4.0f);
170  VERIFY_IS_APPROX(mat3(1,2), 5.0f);
171 
172  VERIFY_IS_APPROX(mat4(0,0), 0.0f);
173  VERIFY_IS_APPROX(mat4(0,1), 1.0f);
174  VERIFY_IS_APPROX(mat4(0,2), 2.0f);
175  VERIFY_IS_APPROX(mat4(1,0), 3.0f);
176  VERIFY_IS_APPROX(mat4(1,1), 4.0f);
177  VERIFY_IS_APPROX(mat4(1,2), 5.0f);
178 }
179 
180 static void test_3d()
181 {
184 
185  VERIFY_IS_EQUAL((mat1.size()), 2*3*7);
186  VERIFY_IS_EQUAL(mat1.rank(), 3);
187  // VERIFY_IS_EQUAL((mat1.dimension(0)), 2);
188  // VERIFY_IS_EQUAL((mat1.dimension(1)), 3);
189  // VERIFY_IS_EQUAL((mat1.dimension(2)), 7);
190 
191  float val = 0.0f;
192  for (int i = 0; i < 2; ++i) {
193  for (int j = 0; j < 3; ++j) {
194  for (int k = 0; k < 7; ++k) {
195  mat1(i,j,k) = val;
196  mat2(i,j,k) = val;
197  val += 1.0f;
198  }
199  }
200  }
201 
203  mat3 = mat1.sqrt();
205  mat4 = mat2.sqrt();
206 
207  VERIFY_IS_EQUAL((mat3.size()), 2*3*7);
208  // VERIFY_IS_EQUAL((mat3.dimension(0)), 2);
209  // VERIFY_IS_EQUAL((mat3.dimension(1)), 3);
210  // VERIFY_IS_EQUAL((mat3.dimension(2)), 7);
211 
212 
213  val = 0.0f;
214  for (int i = 0; i < 2; ++i) {
215  for (int j = 0; j < 3; ++j) {
216  for (int k = 0; k < 7; ++k) {
217  VERIFY_IS_APPROX(mat3(i,j,k), sqrtf(val));
218  VERIFY_IS_APPROX(mat4(i,j,k), sqrtf(val));
219  val += 1.0f;
220  }
221  }
222  }
223 }
224 
225 
226 static void test_array()
227 {
229  float val = 0.0f;
230  for (int i = 0; i < 2; ++i) {
231  for (int j = 0; j < 3; ++j) {
232  for (int k = 0; k < 7; ++k) {
233  mat1(i,j,k) = val;
234  val += 1.0f;
235  }
236  }
237  }
238 
240  mat3 = mat1.pow(3.5f);
241 
242  val = 0.0f;
243  for (int i = 0; i < 2; ++i) {
244  for (int j = 0; j < 3; ++j) {
245  for (int k = 0; k < 7; ++k) {
246  VERIFY_IS_APPROX(mat3(i,j,k), powf(val, 3.5f));
247  val += 1.0f;
248  }
249  }
250  }
251 }
252 
253 EIGEN_DECLARE_TEST(cxx11_tensor_fixed_size)
254 {
261 }
Eigen::TensorFixedSize::dimensions
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Dimensions & dimensions() const
Definition: TensorFixedSize.h:64
Eigen::Tensor
The tensor class.
Definition: Tensor.h:63
Eigen::TensorFixedSize::rank
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank() const
Definition: TensorFixedSize.h:62
mat1
MatrixXd mat1(size, size)
VERIFY_IS_EQUAL
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
test_array
static void test_array()
Definition: cxx11_tensor_fixed_size.cpp:226
test_2d
static void test_2d()
Definition: cxx11_tensor_fixed_size.cpp:130
copy
int EIGEN_BLAS_FUNC() copy(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Definition: level1_impl.h:29
gtsam::vec3
static Vector9 vec3(const Matrix3 &R)
Definition: SO3.cpp:342
test_1d
static void test_1d()
Definition: cxx11_tensor_fixed_size.cpp:47
Eigen::RowMajor
@ RowMajor
Definition: Constants.h:321
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(cxx11_tensor_fixed_size)
Definition: cxx11_tensor_fixed_size.cpp:253
test_3d
static void test_3d()
Definition: cxx11_tensor_fixed_size.cpp:180
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
test_tensor_map
static void test_tensor_map()
Definition: cxx11_tensor_fixed_size.cpp:106
Eigen::TensorMap
A tensor expression mapping an existing array of data.
Definition: TensorForwardDeclarations.h:52
gtsam::vec4
static SO4::VectorN2 vec4(const Matrix4 &Q)
Definition: SO4.cpp:140
test_0d
static void test_0d()
Definition: cxx11_tensor_fixed_size.cpp:18
Eigen::internal::array_prod
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t array_prod(const Sizes< Indices... > &)
Definition: TensorDimensions.h:140
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
VERIFY_IS_APPROX
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:15
Eigen::TensorFixedSize::size
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const
Definition: TensorFixedSize.h:65
VERIFY_IS_NOT_EQUAL
#define VERIFY_IS_NOT_EQUAL(a, b)
Definition: main.h:387
main.h
Eigen::TensorFixedSize
The fixed sized version of the tensor class.
Definition: TensorFixedSize.h:27
Eigen::TensorFixedSize::data
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar * data()
Definition: TensorFixedSize.h:66
vec1
RowVectorXd vec1(3)
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
CALL_SUBTEST
#define CALL_SUBTEST(FUNC)
Definition: main.h:399


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:02:08