Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 1 | // This file is part of Eigen, a lightweight C++ template library |
| 2 | // for linear algebra. Eigen itself is part of the KDE project. |
| 3 | // |
| 4 | // Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr> |
| 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 | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 36 | typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType; |
| 37 | |
| 38 | int rows = m.rows(); |
| 39 | int cols = m.cols(); |
| 40 | |
Benoit Jacob | 46fe7a3 | 2008-09-01 17:31:21 +0000 | [diff] [blame] | 41 | MatrixType m1 = MatrixType::Random(rows, cols), |
| 42 | m2 = MatrixType::Random(rows, cols), |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 43 | m3(rows, 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 |
Benoit Jacob | f5791ee | 2008-07-08 00:49:10 +0000 | [diff] [blame] | 49 | VERIFY_IS_APPROX(m1.cwise() + s1, s1 + m1.cwise()); |
Gael Guennebaud | c10f069 | 2008-07-21 00:34:46 +0000 | [diff] [blame] | 50 | VERIFY_IS_APPROX(m1.cwise() + s1, MatrixType::Constant(rows,cols,s1) + m1); |
| 51 | VERIFY_IS_APPROX((m1*Scalar(2)).cwise() - s2, (m1+m1) - MatrixType::Constant(rows,cols,s2) ); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 52 | m3 = m1; |
Benoit Jacob | f5791ee | 2008-07-08 00:49:10 +0000 | [diff] [blame] | 53 | m3.cwise() += s2; |
| 54 | VERIFY_IS_APPROX(m3, m1.cwise() + s2); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 55 | m3 = m1; |
Benoit Jacob | f5791ee | 2008-07-08 00:49:10 +0000 | [diff] [blame] | 56 | m3.cwise() -= s1; |
| 57 | VERIFY_IS_APPROX(m3, m1.cwise() - s1); |
Gael Guennebaud | 05ad083 | 2008-07-19 13:03:23 +0000 | [diff] [blame] | 58 | |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 59 | // reductions |
Gael Guennebaud | 05ad083 | 2008-07-19 13:03:23 +0000 | [diff] [blame] | 60 | VERIFY_IS_APPROX(m1.colwise().sum().sum(), m1.sum()); |
| 61 | VERIFY_IS_APPROX(m1.rowwise().sum().sum(), m1.sum()); |
Gael Guennebaud | 2120fed | 2008-08-23 15:14:20 +0000 | [diff] [blame] | 62 | if (!ei_isApprox(m1.sum(), (m1+m2).sum())) |
| 63 | VERIFY_IS_NOT_APPROX(((m1+m2).rowwise().sum()).sum(), m1.sum()); |
Gael Guennebaud | 05ad083 | 2008-07-19 13:03:23 +0000 | [diff] [blame] | 64 | VERIFY_IS_APPROX(m1.colwise().sum(), m1.colwise().redux(ei_scalar_sum_op<Scalar>())); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | template<typename MatrixType> void comparisons(const MatrixType& m) |
| 68 | { |
| 69 | typedef typename MatrixType::Scalar Scalar; |
| 70 | typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType; |
| 71 | |
| 72 | int rows = m.rows(); |
| 73 | int cols = m.cols(); |
| 74 | |
| 75 | int r = ei_random<int>(0, rows-1), |
| 76 | c = ei_random<int>(0, cols-1); |
| 77 | |
Gael Guennebaud | c10f069 | 2008-07-21 00:34:46 +0000 | [diff] [blame] | 78 | MatrixType m1 = MatrixType::Random(rows, cols), |
| 79 | m2 = MatrixType::Random(rows, cols), |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 80 | m3(rows, cols); |
| 81 | |
Benoit Jacob | f5791ee | 2008-07-08 00:49:10 +0000 | [diff] [blame] | 82 | VERIFY(((m1.cwise() + Scalar(1)).cwise() > m1).all()); |
| 83 | VERIFY(((m1.cwise() - Scalar(1)).cwise() < m1).all()); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 84 | if (rows*cols>1) |
| 85 | { |
| 86 | m3 = m1; |
| 87 | m3(r,c) += 1; |
Benoit Jacob | f5791ee | 2008-07-08 00:49:10 +0000 | [diff] [blame] | 88 | VERIFY(! (m1.cwise() < m3).all() ); |
| 89 | VERIFY(! (m1.cwise() > m3).all() ); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 90 | } |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 91 | |
Gael Guennebaud | e14aa8c | 2008-09-03 17:56:06 +0000 | [diff] [blame] | 92 | // comparisons to scalar |
| 93 | VERIFY( (m1.cwise() != (m1(r,c)+1) ).any() ); |
| 94 | VERIFY( (m1.cwise() > (m1(r,c)-1) ).any() ); |
| 95 | VERIFY( (m1.cwise() < (m1(r,c)+1) ).any() ); |
| 96 | VERIFY( (m1.cwise() == m1(r,c) ).any() ); |
| 97 | |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 98 | // test Select |
| 99 | VERIFY_IS_APPROX( (m1.cwise()<m2).select(m1,m2), m1.cwise().min(m2) ); |
| 100 | VERIFY_IS_APPROX( (m1.cwise()>m2).select(m1,m2), m1.cwise().max(m2) ); |
| 101 | Scalar mid = (m1.cwise().abs().minCoeff() + m1.cwise().abs().maxCoeff())/Scalar(2); |
| 102 | for (int j=0; j<cols; ++j) |
| 103 | for (int i=0; i<rows; ++i) |
| 104 | m3(i,j) = ei_abs(m1(i,j))<mid ? 0 : m1(i,j); |
| 105 | VERIFY_IS_APPROX( (m1.cwise().abs().cwise()<MatrixType::Constant(rows,cols,mid)) |
| 106 | .select(MatrixType::Zero(rows,cols),m1), m3); |
Gael Guennebaud | e14aa8c | 2008-09-03 17:56:06 +0000 | [diff] [blame] | 107 | // shorter versions: |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 108 | VERIFY_IS_APPROX( (m1.cwise().abs().cwise()<MatrixType::Constant(rows,cols,mid)) |
| 109 | .select(0,m1), m3); |
| 110 | VERIFY_IS_APPROX( (m1.cwise().abs().cwise()>=MatrixType::Constant(rows,cols,mid)) |
| 111 | .select(m1,0), m3); |
Gael Guennebaud | e14aa8c | 2008-09-03 17:56:06 +0000 | [diff] [blame] | 112 | // even shorter version: |
| 113 | VERIFY_IS_APPROX( (m1.cwise().abs().cwise()<mid).select(0,m1), m3); |
Gael Guennebaud | 56c7e16 | 2009-01-24 15:22:44 +0000 | [diff] [blame^] | 114 | |
| 115 | // count |
| 116 | VERIFY(((m1.cwise().abs().cwise()+1).cwise()>0.5).count() == rows*cols); |
| 117 | VERIFY_IS_APPROX(((m1.cwise().abs().cwise()+1).cwise()>0.5).colwise().count(), RowVectorXi::Constant(cols,rows)); |
| 118 | VERIFY_IS_APPROX(((m1.cwise().abs().cwise()+1).cwise()>0.5).rowwise().count(), VectorXi::Constant(rows, cols)); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Benoit Jacob | e800999 | 2008-11-03 22:47:00 +0000 | [diff] [blame] | 121 | template<typename VectorType> void lpNorm(const VectorType& v) |
| 122 | { |
| 123 | VectorType u = VectorType::Random(v.size()); |
| 124 | |
| 125 | VERIFY_IS_APPROX(u.template lpNorm<Infinity>(), u.cwise().abs().maxCoeff()); |
| 126 | VERIFY_IS_APPROX(u.template lpNorm<1>(), u.cwise().abs().sum()); |
| 127 | VERIFY_IS_APPROX(u.template lpNorm<2>(), ei_sqrt(u.cwise().abs().cwise().square().sum())); |
| 128 | VERIFY_IS_APPROX(ei_pow(u.template lpNorm<5>(), typename VectorType::RealScalar(5)), u.cwise().abs().cwise().pow(5).sum()); |
| 129 | } |
| 130 | |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 131 | void test_array() |
| 132 | { |
| 133 | for(int i = 0; i < g_repeat; i++) { |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 134 | CALL_SUBTEST( array(Matrix<float, 1, 1>()) ); |
| 135 | CALL_SUBTEST( array(Matrix2f()) ); |
| 136 | CALL_SUBTEST( array(Matrix4d()) ); |
| 137 | CALL_SUBTEST( array(MatrixXcf(3, 3)) ); |
| 138 | CALL_SUBTEST( array(MatrixXf(8, 12)) ); |
| 139 | CALL_SUBTEST( array(MatrixXi(8, 12)) ); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 140 | } |
| 141 | for(int i = 0; i < g_repeat; i++) { |
| 142 | CALL_SUBTEST( comparisons(Matrix<float, 1, 1>()) ); |
| 143 | CALL_SUBTEST( comparisons(Matrix2f()) ); |
| 144 | CALL_SUBTEST( comparisons(Matrix4d()) ); |
| 145 | CALL_SUBTEST( comparisons(MatrixXf(8, 12)) ); |
| 146 | CALL_SUBTEST( comparisons(MatrixXi(8, 12)) ); |
| 147 | } |
Benoit Jacob | e800999 | 2008-11-03 22:47:00 +0000 | [diff] [blame] | 148 | for(int i = 0; i < g_repeat; i++) { |
| 149 | CALL_SUBTEST( lpNorm(Matrix<float, 1, 1>()) ); |
| 150 | CALL_SUBTEST( lpNorm(Vector2f()) ); |
| 151 | CALL_SUBTEST( lpNorm(Vector3d()) ); |
| 152 | CALL_SUBTEST( lpNorm(Vector4f()) ); |
| 153 | CALL_SUBTEST( lpNorm(VectorXf(16)) ); |
| 154 | CALL_SUBTEST( lpNorm(VectorXcd(10)) ); |
| 155 | } |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 156 | } |