blob: 057c3c77b4996fbaf92d2b0fa063ac4caa33ef22 [file] [log] [blame]
Gael Guennebaud0be89a42009-03-05 10:25:22 +00001// This file is part of Eigen, a lightweight C++ template library
Benoit Jacob6347b1d2009-05-22 20:25:33 +02002// for linear algebra.
Gael Guennebaud0be89a42009-03-05 10:25:22 +00003//
Gael Guennebaud28e64b02010-06-24 23:21:58 +02004// Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
Gael Guennebaud0be89a42009-03-05 10:25:22 +00005//
Benoit Jacob69124cf2012-07-13 14:42:47 -04006// 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/.
Gael Guennebaud0be89a42009-03-05 10:25:22 +00009
10#include "main.h"
Gael Guennebaud0be89a42009-03-05 10:25:22 +000011
12template<typename MatrixType> void replicate(const MatrixType& m)
13{
14 /* this test covers the following files:
15 Replicate.cpp
16 */
Gael Guennebaud0be89a42009-03-05 10:25:22 +000017 typedef typename MatrixType::Scalar Scalar;
Gael Guennebaud0be89a42009-03-05 10:25:22 +000018 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
19 typedef Matrix<Scalar, Dynamic, Dynamic> MatrixX;
20 typedef Matrix<Scalar, Dynamic, 1> VectorX;
21
Hauke Heibelf1679c72010-06-20 17:37:56 +020022 Index rows = m.rows();
23 Index cols = m.cols();
Gael Guennebaud0be89a42009-03-05 10:25:22 +000024
25 MatrixType m1 = MatrixType::Random(rows, cols),
26 m2 = MatrixType::Random(rows, cols);
Gael Guennebaud14430942009-10-13 09:23:09 +020027
Gael Guennebaud0be89a42009-03-05 10:25:22 +000028 VectorType v1 = VectorType::Random(rows);
Gael Guennebaud14430942009-10-13 09:23:09 +020029
Gael Guennebaud0be89a42009-03-05 10:25:22 +000030 MatrixX x1, x2;
31 VectorX vx1;
32
Benoit Jacob47160402010-10-25 10:15:22 -040033 int f1 = internal::random<int>(1,10),
34 f2 = internal::random<int>(1,10);
Gael Guennebaud0be89a42009-03-05 10:25:22 +000035
36 x1.resize(rows*f1,cols*f2);
37 for(int j=0; j<f2; j++)
38 for(int i=0; i<f1; i++)
39 x1.block(i*rows,j*cols,rows,cols) = m1;
40 VERIFY_IS_APPROX(x1, m1.replicate(f1,f2));
Gael Guennebaud14430942009-10-13 09:23:09 +020041
Gael Guennebaud0be89a42009-03-05 10:25:22 +000042 x2.resize(2*rows,3*cols);
43 x2 << m2, m2, m2,
44 m2, m2, m2;
45 VERIFY_IS_APPROX(x2, (m2.template replicate<2,3>()));
Gael Guennebaude6c57232015-06-08 15:42:15 +020046
47 x2.resize(rows,3*cols);
48 x2 << m2, m2, m2;
49 VERIFY_IS_APPROX(x2, (m2.template replicate<1,3>()));
50
51 vx1.resize(3*rows,cols);
52 vx1 << m2, m2, m2;
53 VERIFY_IS_APPROX(vx1+vx1, vx1+(m2.template replicate<3,1>()));
54
55 vx1=m2+(m2.colwise().replicate(1));
56
57 if(m2.cols()==1)
58 VERIFY_IS_APPROX(m2.coeff(0), (m2.template replicate<3,1>().coeff(m2.rows())));
Gael Guennebaud14430942009-10-13 09:23:09 +020059
Gael Guennebaud0be89a42009-03-05 10:25:22 +000060 x2.resize(rows,f1);
61 for (int j=0; j<f1; ++j)
62 x2.col(j) = v1;
63 VERIFY_IS_APPROX(x2, v1.rowwise().replicate(f1));
Gael Guennebaud14430942009-10-13 09:23:09 +020064
Gael Guennebaud0be89a42009-03-05 10:25:22 +000065 vx1.resize(rows*f2);
66 for (int j=0; j<f2; ++j)
67 vx1.segment(j*rows,rows) = v1;
68 VERIFY_IS_APPROX(vx1, v1.colwise().replicate(f2));
69}
70
Gael Guennebaud82f0ce22018-07-17 14:46:15 +020071EIGEN_DECLARE_TEST(array_replicate)
Gael Guennebaud0be89a42009-03-05 10:25:22 +000072{
73 for(int i = 0; i < g_repeat; i++) {
Benoit Jacob2840ac72009-10-28 18:19:29 -040074 CALL_SUBTEST_1( replicate(Matrix<float, 1, 1>()) );
75 CALL_SUBTEST_2( replicate(Vector2f()) );
76 CALL_SUBTEST_3( replicate(Vector3d()) );
77 CALL_SUBTEST_4( replicate(Vector4f()) );
78 CALL_SUBTEST_5( replicate(VectorXf(16)) );
79 CALL_SUBTEST_6( replicate(VectorXcd(10)) );
Gael Guennebaud0be89a42009-03-05 10:25:22 +000080 }
81}