cxx11_tensor_custom_index.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) 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 EIGEN_DECLARE_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 }
Eigen::Tensor
The tensor class.
Definition: Tensor.h:63
test_varlist_as_index
static void test_varlist_as_index()
Definition: cxx11_tensor_custom_index.cpp:61
VERIFY_IS_EQUAL
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
Eigen::Tensor::coeff
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Scalar & coeff(const array< Index, NumIndices > &indices) const
Definition: Tensor.h:124
Eigen::DSizes
Definition: TensorDimensions.h:263
Eigen::Sizes
Definition: TensorDimensions.h:93
Eigen::Tensor::coeffRef
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(const array< Index, NumIndices > &indices)
Definition: Tensor.h:164
Eigen::TensorBase< Tensor< Scalar_, NumIndices_, Options_, IndexType_ > >::setRandom
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor< Scalar_, NumIndices_, Options_, IndexType_ > & setRandom()
Definition: TensorBase.h:996
test_sizes_as_index
static void test_sizes_as_index()
Definition: cxx11_tensor_custom_index.cpp:76
main.h
test_matrix_as_index
static void test_matrix_as_index()
Definition: cxx11_tensor_custom_index.cpp:43
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(cxx11_tensor_custom_index)
Definition: cxx11_tensor_custom_index.cpp:91
test_map_as_index
static void test_map_as_index()
Definition: cxx11_tensor_custom_index.cpp:21


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:02:08