Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +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 | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 3 | // |
Gael Guennebaud | ec5c608 | 2009-07-10 16:10:03 +0200 | [diff] [blame] | 4 | // Copyright (C) 2008-2009 Gael Guennebaud <g.gael@free.fr> |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +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" |
| 26 | #include <Eigen/Array> |
| 27 | |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 28 | template<typename MatrixType> void array(const MatrixType& m) |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 29 | { |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 30 | typedef typename MatrixType::Scalar Scalar; |
Gael Guennebaud | 2120fed | 2008-08-23 15:14:20 +0000 | [diff] [blame] | 31 | typedef typename NumTraits<Scalar>::Real RealScalar; |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame^] | 32 | typedef Array<Scalar, MatrixType::RowsAtCompileTime, 1> ColVectorType; |
| 33 | typedef Array<Scalar, 1, MatrixType::ColsAtCompileTime> RowVectorType; |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 34 | |
| 35 | int rows = m.rows(); |
| 36 | int cols = m.cols(); |
| 37 | |
Benoit Jacob | 46fe7a3 | 2008-09-01 17:31:21 +0000 | [diff] [blame] | 38 | MatrixType m1 = MatrixType::Random(rows, cols), |
| 39 | m2 = MatrixType::Random(rows, cols), |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 40 | m3(rows, cols); |
| 41 | |
Gael Guennebaud | 627595a | 2009-06-10 11:20:30 +0200 | [diff] [blame] | 42 | ColVectorType cv1 = ColVectorType::Random(rows); |
| 43 | RowVectorType rv1 = RowVectorType::Random(cols); |
| 44 | |
Benoit Jacob | 46fe7a3 | 2008-09-01 17:31:21 +0000 | [diff] [blame] | 45 | Scalar s1 = ei_random<Scalar>(), |
| 46 | s2 = ei_random<Scalar>(); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 47 | |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 48 | // scalar addition |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame^] | 49 | VERIFY_IS_APPROX(m1 + s1, s1 + m1); |
| 50 | VERIFY_IS_APPROX(m1 + s1, MatrixType::Constant(rows,cols,s1) + m1); |
| 51 | VERIFY_IS_APPROX(s1 - m1, (-m1)+s1 ); |
| 52 | VERIFY_IS_APPROX(m1 - s1, m1 - MatrixType::Constant(rows,cols,s1)); |
| 53 | VERIFY_IS_APPROX(s1 - m1, MatrixType::Constant(rows,cols,s1) - m1); |
| 54 | VERIFY_IS_APPROX((m1*Scalar(2)) - s2, (m1+m1) - MatrixType::Constant(rows,cols,s2) ); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 55 | m3 = m1; |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame^] | 56 | m3 += s2; |
| 57 | VERIFY_IS_APPROX(m3, m1 + s2); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 58 | m3 = m1; |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame^] | 59 | m3 -= s1; |
| 60 | VERIFY_IS_APPROX(m3, m1 - s1); |
Gael Guennebaud | 05ad083 | 2008-07-19 13:03:23 +0000 | [diff] [blame] | 61 | |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 62 | // reductions |
Gael Guennebaud | 05ad083 | 2008-07-19 13:03:23 +0000 | [diff] [blame] | 63 | VERIFY_IS_APPROX(m1.colwise().sum().sum(), m1.sum()); |
| 64 | VERIFY_IS_APPROX(m1.rowwise().sum().sum(), m1.sum()); |
Gael Guennebaud | 2120fed | 2008-08-23 15:14:20 +0000 | [diff] [blame] | 65 | if (!ei_isApprox(m1.sum(), (m1+m2).sum())) |
| 66 | VERIFY_IS_NOT_APPROX(((m1+m2).rowwise().sum()).sum(), m1.sum()); |
Gael Guennebaud | 05ad083 | 2008-07-19 13:03:23 +0000 | [diff] [blame] | 67 | VERIFY_IS_APPROX(m1.colwise().sum(), m1.colwise().redux(ei_scalar_sum_op<Scalar>())); |
Gael Guennebaud | 627595a | 2009-06-10 11:20:30 +0200 | [diff] [blame] | 68 | |
| 69 | // vector-wise ops |
| 70 | m3 = m1; |
| 71 | VERIFY_IS_APPROX(m3.colwise() += cv1, m1.colwise() + cv1); |
| 72 | m3 = m1; |
| 73 | VERIFY_IS_APPROX(m3.colwise() -= cv1, m1.colwise() - cv1); |
| 74 | m3 = m1; |
| 75 | VERIFY_IS_APPROX(m3.rowwise() += rv1, m1.rowwise() + rv1); |
| 76 | m3 = m1; |
| 77 | VERIFY_IS_APPROX(m3.rowwise() -= rv1, m1.rowwise() - rv1); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | template<typename MatrixType> void comparisons(const MatrixType& m) |
| 81 | { |
| 82 | typedef typename MatrixType::Scalar Scalar; |
Benoit Jacob | 874ff5a | 2009-01-26 17:56:04 +0000 | [diff] [blame] | 83 | typedef typename NumTraits<Scalar>::Real RealScalar; |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame^] | 84 | typedef Array<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType; |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 85 | |
| 86 | int rows = m.rows(); |
| 87 | int cols = m.cols(); |
| 88 | |
| 89 | int r = ei_random<int>(0, rows-1), |
| 90 | c = ei_random<int>(0, cols-1); |
| 91 | |
Gael Guennebaud | c10f069 | 2008-07-21 00:34:46 +0000 | [diff] [blame] | 92 | MatrixType m1 = MatrixType::Random(rows, cols), |
| 93 | m2 = MatrixType::Random(rows, cols), |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 94 | m3(rows, cols); |
| 95 | |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame^] | 96 | VERIFY(((m1 + Scalar(1)) > m1).all()); |
| 97 | VERIFY(((m1 - Scalar(1)) < m1).all()); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 98 | if (rows*cols>1) |
| 99 | { |
| 100 | m3 = m1; |
| 101 | m3(r,c) += 1; |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame^] | 102 | VERIFY(! (m1 < m3).all() ); |
| 103 | VERIFY(! (m1 > m3).all() ); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 104 | } |
Gael Guennebaud | 627595a | 2009-06-10 11:20:30 +0200 | [diff] [blame] | 105 | |
Gael Guennebaud | e14aa8c | 2008-09-03 17:56:06 +0000 | [diff] [blame] | 106 | // comparisons to scalar |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame^] | 107 | VERIFY( (m1 != (m1(r,c)+1) ).any() ); |
| 108 | VERIFY( (m1 > (m1(r,c)-1) ).any() ); |
| 109 | VERIFY( (m1 < (m1(r,c)+1) ).any() ); |
| 110 | VERIFY( (m1 == m1(r,c) ).any() ); |
Gael Guennebaud | 627595a | 2009-06-10 11:20:30 +0200 | [diff] [blame] | 111 | |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 112 | // test Select |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame^] | 113 | VERIFY_IS_APPROX( (m1<m2).select(m1,m2), m1.cwiseMin(m2) ); |
| 114 | VERIFY_IS_APPROX( (m1>m2).select(m1,m2), m1.cwiseMax(m2) ); |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 115 | Scalar mid = (m1.cwiseAbs().minCoeff() + m1.cwiseAbs().maxCoeff())/Scalar(2); |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 116 | for (int j=0; j<cols; ++j) |
| 117 | for (int i=0; i<rows; ++i) |
| 118 | m3(i,j) = ei_abs(m1(i,j))<mid ? 0 : m1(i,j); |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame^] | 119 | VERIFY_IS_APPROX( (m1.abs()<MatrixType::Constant(rows,cols,mid)) |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 120 | .select(MatrixType::Zero(rows,cols),m1), m3); |
Gael Guennebaud | e14aa8c | 2008-09-03 17:56:06 +0000 | [diff] [blame] | 121 | // shorter versions: |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame^] | 122 | VERIFY_IS_APPROX( (m1.abs()<MatrixType::Constant(rows,cols,mid)) |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 123 | .select(0,m1), m3); |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame^] | 124 | VERIFY_IS_APPROX( (m1.abs()>=MatrixType::Constant(rows,cols,mid)) |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 125 | .select(m1,0), m3); |
Gael Guennebaud | e14aa8c | 2008-09-03 17:56:06 +0000 | [diff] [blame] | 126 | // even shorter version: |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame^] | 127 | VERIFY_IS_APPROX( (m1.abs()<mid).select(0,m1), m3); |
Gael Guennebaud | 627595a | 2009-06-10 11:20:30 +0200 | [diff] [blame] | 128 | |
Gael Guennebaud | 56c7e16 | 2009-01-24 15:22:44 +0000 | [diff] [blame] | 129 | // count |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame^] | 130 | VERIFY(((m1.abs()+1)>RealScalar(0.1)).count() == rows*cols); |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 131 | // TODO allows colwise/rowwise for array |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame^] | 132 | VERIFY_IS_APPROX(((m1.abs()+1)>RealScalar(0.1)).colwise().count(), ArrayXi::Constant(cols,rows).transpose()); |
| 133 | VERIFY_IS_APPROX(((m1.abs()+1)>RealScalar(0.1)).rowwise().count(), ArrayXi::Constant(rows, cols)); |
Benoit Jacob | e800999 | 2008-11-03 22:47:00 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 136 | void test_array() |
| 137 | { |
| 138 | for(int i = 0; i < g_repeat; i++) { |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame^] | 139 | CALL_SUBTEST_1( array(Array<float, 1, 1>()) ); |
| 140 | CALL_SUBTEST_2( array(Array22f()) ); |
| 141 | CALL_SUBTEST_3( array(Array44d()) ); |
| 142 | CALL_SUBTEST_4( array(ArrayXXcf(3, 3)) ); |
| 143 | CALL_SUBTEST_5( array(ArrayXXf(8, 12)) ); |
| 144 | CALL_SUBTEST_6( array(ArrayXXi(8, 12)) ); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 145 | } |
| 146 | for(int i = 0; i < g_repeat; i++) { |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame^] | 147 | CALL_SUBTEST_1( comparisons(Array<float, 1, 1>()) ); |
| 148 | CALL_SUBTEST_2( comparisons(Array22f()) ); |
| 149 | CALL_SUBTEST_3( comparisons(Array44d()) ); |
| 150 | CALL_SUBTEST_5( comparisons(ArrayXXf(8, 12)) ); |
| 151 | CALL_SUBTEST_6( comparisons(ArrayXXi(8, 12)) ); |
Benoit Jacob | e800999 | 2008-11-03 22:47:00 +0000 | [diff] [blame] | 152 | } |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 153 | } |