// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2014 Benoit Steiner // // This Source Code Form is subject to the terms of the Mozilla // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. #include "main.h" #include using Eigen::Tensor; template static void test_simple_reshape() { Tensor tensor1(2,3,1,7,1); tensor1.setRandom(); Tensor tensor2(2,3,7); Tensor tensor3(6,7); Tensor tensor4(2,21); Tensor::Dimensions dim1(2,3,7); tensor2 = tensor1.reshape(dim1); Tensor::Dimensions dim2(6,7); tensor3 = tensor1.reshape(dim2); Tensor::Dimensions dim3(2,21); tensor4 = tensor1.reshape(dim1).reshape(dim3); for (int i = 0; i < 2; ++i) { for (int j = 0; j < 3; ++j) { for (int k = 0; k < 7; ++k) { VERIFY_IS_EQUAL(tensor1(i,j,0,k,0), tensor2(i,j,k)); VERIFY_IS_EQUAL(tensor1(i,j,0,k,0), tensor3(i+2*j,k)); VERIFY_IS_EQUAL(tensor1(i,j,0,k,0), tensor4(i,j+3*k)); } } } } template static void test_static_reshape() { #if defined(EIGEN_HAS_INDEX_LIST) using Eigen::type2index; Tensor tensor(2, 3, 1, 7, 1); tensor.setRandom(); // New dimensions: [2, 3, 7] Eigen::IndexList, type2index<3>, type2index<7>> dim; Tensor reshaped = tensor.reshape(static_cast>(dim)); for (int i = 0; i < 2; ++i) { for (int j = 0; j < 3; ++j) { for (int k = 0; k < 7; ++k) { VERIFY_IS_EQUAL(tensor(i, j, 0, k, 0), reshaped(i, j, k)); } } } #endif } template static void test_reshape_in_expr() { MatrixXf m1(2,3*5*7*11); MatrixXf m2(3*5*7*11,13); m1.setRandom(); m2.setRandom(); MatrixXf m3 = m1 * m2; TensorMap> tensor1(m1.data(), 2,3,5,7,11); TensorMap> tensor2(m2.data(), 3,5,7,11,13); Tensor::Dimensions newDims1(2,3*5*7*11); Tensor::Dimensions newDims2(3*5*7*11,13); typedef Tensor::DimensionPair DimPair; array contract_along{{DimPair(1, 0)}}; Tensor tensor3(2,13); tensor3 = tensor1.reshape(newDims1).contract(tensor2.reshape(newDims2), contract_along); Map res(tensor3.data(), 2, 13); for (int i = 0; i < 2; ++i) { for (int j = 0; j < 13; ++j) { VERIFY_IS_APPROX(res(i,j), m3(i,j)); } } } template static void test_reshape_as_lvalue() { Tensor tensor(2,3,7); tensor.setRandom(); Tensor tensor2d(6,7); Tensor::Dimensions dim(2,3,7); tensor2d.reshape(dim) = tensor; float scratch[2*3*1*7*1]; TensorMap> tensor5d(scratch, 2,3,1,7,1); tensor5d.reshape(dim).device(Eigen::DefaultDevice()) = tensor; for (int i = 0; i < 2; ++i) { for (int j = 0; j < 3; ++j) { for (int k = 0; k < 7; ++k) { VERIFY_IS_EQUAL(tensor2d(i+2*j,k), tensor(i,j,k)); VERIFY_IS_EQUAL(tensor5d(i,j,0,k,0), tensor(i,j,k)); } } } } template static void test_simple_slice() { Tensor tensor(2,3,5,7,11); tensor.setRandom(); Tensor slice1(1,1,1,1,1); Eigen::DSizes indices(1,2,3,4,5); Eigen::DSizes sizes(1,1,1,1,1); slice1 = tensor.slice(indices, sizes); VERIFY_IS_EQUAL(slice1(0,0,0,0,0), tensor(1,2,3,4,5)); Tensor slice2(1,1,2,2,3); Eigen::DSizes indices2(1,1,3,4,5); Eigen::DSizes sizes2(1,1,2,2,3); slice2 = tensor.slice(indices2, sizes2); for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { for (int k = 0; k < 3; ++k) { VERIFY_IS_EQUAL(slice2(0,0,i,j,k), tensor(1,1,3+i,4+j,5+k)); } } } } template static void test_const_slice() { const T b[1] = {42}; TensorMap > m(b, 1); DSizes offsets; offsets[0] = 0; TensorRef > slice_ref(m.slice(offsets, m.dimensions())); VERIFY_IS_EQUAL(slice_ref(0), 42); } template static void test_slice_in_expr() { typedef Matrix Mtx; Mtx m1(7,7); Mtx m2(3,3); m1.setRandom(); m2.setRandom(); Mtx m3 = m1.block(1, 2, 3, 3) * m2.block(0, 2, 3, 1); TensorMap> tensor1(m1.data(), 7, 7); TensorMap> tensor2(m2.data(), 3, 3); Tensor tensor3(3,1); typedef typename Tensor::DimensionPair DimPair; array contract_along{{DimPair(1, 0)}}; Eigen::DSizes indices1(1,2); Eigen::DSizes sizes1(3,3); Eigen::DSizes indices2(0,2); Eigen::DSizes sizes2(3,1); tensor3 = tensor1.slice(indices1, sizes1).contract(tensor2.slice(indices2, sizes2), contract_along); Map res(tensor3.data(), 3, 1); for (int i = 0; i < 3; ++i) { for (int j = 0; j < 1; ++j) { VERIFY_IS_APPROX(res(i,j), m3(i,j)); } } // Take an arbitrary slice of an arbitrarily sized tensor. TensorMap> tensor4(m1.data(), 7, 7); Tensor tensor6 = tensor4.reshape(DSizes(7*7)).exp().slice(DSizes(0), DSizes(35)); for (int i = 0; i < 35; ++i) { VERIFY_IS_APPROX(tensor6(i), expf(tensor4.data()[i])); } } template static void test_slice_as_lvalue() { Tensor tensor1(2,2,7); tensor1.setRandom(); Tensor tensor2(2,2,7); tensor2.setRandom(); Tensor tensor3(4,3,5); tensor3.setRandom(); Tensor tensor4(4,3,2); tensor4.setRandom(); Tensor tensor5(10,13,12); tensor5.setRandom(); Tensor result(4,5,7); Eigen::DSizes sizes12(2,2,7); Eigen::DSizes first_slice(0,0,0); result.slice(first_slice, sizes12) = tensor1; Eigen::DSizes second_slice(2,0,0); result.slice(second_slice, sizes12).device(Eigen::DefaultDevice()) = tensor2; Eigen::DSizes sizes3(4,3,5); Eigen::DSizes third_slice(0,2,0); result.slice(third_slice, sizes3) = tensor3; Eigen::DSizes sizes4(4,3,2); Eigen::DSizes fourth_slice(0,2,5); result.slice(fourth_slice, sizes4) = tensor4; for (int j = 0; j < 2; ++j) { for (int k = 0; k < 7; ++k) { for (int i = 0; i < 2; ++i) { VERIFY_IS_EQUAL(result(i,j,k), tensor1(i,j,k)); VERIFY_IS_EQUAL(result(i+2,j,k), tensor2(i,j,k)); } } } for (int i = 0; i < 4; ++i) { for (int j = 2; j < 5; ++j) { for (int k = 0; k < 5; ++k) { VERIFY_IS_EQUAL(result(i,j,k), tensor3(i,j-2,k)); } for (int k = 5; k < 7; ++k) { VERIFY_IS_EQUAL(result(i,j,k), tensor4(i,j-2,k-5)); } } } Eigen::DSizes sizes5(4,5,7); Eigen::DSizes fifth_slice(0,0,0); result.slice(fifth_slice, sizes5) = tensor5.slice(fifth_slice, sizes5); for (int i = 0; i < 4; ++i) { for (int j = 2; j < 5; ++j) { for (int k = 0; k < 7; ++k) { VERIFY_IS_EQUAL(result(i,j,k), tensor5(i,j,k)); } } } } template static void test_slice_raw_data() { Tensor tensor(3,5,7,11); tensor.setRandom(); Eigen::DSizes offsets(1,2,3,4); Eigen::DSizes extents(1,1,1,1); typedef TensorEvaluator SliceEvaluator; auto slice1 = SliceEvaluator(tensor.slice(offsets, extents), DefaultDevice()); VERIFY_IS_EQUAL(slice1.dimensions().TotalSize(), 1); VERIFY_IS_EQUAL(slice1.data()[0], tensor(1,2,3,4)); if (DataLayout == ColMajor) { extents = Eigen::DSizes(2,1,1,1); auto slice2 = SliceEvaluator(tensor.slice(offsets, extents), DefaultDevice()); VERIFY_IS_EQUAL(slice2.dimensions().TotalSize(), 2); VERIFY_IS_EQUAL(slice2.data()[0], tensor(1,2,3,4)); VERIFY_IS_EQUAL(slice2.data()[1], tensor(2,2,3,4)); } else { extents = Eigen::DSizes(1,1,1,2); auto slice2 = SliceEvaluator(tensor.slice(offsets, extents), DefaultDevice()); VERIFY_IS_EQUAL(slice2.dimensions().TotalSize(), 2); VERIFY_IS_EQUAL(slice2.data()[0], tensor(1,2,3,4)); VERIFY_IS_EQUAL(slice2.data()[1], tensor(1,2,3,5)); } extents = Eigen::DSizes(1,2,1,1); auto slice3 = SliceEvaluator(tensor.slice(offsets, extents), DefaultDevice()); VERIFY_IS_EQUAL(slice3.dimensions().TotalSize(), 2); VERIFY_IS_EQUAL(slice3.data(), static_cast(0)); if (DataLayout == ColMajor) { offsets = Eigen::DSizes(0,2,3,4); extents = Eigen::DSizes(3,2,1,1); auto slice4 = SliceEvaluator(tensor.slice(offsets, extents), DefaultDevice()); VERIFY_IS_EQUAL(slice4.dimensions().TotalSize(), 6); for (int i = 0; i < 3; ++i) { for (int j = 0; j < 2; ++j) { VERIFY_IS_EQUAL(slice4.data()[i+3*j], tensor(i,2+j,3,4)); } } } else { offsets = Eigen::DSizes(1,2,3,0); extents = Eigen::DSizes(1,1,2,11); auto slice4 = SliceEvaluator(tensor.slice(offsets, extents), DefaultDevice()); VERIFY_IS_EQUAL(slice4.dimensions().TotalSize(), 22); for (int l = 0; l < 11; ++l) { for (int k = 0; k < 2; ++k) { VERIFY_IS_EQUAL(slice4.data()[l+11*k], tensor(1,2,3+k,l)); } } } if (DataLayout == ColMajor) { offsets = Eigen::DSizes(0,0,0,4); extents = Eigen::DSizes(3,5,7,2); auto slice5 = SliceEvaluator(tensor.slice(offsets, extents), DefaultDevice()); VERIFY_IS_EQUAL(slice5.dimensions().TotalSize(), 210); for (int i = 0; i < 3; ++i) { for (int j = 0; j < 5; ++j) { for (int k = 0; k < 7; ++k) { for (int l = 0; l < 2; ++l) { int slice_index = i + 3 * (j + 5 * (k + 7 * l)); VERIFY_IS_EQUAL(slice5.data()[slice_index], tensor(i,j,k,l+4)); } } } } } else { offsets = Eigen::DSizes(1,0,0,0); extents = Eigen::DSizes(2,5,7,11); auto slice5 = SliceEvaluator(tensor.slice(offsets, extents), DefaultDevice()); VERIFY_IS_EQUAL(slice5.dimensions().TotalSize(), 770); for (int l = 0; l < 11; ++l) { for (int k = 0; k < 7; ++k) { for (int j = 0; j < 5; ++j) { for (int i = 0; i < 2; ++i) { int slice_index = l + 11 * (k + 7 * (j + 5 * i)); VERIFY_IS_EQUAL(slice5.data()[slice_index], tensor(i+1,j,k,l)); } } } } } offsets = Eigen::DSizes(0,0,0,0); extents = Eigen::DSizes(3,5,7,11); auto slice6 = SliceEvaluator(tensor.slice(offsets, extents), DefaultDevice()); VERIFY_IS_EQUAL(slice6.dimensions().TotalSize(), 3*5*7*11); VERIFY_IS_EQUAL(slice6.data(), tensor.data()); } template static void test_strided_slice() { typedef Tensor Tensor5f; typedef Eigen::DSizes Index5; typedef Tensor Tensor2f; typedef Eigen::DSizes Index2; Tensor tensor(2,3,5,7,11); Tensor tensor2(7,11); tensor.setRandom(); tensor2.setRandom(); if (true) { Tensor2f slice(2,3); Index2 strides(-2,-1); Index2 indicesStart(5,7); Index2 indicesStop(0,4); slice = tensor2.stridedSlice(indicesStart, indicesStop, strides); for (int j = 0; j < 2; ++j) { for (int k = 0; k < 3; ++k) { VERIFY_IS_EQUAL(slice(j,k), tensor2(5-2*j,7-k)); } } } if(true) { Tensor2f slice(0,1); Index2 strides(1,1); Index2 indicesStart(5,4); Index2 indicesStop(5,5); slice = tensor2.stridedSlice(indicesStart, indicesStop, strides); } if(true) { // test clamped degenerate interavls Tensor2f slice(7,11); Index2 strides(1,-1); Index2 indicesStart(-3,20); // should become 0,10 Index2 indicesStop(20,-11); // should become 11, -1 slice = tensor2.stridedSlice(indicesStart, indicesStop, strides); for (int j = 0; j < 7; ++j) { for (int k = 0; k < 11; ++k) { VERIFY_IS_EQUAL(slice(j,k), tensor2(j,10-k)); } } } if(true) { Tensor5f slice1(1,1,1,1,1); Eigen::DSizes indicesStart(1, 2, 3, 4, 5); Eigen::DSizes indicesStop(2, 3, 4, 5, 6); Eigen::DSizes strides(1, 1, 1, 1, 1); slice1 = tensor.stridedSlice(indicesStart, indicesStop, strides); VERIFY_IS_EQUAL(slice1(0,0,0,0,0), tensor(1,2,3,4,5)); } if(true) { Tensor5f slice(1,1,2,2,3); Index5 start(1, 1, 3, 4, 5); Index5 stop(2, 2, 5, 6, 8); Index5 strides(1, 1, 1, 1, 1); slice = tensor.stridedSlice(start, stop, strides); for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { for (int k = 0; k < 3; ++k) { VERIFY_IS_EQUAL(slice(0,0,i,j,k), tensor(1,1,3+i,4+j,5+k)); } } } } if(true) { Tensor5f slice(1,1,2,2,3); Index5 strides3(1, 1, -2, 1, -1); Index5 indices3Start(1, 1, 4, 4, 7); Index5 indices3Stop(2, 2, 0, 6, 4); slice = tensor.stridedSlice(indices3Start, indices3Stop, strides3); for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { for (int k = 0; k < 3; ++k) { VERIFY_IS_EQUAL(slice(0,0,i,j,k), tensor(1,1,4-2*i,4+j,7-k)); } } } } if(false) { // tests degenerate interval Tensor5f slice(1,1,2,2,3); Index5 strides3(1, 1, 2, 1, 1); Index5 indices3Start(1, 1, 4, 4, 7); Index5 indices3Stop(2, 2, 0, 6, 4); slice = tensor.stridedSlice(indices3Start, indices3Stop, strides3); } } template static void test_strided_slice_write() { typedef Tensor Tensor2f; typedef Eigen::DSizes Index2; Tensor tensor(7,11),tensor2(7,11); tensor.setRandom(); tensor2=tensor; Tensor2f slice(2,3); slice.setRandom(); Index2 strides(1,1); Index2 indicesStart(3,4); Index2 indicesStop(5,7); Index2 lengths(2,3); tensor.slice(indicesStart,lengths)=slice; tensor2.stridedSlice(indicesStart,indicesStop,strides)=slice; for(int i=0;i<7;i++) for(int j=0;j<11;j++){ VERIFY_IS_EQUAL(tensor(i,j), tensor2(i,j)); } } template static void test_composition() { Eigen::Tensor matrix(7, 11); matrix.setRandom(); const DSizes newDims(1, 1, 11); Eigen::Tensor tensor = matrix.slice(DSizes(2, 0), DSizes(1, 11)).reshape(newDims); VERIFY_IS_EQUAL(tensor.dimensions().TotalSize(), 11); VERIFY_IS_EQUAL(tensor.dimension(0), 1); VERIFY_IS_EQUAL(tensor.dimension(1), 1); VERIFY_IS_EQUAL(tensor.dimension(2), 11); for (int i = 0; i < 11; ++i) { VERIFY_IS_EQUAL(tensor(0,0,i), matrix(2,i)); } } template static void test_empty_slice() { Tensor tensor(2,3,5); tensor.setRandom(); Tensor copy = tensor; // empty size in first dimension Eigen::DSizes indices1(1,2,3); Eigen::DSizes sizes1(0,1,2); Tensor slice1(0,1,2); slice1.setRandom(); tensor.slice(indices1, sizes1) = slice1; // empty size in second dimension Eigen::DSizes indices2(1,2,3); Eigen::DSizes sizes2(1,0,2); Tensor slice2(1,0,2); slice2.setRandom(); tensor.slice(indices2, sizes2) = slice2; // empty size in third dimension Eigen::DSizes indices3(1,2,3); Eigen::DSizes sizes3(1,1,0); Tensor slice3(1,1,0); slice3.setRandom(); tensor.slice(indices3, sizes3) = slice3; // empty size in first and second dimension Eigen::DSizes indices4(1,2,3); Eigen::DSizes sizes4(0,0,2); Tensor slice4(0,0,2); slice4.setRandom(); tensor.slice(indices4, sizes4) = slice4; // empty size in second and third dimension Eigen::DSizes indices5(1,2,3); Eigen::DSizes sizes5(1,0,0); Tensor slice5(1,0,0); slice5.setRandom(); tensor.slice(indices5, sizes5) = slice5; // empty size in all dimensions Eigen::DSizes indices6(1,2,3); Eigen::DSizes sizes6(0,0,0); Tensor slice6(0,0,0); slice6.setRandom(); tensor.slice(indices6, sizes6) = slice6; // none of these operations should change the tensor's components // because all of the rvalue slices have at least one zero dimension for (int i = 0; i < 2; ++i) { for (int j = 0; j < 3; ++j) { for (int k = 0; k < 5; ++k) { VERIFY_IS_EQUAL(tensor(i,j,k), copy(i,j,k)); } } } } #define CALL_SUBTEST_PART(PART) \ CALL_SUBTEST_##PART #define CALL_SUBTESTS_TYPES_LAYOUTS(PART, NAME) \ CALL_SUBTEST_PART(PART)((NAME())); \ CALL_SUBTEST_PART(PART)((NAME())); \ CALL_SUBTEST_PART(PART)((NAME())); \ CALL_SUBTEST_PART(PART)((NAME())) EIGEN_DECLARE_TEST(cxx11_tensor_morphing) { CALL_SUBTEST_1(test_simple_reshape()); CALL_SUBTEST_1(test_static_reshape()); CALL_SUBTEST_1(test_reshape_as_lvalue()); CALL_SUBTEST_1(test_reshape_in_expr()); CALL_SUBTEST_1(test_const_slice()); CALL_SUBTESTS_TYPES_LAYOUTS(2, test_simple_slice); CALL_SUBTESTS_TYPES_LAYOUTS(3, test_slice_as_lvalue); CALL_SUBTESTS_TYPES_LAYOUTS(4, test_slice_raw_data); CALL_SUBTESTS_TYPES_LAYOUTS(5, test_strided_slice_write); CALL_SUBTESTS_TYPES_LAYOUTS(6, test_strided_slice); CALL_SUBTESTS_TYPES_LAYOUTS(7, test_composition); }