12 #include <Eigen/CXX11/Tensor> 27 tensor2 = tensor1.
reshape(dim1);
29 tensor3 = tensor1.
reshape(dim2);
31 tensor4 = tensor1.
reshape(dim1).reshape(dim3);
33 for (
int i = 0; i < 2; ++i) {
34 for (
int j = 0; j < 3; ++j) {
35 for (
int k = 0; k < 7; ++k) {
36 VERIFY_IS_EQUAL(tensor1(i,j,0,k,0), tensor2(i,j,k));
37 VERIFY_IS_EQUAL(tensor1(i,j,0,k,0), tensor3(i+2*j,k));
38 VERIFY_IS_EQUAL(tensor1(i,j,0,k,0), tensor4(i,j+3*k));
46 MatrixXf m1(2,3*5*7*11);
47 MatrixXf m2(3*5*7*11,13);
50 MatrixXf m3 = m1 * m2;
52 TensorMap<Tensor<float, 5>> tensor1(m1.data(), 2,3,5,7,11);
53 TensorMap<Tensor<float, 5>> tensor2(m2.data(), 3,5,7,11,13);
57 array<DimPair, 1> contract_along{{
DimPair(1, 0)}};
59 tensor3 = tensor1.
reshape(newDims1).contract(tensor2.reshape(newDims2), contract_along);
61 Map<MatrixXf> res(tensor3.
data(), 2, 13);
62 for (
int i = 0; i < 2; ++i) {
63 for (
int j = 0; j < 13; ++j) {
64 VERIFY_IS_APPROX(res(i,j), m3(i,j));
79 float scratch[2*3*1*7*1];
80 TensorMap<Tensor<float, 5>> tensor5d(scratch, 2,3,1,7,1);
83 for (
int i = 0; i < 2; ++i) {
84 for (
int j = 0; j < 3; ++j) {
85 for (
int k = 0; k < 7; ++k) {
86 VERIFY_IS_EQUAL(tensor2d(i+2*j,k), tensor(i,j,k));
87 VERIFY_IS_EQUAL(tensor5d(i,j,0,k,0), tensor(i,j,k));
93 template<
int DataLayout>
102 slice1 = tensor.
slice(indices, sizes);
103 VERIFY_IS_EQUAL(slice1(0,0,0,0,0), tensor(1,2,3,4,5));
108 slice2 = tensor.
slice(indices2, sizes2);
109 for (
int i = 0; i < 2; ++i) {
110 for (
int j = 0; j < 2; ++j) {
111 for (
int k = 0; k < 3; ++k) {
112 VERIFY_IS_EQUAL(slice2(0,0,i,j,k), tensor(1,1,3+i,4+j,5+k));
118 template<
typename=
void>
121 const float b[1] = {42};
122 TensorMap<Tensor<const float, 1> > m(b, 1);
123 DSizes<DenseIndex, 1> offsets;
125 TensorRef<Tensor<const float, 1> > slice_ref(m.slice(offsets, m.dimensions()));
126 VERIFY_IS_EQUAL(slice_ref(0), 42);
129 template<
int DataLayout>
131 typedef Matrix<float, Dynamic, Dynamic, DataLayout> Mtx;
137 Mtx m3 = m1.block(1, 2, 3, 3) * m2.block(0, 2, 3, 1);
139 TensorMap<Tensor<float, 2, DataLayout>> tensor1(m1.data(), 7, 7);
140 TensorMap<Tensor<float, 2, DataLayout>> tensor2(m2.data(), 3, 3);
143 array<DimPair, 1> contract_along{{
DimPair(1, 0)}};
149 tensor3 = tensor1.slice(indices1, sizes1).contract(tensor2.slice(indices2, sizes2), contract_along);
151 Map<Mtx> res(tensor3.data(), 3, 1);
152 for (
int i = 0; i < 3; ++i) {
153 for (
int j = 0; j < 1; ++j) {
154 VERIFY_IS_APPROX(res(i,j), m3(i,j));
159 TensorMap<Tensor<const float, 2, DataLayout>> tensor4(m1.data(), 7, 7);
161 for (
int i = 0; i < 35; ++i) {
162 VERIFY_IS_APPROX(tensor6(i), expf(tensor4.data()[i]));
166 template<
int DataLayout>
183 result.
slice(first_slice, sizes12) = tensor1;
189 result.
slice(third_slice, sizes3) = tensor3;
193 result.
slice(fourth_slice, sizes4) = tensor4;
195 for (
int j = 0; j < 2; ++j) {
196 for (
int k = 0; k < 7; ++k) {
197 for (
int i = 0; i < 2; ++i) {
198 VERIFY_IS_EQUAL(result(i,j,k), tensor1(i,j,k));
199 VERIFY_IS_EQUAL(result(i+2,j,k), tensor2(i,j,k));
203 for (
int i = 0; i < 4; ++i) {
204 for (
int j = 2; j < 5; ++j) {
205 for (
int k = 0; k < 5; ++k) {
206 VERIFY_IS_EQUAL(result(i,j,k), tensor3(i,j-2,k));
208 for (
int k = 5; k < 7; ++k) {
209 VERIFY_IS_EQUAL(result(i,j,k), tensor4(i,j-2,k-5));
216 result.
slice(fifth_slice, sizes5) = tensor5.
slice(fifth_slice, sizes5);
217 for (
int i = 0; i < 4; ++i) {
218 for (
int j = 2; j < 5; ++j) {
219 for (
int k = 0; k < 7; ++k) {
220 VERIFY_IS_EQUAL(result(i,j,k), tensor5(i,j,k));
226 template<
int DataLayout>
234 typedef TensorEvaluator<decltype(tensor.
slice(offsets, extents)), DefaultDevice> SliceEvaluator;
235 auto slice1 = SliceEvaluator(tensor.
slice(offsets, extents), DefaultDevice());
236 VERIFY_IS_EQUAL(slice1.dimensions().TotalSize(), 1);
237 VERIFY_IS_EQUAL(slice1.data()[0], tensor(1,2,3,4));
241 auto slice2 = SliceEvaluator(tensor.
slice(offsets, extents), DefaultDevice());
242 VERIFY_IS_EQUAL(slice2.dimensions().
TotalSize(), 2);
243 VERIFY_IS_EQUAL(slice2.data()[0], tensor(1,2,3,4));
244 VERIFY_IS_EQUAL(slice2.data()[1], tensor(2,2,3,4));
247 auto slice2 = SliceEvaluator(tensor.
slice(offsets, extents), DefaultDevice());
248 VERIFY_IS_EQUAL(slice2.dimensions().
TotalSize(), 2);
249 VERIFY_IS_EQUAL(slice2.data()[0], tensor(1,2,3,4));
250 VERIFY_IS_EQUAL(slice2.data()[1], tensor(1,2,3,5));
254 auto slice3 = SliceEvaluator(tensor.
slice(offsets, extents), DefaultDevice());
255 VERIFY_IS_EQUAL(slice3.dimensions().
TotalSize(), 2);
256 VERIFY_IS_EQUAL(slice3.data(),
static_cast<float*
>(0));
261 auto slice4 = SliceEvaluator(tensor.
slice(offsets, extents), DefaultDevice());
262 VERIFY_IS_EQUAL(slice4.dimensions().
TotalSize(), 6);
263 for (
int i = 0; i < 3; ++i) {
264 for (
int j = 0; j < 2; ++j) {
265 VERIFY_IS_EQUAL(slice4.data()[i+3*j], tensor(i,2+j,3,4));
271 auto slice4 = SliceEvaluator(tensor.
slice(offsets, extents), DefaultDevice());
272 VERIFY_IS_EQUAL(slice4.dimensions().
TotalSize(), 22);
273 for (
int l = 0; l < 11; ++l) {
274 for (
int k = 0; k < 2; ++k) {
275 VERIFY_IS_EQUAL(slice4.data()[l+11*k], tensor(1,2,3+k,l));
283 auto slice5 = SliceEvaluator(tensor.
slice(offsets, extents), DefaultDevice());
284 VERIFY_IS_EQUAL(slice5.dimensions().
TotalSize(), 210);
285 for (
int i = 0; i < 3; ++i) {
286 for (
int j = 0; j < 5; ++j) {
287 for (
int k = 0; k < 7; ++k) {
288 for (
int l = 0; l < 2; ++l) {
289 int slice_index = i + 3 * (j + 5 * (k + 7 * l));
290 VERIFY_IS_EQUAL(slice5.data()[slice_index], tensor(i,j,k,l+4));
298 auto slice5 = SliceEvaluator(tensor.
slice(offsets, extents), DefaultDevice());
299 VERIFY_IS_EQUAL(slice5.dimensions().
TotalSize(), 770);
300 for (
int l = 0; l < 11; ++l) {
301 for (
int k = 0; k < 7; ++k) {
302 for (
int j = 0; j < 5; ++j) {
303 for (
int i = 0; i < 2; ++i) {
304 int slice_index = l + 11 * (k + 7 * (j + 5 * i));
305 VERIFY_IS_EQUAL(slice5.data()[slice_index], tensor(i+1,j,k,l));
315 auto slice6 = SliceEvaluator(tensor.
slice(offsets, extents), DefaultDevice());
316 VERIFY_IS_EQUAL(slice6.dimensions().
TotalSize(), 3*5*7*11);
317 VERIFY_IS_EQUAL(slice6.data(), tensor.
data());
321 template<
int DataLayout>
335 Index2 strides(-2,-1);
336 Index2 indicesStart(5,7);
337 Index2 indicesStop(0,4);
338 slice = tensor2.
stridedSlice(indicesStart, indicesStop, strides);
339 for (
int j = 0; j < 2; ++j) {
340 for (
int k = 0; k < 3; ++k) {
341 VERIFY_IS_EQUAL(slice(j,k), tensor2(5-2*j,7-k));
349 Index2 indicesStart(5,4);
350 Index2 indicesStop(5,5);
351 slice = tensor2.
stridedSlice(indicesStart, indicesStop, strides);
355 Tensor2f slice(7,11);
356 Index2 strides(1,-1);
357 Index2 indicesStart(-3,20);
358 Index2 indicesStop(20,-11);
359 slice = tensor2.
stridedSlice(indicesStart, indicesStop, strides);
360 for (
int j = 0; j < 7; ++j) {
361 for (
int k = 0; k < 11; ++k) {
362 VERIFY_IS_EQUAL(slice(j,k), tensor2(j,10-k));
368 Tensor5f slice1(1,1,1,1,1);
372 slice1 = tensor.
stridedSlice(indicesStart, indicesStop, strides);
373 VERIFY_IS_EQUAL(slice1(0,0,0,0,0), tensor(1,2,3,4,5));
377 Tensor5f slice(1,1,2,2,3);
378 Index5 start(1, 1, 3, 4, 5);
379 Index5 stop(2, 2, 5, 6, 8);
380 Index5 strides(1, 1, 1, 1, 1);
382 for (
int i = 0; i < 2; ++i) {
383 for (
int j = 0; j < 2; ++j) {
384 for (
int k = 0; k < 3; ++k) {
385 VERIFY_IS_EQUAL(slice(0,0,i,j,k), tensor(1,1,3+i,4+j,5+k));
392 Tensor5f slice(1,1,2,2,3);
393 Index5 strides3(1, 1, -2, 1, -1);
394 Index5 indices3Start(1, 1, 4, 4, 7);
395 Index5 indices3Stop(2, 2, 0, 6, 4);
396 slice = tensor.
stridedSlice(indices3Start, indices3Stop, strides3);
397 for (
int i = 0; i < 2; ++i) {
398 for (
int j = 0; j < 2; ++j) {
399 for (
int k = 0; k < 3; ++k) {
400 VERIFY_IS_EQUAL(slice(0,0,i,j,k), tensor(1,1,4-2*i,4+j,7-k));
407 Tensor5f slice(1,1,2,2,3);
408 Index5 strides3(1, 1, 2, 1, 1);
409 Index5 indices3Start(1, 1, 4, 4, 7);
410 Index5 indices3Stop(2, 2, 0, 6, 4);
411 slice = tensor.
stridedSlice(indices3Start, indices3Stop, strides3);
415 template<
int DataLayout>
429 Index2 indicesStart(3,4);
430 Index2 indicesStop(5,7);
433 tensor.slice(indicesStart,lengths)=slice;
434 tensor2.
stridedSlice(indicesStart,indicesStop,strides)=slice;
436 for(
int i=0;i<7;i++)
for(
int j=0;j<11;j++){
437 VERIFY_IS_EQUAL(tensor(i,j), tensor2(i,j));
442 template<
int DataLayout>
448 const DSizes<ptrdiff_t, 3> newDims(1, 1, 11);
450 matrix.
slice(DSizes<ptrdiff_t, 2>(2, 0), DSizes<ptrdiff_t, 2>(1, 11)).reshape(newDims);
452 VERIFY_IS_EQUAL(tensor.
dimensions().TotalSize(), 11);
455 VERIFY_IS_EQUAL(tensor.
dimension(2), 11);
456 for (
int i = 0; i < 11; ++i) {
457 VERIFY_IS_EQUAL(tensor(0,0,i), matrix(2,i));
464 CALL_SUBTEST_1(test_simple_reshape<void>());
465 CALL_SUBTEST_1(test_reshape_in_expr<void>());
466 CALL_SUBTEST_1(test_reshape_as_lvalue<void>());
468 CALL_SUBTEST_1(test_simple_slice<ColMajor>());
469 CALL_SUBTEST_1(test_simple_slice<RowMajor>());
471 CALL_SUBTEST_2(test_slice_in_expr<ColMajor>());
472 CALL_SUBTEST_3(test_slice_in_expr<RowMajor>());
473 CALL_SUBTEST_4(test_slice_as_lvalue<ColMajor>());
474 CALL_SUBTEST_4(test_slice_as_lvalue<RowMajor>());
475 CALL_SUBTEST_5(test_slice_raw_data<ColMajor>());
476 CALL_SUBTEST_5(test_slice_raw_data<RowMajor>());
478 CALL_SUBTEST_6(test_strided_slice_write<ColMajor>());
479 CALL_SUBTEST_6(test_strided_slice<ColMajor>());
480 CALL_SUBTEST_6(test_strided_slice_write<RowMajor>());
481 CALL_SUBTEST_6(test_strided_slice<RowMajor>());
483 CALL_SUBTEST_7(test_composition<ColMajor>());
484 CALL_SUBTEST_7(test_composition<RowMajor>());
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const TensorSlicingOp< const StartIndices, const Sizes, const Tensor< Scalar_, NumIndices_, Options_, IndexType_ > > slice(const StartIndices &startIndices, const Sizes &sizes) const
static void test_simple_reshape()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(std::size_t n) const
static void test_strided_slice_write()
static void test_slice_as_lvalue()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const TensorStridingSlicingOp< const StartIndices, const StopIndices, const Strides, const Tensor< Scalar_, NumIndices_, Options_, IndexType_ > > stridedSlice(const StartIndices &startIndices, const StopIndices &stopIndices, const Strides &strides) const
static void test_reshape_as_lvalue()
static void test_const_slice()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor< Scalar_, NumIndices_, Options_, IndexType_ > & setRandom()
static void test_strided_slice()
static void test_slice_raw_data()
static void test_reshape_in_expr()
Tensor< float, 1 >::DimensionPair DimPair
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const TensorReshapingOp< const NewDimensions, const Tensor< Scalar_, NumIndices_, Options_, IndexType_ > > reshape(const NewDimensions &newDimensions) const
void test_cxx11_tensor_morphing()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DenseIndex TotalSize() const
static void test_simple_slice()
static void test_composition()
const mpreal dim(const mpreal &a, const mpreal &b, mp_rnd_t r=mpreal::get_default_rnd())
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar * data()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
static void test_slice_in_expr()
EIGEN_DEVICE_FUNC const Scalar & b