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.

82 lines
2.3 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
  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 MatrixType> void replicate(const MatrixType& m)
  11. {
  12. /* this test covers the following files:
  13. Replicate.cpp
  14. */
  15. typedef typename MatrixType::Index Index;
  16. typedef typename MatrixType::Scalar Scalar;
  17. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
  18. typedef Matrix<Scalar, Dynamic, Dynamic> MatrixX;
  19. typedef Matrix<Scalar, Dynamic, 1> VectorX;
  20. Index rows = m.rows();
  21. Index cols = m.cols();
  22. MatrixType m1 = MatrixType::Random(rows, cols),
  23. m2 = MatrixType::Random(rows, cols);
  24. VectorType v1 = VectorType::Random(rows);
  25. MatrixX x1, x2;
  26. VectorX vx1;
  27. int f1 = internal::random<int>(1,10),
  28. f2 = internal::random<int>(1,10);
  29. x1.resize(rows*f1,cols*f2);
  30. for(int j=0; j<f2; j++)
  31. for(int i=0; i<f1; i++)
  32. x1.block(i*rows,j*cols,rows,cols) = m1;
  33. VERIFY_IS_APPROX(x1, m1.replicate(f1,f2));
  34. x2.resize(2*rows,3*cols);
  35. x2 << m2, m2, m2,
  36. m2, m2, m2;
  37. VERIFY_IS_APPROX(x2, (m2.template replicate<2,3>()));
  38. x2.resize(rows,3*cols);
  39. x2 << m2, m2, m2;
  40. VERIFY_IS_APPROX(x2, (m2.template replicate<1,3>()));
  41. vx1.resize(3*rows,cols);
  42. vx1 << m2, m2, m2;
  43. VERIFY_IS_APPROX(vx1+vx1, vx1+(m2.template replicate<3,1>()));
  44. vx1=m2+(m2.colwise().replicate(1));
  45. if(m2.cols()==1)
  46. VERIFY_IS_APPROX(m2.coeff(0), (m2.template replicate<3,1>().coeff(m2.rows())));
  47. x2.resize(rows,f1);
  48. for (int j=0; j<f1; ++j)
  49. x2.col(j) = v1;
  50. VERIFY_IS_APPROX(x2, v1.rowwise().replicate(f1));
  51. vx1.resize(rows*f2);
  52. for (int j=0; j<f2; ++j)
  53. vx1.segment(j*rows,rows) = v1;
  54. VERIFY_IS_APPROX(vx1, v1.colwise().replicate(f2));
  55. }
  56. void test_array_replicate()
  57. {
  58. for(int i = 0; i < g_repeat; i++) {
  59. CALL_SUBTEST_1( replicate(Matrix<float, 1, 1>()) );
  60. CALL_SUBTEST_2( replicate(Vector2f()) );
  61. CALL_SUBTEST_3( replicate(Vector3d()) );
  62. CALL_SUBTEST_4( replicate(Vector4f()) );
  63. CALL_SUBTEST_5( replicate(VectorXf(16)) );
  64. CALL_SUBTEST_6( replicate(VectorXcd(10)) );
  65. }
  66. }