Brian Silverman | 72890c2 | 2015-09-19 14:37:37 -0400 | [diff] [blame^] | 1 | // This file is part of Eigen, a lightweight C++ template library |
| 2 | // for linear algebra. Eigen itself is part of the KDE project. |
| 3 | // |
| 4 | // Copyright (C) 2007-2010 Benoit Jacob <jacob.benoit.1@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 | template<typename VectorType> void map_class_vector(const VectorType& m) |
| 13 | { |
| 14 | typedef typename VectorType::Scalar Scalar; |
| 15 | |
| 16 | int size = m.size(); |
| 17 | |
| 18 | // test Map.h |
| 19 | Scalar* array1 = ei_aligned_new<Scalar>(size); |
| 20 | Scalar* array2 = ei_aligned_new<Scalar>(size); |
| 21 | Scalar* array3 = new Scalar[size+1]; |
| 22 | Scalar* array3unaligned = std::size_t(array3)%16 == 0 ? array3+1 : array3; |
| 23 | |
| 24 | Map<VectorType, Aligned>(array1, size) = VectorType::Random(size); |
| 25 | Map<VectorType>(array2, size) = Map<VectorType>(array1, size); |
| 26 | Map<VectorType>(array3unaligned, size) = Map<VectorType>((const Scalar*)array1, size); // test non-const-correctness support in eigen2 |
| 27 | VectorType ma1 = Map<VectorType>(array1, size); |
| 28 | VectorType ma2 = Map<VectorType, Aligned>(array2, size); |
| 29 | VectorType ma3 = Map<VectorType>(array3unaligned, size); |
| 30 | VERIFY_IS_APPROX(ma1, ma2); |
| 31 | VERIFY_IS_APPROX(ma1, ma3); |
| 32 | |
| 33 | ei_aligned_delete(array1, size); |
| 34 | ei_aligned_delete(array2, size); |
| 35 | delete[] array3; |
| 36 | } |
| 37 | |
| 38 | template<typename MatrixType> void map_class_matrix(const MatrixType& m) |
| 39 | { |
| 40 | typedef typename MatrixType::Scalar Scalar; |
| 41 | |
| 42 | int rows = m.rows(), cols = m.cols(), size = rows*cols; |
| 43 | |
| 44 | // test Map.h |
| 45 | Scalar* array1 = ei_aligned_new<Scalar>(size); |
| 46 | for(int i = 0; i < size; i++) array1[i] = Scalar(1); |
| 47 | Scalar* array2 = ei_aligned_new<Scalar>(size); |
| 48 | for(int i = 0; i < size; i++) array2[i] = Scalar(1); |
| 49 | Scalar* array3 = new Scalar[size+1]; |
| 50 | for(int i = 0; i < size+1; i++) array3[i] = Scalar(1); |
| 51 | Scalar* array3unaligned = std::size_t(array3)%16 == 0 ? array3+1 : array3; |
| 52 | Map<MatrixType, Aligned>(array1, rows, cols) = MatrixType::Ones(rows,cols); |
| 53 | Map<MatrixType>(array2, rows, cols) = Map<MatrixType>((const Scalar*)array1, rows, cols); // test non-const-correctness support in eigen2 |
| 54 | Map<MatrixType>(array3unaligned, rows, cols) = Map<MatrixType>(array1, rows, cols); |
| 55 | MatrixType ma1 = Map<MatrixType>(array1, rows, cols); |
| 56 | MatrixType ma2 = Map<MatrixType, Aligned>(array2, rows, cols); |
| 57 | VERIFY_IS_APPROX(ma1, ma2); |
| 58 | MatrixType ma3 = Map<MatrixType>(array3unaligned, rows, cols); |
| 59 | VERIFY_IS_APPROX(ma1, ma3); |
| 60 | |
| 61 | ei_aligned_delete(array1, size); |
| 62 | ei_aligned_delete(array2, size); |
| 63 | delete[] array3; |
| 64 | } |
| 65 | |
| 66 | template<typename VectorType> void map_static_methods(const VectorType& m) |
| 67 | { |
| 68 | typedef typename VectorType::Scalar Scalar; |
| 69 | |
| 70 | int size = m.size(); |
| 71 | |
| 72 | // test Map.h |
| 73 | Scalar* array1 = ei_aligned_new<Scalar>(size); |
| 74 | Scalar* array2 = ei_aligned_new<Scalar>(size); |
| 75 | Scalar* array3 = new Scalar[size+1]; |
| 76 | Scalar* array3unaligned = std::size_t(array3)%16 == 0 ? array3+1 : array3; |
| 77 | |
| 78 | VectorType::MapAligned(array1, size) = VectorType::Random(size); |
| 79 | VectorType::Map(array2, size) = VectorType::Map(array1, size); |
| 80 | VectorType::Map(array3unaligned, size) = VectorType::Map(array1, size); |
| 81 | VectorType ma1 = VectorType::Map(array1, size); |
| 82 | VectorType ma2 = VectorType::MapAligned(array2, size); |
| 83 | VectorType ma3 = VectorType::Map(array3unaligned, size); |
| 84 | VERIFY_IS_APPROX(ma1, ma2); |
| 85 | VERIFY_IS_APPROX(ma1, ma3); |
| 86 | |
| 87 | ei_aligned_delete(array1, size); |
| 88 | ei_aligned_delete(array2, size); |
| 89 | delete[] array3; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | void test_eigen2_map() |
| 94 | { |
| 95 | for(int i = 0; i < g_repeat; i++) { |
| 96 | CALL_SUBTEST_1( map_class_vector(Matrix<float, 1, 1>()) ); |
| 97 | CALL_SUBTEST_2( map_class_vector(Vector4d()) ); |
| 98 | CALL_SUBTEST_3( map_class_vector(RowVector4f()) ); |
| 99 | CALL_SUBTEST_4( map_class_vector(VectorXcf(8)) ); |
| 100 | CALL_SUBTEST_5( map_class_vector(VectorXi(12)) ); |
| 101 | |
| 102 | CALL_SUBTEST_1( map_class_matrix(Matrix<float, 1, 1>()) ); |
| 103 | CALL_SUBTEST_2( map_class_matrix(Matrix4d()) ); |
| 104 | CALL_SUBTEST_6( map_class_matrix(Matrix<float,3,5>()) ); |
| 105 | CALL_SUBTEST_4( map_class_matrix(MatrixXcf(ei_random<int>(1,10),ei_random<int>(1,10))) ); |
| 106 | CALL_SUBTEST_5( map_class_matrix(MatrixXi(ei_random<int>(1,10),ei_random<int>(1,10))) ); |
| 107 | |
| 108 | CALL_SUBTEST_1( map_static_methods(Matrix<double, 1, 1>()) ); |
| 109 | CALL_SUBTEST_2( map_static_methods(Vector3f()) ); |
| 110 | CALL_SUBTEST_7( map_static_methods(RowVector3d()) ); |
| 111 | CALL_SUBTEST_4( map_static_methods(VectorXcd(8)) ); |
| 112 | CALL_SUBTEST_5( map_static_methods(VectorXf(12)) ); |
| 113 | } |
| 114 | } |