You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
4.4 KiB

  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. #include "main.h"
  10. template<typename VectorType> void map_class_vector(const VectorType& m)
  11. {
  12. typedef typename VectorType::Scalar Scalar;
  13. int size = m.size();
  14. // test Map.h
  15. Scalar* array1 = ei_aligned_new<Scalar>(size);
  16. Scalar* array2 = ei_aligned_new<Scalar>(size);
  17. Scalar* array3 = new Scalar[size+1];
  18. Scalar* array3unaligned = std::size_t(array3)%16 == 0 ? array3+1 : array3;
  19. Map<VectorType, Aligned>(array1, size) = VectorType::Random(size);
  20. Map<VectorType>(array2, size) = Map<VectorType>(array1, size);
  21. Map<VectorType>(array3unaligned, size) = Map<VectorType>((const Scalar*)array1, size); // test non-const-correctness support in eigen2
  22. VectorType ma1 = Map<VectorType>(array1, size);
  23. VectorType ma2 = Map<VectorType, Aligned>(array2, size);
  24. VectorType ma3 = Map<VectorType>(array3unaligned, size);
  25. VERIFY_IS_APPROX(ma1, ma2);
  26. VERIFY_IS_APPROX(ma1, ma3);
  27. ei_aligned_delete(array1, size);
  28. ei_aligned_delete(array2, size);
  29. delete[] array3;
  30. }
  31. template<typename MatrixType> void map_class_matrix(const MatrixType& m)
  32. {
  33. typedef typename MatrixType::Scalar Scalar;
  34. int rows = m.rows(), cols = m.cols(), size = rows*cols;
  35. // test Map.h
  36. Scalar* array1 = ei_aligned_new<Scalar>(size);
  37. for(int i = 0; i < size; i++) array1[i] = Scalar(1);
  38. Scalar* array2 = ei_aligned_new<Scalar>(size);
  39. for(int i = 0; i < size; i++) array2[i] = Scalar(1);
  40. Scalar* array3 = new Scalar[size+1];
  41. for(int i = 0; i < size+1; i++) array3[i] = Scalar(1);
  42. Scalar* array3unaligned = std::size_t(array3)%16 == 0 ? array3+1 : array3;
  43. Map<MatrixType, Aligned>(array1, rows, cols) = MatrixType::Ones(rows,cols);
  44. Map<MatrixType>(array2, rows, cols) = Map<MatrixType>((const Scalar*)array1, rows, cols); // test non-const-correctness support in eigen2
  45. Map<MatrixType>(array3unaligned, rows, cols) = Map<MatrixType>(array1, rows, cols);
  46. MatrixType ma1 = Map<MatrixType>(array1, rows, cols);
  47. MatrixType ma2 = Map<MatrixType, Aligned>(array2, rows, cols);
  48. VERIFY_IS_APPROX(ma1, ma2);
  49. MatrixType ma3 = Map<MatrixType>(array3unaligned, rows, cols);
  50. VERIFY_IS_APPROX(ma1, ma3);
  51. ei_aligned_delete(array1, size);
  52. ei_aligned_delete(array2, size);
  53. delete[] array3;
  54. }
  55. template<typename VectorType> void map_static_methods(const VectorType& m)
  56. {
  57. typedef typename VectorType::Scalar Scalar;
  58. int size = m.size();
  59. // test Map.h
  60. Scalar* array1 = ei_aligned_new<Scalar>(size);
  61. Scalar* array2 = ei_aligned_new<Scalar>(size);
  62. Scalar* array3 = new Scalar[size+1];
  63. Scalar* array3unaligned = std::size_t(array3)%16 == 0 ? array3+1 : array3;
  64. VectorType::MapAligned(array1, size) = VectorType::Random(size);
  65. VectorType::Map(array2, size) = VectorType::Map(array1, size);
  66. VectorType::Map(array3unaligned, size) = VectorType::Map(array1, size);
  67. VectorType ma1 = VectorType::Map(array1, size);
  68. VectorType ma2 = VectorType::MapAligned(array2, size);
  69. VectorType ma3 = VectorType::Map(array3unaligned, size);
  70. VERIFY_IS_APPROX(ma1, ma2);
  71. VERIFY_IS_APPROX(ma1, ma3);
  72. ei_aligned_delete(array1, size);
  73. ei_aligned_delete(array2, size);
  74. delete[] array3;
  75. }
  76. void test_eigen2_map()
  77. {
  78. for(int i = 0; i < g_repeat; i++) {
  79. CALL_SUBTEST_1( map_class_vector(Matrix<float, 1, 1>()) );
  80. CALL_SUBTEST_2( map_class_vector(Vector4d()) );
  81. CALL_SUBTEST_3( map_class_vector(RowVector4f()) );
  82. CALL_SUBTEST_4( map_class_vector(VectorXcf(8)) );
  83. CALL_SUBTEST_5( map_class_vector(VectorXi(12)) );
  84. CALL_SUBTEST_1( map_class_matrix(Matrix<float, 1, 1>()) );
  85. CALL_SUBTEST_2( map_class_matrix(Matrix4d()) );
  86. CALL_SUBTEST_6( map_class_matrix(Matrix<float,3,5>()) );
  87. CALL_SUBTEST_4( map_class_matrix(MatrixXcf(ei_random<int>(1,10),ei_random<int>(1,10))) );
  88. CALL_SUBTEST_5( map_class_matrix(MatrixXi(ei_random<int>(1,10),ei_random<int>(1,10))) );
  89. CALL_SUBTEST_1( map_static_methods(Matrix<double, 1, 1>()) );
  90. CALL_SUBTEST_2( map_static_methods(Vector3f()) );
  91. CALL_SUBTEST_7( map_static_methods(RowVector3d()) );
  92. CALL_SUBTEST_4( map_static_methods(VectorXcd(8)) );
  93. CALL_SUBTEST_5( map_static_methods(VectorXf(12)) );
  94. }
  95. }