array_replicate.cpp revision c981c48f5bc9aefeffc0bcb0cc3934c2fae179dd
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
10#include "main.h"
11
12template<typename MatrixType> void replicate(const MatrixType& m)
13{
14  /* this test covers the following files:
15     Replicate.cpp
16  */
17  typedef typename MatrixType::Index Index;
18  typedef typename MatrixType::Scalar Scalar;
19  typedef typename NumTraits<Scalar>::Real RealScalar;
20  typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
21  typedef Matrix<Scalar, Dynamic, Dynamic> MatrixX;
22  typedef Matrix<Scalar, Dynamic, 1> VectorX;
23
24  Index rows = m.rows();
25  Index cols = m.cols();
26
27  MatrixType m1 = MatrixType::Random(rows, cols),
28             m2 = MatrixType::Random(rows, cols);
29
30  VectorType v1 = VectorType::Random(rows);
31
32  MatrixX x1, x2;
33  VectorX vx1;
34
35  int  f1 = internal::random<int>(1,10),
36       f2 = internal::random<int>(1,10);
37
38  x1.resize(rows*f1,cols*f2);
39  for(int j=0; j<f2; j++)
40  for(int i=0; i<f1; i++)
41    x1.block(i*rows,j*cols,rows,cols) = m1;
42  VERIFY_IS_APPROX(x1, m1.replicate(f1,f2));
43
44  x2.resize(2*rows,3*cols);
45  x2 << m2, m2, m2,
46        m2, m2, m2;
47  VERIFY_IS_APPROX(x2, (m2.template replicate<2,3>()));
48
49  x2.resize(rows,f1);
50  for (int j=0; j<f1; ++j)
51    x2.col(j) = v1;
52  VERIFY_IS_APPROX(x2, v1.rowwise().replicate(f1));
53
54  vx1.resize(rows*f2);
55  for (int j=0; j<f2; ++j)
56    vx1.segment(j*rows,rows) = v1;
57  VERIFY_IS_APPROX(vx1, v1.colwise().replicate(f2));
58}
59
60void test_array_replicate()
61{
62  for(int i = 0; i < g_repeat; i++) {
63    CALL_SUBTEST_1( replicate(Matrix<float, 1, 1>()) );
64    CALL_SUBTEST_2( replicate(Vector2f()) );
65    CALL_SUBTEST_3( replicate(Vector3d()) );
66    CALL_SUBTEST_4( replicate(Vector4f()) );
67    CALL_SUBTEST_5( replicate(VectorXf(16)) );
68    CALL_SUBTEST_6( replicate(VectorXcd(10)) );
69  }
70}
71