conservative_resize.cpp 4.2 KB

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