• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
test_0d()17 static void test_0d()
18 {
19   Tensor<int, 0> scalar1;
20   Tensor<int, 0, RowMajor> scalar2;
21 
22   TensorMap<Tensor<const int, 0> > scalar3(scalar1.data());
23   TensorMap<Tensor<const int, 0, RowMajor> > scalar4(scalar2.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 
test_1d()35 static void test_1d()
36 {
37   Tensor<int, 1> vec1(6);
38   Tensor<int, 1, RowMajor> vec2(6);
39 
40   TensorMap<Tensor<const int, 1> > vec3(vec1.data(), 6);
41   TensorMap<Tensor<const int, 1, RowMajor> > vec4(vec2.data(), 6);
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 
test_2d()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<Tensor<const int, 2> > mat3(mat1.data(), 2, 3);
89   TensorMap<Tensor<const int, 2, RowMajor> > mat4(mat2.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 
test_3d()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<Tensor<const int, 3> > mat3(mat1.data(), 2, 3, 7);
133   TensorMap<Tensor<const 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 
test_from_tensor()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);
177   TensorMap<Tensor<int, 3, RowMajor> > mat4(mat2);
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 
202   TensorFixedSize<int, Sizes<2,3,7> > mat5;
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 
218   TensorMap<TensorFixedSize<int, Sizes<2,3,7> > > mat6(mat5);
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 
f(const TensorMap<Tensor<int,3>> & tensor)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 
test_casting()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 
test_cxx11_tensor_map()268 void test_cxx11_tensor_map()
269 {
270   CALL_SUBTEST(test_0d());
271   CALL_SUBTEST(test_1d());
272   CALL_SUBTEST(test_2d());
273   CALL_SUBTEST(test_3d());
274 
275   CALL_SUBTEST(test_from_tensor());
276   CALL_SUBTEST(test_casting());
277 }
278