Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 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 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 12 | template <typename MatrixType> |
| 13 | void replicate(const MatrixType& m) { |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 14 | /* 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 Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 25 | MatrixType m1 = MatrixType::Random(rows, cols), m2 = MatrixType::Random(rows, cols); |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 26 | |
| 27 | VectorType v1 = VectorType::Random(rows); |
| 28 | |
| 29 | MatrixX x1, x2; |
| 30 | VectorX vx1; |
| 31 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 32 | int f1 = internal::random<int>(1, 10), f2 = internal::random<int>(1, 10); |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 33 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 34 | 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)); |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 38 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 39 | 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); |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 44 | x2 << m2, m2, m2; |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 45 | VERIFY_IS_APPROX(x2, (m2.template replicate<1, 3>())); |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 46 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 47 | 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; |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 57 | VERIFY_IS_APPROX(x2, v1.rowwise().replicate(f1)); |
| 58 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 59 | vx1.resize(rows * f2); |
| 60 | for (int j = 0; j < f2; ++j) vx1.segment(j * rows, rows) = v1; |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 61 | VERIFY_IS_APPROX(vx1, v1.colwise().replicate(f2)); |
| 62 | } |
| 63 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 64 | EIGEN_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))); |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 72 | } |
| 73 | } |