1 // This file is part of Eigen, a lightweight C++ template library 2 // for linear algebra. 3 // 4 // Copyright (C) 2015 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 #include <limits> 12 #include <map> 13 14 #include <Eigen/Dense> 15 #include <Eigen/CXX11/Tensor> 16 17 using Eigen::Tensor; 18 19 20 template <int DataLayout> 21 static void test_map_as_index() 22 { 23 #ifdef EIGEN_HAS_SFINAE 24 Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7); 25 tensor.setRandom(); 26 27 using NormalIndex = DSizes<ptrdiff_t, 4>; 28 using CustomIndex = std::map<ptrdiff_t, ptrdiff_t>; 29 CustomIndex coeffC; 30 coeffC[0] = 1; 31 coeffC[1] = 2; 32 coeffC[2] = 4; 33 coeffC[3] = 1; 34 NormalIndex coeff(1,2,4,1); 35 36 VERIFY_IS_EQUAL(tensor.coeff(coeffC), tensor.coeff(coeff)); 37 VERIFY_IS_EQUAL(tensor.coeffRef(coeffC), tensor.coeffRef(coeff)); 38 #endif 39 } 40 41 42 template <int DataLayout> 43 static void test_matrix_as_index() 44 { 45 #ifdef EIGEN_HAS_SFINAE 46 Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7); 47 tensor.setRandom(); 48 49 using NormalIndex = DSizes<ptrdiff_t, 4>; 50 using CustomIndex = Matrix<unsigned int, 4, 1>; 51 CustomIndex coeffC(1,2,4,1); 52 NormalIndex coeff(1,2,4,1); 53 54 VERIFY_IS_EQUAL(tensor.coeff(coeffC), tensor.coeff(coeff)); 55 VERIFY_IS_EQUAL(tensor.coeffRef(coeffC), tensor.coeffRef(coeff)); 56 #endif 57 } 58 59 60 template <int DataLayout> 61 static void test_varlist_as_index() 62 { 63 #ifdef EIGEN_HAS_SFINAE 64 Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7); 65 tensor.setRandom(); 66 67 DSizes<ptrdiff_t, 4> coeff(1,2,4,1); 68 69 VERIFY_IS_EQUAL(tensor.coeff({1,2,4,1}), tensor.coeff(coeff)); 70 VERIFY_IS_EQUAL(tensor.coeffRef({1,2,4,1}), tensor.coeffRef(coeff)); 71 #endif 72 } 73 74 75 template <int DataLayout> 76 static void test_sizes_as_index() 77 { 78 #ifdef EIGEN_HAS_SFINAE 79 Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7); 80 tensor.setRandom(); 81 82 DSizes<ptrdiff_t, 4> coeff(1,2,4,1); 83 Sizes<1,2,4,1> coeffC; 84 85 VERIFY_IS_EQUAL(tensor.coeff(coeffC), tensor.coeff(coeff)); 86 VERIFY_IS_EQUAL(tensor.coeffRef(coeffC), tensor.coeffRef(coeff)); 87 #endif 88 } 89 90 91 void test_cxx11_tensor_custom_index() { 92 test_map_as_index<ColMajor>(); 93 test_map_as_index<RowMajor>(); 94 test_matrix_as_index<ColMajor>(); 95 test_matrix_as_index<RowMajor>(); 96 test_varlist_as_index<ColMajor>(); 97 test_varlist_as_index<RowMajor>(); 98 test_sizes_as_index<ColMajor>(); 99 test_sizes_as_index<RowMajor>(); 100 } 101