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" |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 26 | |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 27 | template<typename ArrayType> void array(const ArrayType& m) |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 28 | { |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 29 | typedef typename ArrayType::Scalar Scalar; |
Gael Guennebaud | 2120fed | 2008-08-23 15:14:20 +0000 | [diff] [blame] | 30 | typedef typename NumTraits<Scalar>::Real RealScalar; |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 31 | typedef Array<Scalar, ArrayType::RowsAtCompileTime, 1> ColVectorType; |
| 32 | typedef Array<Scalar, 1, ArrayType::ColsAtCompileTime> RowVectorType; |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 33 | |
| 34 | int rows = m.rows(); |
| 35 | int cols = m.cols(); |
| 36 | |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 37 | ArrayType m1 = ArrayType::Random(rows, cols), |
| 38 | m2 = ArrayType::Random(rows, cols), |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 39 | m3(rows, cols); |
| 40 | |
Gael Guennebaud | 627595a | 2009-06-10 11:20:30 +0200 | [diff] [blame] | 41 | ColVectorType cv1 = ColVectorType::Random(rows); |
| 42 | RowVectorType rv1 = RowVectorType::Random(cols); |
| 43 | |
Benoit Jacob | 46fe7a3 | 2008-09-01 17:31:21 +0000 | [diff] [blame] | 44 | Scalar s1 = ei_random<Scalar>(), |
| 45 | s2 = ei_random<Scalar>(); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 46 | |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 47 | // scalar addition |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame] | 48 | VERIFY_IS_APPROX(m1 + s1, s1 + m1); |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 49 | VERIFY_IS_APPROX(m1 + s1, ArrayType::Constant(rows,cols,s1) + m1); |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame] | 50 | VERIFY_IS_APPROX(s1 - m1, (-m1)+s1 ); |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 51 | VERIFY_IS_APPROX(m1 - s1, m1 - ArrayType::Constant(rows,cols,s1)); |
| 52 | VERIFY_IS_APPROX(s1 - m1, ArrayType::Constant(rows,cols,s1) - m1); |
| 53 | VERIFY_IS_APPROX((m1*Scalar(2)) - s2, (m1+m1) - ArrayType::Constant(rows,cols,s2) ); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 54 | m3 = m1; |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame] | 55 | m3 += s2; |
| 56 | VERIFY_IS_APPROX(m3, m1 + s2); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 57 | m3 = m1; |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame] | 58 | m3 -= s1; |
| 59 | VERIFY_IS_APPROX(m3, m1 - s1); |
Gael Guennebaud | 05ad083 | 2008-07-19 13:03:23 +0000 | [diff] [blame] | 60 | |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 61 | // reductions |
Gael Guennebaud | 05ad083 | 2008-07-19 13:03:23 +0000 | [diff] [blame] | 62 | VERIFY_IS_APPROX(m1.colwise().sum().sum(), m1.sum()); |
| 63 | VERIFY_IS_APPROX(m1.rowwise().sum().sum(), m1.sum()); |
Gael Guennebaud | 2120fed | 2008-08-23 15:14:20 +0000 | [diff] [blame] | 64 | if (!ei_isApprox(m1.sum(), (m1+m2).sum())) |
| 65 | VERIFY_IS_NOT_APPROX(((m1+m2).rowwise().sum()).sum(), m1.sum()); |
Gael Guennebaud | 05ad083 | 2008-07-19 13:03:23 +0000 | [diff] [blame] | 66 | 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] | 67 | |
| 68 | // vector-wise ops |
| 69 | m3 = m1; |
| 70 | VERIFY_IS_APPROX(m3.colwise() += cv1, m1.colwise() + cv1); |
| 71 | m3 = m1; |
| 72 | VERIFY_IS_APPROX(m3.colwise() -= cv1, m1.colwise() - cv1); |
| 73 | m3 = m1; |
| 74 | VERIFY_IS_APPROX(m3.rowwise() += rv1, m1.rowwise() + rv1); |
| 75 | m3 = m1; |
| 76 | VERIFY_IS_APPROX(m3.rowwise() -= rv1, m1.rowwise() - rv1); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 79 | template<typename ArrayType> void comparisons(const ArrayType& m) |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 80 | { |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 81 | typedef typename ArrayType::Scalar Scalar; |
Benoit Jacob | 874ff5a | 2009-01-26 17:56:04 +0000 | [diff] [blame] | 82 | typedef typename NumTraits<Scalar>::Real RealScalar; |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 83 | typedef Array<Scalar, ArrayType::RowsAtCompileTime, 1> VectorType; |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 84 | |
| 85 | int rows = m.rows(); |
| 86 | int cols = m.cols(); |
| 87 | |
| 88 | int r = ei_random<int>(0, rows-1), |
| 89 | c = ei_random<int>(0, cols-1); |
| 90 | |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 91 | ArrayType m1 = ArrayType::Random(rows, cols), |
| 92 | m2 = ArrayType::Random(rows, cols), |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 93 | m3(rows, cols); |
| 94 | |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame] | 95 | VERIFY(((m1 + Scalar(1)) > m1).all()); |
| 96 | VERIFY(((m1 - Scalar(1)) < m1).all()); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 97 | if (rows*cols>1) |
| 98 | { |
| 99 | m3 = m1; |
| 100 | m3(r,c) += 1; |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame] | 101 | VERIFY(! (m1 < m3).all() ); |
| 102 | VERIFY(! (m1 > m3).all() ); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 103 | } |
Gael Guennebaud | 627595a | 2009-06-10 11:20:30 +0200 | [diff] [blame] | 104 | |
Gael Guennebaud | e14aa8c | 2008-09-03 17:56:06 +0000 | [diff] [blame] | 105 | // comparisons to scalar |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame] | 106 | VERIFY( (m1 != (m1(r,c)+1) ).any() ); |
| 107 | VERIFY( (m1 > (m1(r,c)-1) ).any() ); |
| 108 | VERIFY( (m1 < (m1(r,c)+1) ).any() ); |
| 109 | VERIFY( (m1 == m1(r,c) ).any() ); |
Gael Guennebaud | 627595a | 2009-06-10 11:20:30 +0200 | [diff] [blame] | 110 | |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 111 | // test Select |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame] | 112 | VERIFY_IS_APPROX( (m1<m2).select(m1,m2), m1.cwiseMin(m2) ); |
| 113 | VERIFY_IS_APPROX( (m1>m2).select(m1,m2), m1.cwiseMax(m2) ); |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 114 | Scalar mid = (m1.cwiseAbs().minCoeff() + m1.cwiseAbs().maxCoeff())/Scalar(2); |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 115 | for (int j=0; j<cols; ++j) |
| 116 | for (int i=0; i<rows; ++i) |
| 117 | m3(i,j) = ei_abs(m1(i,j))<mid ? 0 : m1(i,j); |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 118 | VERIFY_IS_APPROX( (m1.abs()<ArrayType::Constant(rows,cols,mid)) |
| 119 | .select(ArrayType::Zero(rows,cols),m1), m3); |
Gael Guennebaud | e14aa8c | 2008-09-03 17:56:06 +0000 | [diff] [blame] | 120 | // shorter versions: |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 121 | VERIFY_IS_APPROX( (m1.abs()<ArrayType::Constant(rows,cols,mid)) |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 122 | .select(0,m1), m3); |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 123 | VERIFY_IS_APPROX( (m1.abs()>=ArrayType::Constant(rows,cols,mid)) |
Gael Guennebaud | 59dc1da | 2008-09-03 17:16:28 +0000 | [diff] [blame] | 124 | .select(m1,0), m3); |
Gael Guennebaud | e14aa8c | 2008-09-03 17:56:06 +0000 | [diff] [blame] | 125 | // even shorter version: |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame] | 126 | VERIFY_IS_APPROX( (m1.abs()<mid).select(0,m1), m3); |
Gael Guennebaud | 627595a | 2009-06-10 11:20:30 +0200 | [diff] [blame] | 127 | |
Gael Guennebaud | 56c7e16 | 2009-01-24 15:22:44 +0000 | [diff] [blame] | 128 | // count |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame] | 129 | VERIFY(((m1.abs()+1)>RealScalar(0.1)).count() == rows*cols); |
Benoit Jacob | aaaade4 | 2010-05-30 16:00:58 -0400 | [diff] [blame] | 130 | |
| 131 | typedef Array<typename ArrayType::Index, Dynamic, 1> ArrayOfIndices; |
Gael Guennebaud | 4ebb804 | 2010-06-02 09:45:57 +0200 | [diff] [blame] | 132 | |
Gael Guennebaud | 9f79558 | 2009-12-16 19:18:40 +0100 | [diff] [blame] | 133 | // TODO allows colwise/rowwise for array |
Benoit Jacob | aaaade4 | 2010-05-30 16:00:58 -0400 | [diff] [blame] | 134 | VERIFY_IS_APPROX(((m1.abs()+1)>RealScalar(0.1)).colwise().count(), ArrayOfIndices::Constant(cols,rows).transpose()); |
| 135 | VERIFY_IS_APPROX(((m1.abs()+1)>RealScalar(0.1)).rowwise().count(), ArrayOfIndices::Constant(rows, cols)); |
Benoit Jacob | e800999 | 2008-11-03 22:47:00 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 138 | template<typename ArrayType> void array_real(const ArrayType& m) |
Gael Guennebaud | 0ce5bc0 | 2010-01-27 23:23:59 +0100 | [diff] [blame] | 139 | { |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 140 | typedef typename ArrayType::Scalar Scalar; |
Gael Guennebaud | 0ce5bc0 | 2010-01-27 23:23:59 +0100 | [diff] [blame] | 141 | typedef typename NumTraits<Scalar>::Real RealScalar; |
| 142 | |
| 143 | int rows = m.rows(); |
| 144 | int cols = m.cols(); |
| 145 | |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 146 | ArrayType m1 = ArrayType::Random(rows, cols), |
| 147 | m2 = ArrayType::Random(rows, cols), |
Gael Guennebaud | 0ce5bc0 | 2010-01-27 23:23:59 +0100 | [diff] [blame] | 148 | m3(rows, cols); |
| 149 | |
| 150 | VERIFY_IS_APPROX(m1.sin(), std::sin(m1)); |
| 151 | VERIFY_IS_APPROX(m1.sin(), ei_sin(m1)); |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 152 | VERIFY_IS_APPROX(m1.cos(), std::cos(m1)); |
Gael Guennebaud | 0ce5bc0 | 2010-01-27 23:23:59 +0100 | [diff] [blame] | 153 | VERIFY_IS_APPROX(m1.cos(), ei_cos(m1)); |
Gael Guennebaud | 4ebb804 | 2010-06-02 09:45:57 +0200 | [diff] [blame] | 154 | |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 155 | VERIFY_IS_APPROX(ei_cos(m1+RealScalar(3)*m2), ei_cos((m1+RealScalar(3)*m2).eval())); |
| 156 | VERIFY_IS_APPROX(std::cos(m1+RealScalar(3)*m2), std::cos((m1+RealScalar(3)*m2).eval())); |
Gael Guennebaud | 4ebb804 | 2010-06-02 09:45:57 +0200 | [diff] [blame] | 157 | |
Gael Guennebaud | 0ce5bc0 | 2010-01-27 23:23:59 +0100 | [diff] [blame] | 158 | VERIFY_IS_APPROX(m1.abs().sqrt(), std::sqrt(std::abs(m1))); |
| 159 | VERIFY_IS_APPROX(m1.abs().sqrt(), ei_sqrt(ei_abs(m1))); |
Benoit Jacob | 5d63d2c | 2010-04-28 22:42:34 -0400 | [diff] [blame] | 160 | VERIFY_IS_APPROX(m1.abs(), ei_sqrt(ei_abs2(m1))); |
| 161 | |
| 162 | VERIFY_IS_APPROX(ei_abs2(ei_real(m1)) + ei_abs2(ei_imag(m1)), ei_abs2(m1)); |
| 163 | VERIFY_IS_APPROX(ei_abs2(std::real(m1)) + ei_abs2(std::imag(m1)), ei_abs2(m1)); |
| 164 | if(!NumTraits<Scalar>::IsComplex) |
| 165 | VERIFY_IS_APPROX(ei_real(m1), m1); |
Gael Guennebaud | 4ebb804 | 2010-06-02 09:45:57 +0200 | [diff] [blame] | 166 | |
Gael Guennebaud | 0ce5bc0 | 2010-01-27 23:23:59 +0100 | [diff] [blame] | 167 | VERIFY_IS_APPROX(m1.abs().log(), std::log(std::abs(m1))); |
| 168 | VERIFY_IS_APPROX(m1.abs().log(), ei_log(ei_abs(m1))); |
Gael Guennebaud | 4ebb804 | 2010-06-02 09:45:57 +0200 | [diff] [blame] | 169 | |
Gael Guennebaud | 0ce5bc0 | 2010-01-27 23:23:59 +0100 | [diff] [blame] | 170 | VERIFY_IS_APPROX(m1.exp(), std::exp(m1)); |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 171 | VERIFY_IS_APPROX(m1.exp() * m2.exp(), std::exp(m1+m2)); |
Gael Guennebaud | 0ce5bc0 | 2010-01-27 23:23:59 +0100 | [diff] [blame] | 172 | VERIFY_IS_APPROX(m1.exp(), ei_exp(m1)); |
Benoit Jacob | e277586 | 2010-04-28 18:51:38 -0400 | [diff] [blame] | 173 | VERIFY_IS_APPROX(m1.exp() / m2.exp(), std::exp(m1-m2)); |
Gael Guennebaud | 575ac54 | 2010-06-19 23:17:07 +0200 | [diff] [blame] | 174 | |
| 175 | VERIFY_IS_APPROX(m1.pow(2), m1.square()); |
| 176 | VERIFY_IS_APPROX(std::pow(m1,2), m1.square()); |
| 177 | m3 = m1.abs(); |
| 178 | VERIFY_IS_APPROX(m3.pow(RealScalar(0.5)), m3.sqrt()); |
| 179 | VERIFY_IS_APPROX(std::pow(m3,RealScalar(0.5)), m3.sqrt()); |
Gael Guennebaud | 0ce5bc0 | 2010-01-27 23:23:59 +0100 | [diff] [blame] | 180 | } |
| 181 | |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 182 | void test_array() |
| 183 | { |
| 184 | for(int i = 0; i < g_repeat; i++) { |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame] | 185 | CALL_SUBTEST_1( array(Array<float, 1, 1>()) ); |
| 186 | CALL_SUBTEST_2( array(Array22f()) ); |
| 187 | CALL_SUBTEST_3( array(Array44d()) ); |
| 188 | CALL_SUBTEST_4( array(ArrayXXcf(3, 3)) ); |
| 189 | CALL_SUBTEST_5( array(ArrayXXf(8, 12)) ); |
| 190 | CALL_SUBTEST_6( array(ArrayXXi(8, 12)) ); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 191 | } |
| 192 | for(int i = 0; i < g_repeat; i++) { |
Gael Guennebaud | c70d542 | 2010-01-18 22:54:20 +0100 | [diff] [blame] | 193 | CALL_SUBTEST_1( comparisons(Array<float, 1, 1>()) ); |
| 194 | CALL_SUBTEST_2( comparisons(Array22f()) ); |
| 195 | CALL_SUBTEST_3( comparisons(Array44d()) ); |
| 196 | CALL_SUBTEST_5( comparisons(ArrayXXf(8, 12)) ); |
| 197 | CALL_SUBTEST_6( comparisons(ArrayXXi(8, 12)) ); |
Benoit Jacob | e800999 | 2008-11-03 22:47:00 +0000 | [diff] [blame] | 198 | } |
Gael Guennebaud | 0ce5bc0 | 2010-01-27 23:23:59 +0100 | [diff] [blame] | 199 | for(int i = 0; i < g_repeat; i++) { |
| 200 | CALL_SUBTEST_1( array_real(Array<float, 1, 1>()) ); |
| 201 | CALL_SUBTEST_2( array_real(Array22f()) ); |
| 202 | CALL_SUBTEST_3( array_real(Array44d()) ); |
| 203 | CALL_SUBTEST_5( array_real(ArrayXXf(8, 12)) ); |
| 204 | } |
Benoit Jacob | 5d63d2c | 2010-04-28 22:42:34 -0400 | [diff] [blame] | 205 | |
| 206 | VERIFY((ei_is_same_type< ei_global_math_functions_filtering_base<int>::type, int >::ret)); |
| 207 | VERIFY((ei_is_same_type< ei_global_math_functions_filtering_base<float>::type, float >::ret)); |
| 208 | VERIFY((ei_is_same_type< ei_global_math_functions_filtering_base<Array2i>::type, ArrayBase<Array2i> >::ret)); |
| 209 | typedef CwiseUnaryOp<ei_scalar_sum_op<double>, ArrayXd > Xpr; |
| 210 | VERIFY((ei_is_same_type< ei_global_math_functions_filtering_base<Xpr>::type, |
| 211 | ArrayBase<Xpr> |
| 212 | >::ret)); |
Gael Guennebaud | 8cef541 | 2008-06-21 17:28:07 +0000 | [diff] [blame] | 213 | } |