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

Functions

template<int DataLayout>
static void test_simple_roll ()
 
template<int DataLayout>
static void test_expr_roll (bool LValue)
 
 EIGEN_DECLARE_TEST (cxx11_tensor_roll)
 

Function Documentation

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( cxx11_tensor_roll  )
149  {
150  CALL_SUBTEST(test_simple_roll<ColMajor>());
151  CALL_SUBTEST(test_simple_roll<RowMajor>());
152  CALL_SUBTEST(test_expr_roll<ColMajor>(true));
153  CALL_SUBTEST(test_expr_roll<RowMajor>(true));
154  CALL_SUBTEST(test_expr_roll<ColMajor>(false));
155  CALL_SUBTEST(test_expr_roll<RowMajor>(false));
156 }
#define CALL_SUBTEST(FUNC)
Definition: main.h:382

References CALL_SUBTEST.

◆ test_expr_roll()

template<int DataLayout>
static void test_expr_roll ( bool  LValue)
static
70  {
71  Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7);
72  tensor.setRandom();
73 
74  array<bool, 4> dim_roll;
75  dim_roll[0] = 2;
76  dim_roll[1] = 1;
77  dim_roll[2] = 0;
78  dim_roll[3] = 3;
79 
80  Tensor<float, 4, DataLayout> expected(tensor.dimensions());
81  if (LValue) {
82  expected.roll(dim_roll) = tensor;
83  } else {
84  expected = tensor.roll(dim_roll);
85  }
86 
87  Tensor<float, 4, DataLayout> result(tensor.dimensions());
88 
89  array<ptrdiff_t, 4> src_slice_dim;
90  src_slice_dim[0] = tensor.dimension(0);
91  src_slice_dim[1] = tensor.dimension(1);
92  src_slice_dim[2] = 1;
93  src_slice_dim[3] = tensor.dimension(3);
94  array<ptrdiff_t, 4> src_slice_start;
95  src_slice_start[0] = 0;
96  src_slice_start[1] = 0;
97  src_slice_start[2] = 0;
98  src_slice_start[3] = 0;
99  array<ptrdiff_t, 4> dst_slice_dim = src_slice_dim;
100  array<ptrdiff_t, 4> dst_slice_start = src_slice_start;
101 
102  for (int i = 0; i < tensor.dimension(2); ++i) {
103  if (LValue) {
104  result.slice(dst_slice_start, dst_slice_dim).roll(dim_roll) = tensor.slice(src_slice_start, src_slice_dim);
105  } else {
106  result.slice(dst_slice_start, dst_slice_dim) = tensor.slice(src_slice_start, src_slice_dim).roll(dim_roll);
107  }
108  src_slice_start[2] += 1;
109  dst_slice_start[2] += 1;
110  }
111 
112  VERIFY_IS_EQUAL(result.dimension(0), tensor.dimension(0));
113  VERIFY_IS_EQUAL(result.dimension(1), tensor.dimension(1));
114  VERIFY_IS_EQUAL(result.dimension(2), tensor.dimension(2));
115  VERIFY_IS_EQUAL(result.dimension(3), tensor.dimension(3));
116 
117  for (int i = 0; i < expected.dimension(0); ++i) {
118  for (int j = 0; j < expected.dimension(1); ++j) {
119  for (int k = 0; k < expected.dimension(2); ++k) {
120  for (int l = 0; l < expected.dimension(3); ++l) {
121  VERIFY_IS_EQUAL(result(i, j, k, l), expected(i, j, k, l));
122  }
123  }
124  }
125  }
126 
127  dst_slice_start[2] = 0;
128  result.setRandom();
129  for (int i = 0; i < tensor.dimension(2); ++i) {
130  if (LValue) {
131  result.slice(dst_slice_start, dst_slice_dim).roll(dim_roll) = tensor.slice(dst_slice_start, dst_slice_dim);
132  } else {
133  result.slice(dst_slice_start, dst_slice_dim) = tensor.roll(dim_roll).slice(dst_slice_start, dst_slice_dim);
134  }
135  dst_slice_start[2] += 1;
136  }
137 
138  for (int i = 0; i < expected.dimension(0); ++i) {
139  for (int j = 0; j < expected.dimension(1); ++j) {
140  for (int k = 0; k < expected.dimension(2); ++k) {
141  for (int l = 0; l < expected.dimension(3); ++l) {
142  VERIFY_IS_EQUAL(result(i, j, k, l), expected(i, j, k, l));
143  }
144  }
145  }
146  }
147 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
The tensor class.
Definition: Tensor.h:68
char char char int int * k
Definition: level2_impl.h:374
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:367
std::array< T, N > array
Definition: EmulateArray.h:231
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

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

◆ test_simple_roll()

template<int DataLayout>
static void test_simple_roll ( )
static
18  {
19  Tensor<float, 4, DataLayout> tensor(2, 3, 5, 7);
20  tensor.setRandom();
21 
22  array<Index, 4> dim_roll;
23  dim_roll[0] = 0;
24  dim_roll[1] = 1;
25  dim_roll[2] = 4;
26  dim_roll[3] = 8;
27 
28  Tensor<float, 4, DataLayout> rolled_tensor;
29  rolled_tensor = tensor.roll(dim_roll);
30 
31  VERIFY_IS_EQUAL(rolled_tensor.dimension(0), 2);
32  VERIFY_IS_EQUAL(rolled_tensor.dimension(1), 3);
33  VERIFY_IS_EQUAL(rolled_tensor.dimension(2), 5);
34  VERIFY_IS_EQUAL(rolled_tensor.dimension(3), 7);
35 
36  for (int i = 0; i < 2; ++i) {
37  for (int j = 0; j < 3; ++j) {
38  for (int k = 0; k < 5; ++k) {
39  for (int l = 0; l < 7; ++l) {
40  VERIFY_IS_EQUAL(tensor(i, (j + 1) % 3, (k + 4) % 5, (l + 8) % 7), rolled_tensor(i, j, k, l));
41  }
42  }
43  }
44  }
45 
46  dim_roll[0] = -3;
47  dim_roll[1] = -2;
48  dim_roll[2] = -1;
49  dim_roll[3] = 0;
50 
51  rolled_tensor = tensor.roll(dim_roll);
52 
53  VERIFY_IS_EQUAL(rolled_tensor.dimension(0), 2);
54  VERIFY_IS_EQUAL(rolled_tensor.dimension(1), 3);
55  VERIFY_IS_EQUAL(rolled_tensor.dimension(2), 5);
56  VERIFY_IS_EQUAL(rolled_tensor.dimension(3), 7);
57 
58  for (int i = 0; i < 2; ++i) {
59  for (int j = 0; j < 3; ++j) {
60  for (int k = 0; k < 5; ++k) {
61  for (int l = 0; l < 7; ++l) {
62  VERIFY_IS_EQUAL(tensor((i + 1) % 2, (j + 1) % 3, (k + 4) % 5, l), rolled_tensor(i, j, k, l));
63  }
64  }
65  }
66  }
67 }
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const TensorRollOp< const Rolls, const Derived > roll(const Rolls &roll) const
Definition: TensorBase.h:1176
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(std::size_t n) const
Definition: Tensor.h:99

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