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
3.6 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 Eigen;
  12. template <typename Scalar, int Storage>
  13. void run_matrix_tests()
  14. {
  15. typedef Matrix<Scalar, Eigen::Dynamic, Eigen::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, Eigen::Dynamic> MatrixType;
  53. MatrixType m, n;
  54. // boundary cases ...
  55. m = n = MatrixType::Random(50);
  56. m.conservativeResize(1);
  57. VERIFY_IS_APPROX(m, n.segment(0,1));
  58. m = n = MatrixType::Random(50);
  59. m.conservativeResize(50);
  60. VERIFY_IS_APPROX(m, n.segment(0,50));
  61. // random shrinking ...
  62. for (int i=0; i<50; ++i)
  63. {
  64. const int size = internal::random<int>(1,50);
  65. m = n = MatrixType::Random(50);
  66. m.conservativeResize(size);
  67. VERIFY_IS_APPROX(m, n.segment(0,size));
  68. }
  69. // random growing with zeroing ...
  70. for (int i=0; i<50; ++i)
  71. {
  72. const int size = internal::random<int>(50,100);
  73. m = n = MatrixType::Random(50);
  74. m.conservativeResizeLike(MatrixType::Zero(size));
  75. VERIFY_IS_APPROX(m.segment(0,50), n);
  76. VERIFY( size<=50 || m.segment(50,size-50).sum() == Scalar(0) );
  77. }
  78. }
  79. void test_conservative_resize()
  80. {
  81. CALL_SUBTEST_1((run_matrix_tests<int, Eigen::RowMajor>()));
  82. CALL_SUBTEST_1((run_matrix_tests<int, Eigen::ColMajor>()));
  83. CALL_SUBTEST_2((run_matrix_tests<float, Eigen::RowMajor>()));
  84. CALL_SUBTEST_2((run_matrix_tests<float, Eigen::ColMajor>()));
  85. CALL_SUBTEST_3((run_matrix_tests<double, Eigen::RowMajor>()));
  86. CALL_SUBTEST_3((run_matrix_tests<double, Eigen::ColMajor>()));
  87. CALL_SUBTEST_4((run_matrix_tests<std::complex<float>, Eigen::RowMajor>()));
  88. CALL_SUBTEST_4((run_matrix_tests<std::complex<float>, Eigen::ColMajor>()));
  89. CALL_SUBTEST_5((run_matrix_tests<std::complex<double>, Eigen::RowMajor>()));
  90. CALL_SUBTEST_6((run_matrix_tests<std::complex<double>, Eigen::ColMajor>()));
  91. CALL_SUBTEST_1((run_vector_tests<int>()));
  92. CALL_SUBTEST_2((run_vector_tests<float>()));
  93. CALL_SUBTEST_3((run_vector_tests<double>()));
  94. CALL_SUBTEST_4((run_vector_tests<std::complex<float> >()));
  95. CALL_SUBTEST_5((run_vector_tests<std::complex<double> >()));
  96. }