blob: b8dac7884d54357ba961ea95fe89ccb3237775a9 [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//
4// Copyright (C) 2006-2008 Benoit Jacob <jacob@math.jussieu.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 <iostream>
27#include <cmath>
28#include <cstdlib>
29
30namespace Eigen {
31
Gael Guennebaud25568922008-03-03 10:52:44 +000032struct AddIfNull {
33 template<typename Scalar> static Scalar op(const Scalar a, const Scalar b) {return a<=1e-3 ? b : a;}
Benoit Jacoba2f8d4b2008-02-29 14:35:14 +000034};
35
36template<typename MatrixType> void cwiseops(const MatrixType& m)
37{
38 typedef typename MatrixType::Scalar Scalar;
39 typedef Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, 1> VectorType;
40
41 int rows = m.rows();
42 int cols = m.cols();
43
44 MatrixType m1 = MatrixType::random(rows, cols),
45 m2 = MatrixType::random(rows, cols),
46 m3(rows, cols),
47 mzero = MatrixType::zero(rows, cols),
48 mones = MatrixType::ones(rows, cols),
49 identity = Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, MatrixType::Traits::RowsAtCompileTime>
50 ::identity(rows, rows),
51 square = Matrix<Scalar, MatrixType::Traits::RowsAtCompileTime, MatrixType::Traits::RowsAtCompileTime>
52 ::random(rows, rows);
53 VectorType v1 = VectorType::random(rows),
54 v2 = VectorType::random(rows),
55 vzero = VectorType::zero(rows);
56
Gael Guennebaud25568922008-03-03 10:52:44 +000057 m2 = m2.template cwise<AddIfNull>(mones);
Benoit Jacoba2f8d4b2008-02-29 14:35:14 +000058
59 VERIFY_IS_APPROX( mzero, m1-m1);
60 VERIFY_IS_APPROX( m2, m1+m2-m1);
61 VERIFY_IS_APPROX( mones, m2.cwiseQuotient(m2));
62 VERIFY_IS_APPROX( m1.cwiseProduct(m2), m2.cwiseProduct(m1));
63 //VERIFY_IS_APPROX( m1, m2.cwiseProduct(m1).cwiseQuotient(m2));
64
65// VERIFY_IS_APPROX( cwiseMin(m1,m2), cwiseMin(m2,m1) );
66// VERIFY_IS_APPROX( cwiseMin(m1,m1+mones), m1 );
67// VERIFY_IS_APPROX( cwiseMin(m1,m1-mones), m1-mones );
68}
69
70void EigenTest::testCwiseops()
71{
Gael Guennebaud25568922008-03-03 10:52:44 +000072 for(int i = 0; i < m_repeat ; i++) {
73 cwiseops(Matrix<float, 1, 1>());
74 cwiseops(Matrix4d());
75 cwiseops(MatrixXf(3, 3));
Benoit Jacoba2f8d4b2008-02-29 14:35:14 +000076 cwiseops(MatrixXi(8, 12));
Gael Guennebaud25568922008-03-03 10:52:44 +000077 cwiseops(MatrixXd(20, 20));
Benoit Jacoba2f8d4b2008-02-29 14:35:14 +000078 }
79}
80
81} // namespace Eigen