cxx11_tensor_index_list.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 #ifdef EIGEN_HAS_INDEX_LIST
15 
16 static void test_static_index_list()
17 {
18  Tensor<float, 4> tensor(2,3,5,7);
19  tensor.setRandom();
20 
21  constexpr auto reduction_axis = make_index_list(0, 1, 2);
22  VERIFY_IS_EQUAL(internal::array_get<0>(reduction_axis), 0);
23  VERIFY_IS_EQUAL(internal::array_get<1>(reduction_axis), 1);
24  VERIFY_IS_EQUAL(internal::array_get<2>(reduction_axis), 2);
25  VERIFY_IS_EQUAL(static_cast<DenseIndex>(reduction_axis[0]), 0);
26  VERIFY_IS_EQUAL(static_cast<DenseIndex>(reduction_axis[1]), 1);
27  VERIFY_IS_EQUAL(static_cast<DenseIndex>(reduction_axis[2]), 2);
28 
29  EIGEN_STATIC_ASSERT((internal::array_get<0>(reduction_axis) == 0), YOU_MADE_A_PROGRAMMING_MISTAKE);
30  EIGEN_STATIC_ASSERT((internal::array_get<1>(reduction_axis) == 1), YOU_MADE_A_PROGRAMMING_MISTAKE);
31  EIGEN_STATIC_ASSERT((internal::array_get<2>(reduction_axis) == 2), YOU_MADE_A_PROGRAMMING_MISTAKE);
32 
33  Tensor<float, 1> result = tensor.sum(reduction_axis);
34  for (int i = 0; i < result.size(); ++i) {
35  float expected = 0.0f;
36  for (int j = 0; j < 2; ++j) {
37  for (int k = 0; k < 3; ++k) {
38  for (int l = 0; l < 5; ++l) {
39  expected += tensor(j,k,l,i);
40  }
41  }
42  }
43  VERIFY_IS_APPROX(result(i), expected);
44  }
45 }
46 
47 
48 static void test_type2index_list()
49 {
50  Tensor<float, 5> tensor(2,3,5,7,11);
51  tensor.setRandom();
52  tensor += tensor.constant(10.0f);
53 
54  typedef Eigen::IndexList<Eigen::type2index<0>> Dims0;
55  typedef Eigen::IndexList<Eigen::type2index<0>, Eigen::type2index<1>> Dims1;
56  typedef Eigen::IndexList<Eigen::type2index<0>, Eigen::type2index<1>, Eigen::type2index<2>> Dims2;
57  typedef Eigen::IndexList<Eigen::type2index<0>, Eigen::type2index<1>, Eigen::type2index<2>, Eigen::type2index<3>> Dims3;
58  typedef Eigen::IndexList<Eigen::type2index<0>, Eigen::type2index<1>, Eigen::type2index<2>, Eigen::type2index<3>, Eigen::type2index<4>> Dims4;
59 
60 #if 0
61  EIGEN_STATIC_ASSERT((internal::indices_statically_known_to_increase<Dims0>() == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
62  EIGEN_STATIC_ASSERT((internal::indices_statically_known_to_increase<Dims1>() == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
63  EIGEN_STATIC_ASSERT((internal::indices_statically_known_to_increase<Dims2>() == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
64  EIGEN_STATIC_ASSERT((internal::indices_statically_known_to_increase<Dims3>() == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
65  EIGEN_STATIC_ASSERT((internal::indices_statically_known_to_increase<Dims4>() == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
66 #endif
67 
68  EIGEN_STATIC_ASSERT((internal::are_inner_most_dims<Dims0, 1, ColMajor>::value == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
69  EIGEN_STATIC_ASSERT((internal::are_inner_most_dims<Dims1, 2, ColMajor>::value == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
70  EIGEN_STATIC_ASSERT((internal::are_inner_most_dims<Dims2, 3, ColMajor>::value == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
71  EIGEN_STATIC_ASSERT((internal::are_inner_most_dims<Dims3, 4, ColMajor>::value == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
72  EIGEN_STATIC_ASSERT((internal::are_inner_most_dims<Dims4, 5, ColMajor>::value == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
73 
74  EIGEN_STATIC_ASSERT((internal::are_inner_most_dims<Dims0, 1, RowMajor>::value == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
75  EIGEN_STATIC_ASSERT((internal::are_inner_most_dims<Dims1, 2, RowMajor>::value == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
76  EIGEN_STATIC_ASSERT((internal::are_inner_most_dims<Dims2, 3, RowMajor>::value == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
77  EIGEN_STATIC_ASSERT((internal::are_inner_most_dims<Dims3, 4, RowMajor>::value == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
78  EIGEN_STATIC_ASSERT((internal::are_inner_most_dims<Dims4, 5, RowMajor>::value == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
79 
80  const Dims0 reduction_axis0;
81  Tensor<float, 4> result0 = tensor.sum(reduction_axis0);
82  for (int m = 0; m < 11; ++m) {
83  for (int l = 0; l < 7; ++l) {
84  for (int k = 0; k < 5; ++k) {
85  for (int j = 0; j < 3; ++j) {
86  float expected = 0.0f;
87  for (int i = 0; i < 2; ++i) {
88  expected += tensor(i,j,k,l,m);
89  }
90  VERIFY_IS_APPROX(result0(j,k,l,m), expected);
91  }
92  }
93  }
94  }
95 
96  const Dims1 reduction_axis1;
97  Tensor<float, 3> result1 = tensor.sum(reduction_axis1);
98  for (int m = 0; m < 11; ++m) {
99  for (int l = 0; l < 7; ++l) {
100  for (int k = 0; k < 5; ++k) {
101  float expected = 0.0f;
102  for (int j = 0; j < 3; ++j) {
103  for (int i = 0; i < 2; ++i) {
104  expected += tensor(i,j,k,l,m);
105  }
106  }
107  VERIFY_IS_APPROX(result1(k,l,m), expected);
108  }
109  }
110  }
111 
112  const Dims2 reduction_axis2;
113  Tensor<float, 2> result2 = tensor.sum(reduction_axis2);
114  for (int m = 0; m < 11; ++m) {
115  for (int l = 0; l < 7; ++l) {
116  float expected = 0.0f;
117  for (int k = 0; k < 5; ++k) {
118  for (int j = 0; j < 3; ++j) {
119  for (int i = 0; i < 2; ++i) {
120  expected += tensor(i,j,k,l,m);
121  }
122  }
123  }
124  VERIFY_IS_APPROX(result2(l,m), expected);
125  }
126  }
127 
128  const Dims3 reduction_axis3;
129  Tensor<float, 1> result3 = tensor.sum(reduction_axis3);
130  for (int m = 0; m < 11; ++m) {
131  float expected = 0.0f;
132  for (int l = 0; l < 7; ++l) {
133  for (int k = 0; k < 5; ++k) {
134  for (int j = 0; j < 3; ++j) {
135  for (int i = 0; i < 2; ++i) {
136  expected += tensor(i,j,k,l,m);
137  }
138  }
139  }
140  }
141  VERIFY_IS_APPROX(result3(m), expected);
142  }
143 
144  const Dims4 reduction_axis4;
145  Tensor<float, 0> result4 = tensor.sum(reduction_axis4);
146  float expected = 0.0f;
147  for (int m = 0; m < 11; ++m) {
148  for (int l = 0; l < 7; ++l) {
149  for (int k = 0; k < 5; ++k) {
150  for (int j = 0; j < 3; ++j) {
151  for (int i = 0; i < 2; ++i) {
152  expected += tensor(i,j,k,l,m);
153  }
154  }
155  }
156  }
157  }
158  VERIFY_IS_APPROX(result4(), expected);
159 }
160 
161 
162 static void test_type2indexpair_list()
163 {
164  Tensor<float, 5> tensor(2,3,5,7,11);
165  tensor.setRandom();
166  tensor += tensor.constant(10.0f);
167 
168  typedef Eigen::IndexPairList<Eigen::type2indexpair<0,10>> Dims0;
169  typedef Eigen::IndexPairList<Eigen::type2indexpair<0,10>, Eigen::type2indexpair<1,11>, Eigen::type2indexpair<2,12>> Dims2_a;
170  typedef Eigen::IndexPairList<Eigen::type2indexpair<0,10>, Eigen::IndexPair<DenseIndex>, Eigen::type2indexpair<2,12>> Dims2_b;
171  typedef Eigen::IndexPairList<Eigen::IndexPair<DenseIndex>, Eigen::type2indexpair<1,11>, Eigen::IndexPair<DenseIndex>> Dims2_c;
172 
173  Dims0 d0;
174  Dims2_a d2_a;
175 
176  Dims2_b d2_b;
177  d2_b.set(1, Eigen::IndexPair<DenseIndex>(1,11));
178 
179  Dims2_c d2_c;
180  d2_c.set(0, Eigen::IndexPair<DenseIndex>(Eigen::IndexPair<DenseIndex>(0,10)));
181  d2_c.set(1, Eigen::IndexPair<DenseIndex>(1,11)); // setting type2indexpair to correct value.
182  d2_c.set(2, Eigen::IndexPair<DenseIndex>(2,12));
183 
184  VERIFY_IS_EQUAL(d2_a[0].first, 0);
185  VERIFY_IS_EQUAL(d2_a[0].second, 10);
186  VERIFY_IS_EQUAL(d2_a[1].first, 1);
187  VERIFY_IS_EQUAL(d2_a[1].second, 11);
188  VERIFY_IS_EQUAL(d2_a[2].first, 2);
189  VERIFY_IS_EQUAL(d2_a[2].second, 12);
190 
191  VERIFY_IS_EQUAL(d2_b[0].first, 0);
192  VERIFY_IS_EQUAL(d2_b[0].second, 10);
193  VERIFY_IS_EQUAL(d2_b[1].first, 1);
194  VERIFY_IS_EQUAL(d2_b[1].second, 11);
195  VERIFY_IS_EQUAL(d2_b[2].first, 2);
196  VERIFY_IS_EQUAL(d2_b[2].second, 12);
197 
198  VERIFY_IS_EQUAL(d2_c[0].first, 0);
199  VERIFY_IS_EQUAL(d2_c[0].second, 10);
200  VERIFY_IS_EQUAL(d2_c[1].first, 1);
201  VERIFY_IS_EQUAL(d2_c[1].second, 11);
202  VERIFY_IS_EQUAL(d2_c[2].first, 2);
203  VERIFY_IS_EQUAL(d2_c[2].second, 12);
204 
205  EIGEN_STATIC_ASSERT((d2_a.value_known_statically(0) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
206  EIGEN_STATIC_ASSERT((d2_a.value_known_statically(1) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
207  EIGEN_STATIC_ASSERT((d2_a.value_known_statically(2) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
208 
209  EIGEN_STATIC_ASSERT((d2_b.value_known_statically(0) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
210  EIGEN_STATIC_ASSERT((d2_b.value_known_statically(1) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
211  EIGEN_STATIC_ASSERT((d2_b.value_known_statically(2) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
212 
213  EIGEN_STATIC_ASSERT((d2_c.value_known_statically(0) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
214  EIGEN_STATIC_ASSERT((d2_c.value_known_statically(1) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
215  EIGEN_STATIC_ASSERT((d2_c.value_known_statically(2) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
216 
217  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims0>(0, 0) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
218  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims0>(0, 1) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
219 
220  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_a>(0, 0) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
221  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_a>(0, 1) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
222  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_a>(1, 1) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
223  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_a>(1, 2) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
224  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_a>(2, 2) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
225  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_a>(2, 3) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
226 
227  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_b>(0, 0) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
228  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_b>(0, 1) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
229  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_b>(1, 1) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
230  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_b>(1, 2) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
231  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_b>(2, 2) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
232  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_b>(2, 3) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
233 
234  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_c>(0, 0) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
235  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_c>(0, 1) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
236  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_c>(1, 1) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
237  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_c>(1, 2) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
238  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_c>(2, 2) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
239  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_c>(2, 3) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
240 
241  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims0>(0, 10) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
242  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims0>(0, 11) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
243 
244  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_a>(0, 10) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
245  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_a>(0, 11) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
246  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_a>(1, 11) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
247  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_a>(1, 12) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
248  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_a>(2, 12) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
249  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_a>(2, 13) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
250 
251  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_b>(0, 10) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
252  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_b>(0, 11) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
253  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_b>(1, 11) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
254  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_b>(1, 12) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
255  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_b>(2, 12) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
256  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_b>(2, 13) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
257 
258  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_c>(0, 10) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
259  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_c>(0, 11) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
260  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_c>(1, 11) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
261  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_c>(1, 12) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
262  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_c>(2, 12) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
263  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_c>(2, 13) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
264 }
265 
266 
267 static void test_dynamic_index_list()
268 {
269  Tensor<float, 4> tensor(2,3,5,7);
270  tensor.setRandom();
271 
272  int dim1 = 2;
273  int dim2 = 1;
274  int dim3 = 0;
275 
276  auto reduction_axis = make_index_list(dim1, dim2, dim3);
277 
278  VERIFY_IS_EQUAL(internal::array_get<0>(reduction_axis), 2);
279  VERIFY_IS_EQUAL(internal::array_get<1>(reduction_axis), 1);
280  VERIFY_IS_EQUAL(internal::array_get<2>(reduction_axis), 0);
281  VERIFY_IS_EQUAL(static_cast<DenseIndex>(reduction_axis[0]), 2);
282  VERIFY_IS_EQUAL(static_cast<DenseIndex>(reduction_axis[1]), 1);
283  VERIFY_IS_EQUAL(static_cast<DenseIndex>(reduction_axis[2]), 0);
284 
285  Tensor<float, 1> result = tensor.sum(reduction_axis);
286  for (int i = 0; i < result.size(); ++i) {
287  float expected = 0.0f;
288  for (int j = 0; j < 2; ++j) {
289  for (int k = 0; k < 3; ++k) {
290  for (int l = 0; l < 5; ++l) {
291  expected += tensor(j,k,l,i);
292  }
293  }
294  }
295  VERIFY_IS_APPROX(result(i), expected);
296  }
297 }
298 
299 static void test_mixed_index_list()
300 {
301  Tensor<float, 4> tensor(2,3,5,7);
302  tensor.setRandom();
303 
304  int dim2 = 1;
305  int dim4 = 3;
306 
307  auto reduction_axis = make_index_list(0, dim2, 2, dim4);
308 
309  VERIFY_IS_EQUAL(internal::array_get<0>(reduction_axis), 0);
310  VERIFY_IS_EQUAL(internal::array_get<1>(reduction_axis), 1);
311  VERIFY_IS_EQUAL(internal::array_get<2>(reduction_axis), 2);
312  VERIFY_IS_EQUAL(internal::array_get<3>(reduction_axis), 3);
313  VERIFY_IS_EQUAL(static_cast<DenseIndex>(reduction_axis[0]), 0);
314  VERIFY_IS_EQUAL(static_cast<DenseIndex>(reduction_axis[1]), 1);
315  VERIFY_IS_EQUAL(static_cast<DenseIndex>(reduction_axis[2]), 2);
316  VERIFY_IS_EQUAL(static_cast<DenseIndex>(reduction_axis[3]), 3);
317 
318  typedef IndexList<type2index<0>, int, type2index<2>, int> ReductionIndices;
319  ReductionIndices reduction_indices;
320  reduction_indices.set(1, 1);
321  reduction_indices.set(3, 3);
322  EIGEN_STATIC_ASSERT((internal::array_get<0>(reduction_indices) == 0), YOU_MADE_A_PROGRAMMING_MISTAKE);
323  EIGEN_STATIC_ASSERT((internal::array_get<2>(reduction_indices) == 2), YOU_MADE_A_PROGRAMMING_MISTAKE);
324  EIGEN_STATIC_ASSERT((internal::index_known_statically<ReductionIndices>(0) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
325  EIGEN_STATIC_ASSERT((internal::index_known_statically<ReductionIndices>(2) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
326  EIGEN_STATIC_ASSERT((internal::index_statically_eq<ReductionIndices>(0, 0) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
327  EIGEN_STATIC_ASSERT((internal::index_statically_eq<ReductionIndices>(2, 2) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
328 #if 0
329  EIGEN_STATIC_ASSERT((internal::all_indices_known_statically<ReductionIndices>() == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
330  EIGEN_STATIC_ASSERT((internal::indices_statically_known_to_increase<ReductionIndices>() == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
331 #endif
332 
333  typedef IndexList<type2index<0>, type2index<1>, type2index<2>, type2index<3>> ReductionList;
334  ReductionList reduction_list;
335  EIGEN_STATIC_ASSERT((internal::index_statically_eq<ReductionList>(0, 0) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
336  EIGEN_STATIC_ASSERT((internal::index_statically_eq<ReductionList>(1, 1) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
337  EIGEN_STATIC_ASSERT((internal::index_statically_eq<ReductionList>(2, 2) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
338  EIGEN_STATIC_ASSERT((internal::index_statically_eq<ReductionList>(3, 3) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
339 #if 0
340  EIGEN_STATIC_ASSERT((internal::all_indices_known_statically<ReductionList>() == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
341  EIGEN_STATIC_ASSERT((internal::indices_statically_known_to_increase<ReductionList>() == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
342 #endif
343 
344  Tensor<float, 0> result1 = tensor.sum(reduction_axis);
345  Tensor<float, 0> result2 = tensor.sum(reduction_indices);
346  Tensor<float, 0> result3 = tensor.sum(reduction_list);
347 
348  float expected = 0.0f;
349  for (int i = 0; i < 2; ++i) {
350  for (int j = 0; j < 3; ++j) {
351  for (int k = 0; k < 5; ++k) {
352  for (int l = 0; l < 7; ++l) {
353  expected += tensor(i,j,k,l);
354  }
355  }
356  }
357  }
358  VERIFY_IS_APPROX(result1(), expected);
359  VERIFY_IS_APPROX(result2(), expected);
360  VERIFY_IS_APPROX(result3(), expected);
361 }
362 
363 
364 static void test_dim_check()
365 {
366  Eigen::IndexList<Eigen::type2index<1>, int> dim1;
367  dim1.set(1, 2);
368  Eigen::IndexList<Eigen::type2index<1>, int> dim2;
369  dim2.set(1, 2);
370  VERIFY(dimensions_match(dim1, dim2));
371 }
372 
373 
374 #endif
375 
377 {
378 #ifdef EIGEN_HAS_INDEX_LIST
379  CALL_SUBTEST(test_static_index_list());
380  CALL_SUBTEST(test_type2index_list());
381  CALL_SUBTEST(test_type2indexpair_list());
382  CALL_SUBTEST(test_dynamic_index_list());
383  CALL_SUBTEST(test_mixed_index_list());
384  CALL_SUBTEST(test_dim_check());
385 #endif
386 }
void test_cxx11_tensor_index_list()
static int f(const TensorMap< Tensor< int, 3 > > &tensor)
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition: StaticAssert.h:122
EIGEN_DEVICE_FUNC bool dimensions_match(Dims1 &dims1, Dims2 &dims2)
EIGEN_DEVICE_FUNC void set(IndexPair< Idx > val)
Definition: TensorMeta.h:158


hebiros
Author(s): Xavier Artache , Matthew Tesch
autogenerated on Thu Sep 3 2020 04:08:09