Gael Guennebaud | 0be89a4 | 2009-03-05 10:25:22 +0000 | [diff] [blame] | 1 | // This file is part of Eigen, a lightweight C++ template library |
Benoit Jacob | 6347b1d | 2009-05-22 20:25:33 +0200 | [diff] [blame] | 2 | // for linear algebra. |
Gael Guennebaud | 0be89a4 | 2009-03-05 10:25:22 +0000 | [diff] [blame] | 3 | // |
Gael Guennebaud | 28e64b0 | 2010-06-24 23:21:58 +0200 | [diff] [blame^] | 4 | // Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr> |
Gael Guennebaud | 0be89a4 | 2009-03-05 10:25:22 +0000 | [diff] [blame] | 5 | // |
| 6 | // Eigen is free software; you can redistribute it and/or |
| 7 | // modify it under the terms of the GNU Lesser General Public |
| 8 | // License as published by the Free Software Foundation; either |
| 9 | // version 3 of the License, or (at your option) any later version. |
| 10 | // |
| 11 | // Alternatively, you can redistribute it and/or |
| 12 | // modify it under the terms of the GNU General Public License as |
| 13 | // published by the Free Software Foundation; either version 2 of |
| 14 | // the License, or (at your option) any later version. |
| 15 | // |
| 16 | // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY |
| 17 | // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 18 | // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the |
| 19 | // GNU General Public License for more details. |
| 20 | // |
| 21 | // You should have received a copy of the GNU Lesser General Public |
| 22 | // License and a copy of the GNU General Public License along with |
| 23 | // Eigen. If not, see <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | #include "main.h" |
Gael Guennebaud | 0be89a4 | 2009-03-05 10:25:22 +0000 | [diff] [blame] | 26 | |
| 27 | template<typename MatrixType> void replicate(const MatrixType& m) |
| 28 | { |
| 29 | /* this test covers the following files: |
| 30 | Replicate.cpp |
| 31 | */ |
Hauke Heibel | f1679c7 | 2010-06-20 17:37:56 +0200 | [diff] [blame] | 32 | typedef typename MatrixType::Index Index; |
Gael Guennebaud | 0be89a4 | 2009-03-05 10:25:22 +0000 | [diff] [blame] | 33 | typedef typename MatrixType::Scalar Scalar; |
| 34 | typedef typename NumTraits<Scalar>::Real RealScalar; |
| 35 | typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType; |
| 36 | typedef Matrix<Scalar, Dynamic, Dynamic> MatrixX; |
| 37 | typedef Matrix<Scalar, Dynamic, 1> VectorX; |
| 38 | |
Hauke Heibel | f1679c7 | 2010-06-20 17:37:56 +0200 | [diff] [blame] | 39 | Index rows = m.rows(); |
| 40 | Index cols = m.cols(); |
Gael Guennebaud | 0be89a4 | 2009-03-05 10:25:22 +0000 | [diff] [blame] | 41 | |
| 42 | MatrixType m1 = MatrixType::Random(rows, cols), |
| 43 | m2 = MatrixType::Random(rows, cols); |
Gael Guennebaud | 1443094 | 2009-10-13 09:23:09 +0200 | [diff] [blame] | 44 | |
Gael Guennebaud | 0be89a4 | 2009-03-05 10:25:22 +0000 | [diff] [blame] | 45 | VectorType v1 = VectorType::Random(rows); |
Gael Guennebaud | 1443094 | 2009-10-13 09:23:09 +0200 | [diff] [blame] | 46 | |
Gael Guennebaud | 0be89a4 | 2009-03-05 10:25:22 +0000 | [diff] [blame] | 47 | MatrixX x1, x2; |
| 48 | VectorX vx1; |
| 49 | |
| 50 | int f1 = ei_random<int>(1,10), |
| 51 | f2 = ei_random<int>(1,10); |
| 52 | |
| 53 | x1.resize(rows*f1,cols*f2); |
| 54 | for(int j=0; j<f2; j++) |
| 55 | for(int i=0; i<f1; i++) |
| 56 | x1.block(i*rows,j*cols,rows,cols) = m1; |
| 57 | VERIFY_IS_APPROX(x1, m1.replicate(f1,f2)); |
Gael Guennebaud | 1443094 | 2009-10-13 09:23:09 +0200 | [diff] [blame] | 58 | |
Gael Guennebaud | 0be89a4 | 2009-03-05 10:25:22 +0000 | [diff] [blame] | 59 | x2.resize(2*rows,3*cols); |
| 60 | x2 << m2, m2, m2, |
| 61 | m2, m2, m2; |
| 62 | VERIFY_IS_APPROX(x2, (m2.template replicate<2,3>())); |
Gael Guennebaud | 1443094 | 2009-10-13 09:23:09 +0200 | [diff] [blame] | 63 | |
Gael Guennebaud | 0be89a4 | 2009-03-05 10:25:22 +0000 | [diff] [blame] | 64 | x2.resize(rows,f1); |
| 65 | for (int j=0; j<f1; ++j) |
| 66 | x2.col(j) = v1; |
| 67 | VERIFY_IS_APPROX(x2, v1.rowwise().replicate(f1)); |
Gael Guennebaud | 1443094 | 2009-10-13 09:23:09 +0200 | [diff] [blame] | 68 | |
Gael Guennebaud | 0be89a4 | 2009-03-05 10:25:22 +0000 | [diff] [blame] | 69 | vx1.resize(rows*f2); |
| 70 | for (int j=0; j<f2; ++j) |
| 71 | vx1.segment(j*rows,rows) = v1; |
| 72 | VERIFY_IS_APPROX(vx1, v1.colwise().replicate(f2)); |
| 73 | } |
| 74 | |
| 75 | void test_array_replicate() |
| 76 | { |
| 77 | for(int i = 0; i < g_repeat; i++) { |
Benoit Jacob | 2840ac7 | 2009-10-28 18:19:29 -0400 | [diff] [blame] | 78 | CALL_SUBTEST_1( replicate(Matrix<float, 1, 1>()) ); |
| 79 | CALL_SUBTEST_2( replicate(Vector2f()) ); |
| 80 | CALL_SUBTEST_3( replicate(Vector3d()) ); |
| 81 | CALL_SUBTEST_4( replicate(Vector4f()) ); |
| 82 | CALL_SUBTEST_5( replicate(VectorXf(16)) ); |
| 83 | CALL_SUBTEST_6( replicate(VectorXcd(10)) ); |
Gael Guennebaud | 0be89a4 | 2009-03-05 10:25:22 +0000 | [diff] [blame] | 84 | } |
| 85 | } |