Benoit Jacob | a2f8d4b | 2008-02-29 14:35:14 +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 | // |
Gael Guennebaud | 46885d3 | 2008-03-03 11:02:52 +0000 | [diff] [blame] | 4 | // Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr> |
Benoit Jacob | a2f8d4b | 2008-02-29 14:35:14 +0000 | [diff] [blame] | 5 | // Copyright (C) 2006-2008 Benoit Jacob <jacob@math.jussieu.fr> |
| 6 | // |
| 7 | // Eigen is free software; you can redistribute it and/or |
| 8 | // modify it under the terms of the GNU Lesser General Public |
Gael Guennebaud | 46885d3 | 2008-03-03 11:02:52 +0000 | [diff] [blame] | 9 | // License as published by the Free Software Foundation; either |
Benoit Jacob | a2f8d4b | 2008-02-29 14:35:14 +0000 | [diff] [blame] | 10 | // version 3 of the License, or (at your option) any later version. |
| 11 | // |
| 12 | // Alternatively, you can redistribute it and/or |
| 13 | // modify it under the terms of the GNU General Public License as |
Gael Guennebaud | 46885d3 | 2008-03-03 11:02:52 +0000 | [diff] [blame] | 14 | // published by the Free Software Foundation; either version 2 of |
Benoit Jacob | a2f8d4b | 2008-02-29 14:35:14 +0000 | [diff] [blame] | 15 | // the License, or (at your option) any later version. |
| 16 | // |
| 17 | // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY |
| 18 | // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 | // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the |
| 20 | // GNU General Public License for more details. |
| 21 | // |
Gael Guennebaud | 46885d3 | 2008-03-03 11:02:52 +0000 | [diff] [blame] | 22 | // You should have received a copy of the GNU Lesser General Public |
Benoit Jacob | a2f8d4b | 2008-02-29 14:35:14 +0000 | [diff] [blame] | 23 | // License and a copy of the GNU General Public License along with |
| 24 | // Eigen. If not, see <http://www.gnu.org/licenses/>. |
| 25 | |
| 26 | #include "main.h" |
| 27 | #include <iostream> |
| 28 | #include <cmath> |
| 29 | #include <cstdlib> |
| 30 | |
| 31 | namespace Eigen { |
| 32 | |
Gael Guennebaud | 2556892 | 2008-03-03 10:52:44 +0000 | [diff] [blame] | 33 | struct AddIfNull { |
Gael Guennebaud | 138aad0 | 2008-03-06 11:36:27 +0000 | [diff] [blame] | 34 | template<typename Scalar> Scalar operator() (const Scalar a, const Scalar b) const {return a<=1e-3 ? b : a;} |
Benoit Jacob | a2f8d4b | 2008-02-29 14:35:14 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | template<typename MatrixType> void cwiseops(const MatrixType& m) |
| 38 | { |
| 39 | typedef typename MatrixType::Scalar Scalar; |
Benoit Jacob | 2ee68a0 | 2008-03-12 17:17:36 +0000 | [diff] [blame^] | 40 | typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType; |
Benoit Jacob | a2f8d4b | 2008-02-29 14:35:14 +0000 | [diff] [blame] | 41 | |
| 42 | int rows = m.rows(); |
| 43 | int cols = m.cols(); |
| 44 | |
| 45 | MatrixType m1 = MatrixType::random(rows, cols), |
| 46 | m2 = MatrixType::random(rows, cols), |
| 47 | m3(rows, cols), |
| 48 | mzero = MatrixType::zero(rows, cols), |
| 49 | mones = MatrixType::ones(rows, cols), |
Benoit Jacob | 2ee68a0 | 2008-03-12 17:17:36 +0000 | [diff] [blame^] | 50 | identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> |
Benoit Jacob | a2f8d4b | 2008-02-29 14:35:14 +0000 | [diff] [blame] | 51 | ::identity(rows, rows), |
Benoit Jacob | 2ee68a0 | 2008-03-12 17:17:36 +0000 | [diff] [blame^] | 52 | square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> |
Benoit Jacob | a2f8d4b | 2008-02-29 14:35:14 +0000 | [diff] [blame] | 53 | ::random(rows, rows); |
| 54 | VectorType v1 = VectorType::random(rows), |
| 55 | v2 = VectorType::random(rows), |
| 56 | vzero = VectorType::zero(rows); |
| 57 | |
Gael Guennebaud | 2556892 | 2008-03-03 10:52:44 +0000 | [diff] [blame] | 58 | m2 = m2.template cwise<AddIfNull>(mones); |
Benoit Jacob | a2f8d4b | 2008-02-29 14:35:14 +0000 | [diff] [blame] | 59 | |
| 60 | VERIFY_IS_APPROX( mzero, m1-m1); |
| 61 | VERIFY_IS_APPROX( m2, m1+m2-m1); |
| 62 | VERIFY_IS_APPROX( mones, m2.cwiseQuotient(m2)); |
| 63 | VERIFY_IS_APPROX( m1.cwiseProduct(m2), m2.cwiseProduct(m1)); |
| 64 | //VERIFY_IS_APPROX( m1, m2.cwiseProduct(m1).cwiseQuotient(m2)); |
| 65 | |
| 66 | // VERIFY_IS_APPROX( cwiseMin(m1,m2), cwiseMin(m2,m1) ); |
| 67 | // VERIFY_IS_APPROX( cwiseMin(m1,m1+mones), m1 ); |
| 68 | // VERIFY_IS_APPROX( cwiseMin(m1,m1-mones), m1-mones ); |
| 69 | } |
| 70 | |
| 71 | void EigenTest::testCwiseops() |
| 72 | { |
Gael Guennebaud | 2556892 | 2008-03-03 10:52:44 +0000 | [diff] [blame] | 73 | for(int i = 0; i < m_repeat ; i++) { |
| 74 | cwiseops(Matrix<float, 1, 1>()); |
| 75 | cwiseops(Matrix4d()); |
| 76 | cwiseops(MatrixXf(3, 3)); |
Benoit Jacob | a2f8d4b | 2008-02-29 14:35:14 +0000 | [diff] [blame] | 77 | cwiseops(MatrixXi(8, 12)); |
Gael Guennebaud | 2556892 | 2008-03-03 10:52:44 +0000 | [diff] [blame] | 78 | cwiseops(MatrixXd(20, 20)); |
Benoit Jacob | a2f8d4b | 2008-02-29 14:35:14 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | } // namespace Eigen |