blob: 5ca51b74e141642fc49269842927e970e8a0abc5 [file] [log] [blame]
Benoit Jacoba2f8d4b2008-02-29 14:35:14 +00001// 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 Guennebaud46885d32008-03-03 11:02:52 +00004// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
Benoit Jacoba2f8d4b2008-02-29 14:35:14 +00005// 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 Guennebaud46885d32008-03-03 11:02:52 +00009// License as published by the Free Software Foundation; either
Benoit Jacoba2f8d4b2008-02-29 14:35:14 +000010// 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 Guennebaud46885d32008-03-03 11:02:52 +000014// published by the Free Software Foundation; either version 2 of
Benoit Jacoba2f8d4b2008-02-29 14:35:14 +000015// 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 Guennebaud46885d32008-03-03 11:02:52 +000022// You should have received a copy of the GNU Lesser General Public
Benoit Jacoba2f8d4b2008-02-29 14:35:14 +000023// 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
31namespace Eigen {
32
Gael Guennebaud25568922008-03-03 10:52:44 +000033struct AddIfNull {
Gael Guennebaud138aad02008-03-06 11:36:27 +000034 template<typename Scalar> Scalar operator() (const Scalar a, const Scalar b) const {return a<=1e-3 ? b : a;}
Benoit Jacoba2f8d4b2008-02-29 14:35:14 +000035};
36
37template<typename MatrixType> void cwiseops(const MatrixType& m)
38{
39 typedef typename MatrixType::Scalar Scalar;
Benoit Jacob2ee68a02008-03-12 17:17:36 +000040 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
Benoit Jacoba2f8d4b2008-02-29 14:35:14 +000041
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 Jacob2ee68a02008-03-12 17:17:36 +000050 identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
Benoit Jacoba2f8d4b2008-02-29 14:35:14 +000051 ::identity(rows, rows),
Benoit Jacob2ee68a02008-03-12 17:17:36 +000052 square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
Benoit Jacoba2f8d4b2008-02-29 14:35:14 +000053 ::random(rows, rows);
54 VectorType v1 = VectorType::random(rows),
55 v2 = VectorType::random(rows),
56 vzero = VectorType::zero(rows);
57
Gael Guennebaud25568922008-03-03 10:52:44 +000058 m2 = m2.template cwise<AddIfNull>(mones);
Benoit Jacoba2f8d4b2008-02-29 14:35:14 +000059
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
71void EigenTest::testCwiseops()
72{
Gael Guennebaud25568922008-03-03 10:52:44 +000073 for(int i = 0; i < m_repeat ; i++) {
74 cwiseops(Matrix<float, 1, 1>());
75 cwiseops(Matrix4d());
76 cwiseops(MatrixXf(3, 3));
Benoit Jacoba2f8d4b2008-02-29 14:35:14 +000077 cwiseops(MatrixXi(8, 12));
Gael Guennebaud25568922008-03-03 10:52:44 +000078 cwiseops(MatrixXd(20, 20));
Benoit Jacoba2f8d4b2008-02-29 14:35:14 +000079 }
80}
81
82} // namespace Eigen