cxx11_tensor_layout_swap.cpp (1639B)
1 // This file is part of Eigen, a lightweight C++ template library 2 // for linear algebra. 3 // 4 // Copyright (C) 2014 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 12 #include <Eigen/CXX11/Tensor> 13 14 using Eigen::Tensor; 15 16 static void test_simple_swap() 17 { 18 Tensor<float, 3, ColMajor> tensor(2,3,7); 19 tensor.setRandom(); 20 21 Tensor<float, 3, RowMajor> tensor2 = tensor.swap_layout(); 22 VERIFY_IS_EQUAL(tensor.dimension(0), tensor2.dimension(2)); 23 VERIFY_IS_EQUAL(tensor.dimension(1), tensor2.dimension(1)); 24 VERIFY_IS_EQUAL(tensor.dimension(2), tensor2.dimension(0)); 25 26 for (int i = 0; i < 2; ++i) { 27 for (int j = 0; j < 3; ++j) { 28 for (int k = 0; k < 7; ++k) { 29 VERIFY_IS_EQUAL(tensor(i,j,k), tensor2(k,j,i)); 30 } 31 } 32 } 33 } 34 35 36 static void test_swap_as_lvalue() 37 { 38 Tensor<float, 3, ColMajor> tensor(2,3,7); 39 tensor.setRandom(); 40 41 Tensor<float, 3, RowMajor> tensor2(7,3,2); 42 tensor2.swap_layout() = tensor; 43 VERIFY_IS_EQUAL(tensor.dimension(0), tensor2.dimension(2)); 44 VERIFY_IS_EQUAL(tensor.dimension(1), tensor2.dimension(1)); 45 VERIFY_IS_EQUAL(tensor.dimension(2), tensor2.dimension(0)); 46 47 for (int i = 0; i < 2; ++i) { 48 for (int j = 0; j < 3; ++j) { 49 for (int k = 0; k < 7; ++k) { 50 VERIFY_IS_EQUAL(tensor(i,j,k), tensor2(k,j,i)); 51 } 52 } 53 } 54 } 55 56 57 EIGEN_DECLARE_TEST(cxx11_tensor_layout_swap) 58 { 59 CALL_SUBTEST(test_simple_swap()); 60 CALL_SUBTEST(test_swap_as_lvalue()); 61 }