cxx11_tensor_ref.cpp File Reference
#include "main.h"
#include <Eigen/CXX11/Tensor>

Functions

static void test_simple_lvalue_ref ()
 
static void test_simple_rvalue_ref ()
 
static void test_multiple_dims ()
 
static void test_slice ()
 
static void test_ref_of_ref ()
 
static void test_ref_in_expr ()
 
static void test_coeff_ref ()
 
static void test_nested_ops_with_ref ()
 
 EIGEN_DECLARE_TEST (cxx11_tensor_ref)
 

Function Documentation

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( cxx11_tensor_ref  )
222  {
231 }
static void test_slice()
Definition: cxx11_tensor_ref.cpp:85
static void test_multiple_dims()
Definition: cxx11_tensor_ref.cpp:66
static void test_ref_of_ref()
Definition: cxx11_tensor_ref.cpp:111
static void test_simple_lvalue_ref()
Definition: cxx11_tensor_ref.cpp:17
static void test_simple_rvalue_ref()
Definition: cxx11_tensor_ref.cpp:46
static void test_coeff_ref()
Definition: cxx11_tensor_ref.cpp:172
static void test_nested_ops_with_ref()
Definition: cxx11_tensor_ref.cpp:185
static void test_ref_in_expr()
Definition: cxx11_tensor_ref.cpp:140
#define CALL_SUBTEST(FUNC)
Definition: main.h:382

References CALL_SUBTEST, test_coeff_ref(), test_multiple_dims(), test_nested_ops_with_ref(), test_ref_in_expr(), test_ref_of_ref(), test_simple_lvalue_ref(), test_simple_rvalue_ref(), and test_slice().

◆ test_coeff_ref()

static void test_coeff_ref ( )
static
172  {
173  Tensor<float, 5> tensor(2, 3, 5, 7, 11);
174  tensor.setRandom();
175  Tensor<float, 5> original = tensor;
176 
177  TensorRef<Tensor<float, 4>> slice = tensor.chip(7, 4);
178  slice.coeffRef(0, 0, 0, 0) = 1.0f;
179  slice.coeffRef(1, 0, 0, 0) += 2.0f;
180 
181  VERIFY_IS_EQUAL(tensor(0, 0, 0, 0, 7), 1.0f);
182  VERIFY_IS_EQUAL(tensor(1, 0, 0, 0, 7), original(1, 0, 0, 0, 7) + 2.0f);
183 }
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const TensorChippingOp< DimId, const Derived > chip(const Index offset) const
Definition: TensorBase.h:1141
A reference to a tensor expression The expression will be evaluated lazily (as much as possible).
Definition: TensorRef.h:114
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(Index firstIndex, IndexTypes... otherIndices)
Definition: TensorRef.h:192
The tensor class.
Definition: Tensor.h:68
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:367

References Eigen::TensorBase< Derived, AccessLevel >::chip(), Eigen::TensorRef< PlainObjectType >::coeffRef(), Eigen::TensorBase< Derived, AccessLevel >::setRandom(), and VERIFY_IS_EQUAL.

Referenced by EIGEN_DECLARE_TEST().

◆ test_multiple_dims()

static void test_multiple_dims ( )
static
66  {
67  Tensor<float, 3> input(3, 5, 7);
68  input.setRandom();
69 
70  TensorRef<Tensor<float, 3>> ref(input);
71  VERIFY_IS_EQUAL(ref.data(), input.data());
72  VERIFY_IS_EQUAL(ref.dimension(0), 3);
73  VERIFY_IS_EQUAL(ref.dimension(1), 5);
74  VERIFY_IS_EQUAL(ref.dimension(2), 7);
75 
76  for (int i = 0; i < 3; ++i) {
77  for (int j = 0; j < 5; ++j) {
78  for (int k = 0; k < 7; ++k) {
79  VERIFY_IS_EQUAL(ref(i, j, k), input(i, j, k));
80  }
81  }
82  }
83 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
char char char int int * k
Definition: level2_impl.h:374
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

References Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::data(), Eigen::TensorRef< PlainObjectType >::data(), Eigen::TensorRef< PlainObjectType >::dimension(), i, j, k, Eigen::TensorBase< Derived, AccessLevel >::setRandom(), and VERIFY_IS_EQUAL.

Referenced by EIGEN_DECLARE_TEST().

◆ test_nested_ops_with_ref()

static void test_nested_ops_with_ref ( )
static
185  {
186  Tensor<float, 4> t(2, 3, 5, 7);
187  t.setRandom();
188  TensorMap<Tensor<const float, 4>> m(t.data(), 2, 3, 5, 7);
190  paddings[0] = std::make_pair(0, 0);
191  paddings[1] = std::make_pair(2, 1);
192  paddings[2] = std::make_pair(3, 4);
193  paddings[3] = std::make_pair(0, 0);
194  DSizes<Eigen::DenseIndex, 4> shuffle_dims(0, 1, 2, 3);
195  TensorRef<Tensor<const float, 4>> ref(m.pad(paddings));
197  trivial[0] = std::make_pair(0, 0);
198  trivial[1] = std::make_pair(0, 0);
199  trivial[2] = std::make_pair(0, 0);
200  trivial[3] = std::make_pair(0, 0);
201  Tensor<float, 4> padded = ref.shuffle(shuffle_dims).pad(trivial);
202  VERIFY_IS_EQUAL(padded.dimension(0), 2 + 0);
203  VERIFY_IS_EQUAL(padded.dimension(1), 3 + 3);
204  VERIFY_IS_EQUAL(padded.dimension(2), 5 + 7);
205  VERIFY_IS_EQUAL(padded.dimension(3), 7 + 0);
206 
207  for (int i = 0; i < 2; ++i) {
208  for (int j = 0; j < 6; ++j) {
209  for (int k = 0; k < 12; ++k) {
210  for (int l = 0; l < 7; ++l) {
211  if (j >= 2 && j < 5 && k >= 3 && k < 8) {
212  VERIFY_IS_EQUAL(padded(i, j, k, l), t(i, j - 2, k - 3, l));
213  } else {
214  VERIFY_IS_EQUAL(padded(i, j, k, l), 0.0f);
215  }
216  }
217  }
218  }
219  }
220 }
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const TensorShufflingOp< const Shuffle, const Derived > shuffle(const Shuffle &shfl) const
Definition: TensorBase.h:1187
A tensor expression mapping an existing array of data.
Definition: TensorMap.h:33
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(std::size_t n) const
Definition: Tensor.h:99
int * m
Definition: level2_cplx_impl.h:294
std::array< T, N > array
Definition: EmulateArray.h:231
t
Definition: plotPSD.py:36
Definition: TensorDimensions.h:161

References Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::dimension(), i, j, k, m, Eigen::TensorBase< Derived, AccessLevel >::shuffle(), plotPSD::t, and VERIFY_IS_EQUAL.

Referenced by EIGEN_DECLARE_TEST().

◆ test_ref_in_expr()

static void test_ref_in_expr ( )
static
140  {
141  Tensor<float, 3> input(3, 5, 7);
142  input.setRandom();
143  TensorRef<Tensor<float, 3>> input_ref(input);
144 
145  Tensor<float, 3> result(3, 5, 7);
146  result.setRandom();
147  TensorRef<Tensor<float, 3>> result_ref(result);
148 
149  Tensor<float, 3> bias(3, 5, 7);
150  bias.setRandom();
151 
152  result_ref = input_ref + bias;
153  for (int i = 0; i < 3; ++i) {
154  for (int j = 0; j < 5; ++j) {
155  for (int k = 0; k < 7; ++k) {
156  VERIFY_IS_EQUAL(result_ref(i, j, k), input(i, j, k) + bias(i, j, k));
157  VERIFY_IS_NOT_EQUAL(result(i, j, k), input(i, j, k) + bias(i, j, k));
158  }
159  }
160  }
161 
162  result = result_ref;
163  for (int i = 0; i < 3; ++i) {
164  for (int j = 0; j < 5; ++j) {
165  for (int k = 0; k < 7; ++k) {
166  VERIFY_IS_EQUAL(result(i, j, k), input(i, j, k) + bias(i, j, k));
167  }
168  }
169  }
170 }
#define VERIFY_IS_NOT_EQUAL(a, b)
Definition: main.h:368

References i, j, k, Eigen::TensorBase< Derived, AccessLevel >::setRandom(), VERIFY_IS_EQUAL, and VERIFY_IS_NOT_EQUAL.

Referenced by EIGEN_DECLARE_TEST().

◆ test_ref_of_ref()

static void test_ref_of_ref ( )
static
111  {
112  Tensor<float, 3> input(3, 5, 7);
113  input.setRandom();
114 
115  TensorRef<Tensor<float, 3>> ref(input);
116  TensorRef<Tensor<float, 3>> ref_of_ref(ref);
117  TensorRef<Tensor<float, 3>> ref_of_ref2;
118  ref_of_ref2 = ref;
119 
120  VERIFY_IS_EQUAL(ref_of_ref.data(), input.data());
121  VERIFY_IS_EQUAL(ref_of_ref.dimension(0), 3);
122  VERIFY_IS_EQUAL(ref_of_ref.dimension(1), 5);
123  VERIFY_IS_EQUAL(ref_of_ref.dimension(2), 7);
124 
125  VERIFY_IS_EQUAL(ref_of_ref2.data(), input.data());
126  VERIFY_IS_EQUAL(ref_of_ref2.dimension(0), 3);
127  VERIFY_IS_EQUAL(ref_of_ref2.dimension(1), 5);
128  VERIFY_IS_EQUAL(ref_of_ref2.dimension(2), 7);
129 
130  for (int i = 0; i < 3; ++i) {
131  for (int j = 0; j < 5; ++j) {
132  for (int k = 0; k < 7; ++k) {
133  VERIFY_IS_EQUAL(ref_of_ref(i, j, k), input(i, j, k));
134  VERIFY_IS_EQUAL(ref_of_ref2(i, j, k), input(i, j, k));
135  }
136  }
137  }
138 }
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(Index n) const
Definition: TensorRef.h:178
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar * data() const
Definition: TensorRef.h:181

References Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::data(), Eigen::TensorRef< PlainObjectType >::data(), Eigen::TensorRef< PlainObjectType >::dimension(), i, j, k, Eigen::TensorBase< Derived, AccessLevel >::setRandom(), and VERIFY_IS_EQUAL.

Referenced by EIGEN_DECLARE_TEST().

◆ test_simple_lvalue_ref()

static void test_simple_lvalue_ref ( )
static
17  {
18  Tensor<int, 1> input(6);
19  input.setRandom();
20 
21  TensorRef<Tensor<int, 1>> ref3(input);
22  TensorRef<Tensor<int, 1>> ref4 = input;
23 
24  VERIFY_IS_EQUAL(ref3.data(), input.data());
25  VERIFY_IS_EQUAL(ref4.data(), input.data());
26 
27  for (int i = 0; i < 6; ++i) {
28  VERIFY_IS_EQUAL(ref3(i), input(i));
29  VERIFY_IS_EQUAL(ref4(i), input(i));
30  }
31 
32  for (int i = 0; i < 6; ++i) {
33  ref3.coeffRef(i) = i;
34  }
35  for (int i = 0; i < 6; ++i) {
36  VERIFY_IS_EQUAL(input(i), i);
37  }
38  for (int i = 0; i < 6; ++i) {
39  ref4.coeffRef(i) = -i * 2;
40  }
41  for (int i = 0; i < 6; ++i) {
42  VERIFY_IS_EQUAL(input(i), -i * 2);
43  }
44 }

References Eigen::TensorRef< PlainObjectType >::coeffRef(), Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::data(), Eigen::TensorRef< PlainObjectType >::data(), i, Eigen::TensorBase< Derived, AccessLevel >::setRandom(), and VERIFY_IS_EQUAL.

Referenced by EIGEN_DECLARE_TEST().

◆ test_simple_rvalue_ref()

static void test_simple_rvalue_ref ( )
static
46  {
47  Tensor<int, 1> input1(6);
48  input1.setRandom();
49  Tensor<int, 1> input2(6);
50  input2.setRandom();
51 
52  TensorRef<Tensor<int, 1>> ref3(input1 + input2);
53  TensorRef<Tensor<int, 1>> ref4 = input1 + input2;
54 
55  VERIFY_IS_NOT_EQUAL(ref3.data(), input1.data());
56  VERIFY_IS_NOT_EQUAL(ref4.data(), input1.data());
57  VERIFY_IS_NOT_EQUAL(ref3.data(), input2.data());
58  VERIFY_IS_NOT_EQUAL(ref4.data(), input2.data());
59 
60  for (int i = 0; i < 6; ++i) {
61  VERIFY_IS_EQUAL(ref3(i), input1(i) + input2(i));
62  VERIFY_IS_EQUAL(ref4(i), input1(i) + input2(i));
63  }
64 }

References Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::data(), Eigen::TensorRef< PlainObjectType >::data(), i, Eigen::TensorBase< Derived, AccessLevel >::setRandom(), VERIFY_IS_EQUAL, and VERIFY_IS_NOT_EQUAL.

Referenced by EIGEN_DECLARE_TEST().

◆ test_slice()

static void test_slice ( )
static
85  {
86  Tensor<float, 5> tensor(2, 3, 5, 7, 11);
87  tensor.setRandom();
88 
89  Eigen::DSizes<ptrdiff_t, 5> indices(1, 2, 3, 4, 5);
90  Eigen::DSizes<ptrdiff_t, 5> sizes(1, 1, 1, 1, 1);
91  TensorRef<Tensor<float, 5>> slice = tensor.slice(indices, sizes);
92  VERIFY_IS_EQUAL(slice(0, 0, 0, 0, 0), tensor(1, 2, 3, 4, 5));
93 
94  Eigen::DSizes<ptrdiff_t, 5> indices2(1, 1, 3, 4, 5);
95  Eigen::DSizes<ptrdiff_t, 5> sizes2(1, 1, 2, 2, 3);
96  slice = tensor.slice(indices2, sizes2);
97  for (int i = 0; i < 2; ++i) {
98  for (int j = 0; j < 2; ++j) {
99  for (int k = 0; k < 3; ++k) {
100  VERIFY_IS_EQUAL(slice(0, 0, i, j, k), tensor(1, 1, 3 + i, 4 + j, 5 + k));
101  }
102  }
103  }
104 
105  Eigen::DSizes<ptrdiff_t, 5> indices3(0, 0, 0, 0, 0);
106  Eigen::DSizes<ptrdiff_t, 5> sizes3(2, 3, 1, 1, 1);
107  slice = tensor.slice(indices3, sizes3);
108  VERIFY_IS_EQUAL(slice.data(), tensor.data());
109 }
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const TensorSlicingOp< const StartIndices, const Sizes, const Derived > slice(const StartIndices &startIndices, const Sizes &sizes) const
Definition: TensorBase.h:1117
std::vector< Array2i > sizes
Definition: dense_solvers.cpp:12

References Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::data(), Eigen::TensorRef< PlainObjectType >::data(), i, j, k, Eigen::TensorBase< Derived, AccessLevel >::setRandom(), sizes, Eigen::TensorBase< Derived, AccessLevel >::slice(), and VERIFY_IS_EQUAL.

Referenced by EIGEN_DECLARE_TEST().