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 | { |
| 30 | /* this test covers the following files: |
| 31 | Array.cpp |
| 32 | */ |
| 33 | |
| 34 | typedef typename MatrixType::Scalar Scalar; |
Gael Guennebaud | 2120fed | 2008-08-23 15:14:20 +0000 | [diff] [blame] | 35 | typedef typename NumTraits<Scalar>::Real RealScalar; |
Gael Guennebaud | 627595a | 2009-06-10 11:20:30 +0200 | [diff] [blame] | 36 | typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> ColVectorType; |
| 37 | typedef Matrix<Scalar, 1, MatrixType::ColsAtCompileTime> RowVectorType; |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 38 | |
| 39 | int rows = m.rows(); |
| 40 | int cols = m.cols(); |
| 41 | |
Benoit Jacob | 46fe7a3 | 2008-09-01 17:31:21 +0000 | [diff] [blame] | 42 | MatrixType m1 = MatrixType::Random(rows, cols), |
| 43 | m2 = MatrixType::Random(rows, cols), |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 44 | m3(rows, cols); |
| 45 | |
Gael Guennebaud | 627595a | 2009-06-10 11:20:30 +0200 | [diff] [blame] | 46 | ColVectorType cv1 = ColVectorType::Random(rows); |
| 47 | RowVectorType rv1 = RowVectorType::Random(cols); |
| 48 | |
Benoit Jacob | 46fe7a3 | 2008-09-01 17:31:21 +0000 | [diff] [blame] | 49 | Scalar s1 = ei_random<Scalar>(), |
| 50 | s2 = ei_random<Scalar>(); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 51 | |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 52 | // scalar addition |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 53 | VERIFY_IS_APPROX(m1.array() + s1, s1 + m1.array()); |
| 54 | VERIFY_IS_APPROX((m1.array() + s1).asMatrix(), MatrixType::Constant(rows,cols,s1) + m1); |
| 55 | VERIFY_IS_APPROX(((m1*Scalar(2)).array() - s2).asMatrix(), (m1+m1) - MatrixType::Constant(rows,cols,s2) ); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 56 | m3 = m1; |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 57 | m3.array() += s2; |
| 58 | VERIFY_IS_APPROX(m3, (m1.array() + s2).asMatrix()); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 59 | m3 = m1; |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 60 | m3.array() -= s1; |
| 61 | VERIFY_IS_APPROX(m3, (m1.array() - s1).asMatrix()); |
Gael Guennebaud | 05ad083 | 2008-07-19 13:03:23 +0000 | [diff] [blame] | 62 | |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 63 | // reductions |
Gael Guennebaud | 05ad083 | 2008-07-19 13:03:23 +0000 | [diff] [blame] | 64 | VERIFY_IS_APPROX(m1.colwise().sum().sum(), m1.sum()); |
| 65 | VERIFY_IS_APPROX(m1.rowwise().sum().sum(), m1.sum()); |
Gael Guennebaud | 2120fed | 2008-08-23 15:14:20 +0000 | [diff] [blame] | 66 | if (!ei_isApprox(m1.sum(), (m1+m2).sum())) |
| 67 | VERIFY_IS_NOT_APPROX(((m1+m2).rowwise().sum()).sum(), m1.sum()); |
Gael Guennebaud | 05ad083 | 2008-07-19 13:03:23 +0000 | [diff] [blame] | 68 | 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] | 69 | |
| 70 | // vector-wise ops |
| 71 | m3 = m1; |
| 72 | VERIFY_IS_APPROX(m3.colwise() += cv1, m1.colwise() + cv1); |
| 73 | m3 = m1; |
| 74 | VERIFY_IS_APPROX(m3.colwise() -= cv1, m1.colwise() - cv1); |
| 75 | m3 = m1; |
| 76 | VERIFY_IS_APPROX(m3.rowwise() += rv1, m1.rowwise() + rv1); |
| 77 | m3 = m1; |
| 78 | VERIFY_IS_APPROX(m3.rowwise() -= rv1, m1.rowwise() - rv1); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | template<typename MatrixType> void comparisons(const MatrixType& m) |
| 82 | { |
| 83 | typedef typename MatrixType::Scalar Scalar; |
Benoit Jacob | 874ff5a | 2009-01-26 17:56:04 +0000 | [diff] [blame] | 84 | typedef typename NumTraits<Scalar>::Real RealScalar; |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 85 | typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType; |
| 86 | |
| 87 | int rows = m.rows(); |
| 88 | int cols = m.cols(); |
| 89 | |
| 90 | int r = ei_random<int>(0, rows-1), |
| 91 | c = ei_random<int>(0, cols-1); |
| 92 | |
Gael Guennebaud | c10f069 | 2008-07-21 00:34:46 +0000 | [diff] [blame] | 93 | MatrixType m1 = MatrixType::Random(rows, cols), |
| 94 | m2 = MatrixType::Random(rows, cols), |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 95 | m3(rows, cols); |
| 96 | |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 97 | VERIFY(((m1.array() + Scalar(1)) > m1.array()).all()); |
| 98 | VERIFY(((m1.array() - Scalar(1)) < m1.array()).all()); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 99 | if (rows*cols>1) |
| 100 | { |
| 101 | m3 = m1; |
| 102 | m3(r,c) += 1; |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 103 | VERIFY(! (m1.array() < m3.array()).all() ); |
| 104 | VERIFY(! (m1.array() > m3.array()).all() ); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 105 | } |
Gael Guennebaud | 627595a | 2009-06-10 11:20:30 +0200 | [diff] [blame] | 106 | |
Gael Guennebaud | e14aa8c | 2008-09-03 17:56:06 +0000 | [diff] [blame] | 107 | // comparisons to scalar |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 108 | VERIFY( (m1.array() != (m1(r,c)+1) ).any() ); |
| 109 | VERIFY( (m1.array() > (m1(r,c)-1) ).any() ); |
| 110 | VERIFY( (m1.array() < (m1(r,c)+1) ).any() ); |
| 111 | VERIFY( (m1.array() == m1(r,c) ).any() ); |
Gael Guennebaud | 627595a | 2009-06-10 11:20:30 +0200 | [diff] [blame] | 112 | |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 113 | // test Select |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 114 | VERIFY_IS_APPROX( (m1.array()<m2.array()).select(m1,m2), m1.cwiseMin(m2) ); |
| 115 | VERIFY_IS_APPROX( (m1.array()>m2.array()).select(m1,m2), m1.cwiseMax(m2) ); |
| 116 | Scalar mid = (m1.cwiseAbs().minCoeff() + m1.cwiseAbs().maxCoeff())/Scalar(2); |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 117 | for (int j=0; j<cols; ++j) |
| 118 | for (int i=0; i<rows; ++i) |
| 119 | m3(i,j) = ei_abs(m1(i,j))<mid ? 0 : m1(i,j); |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 120 | VERIFY_IS_APPROX( (m1.array().abs()<MatrixType::Constant(rows,cols,mid).array()) |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 121 | .select(MatrixType::Zero(rows,cols),m1), m3); |
Gael Guennebaud | e14aa8c | 2008-09-03 17:56:06 +0000 | [diff] [blame] | 122 | // shorter versions: |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 123 | VERIFY_IS_APPROX( (m1.array().abs()<MatrixType::Constant(rows,cols,mid).array()) |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 124 | .select(0,m1), m3); |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 125 | VERIFY_IS_APPROX( (m1.array().abs()>=MatrixType::Constant(rows,cols,mid).array()) |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 126 | .select(m1,0), m3); |
Gael Guennebaud | e14aa8c | 2008-09-03 17:56:06 +0000 | [diff] [blame] | 127 | // even shorter version: |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 128 | VERIFY_IS_APPROX( (m1.array().abs()<mid).select(0,m1), m3); |
Gael Guennebaud | 627595a | 2009-06-10 11:20:30 +0200 | [diff] [blame] | 129 | |
Gael Guennebaud | 56c7e16 | 2009-01-24 15:22:44 +0000 | [diff] [blame] | 130 | // count |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 131 | VERIFY(((m1.array().abs()+1)>RealScalar(0.1)).count() == rows*cols); |
| 132 | // TODO allows colwise/rowwise for array |
| 133 | VERIFY_IS_APPROX(((m1.array().abs()+1)>RealScalar(0.1)).asMatrix().colwise().count(), RowVectorXi::Constant(cols,rows)); |
| 134 | VERIFY_IS_APPROX(((m1.array().abs()+1)>RealScalar(0.1)).asMatrix().rowwise().count(), VectorXi::Constant(rows, cols)); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Benoit Jacob | e800999 | 2008-11-03 22:47:00 +0000 | [diff] [blame] | 137 | template<typename VectorType> void lpNorm(const VectorType& v) |
| 138 | { |
| 139 | VectorType u = VectorType::Random(v.size()); |
| 140 | |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 141 | VERIFY_IS_APPROX(u.template lpNorm<Infinity>(), u.cwiseAbs().maxCoeff()); |
| 142 | VERIFY_IS_APPROX(u.template lpNorm<1>(), u.cwiseAbs().sum()); |
| 143 | VERIFY_IS_APPROX(u.template lpNorm<2>(), ei_sqrt(u.array().abs().square().sum())); |
| 144 | VERIFY_IS_APPROX(ei_pow(u.template lpNorm<5>(), typename VectorType::RealScalar(5)), u.array().abs().pow(5).sum()); |
Benoit Jacob | e800999 | 2008-11-03 22:47:00 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 147 | void test_array() |
| 148 | { |
| 149 | for(int i = 0; i < g_repeat; i++) { |
Benoit Jacob | 2840ac7 | 2009-10-28 18:19:29 -0400 | [diff] [blame] | 150 | CALL_SUBTEST_1( array(Matrix<float, 1, 1>()) ); |
| 151 | CALL_SUBTEST_2( array(Matrix2f()) ); |
| 152 | CALL_SUBTEST_3( array(Matrix4d()) ); |
| 153 | CALL_SUBTEST_4( array(MatrixXcf(3, 3)) ); |
| 154 | CALL_SUBTEST_5( array(MatrixXf(8, 12)) ); |
| 155 | CALL_SUBTEST_6( array(MatrixXi(8, 12)) ); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 156 | } |
| 157 | for(int i = 0; i < g_repeat; i++) { |
Benoit Jacob | 2840ac7 | 2009-10-28 18:19:29 -0400 | [diff] [blame] | 158 | CALL_SUBTEST_1( comparisons(Matrix<float, 1, 1>()) ); |
| 159 | CALL_SUBTEST_2( comparisons(Matrix2f()) ); |
| 160 | CALL_SUBTEST_3( comparisons(Matrix4d()) ); |
| 161 | CALL_SUBTEST_5( comparisons(MatrixXf(8, 12)) ); |
| 162 | CALL_SUBTEST_6( comparisons(MatrixXi(8, 12)) ); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 163 | } |
Benoit Jacob | e800999 | 2008-11-03 22:47:00 +0000 | [diff] [blame] | 164 | for(int i = 0; i < g_repeat; i++) { |
Benoit Jacob | 2840ac7 | 2009-10-28 18:19:29 -0400 | [diff] [blame] | 165 | CALL_SUBTEST_1( lpNorm(Matrix<float, 1, 1>()) ); |
| 166 | CALL_SUBTEST_2( lpNorm(Vector2f()) ); |
| 167 | CALL_SUBTEST_7( lpNorm(Vector3d()) ); |
| 168 | CALL_SUBTEST_8( lpNorm(Vector4f()) ); |
| 169 | CALL_SUBTEST_5( lpNorm(VectorXf(16)) ); |
| 170 | CALL_SUBTEST_4( lpNorm(VectorXcf(10)) ); |
Benoit Jacob | e800999 | 2008-11-03 22:47:00 +0000 | [diff] [blame] | 171 | } |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 172 | } |