cxx11_tensor_map.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 static void test_0d()
18 {
19  Tensor<int, 0> scalar1;
21 
22  TensorMap<const Tensor<int, 0> > scalar3(scalar1.data());
24 
25  scalar1() = 7;
26  scalar2() = 13;
27 
28  VERIFY_IS_EQUAL(scalar1.rank(), 0);
29  VERIFY_IS_EQUAL(scalar1.size(), 1);
30 
31  VERIFY_IS_EQUAL(scalar3(), 7);
32  VERIFY_IS_EQUAL(scalar4(), 13);
33 }
34 
35 static void test_1d()
36 {
39 
42 
43  vec1(0) = 4; vec2(0) = 0;
44  vec1(1) = 8; vec2(1) = 1;
45  vec1(2) = 15; vec2(2) = 2;
46  vec1(3) = 16; vec2(3) = 3;
47  vec1(4) = 23; vec2(4) = 4;
48  vec1(5) = 42; vec2(5) = 5;
49 
50  VERIFY_IS_EQUAL(vec1.rank(), 1);
51  VERIFY_IS_EQUAL(vec1.size(), 6);
52  VERIFY_IS_EQUAL(vec1.dimension(0), 6);
53 
54  VERIFY_IS_EQUAL(vec3(0), 4);
55  VERIFY_IS_EQUAL(vec3(1), 8);
56  VERIFY_IS_EQUAL(vec3(2), 15);
57  VERIFY_IS_EQUAL(vec3(3), 16);
58  VERIFY_IS_EQUAL(vec3(4), 23);
59  VERIFY_IS_EQUAL(vec3(5), 42);
60 
61  VERIFY_IS_EQUAL(vec4(0), 0);
62  VERIFY_IS_EQUAL(vec4(1), 1);
63  VERIFY_IS_EQUAL(vec4(2), 2);
64  VERIFY_IS_EQUAL(vec4(3), 3);
65  VERIFY_IS_EQUAL(vec4(4), 4);
66  VERIFY_IS_EQUAL(vec4(5), 5);
67 }
68 
69 static void test_2d()
70 {
71  Tensor<int, 2> mat1(2,3);
72  Tensor<int, 2, RowMajor> mat2(2,3);
73 
74  mat1(0,0) = 0;
75  mat1(0,1) = 1;
76  mat1(0,2) = 2;
77  mat1(1,0) = 3;
78  mat1(1,1) = 4;
79  mat1(1,2) = 5;
80 
81  mat2(0,0) = 0;
82  mat2(0,1) = 1;
83  mat2(0,2) = 2;
84  mat2(1,0) = 3;
85  mat2(1,1) = 4;
86  mat2(1,2) = 5;
87 
88  TensorMap<const Tensor<int, 2> > mat3(mat1.data(), 2, 3);
90 
91  VERIFY_IS_EQUAL(mat3.rank(), 2);
92  VERIFY_IS_EQUAL(mat3.size(), 6);
93  VERIFY_IS_EQUAL(mat3.dimension(0), 2);
94  VERIFY_IS_EQUAL(mat3.dimension(1), 3);
95 
96  VERIFY_IS_EQUAL(mat4.rank(), 2);
97  VERIFY_IS_EQUAL(mat4.size(), 6);
98  VERIFY_IS_EQUAL(mat4.dimension(0), 2);
99  VERIFY_IS_EQUAL(mat4.dimension(1), 3);
100 
101  VERIFY_IS_EQUAL(mat3(0,0), 0);
102  VERIFY_IS_EQUAL(mat3(0,1), 1);
103  VERIFY_IS_EQUAL(mat3(0,2), 2);
104  VERIFY_IS_EQUAL(mat3(1,0), 3);
105  VERIFY_IS_EQUAL(mat3(1,1), 4);
106  VERIFY_IS_EQUAL(mat3(1,2), 5);
107 
108  VERIFY_IS_EQUAL(mat4(0,0), 0);
109  VERIFY_IS_EQUAL(mat4(0,1), 1);
110  VERIFY_IS_EQUAL(mat4(0,2), 2);
111  VERIFY_IS_EQUAL(mat4(1,0), 3);
112  VERIFY_IS_EQUAL(mat4(1,1), 4);
113  VERIFY_IS_EQUAL(mat4(1,2), 5);
114 }
115 
116 static void test_3d()
117 {
118  Tensor<int, 3> mat1(2,3,7);
119  Tensor<int, 3, RowMajor> mat2(2,3,7);
120 
121  int val = 0;
122  for (int i = 0; i < 2; ++i) {
123  for (int j = 0; j < 3; ++j) {
124  for (int k = 0; k < 7; ++k) {
125  mat1(i,j,k) = val;
126  mat2(i,j,k) = val;
127  val++;
128  }
129  }
130  }
131 
132  TensorMap<const Tensor<int, 3> > mat3(mat1.data(), 2, 3, 7);
133  TensorMap<const Tensor<int, 3, RowMajor> > mat4(mat2.data(), 2, 3, 7);
134 
135  VERIFY_IS_EQUAL(mat3.rank(), 3);
136  VERIFY_IS_EQUAL(mat3.size(), 2*3*7);
137  VERIFY_IS_EQUAL(mat3.dimension(0), 2);
138  VERIFY_IS_EQUAL(mat3.dimension(1), 3);
139  VERIFY_IS_EQUAL(mat3.dimension(2), 7);
140 
141  VERIFY_IS_EQUAL(mat4.rank(), 3);
142  VERIFY_IS_EQUAL(mat4.size(), 2*3*7);
143  VERIFY_IS_EQUAL(mat4.dimension(0), 2);
144  VERIFY_IS_EQUAL(mat4.dimension(1), 3);
145  VERIFY_IS_EQUAL(mat4.dimension(2), 7);
146 
147  val = 0;
148  for (int i = 0; i < 2; ++i) {
149  for (int j = 0; j < 3; ++j) {
150  for (int k = 0; k < 7; ++k) {
151  VERIFY_IS_EQUAL(mat3(i,j,k), val);
152  VERIFY_IS_EQUAL(mat4(i,j,k), val);
153  val++;
154  }
155  }
156  }
157 }
158 
159 
160 static void test_from_tensor()
161 {
162  Tensor<int, 3> mat1(2,3,7);
163  Tensor<int, 3, RowMajor> mat2(2,3,7);
164 
165  int val = 0;
166  for (int i = 0; i < 2; ++i) {
167  for (int j = 0; j < 3; ++j) {
168  for (int k = 0; k < 7; ++k) {
169  mat1(i,j,k) = val;
170  mat2(i,j,k) = val;
171  val++;
172  }
173  }
174  }
175 
176  TensorMap<Tensor<int, 3> > mat3(mat1);
178 
179  VERIFY_IS_EQUAL(mat3.rank(), 3);
180  VERIFY_IS_EQUAL(mat3.size(), 2*3*7);
181  VERIFY_IS_EQUAL(mat3.dimension(0), 2);
182  VERIFY_IS_EQUAL(mat3.dimension(1), 3);
183  VERIFY_IS_EQUAL(mat3.dimension(2), 7);
184 
185  VERIFY_IS_EQUAL(mat4.rank(), 3);
186  VERIFY_IS_EQUAL(mat4.size(), 2*3*7);
187  VERIFY_IS_EQUAL(mat4.dimension(0), 2);
188  VERIFY_IS_EQUAL(mat4.dimension(1), 3);
189  VERIFY_IS_EQUAL(mat4.dimension(2), 7);
190 
191  val = 0;
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  VERIFY_IS_EQUAL(mat3(i,j,k), val);
196  VERIFY_IS_EQUAL(mat4(i,j,k), val);
197  val++;
198  }
199  }
200  }
201 
203 
204  val = 0;
205  for (int i = 0; i < 2; ++i) {
206  for (int j = 0; j < 3; ++j) {
207  for (int k = 0; k < 7; ++k) {
208  array<ptrdiff_t, 3> coords;
209  coords[0] = i;
210  coords[1] = j;
211  coords[2] = k;
212  mat5(coords) = val;
213  val++;
214  }
215  }
216  }
217 
219 
220  VERIFY_IS_EQUAL(mat6.rank(), 3);
221  VERIFY_IS_EQUAL(mat6.size(), 2*3*7);
222  VERIFY_IS_EQUAL(mat6.dimension(0), 2);
223  VERIFY_IS_EQUAL(mat6.dimension(1), 3);
224  VERIFY_IS_EQUAL(mat6.dimension(2), 7);
225 
226  val = 0;
227  for (int i = 0; i < 2; ++i) {
228  for (int j = 0; j < 3; ++j) {
229  for (int k = 0; k < 7; ++k) {
230  VERIFY_IS_EQUAL(mat6(i,j,k), val);
231  val++;
232  }
233  }
234  }
235 }
236 
237 
238 static int f(const TensorMap<Tensor<int, 3> >& tensor) {
239  // Size<0> empty;
240  EIGEN_STATIC_ASSERT((internal::array_size<Sizes<> >::value == 0), YOU_MADE_A_PROGRAMMING_MISTAKE);
241  EIGEN_STATIC_ASSERT((internal::array_size<DSizes<int, 0> >::value == 0), YOU_MADE_A_PROGRAMMING_MISTAKE);
242  Tensor<int, 0> result = tensor.sum();
243  return result();
244 }
245 
246 static void test_casting()
247 {
248  Tensor<int, 3> tensor(2,3,7);
249 
250  int val = 0;
251  for (int i = 0; i < 2; ++i) {
252  for (int j = 0; j < 3; ++j) {
253  for (int k = 0; k < 7; ++k) {
254  tensor(i,j,k) = val;
255  val++;
256  }
257  }
258  }
259 
260  TensorMap<Tensor<int, 3> > map(tensor);
261  int sum1 = f(map);
262  int sum2 = f(tensor);
263 
264  VERIFY_IS_EQUAL(sum1, sum2);
265  VERIFY_IS_EQUAL(sum1, 861);
266 }
267 
268 template<typename T>
269 static const T& add_const(T& value) {
270  return value;
271 }
272 
273 static void test_0d_const_tensor()
274 {
275  Tensor<int, 0> scalar1;
276  Tensor<int, 0, RowMajor> scalar2;
277 
278  TensorMap<const Tensor<int, 0> > scalar3(add_const(scalar1).data());
280 
281  scalar1() = 7;
282  scalar2() = 13;
283 
284  VERIFY_IS_EQUAL(scalar1.rank(), 0);
285  VERIFY_IS_EQUAL(scalar1.size(), 1);
286 
287  VERIFY_IS_EQUAL(scalar3(), 7);
288  VERIFY_IS_EQUAL(scalar4(), 13);
289 }
290 
292 {
293  Tensor<int, 0> scalar1;
294  Tensor<int, 0, RowMajor> scalar2;
295 
296  const TensorMap<Tensor<int, 0> > scalar3(scalar1.data());
297  const TensorMap<Tensor<int, 0, RowMajor> > scalar4(scalar2.data());
298 
299  // Although TensorMap is constant, we still can write to the underlying
300  // storage, because we map over non-constant Tensor.
301  scalar3() = 7;
302  scalar4() = 13;
303 
304  VERIFY_IS_EQUAL(scalar1(), 7);
305  VERIFY_IS_EQUAL(scalar2(), 13);
306 
307  // Pointer to the underlying storage is also non-const.
308  scalar3.data()[0] = 8;
309  scalar4.data()[0] = 14;
310 
311  VERIFY_IS_EQUAL(scalar1(), 8);
312  VERIFY_IS_EQUAL(scalar2(), 14);
313 }
314 
315 EIGEN_DECLARE_TEST(cxx11_tensor_map)
316 {
321 
324 
327 }
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const
Definition: Tensor.h:103
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(Index n) const
Definition: TensorMap.h:131
static void test_3d()
static int f(const TensorMap< Tensor< int, 3 > > &tensor)
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition: StaticAssert.h:127
MatrixXd mat1(size, size)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank() const
Definition: Tensor.h:100
static void test_0d()
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
static const T & add_const(T &value)
Values result
A tensor expression mapping an existing array of data.
static void test_from_tensor()
int data[]
static SO4::VectorN2 vec4(const Matrix4 &Q)
Definition: SO4.cpp:140
static void test_1d()
EIGEN_DECLARE_TEST(cxx11_tensor_map)
The fixed sized version of the tensor class.
static void test_0d_const_tensor()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar * data()
Definition: Tensor.h:104
RowVectorXd vec1(3)
#define CALL_SUBTEST(FUNC)
Definition: main.h:399
static Vector9 vec3(const Matrix3 &R)
Definition: SO3.cpp:342
static void test_2d()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(std::size_t n) const
Definition: Tensor.h:101
static void test_casting()
std::ptrdiff_t j
The tensor class.
Definition: Tensor.h:63
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const
Definition: TensorMap.h:135
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank() const
Definition: TensorMap.h:129
static void test_0d_const_tensor_map()


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