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.

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