array_replicate.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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::Scalar Scalar;
  16. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
  17. typedef Matrix<Scalar, Dynamic, Dynamic> MatrixX;
  18. typedef Matrix<Scalar, Dynamic, 1> VectorX;
  19. Index rows = m.rows();
  20. Index cols = m.cols();
  21. MatrixType m1 = MatrixType::Random(rows, cols),
  22. m2 = MatrixType::Random(rows, cols);
  23. VectorType v1 = VectorType::Random(rows);
  24. MatrixX x1, x2;
  25. VectorX vx1;
  26. int f1 = internal::random<int>(1,10),
  27. f2 = internal::random<int>(1,10);
  28. x1.resize(rows*f1,cols*f2);
  29. for(int j=0; j<f2; j++)
  30. for(int i=0; i<f1; i++)
  31. x1.block(i*rows,j*cols,rows,cols) = m1;
  32. VERIFY_IS_APPROX(x1, m1.replicate(f1,f2));
  33. x2.resize(2*rows,3*cols);
  34. x2 << m2, m2, m2,
  35. m2, m2, m2;
  36. VERIFY_IS_APPROX(x2, (m2.template replicate<2,3>()));
  37. x2.resize(rows,3*cols);
  38. x2 << m2, m2, m2;
  39. VERIFY_IS_APPROX(x2, (m2.template replicate<1,3>()));
  40. vx1.resize(3*rows,cols);
  41. vx1 << m2, m2, m2;
  42. VERIFY_IS_APPROX(vx1+vx1, vx1+(m2.template replicate<3,1>()));
  43. vx1=m2+(m2.colwise().replicate(1));
  44. if(m2.cols()==1)
  45. VERIFY_IS_APPROX(m2.coeff(0), (m2.template replicate<3,1>().coeff(m2.rows())));
  46. x2.resize(rows,f1);
  47. for (int j=0; j<f1; ++j)
  48. x2.col(j) = v1;
  49. VERIFY_IS_APPROX(x2, v1.rowwise().replicate(f1));
  50. vx1.resize(rows*f2);
  51. for (int j=0; j<f2; ++j)
  52. vx1.segment(j*rows,rows) = v1;
  53. VERIFY_IS_APPROX(vx1, v1.colwise().replicate(f2));
  54. }
  55. EIGEN_DECLARE_TEST(array_replicate)
  56. {
  57. for(int i = 0; i < g_repeat; i++) {
  58. CALL_SUBTEST_1( replicate(Matrix<float, 1, 1>()) );
  59. CALL_SUBTEST_2( replicate(Vector2f()) );
  60. CALL_SUBTEST_3( replicate(Vector3d()) );
  61. CALL_SUBTEST_4( replicate(Vector4f()) );
  62. CALL_SUBTEST_5( replicate(VectorXf(16)) );
  63. CALL_SUBTEST_6( replicate(VectorXcd(10)) );
  64. }
  65. }