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.

134 lines
4.3 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2009 Hauke Heibel <hauke.heibel@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. #include <Eigen/Core>
  11. using namespace StormEigen;
  12. template <typename Scalar, int Storage>
  13. void run_matrix_tests()
  14. {
  15. typedef Matrix<Scalar, StormEigen::Dynamic, StormEigen::Dynamic, Storage> MatrixType;
  16. typedef typename MatrixType::Index Index;
  17. MatrixType m, n;
  18. // boundary cases ...
  19. m = n = MatrixType::Random(50,50);
  20. m.conservativeResize(1,50);
  21. VERIFY_IS_APPROX(m, n.block(0,0,1,50));
  22. m = n = MatrixType::Random(50,50);
  23. m.conservativeResize(50,1);
  24. VERIFY_IS_APPROX(m, n.block(0,0,50,1));
  25. m = n = MatrixType::Random(50,50);
  26. m.conservativeResize(50,50);
  27. VERIFY_IS_APPROX(m, n.block(0,0,50,50));
  28. // random shrinking ...
  29. for (int i=0; i<25; ++i)
  30. {
  31. const Index rows = internal::random<Index>(1,50);
  32. const Index cols = internal::random<Index>(1,50);
  33. m = n = MatrixType::Random(50,50);
  34. m.conservativeResize(rows,cols);
  35. VERIFY_IS_APPROX(m, n.block(0,0,rows,cols));
  36. }
  37. // random growing with zeroing ...
  38. for (int i=0; i<25; ++i)
  39. {
  40. const Index rows = internal::random<Index>(50,75);
  41. const Index cols = internal::random<Index>(50,75);
  42. m = n = MatrixType::Random(50,50);
  43. m.conservativeResizeLike(MatrixType::Zero(rows,cols));
  44. VERIFY_IS_APPROX(m.block(0,0,n.rows(),n.cols()), n);
  45. VERIFY( rows<=50 || m.block(50,0,rows-50,cols).sum() == Scalar(0) );
  46. VERIFY( cols<=50 || m.block(0,50,rows,cols-50).sum() == Scalar(0) );
  47. }
  48. }
  49. template <typename Scalar>
  50. void run_vector_tests()
  51. {
  52. typedef Matrix<Scalar, 1, StormEigen::Dynamic> VectorType;
  53. VectorType m, n;
  54. // boundary cases ...
  55. m = n = VectorType::Random(50);
  56. m.conservativeResize(1);
  57. VERIFY_IS_APPROX(m, n.segment(0,1));
  58. m = n = VectorType::Random(50);
  59. m.conservativeResize(50);
  60. VERIFY_IS_APPROX(m, n.segment(0,50));
  61. m = n = VectorType::Random(50);
  62. m.conservativeResize(m.rows(),1);
  63. VERIFY_IS_APPROX(m, n.segment(0,1));
  64. m = n = VectorType::Random(50);
  65. m.conservativeResize(m.rows(),50);
  66. VERIFY_IS_APPROX(m, n.segment(0,50));
  67. // random shrinking ...
  68. for (int i=0; i<50; ++i)
  69. {
  70. const int size = internal::random<int>(1,50);
  71. m = n = VectorType::Random(50);
  72. m.conservativeResize(size);
  73. VERIFY_IS_APPROX(m, n.segment(0,size));
  74. m = n = VectorType::Random(50);
  75. m.conservativeResize(m.rows(), size);
  76. VERIFY_IS_APPROX(m, n.segment(0,size));
  77. }
  78. // random growing with zeroing ...
  79. for (int i=0; i<50; ++i)
  80. {
  81. const int size = internal::random<int>(50,100);
  82. m = n = VectorType::Random(50);
  83. m.conservativeResizeLike(VectorType::Zero(size));
  84. VERIFY_IS_APPROX(m.segment(0,50), n);
  85. VERIFY( size<=50 || m.segment(50,size-50).sum() == Scalar(0) );
  86. m = n = VectorType::Random(50);
  87. m.conservativeResizeLike(Matrix<Scalar,Dynamic,Dynamic>::Zero(1,size));
  88. VERIFY_IS_APPROX(m.segment(0,50), n);
  89. VERIFY( size<=50 || m.segment(50,size-50).sum() == Scalar(0) );
  90. }
  91. }
  92. void test_conservative_resize()
  93. {
  94. for(int i=0; i<g_repeat; ++i)
  95. {
  96. CALL_SUBTEST_1((run_matrix_tests<int, StormEigen::RowMajor>()));
  97. CALL_SUBTEST_1((run_matrix_tests<int, StormEigen::ColMajor>()));
  98. CALL_SUBTEST_2((run_matrix_tests<float, StormEigen::RowMajor>()));
  99. CALL_SUBTEST_2((run_matrix_tests<float, StormEigen::ColMajor>()));
  100. CALL_SUBTEST_3((run_matrix_tests<double, StormEigen::RowMajor>()));
  101. CALL_SUBTEST_3((run_matrix_tests<double, StormEigen::ColMajor>()));
  102. CALL_SUBTEST_4((run_matrix_tests<std::complex<float>, StormEigen::RowMajor>()));
  103. CALL_SUBTEST_4((run_matrix_tests<std::complex<float>, StormEigen::ColMajor>()));
  104. CALL_SUBTEST_5((run_matrix_tests<std::complex<double>, StormEigen::RowMajor>()));
  105. CALL_SUBTEST_6((run_matrix_tests<std::complex<double>, StormEigen::ColMajor>()));
  106. CALL_SUBTEST_1((run_vector_tests<int>()));
  107. CALL_SUBTEST_2((run_vector_tests<float>()));
  108. CALL_SUBTEST_3((run_vector_tests<double>()));
  109. CALL_SUBTEST_4((run_vector_tests<std::complex<float> >()));
  110. CALL_SUBTEST_5((run_vector_tests<std::complex<double> >()));
  111. }
  112. }