blob: f5c92464411a6c9418aac2cd9aaf613a3a07743b [file] [log] [blame]
Googler45874d82019-08-21 12:06:47 -07001// 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
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080012template <typename MatrixType>
13void replicate(const MatrixType& m) {
Googler45874d82019-08-21 12:06:47 -070014 /* this test covers the following files:
15 Replicate.cpp
16 */
17 typedef typename MatrixType::Scalar Scalar;
18 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
19 typedef Matrix<Scalar, Dynamic, Dynamic> MatrixX;
20 typedef Matrix<Scalar, Dynamic, 1> VectorX;
21
22 Index rows = m.rows();
23 Index cols = m.cols();
24
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080025 MatrixType m1 = MatrixType::Random(rows, cols), m2 = MatrixType::Random(rows, cols);
Googler45874d82019-08-21 12:06:47 -070026
27 VectorType v1 = VectorType::Random(rows);
28
29 MatrixX x1, x2;
30 VectorX vx1;
31
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080032 int f1 = internal::random<int>(1, 10), f2 = internal::random<int>(1, 10);
Googler45874d82019-08-21 12:06:47 -070033
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080034 x1.resize(rows * f1, cols * f2);
35 for (int j = 0; j < f2; j++)
36 for (int i = 0; i < f1; i++) x1.block(i * rows, j * cols, rows, cols) = m1;
37 VERIFY_IS_APPROX(x1, m1.replicate(f1, f2));
Googler45874d82019-08-21 12:06:47 -070038
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080039 x2.resize(2 * rows, 3 * cols);
40 x2 << m2, m2, m2, m2, m2, m2;
41 VERIFY_IS_APPROX(x2, (m2.template replicate<2, 3>()));
42
43 x2.resize(rows, 3 * cols);
Googler45874d82019-08-21 12:06:47 -070044 x2 << m2, m2, m2;
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080045 VERIFY_IS_APPROX(x2, (m2.template replicate<1, 3>()));
Googler45874d82019-08-21 12:06:47 -070046
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080047 vx1.resize(3 * rows, cols);
48 vx1 << m2, m2, m2;
49 VERIFY_IS_APPROX(vx1 + vx1, vx1 + (m2.template replicate<3, 1>()));
50
51 vx1 = m2 + (m2.colwise().replicate(1));
52
53 if (m2.cols() == 1) VERIFY_IS_APPROX(m2.coeff(0), (m2.template replicate<3, 1>().coeff(m2.rows())));
54
55 x2.resize(rows, f1);
56 for (int j = 0; j < f1; ++j) x2.col(j) = v1;
Googler45874d82019-08-21 12:06:47 -070057 VERIFY_IS_APPROX(x2, v1.rowwise().replicate(f1));
58
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080059 vx1.resize(rows * f2);
60 for (int j = 0; j < f2; ++j) vx1.segment(j * rows, rows) = v1;
Googler45874d82019-08-21 12:06:47 -070061 VERIFY_IS_APPROX(vx1, v1.colwise().replicate(f2));
62}
63
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080064EIGEN_DECLARE_TEST(array_replicate) {
65 for (int i = 0; i < g_repeat; i++) {
66 CALL_SUBTEST_1(replicate(Matrix<float, 1, 1>()));
67 CALL_SUBTEST_2(replicate(Vector2f()));
68 CALL_SUBTEST_3(replicate(Vector3d()));
69 CALL_SUBTEST_4(replicate(Vector4f()));
70 CALL_SUBTEST_5(replicate(VectorXf(16)));
71 CALL_SUBTEST_6(replicate(VectorXcd(10)));
Googler45874d82019-08-21 12:06:47 -070072 }
73}