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<Index>(reduction_axis[0]), 0);
26  VERIFY_IS_EQUAL(static_cast<Index>(reduction_axis[1]), 1);
27  VERIFY_IS_EQUAL(static_cast<Index>(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<Index>, Eigen::type2indexpair<2,12>> Dims2_b;
171  typedef Eigen::IndexPairList<Eigen::IndexPair<Index>, Eigen::type2indexpair<1,11>, Eigen::IndexPair<Index>> Dims2_c;
172 
173  Dims2_a d2_a;
174 
175  Dims2_b d2_b;
176  d2_b.set(1, Eigen::IndexPair<Index>(1,11));
177 
178  Dims2_c d2_c;
179  d2_c.set(0, Eigen::IndexPair<Index>(Eigen::IndexPair<Index>(0,10)));
180  d2_c.set(1, Eigen::IndexPair<Index>(1,11)); // setting type2indexpair to correct value.
181  d2_c.set(2, Eigen::IndexPair<Index>(2,12));
182 
183  VERIFY_IS_EQUAL(d2_a[0].first, 0);
184  VERIFY_IS_EQUAL(d2_a[0].second, 10);
185  VERIFY_IS_EQUAL(d2_a[1].first, 1);
186  VERIFY_IS_EQUAL(d2_a[1].second, 11);
187  VERIFY_IS_EQUAL(d2_a[2].first, 2);
188  VERIFY_IS_EQUAL(d2_a[2].second, 12);
189 
190  VERIFY_IS_EQUAL(d2_b[0].first, 0);
191  VERIFY_IS_EQUAL(d2_b[0].second, 10);
192  VERIFY_IS_EQUAL(d2_b[1].first, 1);
193  VERIFY_IS_EQUAL(d2_b[1].second, 11);
194  VERIFY_IS_EQUAL(d2_b[2].first, 2);
195  VERIFY_IS_EQUAL(d2_b[2].second, 12);
196 
197  VERIFY_IS_EQUAL(d2_c[0].first, 0);
198  VERIFY_IS_EQUAL(d2_c[0].second, 10);
199  VERIFY_IS_EQUAL(d2_c[1].first, 1);
200  VERIFY_IS_EQUAL(d2_c[1].second, 11);
201  VERIFY_IS_EQUAL(d2_c[2].first, 2);
202  VERIFY_IS_EQUAL(d2_c[2].second, 12);
203 
204  EIGEN_STATIC_ASSERT((d2_a.value_known_statically(0) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
205  EIGEN_STATIC_ASSERT((d2_a.value_known_statically(1) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
206  EIGEN_STATIC_ASSERT((d2_a.value_known_statically(2) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
207 
208  EIGEN_STATIC_ASSERT((d2_b.value_known_statically(0) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
209  EIGEN_STATIC_ASSERT((d2_b.value_known_statically(1) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
210  EIGEN_STATIC_ASSERT((d2_b.value_known_statically(2) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
211 
212  EIGEN_STATIC_ASSERT((d2_c.value_known_statically(0) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
213  EIGEN_STATIC_ASSERT((d2_c.value_known_statically(1) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
214  EIGEN_STATIC_ASSERT((d2_c.value_known_statically(2) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
215 
216  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims0>(0, 0) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
217  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims0>(0, 1) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
218 
219  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_a>(0, 0) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
220  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_a>(0, 1) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
221  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_a>(1, 1) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
222  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_a>(1, 2) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
223  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_a>(2, 2) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
224  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_a>(2, 3) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
225 
226  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_b>(0, 0) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
227  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_b>(0, 1) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
228  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_b>(1, 1) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
229  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_b>(1, 2) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
230  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_b>(2, 2) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
231  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_b>(2, 3) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
232 
233  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_c>(0, 0) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
234  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_c>(0, 1) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
235  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_c>(1, 1) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
236  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_c>(1, 2) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
237  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_c>(2, 2) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
238  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_first_statically_eq<Dims2_c>(2, 3) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
239 
240  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims0>(0, 10) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
241  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims0>(0, 11) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
242 
243  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_a>(0, 10) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
244  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_a>(0, 11) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
245  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_a>(1, 11) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
246  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_a>(1, 12) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
247  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_a>(2, 12) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
248  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_a>(2, 13) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
249 
250  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_b>(0, 10) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
251  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_b>(0, 11) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
252  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_b>(1, 11) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
253  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_b>(1, 12) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
254  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_b>(2, 12) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
255  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_b>(2, 13) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
256 
257  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_c>(0, 10) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
258  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_c>(0, 11) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
259  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_c>(1, 11) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
260  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_c>(1, 12) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
261  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_c>(2, 12) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
262  EIGEN_STATIC_ASSERT((Eigen::internal::index_pair_second_statically_eq<Dims2_c>(2, 13) == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
263 }
264 
265 
266 static void test_dynamic_index_list()
267 {
268  Tensor<float, 4> tensor(2,3,5,7);
269  tensor.setRandom();
270 
271  int dim1 = 2;
272  int dim2 = 1;
273  int dim3 = 0;
274 
275  auto reduction_axis = make_index_list(dim1, dim2, dim3);
276 
277  VERIFY_IS_EQUAL(internal::array_get<0>(reduction_axis), 2);
278  VERIFY_IS_EQUAL(internal::array_get<1>(reduction_axis), 1);
279  VERIFY_IS_EQUAL(internal::array_get<2>(reduction_axis), 0);
280  VERIFY_IS_EQUAL(static_cast<Index>(reduction_axis[0]), 2);
281  VERIFY_IS_EQUAL(static_cast<Index>(reduction_axis[1]), 1);
282  VERIFY_IS_EQUAL(static_cast<Index>(reduction_axis[2]), 0);
283 
284  Tensor<float, 1> result = tensor.sum(reduction_axis);
285  for (int i = 0; i < result.size(); ++i) {
286  float expected = 0.0f;
287  for (int j = 0; j < 2; ++j) {
288  for (int k = 0; k < 3; ++k) {
289  for (int l = 0; l < 5; ++l) {
290  expected += tensor(j,k,l,i);
291  }
292  }
293  }
294  VERIFY_IS_APPROX(result(i), expected);
295  }
296 }
297 
298 static void test_mixed_index_list()
299 {
300  Tensor<float, 4> tensor(2,3,5,7);
301  tensor.setRandom();
302 
303  int dim2 = 1;
304  int dim4 = 3;
305 
306  auto reduction_axis = make_index_list(0, dim2, 2, dim4);
307 
308  VERIFY_IS_EQUAL(internal::array_get<0>(reduction_axis), 0);
309  VERIFY_IS_EQUAL(internal::array_get<1>(reduction_axis), 1);
310  VERIFY_IS_EQUAL(internal::array_get<2>(reduction_axis), 2);
311  VERIFY_IS_EQUAL(internal::array_get<3>(reduction_axis), 3);
312  VERIFY_IS_EQUAL(static_cast<Index>(reduction_axis[0]), 0);
313  VERIFY_IS_EQUAL(static_cast<Index>(reduction_axis[1]), 1);
314  VERIFY_IS_EQUAL(static_cast<Index>(reduction_axis[2]), 2);
315  VERIFY_IS_EQUAL(static_cast<Index>(reduction_axis[3]), 3);
316 
317  typedef IndexList<type2index<0>, int, type2index<2>, int> ReductionIndices;
318  ReductionIndices reduction_indices;
319  reduction_indices.set(1, 1);
320  reduction_indices.set(3, 3);
321  EIGEN_STATIC_ASSERT((internal::array_get<0>(reduction_indices) == 0), YOU_MADE_A_PROGRAMMING_MISTAKE);
322  EIGEN_STATIC_ASSERT((internal::array_get<2>(reduction_indices) == 2), YOU_MADE_A_PROGRAMMING_MISTAKE);
323  EIGEN_STATIC_ASSERT((internal::index_known_statically<ReductionIndices>(0) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
324  EIGEN_STATIC_ASSERT((internal::index_known_statically<ReductionIndices>(2) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
325  EIGEN_STATIC_ASSERT((internal::index_statically_eq<ReductionIndices>(0, 0) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
326  EIGEN_STATIC_ASSERT((internal::index_statically_eq<ReductionIndices>(2, 2) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
327 #if 0
328  EIGEN_STATIC_ASSERT((internal::all_indices_known_statically<ReductionIndices>() == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
329  EIGEN_STATIC_ASSERT((internal::indices_statically_known_to_increase<ReductionIndices>() == false), YOU_MADE_A_PROGRAMMING_MISTAKE);
330 #endif
331 
332  typedef IndexList<type2index<0>, type2index<1>, type2index<2>, type2index<3>> ReductionList;
333  ReductionList reduction_list;
334  EIGEN_STATIC_ASSERT((internal::index_statically_eq<ReductionList>(0, 0) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
335  EIGEN_STATIC_ASSERT((internal::index_statically_eq<ReductionList>(1, 1) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
336  EIGEN_STATIC_ASSERT((internal::index_statically_eq<ReductionList>(2, 2) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
337  EIGEN_STATIC_ASSERT((internal::index_statically_eq<ReductionList>(3, 3) == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
338 #if 0
339  EIGEN_STATIC_ASSERT((internal::all_indices_known_statically<ReductionList>() == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
340  EIGEN_STATIC_ASSERT((internal::indices_statically_known_to_increase<ReductionList>() == true), YOU_MADE_A_PROGRAMMING_MISTAKE);
341 #endif
342 
343  Tensor<float, 0> result1 = tensor.sum(reduction_axis);
344  Tensor<float, 0> result2 = tensor.sum(reduction_indices);
345  Tensor<float, 0> result3 = tensor.sum(reduction_list);
346 
347  float expected = 0.0f;
348  for (int i = 0; i < 2; ++i) {
349  for (int j = 0; j < 3; ++j) {
350  for (int k = 0; k < 5; ++k) {
351  for (int l = 0; l < 7; ++l) {
352  expected += tensor(i,j,k,l);
353  }
354  }
355  }
356  }
357  VERIFY_IS_APPROX(result1(), expected);
358  VERIFY_IS_APPROX(result2(), expected);
359  VERIFY_IS_APPROX(result3(), expected);
360 }
361 
362 
363 static void test_dim_check()
364 {
365  Eigen::IndexList<Eigen::type2index<1>, int> dim1;
366  dim1.set(1, 2);
367  Eigen::IndexList<Eigen::type2index<1>, int> dim2;
368  dim2.set(1, 2);
369  VERIFY(dimensions_match(dim1, dim2));
370 }
371 
372 
373 #endif
374 
375 EIGEN_DECLARE_TEST(cxx11_tensor_index_list)
376 {
377 #ifdef EIGEN_HAS_INDEX_LIST
378  CALL_SUBTEST(test_static_index_list());
379  CALL_SUBTEST(test_type2index_list());
380  CALL_SUBTEST(test_type2indexpair_list());
381  CALL_SUBTEST(test_dynamic_index_list());
382  CALL_SUBTEST(test_mixed_index_list());
383  CALL_SUBTEST(test_dim_check());
384 #endif
385 }
Matrix3f m
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const
Definition: Tensor.h:103
Matrix expected
Definition: testMatrix.cpp:971
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool dimensions_match(Dims1 dims1, Dims2 dims2)
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition: StaticAssert.h:127
EIGEN_CONSTEXPR Index first(const T &x) EIGEN_NOEXCEPT
#define VERIFY_IS_APPROX(a, b)
static const Line3 l(Rot3(), 1, 1)
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
Values result
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
#define CALL_SUBTEST(FUNC)
Definition: main.h:399
#define VERIFY(a)
Definition: main.h:380
EIGEN_DECLARE_TEST(cxx11_tensor_index_list)
EIGEN_DEVICE_FUNC void set(IndexPair< Idx > val)
Definition: TensorMeta.h:251
std::ptrdiff_t j
The tensor class.
Definition: Tensor.h:63


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