source: pacpussensors/trunk/Vislab/lib3dv-1.2.0/lib3dv/eigen/test/eigen2/eigen2_cwiseop.cpp

Last change on this file was 136, checked in by ldecherf, 8 years ago

Doc

File size: 5.6 KB
Line 
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// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10
11#include "main.h"
12#include <functional>
13#include <Eigen/Array>
14
15using namespace std;
16
17template<typename Scalar> struct AddIfNull {
18 const Scalar operator() (const Scalar a, const Scalar b) const {return a<=1e-3 ? b : a;}
19 enum { Cost = NumTraits<Scalar>::AddCost };
20};
21
22template<typename MatrixType> void cwiseops(const MatrixType& m)
23{
24 typedef typename MatrixType::Scalar Scalar;
25 typedef typename NumTraits<Scalar>::Real RealScalar;
26 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
27
28 int rows = m.rows();
29 int cols = m.cols();
30
31 MatrixType m1 = MatrixType::Random(rows, cols),
32 m2 = MatrixType::Random(rows, cols),
33 m3(rows, cols),
34 m4(rows, cols),
35 mzero = MatrixType::Zero(rows, cols),
36 mones = MatrixType::Ones(rows, cols),
37 identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
38 ::Identity(rows, rows);
39 VectorType vzero = VectorType::Zero(rows),
40 vones = VectorType::Ones(rows),
41 v3(rows);
42
43 int r = ei_random<int>(0, rows-1),
44 c = ei_random<int>(0, cols-1);
45
46 Scalar s1 = ei_random<Scalar>();
47
48 // test Zero, Ones, Constant, and the set* variants
49 m3 = MatrixType::Constant(rows, cols, s1);
50 for (int j=0; j<cols; ++j)
51 for (int i=0; i<rows; ++i)
52 {
53 VERIFY_IS_APPROX(mzero(i,j), Scalar(0));
54 VERIFY_IS_APPROX(mones(i,j), Scalar(1));
55 VERIFY_IS_APPROX(m3(i,j), s1);
56 }
57 VERIFY(mzero.isZero());
58 VERIFY(mones.isOnes());
59 VERIFY(m3.isConstant(s1));
60 VERIFY(identity.isIdentity());
61 VERIFY_IS_APPROX(m4.setConstant(s1), m3);
62 VERIFY_IS_APPROX(m4.setConstant(rows,cols,s1), m3);
63 VERIFY_IS_APPROX(m4.setZero(), mzero);
64 VERIFY_IS_APPROX(m4.setZero(rows,cols), mzero);
65 VERIFY_IS_APPROX(m4.setOnes(), mones);
66 VERIFY_IS_APPROX(m4.setOnes(rows,cols), mones);
67 m4.fill(s1);
68 VERIFY_IS_APPROX(m4, m3);
69
70 VERIFY_IS_APPROX(v3.setConstant(rows, s1), VectorType::Constant(rows,s1));
71 VERIFY_IS_APPROX(v3.setZero(rows), vzero);
72 VERIFY_IS_APPROX(v3.setOnes(rows), vones);
73
74 m2 = m2.template binaryExpr<AddIfNull<Scalar> >(mones);
75
76 VERIFY_IS_APPROX(m1.cwise().pow(2), m1.cwise().abs2());
77 VERIFY_IS_APPROX(m1.cwise().pow(2), m1.cwise().square());
78 VERIFY_IS_APPROX(m1.cwise().pow(3), m1.cwise().cube());
79
80 VERIFY_IS_APPROX(m1 + mones, m1.cwise()+Scalar(1));
81 VERIFY_IS_APPROX(m1 - mones, m1.cwise()-Scalar(1));
82 m3 = m1; m3.cwise() += 1;
83 VERIFY_IS_APPROX(m1 + mones, m3);
84 m3 = m1; m3.cwise() -= 1;
85 VERIFY_IS_APPROX(m1 - mones, m3);
86
87 VERIFY_IS_APPROX(m2, m2.cwise() * mones);
88 VERIFY_IS_APPROX(m1.cwise() * m2, m2.cwise() * m1);
89 m3 = m1;
90 m3.cwise() *= m2;
91 VERIFY_IS_APPROX(m3, m1.cwise() * m2);
92
93 VERIFY_IS_APPROX(mones, m2.cwise()/m2);
94 if(NumTraits<Scalar>::HasFloatingPoint)
95 {
96 VERIFY_IS_APPROX(m1.cwise() / m2, m1.cwise() * (m2.cwise().inverse()));
97 m3 = m1.cwise().abs().cwise().sqrt();
98 VERIFY_IS_APPROX(m3.cwise().square(), m1.cwise().abs());
99 VERIFY_IS_APPROX(m1.cwise().square().cwise().sqrt(), m1.cwise().abs());
100 VERIFY_IS_APPROX(m1.cwise().abs().cwise().log().cwise().exp() , m1.cwise().abs());
101
102 VERIFY_IS_APPROX(m1.cwise().pow(2), m1.cwise().square());
103 m3 = (m1.cwise().abs().cwise()<=RealScalar(0.01)).select(mones,m1);
104 VERIFY_IS_APPROX(m3.cwise().pow(-1), m3.cwise().inverse());
105 m3 = m1.cwise().abs();
106 VERIFY_IS_APPROX(m3.cwise().pow(RealScalar(0.5)), m3.cwise().sqrt());
107
108// VERIFY_IS_APPROX(m1.cwise().tan(), m1.cwise().sin().cwise() / m1.cwise().cos());
109 VERIFY_IS_APPROX(mones, m1.cwise().sin().cwise().square() + m1.cwise().cos().cwise().square());
110 m3 = m1;
111 m3.cwise() /= m2;
112 VERIFY_IS_APPROX(m3, m1.cwise() / m2);
113 }
114
115 // check min
116 VERIFY_IS_APPROX( m1.cwise().min(m2), m2.cwise().min(m1) );
117 VERIFY_IS_APPROX( m1.cwise().min(m1+mones), m1 );
118 VERIFY_IS_APPROX( m1.cwise().min(m1-mones), m1-mones );
119
120 // check max
121 VERIFY_IS_APPROX( m1.cwise().max(m2), m2.cwise().max(m1) );
122 VERIFY_IS_APPROX( m1.cwise().max(m1-mones), m1 );
123 VERIFY_IS_APPROX( m1.cwise().max(m1+mones), m1+mones );
124
125 VERIFY( (m1.cwise() == m1).all() );
126 VERIFY( (m1.cwise() != m2).any() );
127 VERIFY(!(m1.cwise() == (m1+mones)).any() );
128 if (rows*cols>1)
129 {
130 m3 = m1;
131 m3(r,c) += 1;
132 VERIFY( (m1.cwise() == m3).any() );
133 VERIFY( !(m1.cwise() == m3).all() );
134 }
135 VERIFY( (m1.cwise().min(m2).cwise() <= m2).all() );
136 VERIFY( (m1.cwise().max(m2).cwise() >= m2).all() );
137 VERIFY( (m1.cwise().min(m2).cwise() < (m1+mones)).all() );
138 VERIFY( (m1.cwise().max(m2).cwise() > (m1-mones)).all() );
139
140 VERIFY( (m1.cwise()<m1.unaryExpr(bind2nd(plus<Scalar>(), Scalar(1)))).all() );
141 VERIFY( !(m1.cwise()<m1.unaryExpr(bind2nd(minus<Scalar>(), Scalar(1)))).all() );
142 VERIFY( !(m1.cwise()>m1.unaryExpr(bind2nd(plus<Scalar>(), Scalar(1)))).any() );
143}
144
145void test_eigen2_cwiseop()
146{
147 for(int i = 0; i < g_repeat ; i++) {
148 CALL_SUBTEST_1( cwiseops(Matrix<float, 1, 1>()) );
149 CALL_SUBTEST_2( cwiseops(Matrix4d()) );
150 CALL_SUBTEST_3( cwiseops(MatrixXf(3, 3)) );
151 CALL_SUBTEST_3( cwiseops(MatrixXf(22, 22)) );
152 CALL_SUBTEST_4( cwiseops(MatrixXi(8, 12)) );
153 CALL_SUBTEST_5( cwiseops(MatrixXd(20, 20)) );
154 }
155}
Note: See TracBrowser for help on using the repository browser.