1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.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 #define EIGEN2_SUPPORT
12 #define EIGEN_NO_STATIC_ASSERT
13 #include "main.h"
14 #include <functional>
15
16 #ifdef min
17 #undef min
18 #endif
19
20 #ifdef max
21 #undef max
22 #endif
23
24 using namespace std;
25
26 template<typename Scalar> struct AddIfNull {
operator ()AddIfNull27 const Scalar operator() (const Scalar a, const Scalar b) const {return a<=1e-3 ? b : a;}
28 enum { Cost = NumTraits<Scalar>::AddCost };
29 };
30
cwiseops(const MatrixType & m)31 template<typename MatrixType> void cwiseops(const MatrixType& m)
32 {
33 typedef typename MatrixType::Index Index;
34 typedef typename MatrixType::Scalar Scalar;
35 typedef typename NumTraits<Scalar>::Real RealScalar;
36 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
37
38 Index rows = m.rows();
39 Index cols = m.cols();
40
41 MatrixType m1 = MatrixType::Random(rows, cols),
42 m2 = MatrixType::Random(rows, cols),
43 m3(rows, cols),
44 m4(rows, cols),
45 mzero = MatrixType::Zero(rows, cols),
46 mones = MatrixType::Ones(rows, cols),
47 identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
48 ::Identity(rows, rows);
49 VectorType vzero = VectorType::Zero(rows),
50 vones = VectorType::Ones(rows),
51 v3(rows);
52
53 Index r = internal::random<Index>(0, rows-1),
54 c = internal::random<Index>(0, cols-1);
55
56 Scalar s1 = internal::random<Scalar>();
57
58 // test Zero, Ones, Constant, and the set* variants
59 m3 = MatrixType::Constant(rows, cols, s1);
60 for (int j=0; j<cols; ++j)
61 for (int i=0; i<rows; ++i)
62 {
63 VERIFY_IS_APPROX(mzero(i,j), Scalar(0));
64 VERIFY_IS_APPROX(mones(i,j), Scalar(1));
65 VERIFY_IS_APPROX(m3(i,j), s1);
66 }
67 VERIFY(mzero.isZero());
68 VERIFY(mones.isOnes());
69 VERIFY(m3.isConstant(s1));
70 VERIFY(identity.isIdentity());
71 VERIFY_IS_APPROX(m4.setConstant(s1), m3);
72 VERIFY_IS_APPROX(m4.setConstant(rows,cols,s1), m3);
73 VERIFY_IS_APPROX(m4.setZero(), mzero);
74 VERIFY_IS_APPROX(m4.setZero(rows,cols), mzero);
75 VERIFY_IS_APPROX(m4.setOnes(), mones);
76 VERIFY_IS_APPROX(m4.setOnes(rows,cols), mones);
77 m4.fill(s1);
78 VERIFY_IS_APPROX(m4, m3);
79
80 VERIFY_IS_APPROX(v3.setConstant(rows, s1), VectorType::Constant(rows,s1));
81 VERIFY_IS_APPROX(v3.setZero(rows), vzero);
82 VERIFY_IS_APPROX(v3.setOnes(rows), vones);
83
84 m2 = m2.template binaryExpr<AddIfNull<Scalar> >(mones);
85
86 VERIFY_IS_APPROX(m1.cwise().pow(2), m1.cwise().abs2());
87 VERIFY_IS_APPROX(m1.cwise().pow(2), m1.cwise().square());
88 VERIFY_IS_APPROX(m1.cwise().pow(3), m1.cwise().cube());
89
90 VERIFY_IS_APPROX(m1 + mones, m1.cwise()+Scalar(1));
91 VERIFY_IS_APPROX(m1 - mones, m1.cwise()-Scalar(1));
92 m3 = m1; m3.cwise() += 1;
93 VERIFY_IS_APPROX(m1 + mones, m3);
94 m3 = m1; m3.cwise() -= 1;
95 VERIFY_IS_APPROX(m1 - mones, m3);
96
97 VERIFY_IS_APPROX(m2, m2.cwise() * mones);
98 VERIFY_IS_APPROX(m1.cwise() * m2, m2.cwise() * m1);
99 m3 = m1;
100 m3.cwise() *= m2;
101 VERIFY_IS_APPROX(m3, m1.cwise() * m2);
102
103 VERIFY_IS_APPROX(mones, m2.cwise()/m2);
104 if(!NumTraits<Scalar>::IsInteger)
105 {
106 VERIFY_IS_APPROX(m1.cwise() / m2, m1.cwise() * (m2.cwise().inverse()));
107 m3 = m1.cwise().abs().cwise().sqrt();
108 VERIFY_IS_APPROX(m3.cwise().square(), m1.cwise().abs());
109 VERIFY_IS_APPROX(m1.cwise().square().cwise().sqrt(), m1.cwise().abs());
110 VERIFY_IS_APPROX(m1.cwise().abs().cwise().log().cwise().exp() , m1.cwise().abs());
111
112 VERIFY_IS_APPROX(m1.cwise().pow(2), m1.cwise().square());
113 m3 = (m1.cwise().abs().cwise()<=RealScalar(0.01)).select(mones,m1);
114 VERIFY_IS_APPROX(m3.cwise().pow(-1), m3.cwise().inverse());
115 m3 = m1.cwise().abs();
116 VERIFY_IS_APPROX(m3.cwise().pow(RealScalar(0.5)), m3.cwise().sqrt());
117
118 // VERIFY_IS_APPROX(m1.cwise().tan(), m1.cwise().sin().cwise() / m1.cwise().cos());
119 VERIFY_IS_APPROX(mones, m1.cwise().sin().cwise().square() + m1.cwise().cos().cwise().square());
120 m3 = m1;
121 m3.cwise() /= m2;
122 VERIFY_IS_APPROX(m3, m1.cwise() / m2);
123 }
124
125 // check min
126 VERIFY_IS_APPROX( m1.cwise().min(m2), m2.cwise().min(m1) );
127 VERIFY_IS_APPROX( m1.cwise().min(m1+mones), m1 );
128 VERIFY_IS_APPROX( m1.cwise().min(m1-mones), m1-mones );
129
130 // check max
131 VERIFY_IS_APPROX( m1.cwise().max(m2), m2.cwise().max(m1) );
132 VERIFY_IS_APPROX( m1.cwise().max(m1-mones), m1 );
133 VERIFY_IS_APPROX( m1.cwise().max(m1+mones), m1+mones );
134
135 VERIFY( (m1.cwise() == m1).all() );
136 VERIFY( (m1.cwise() != m2).any() );
137 VERIFY(!(m1.cwise() == (m1+mones)).any() );
138 if (rows*cols>1)
139 {
140 m3 = m1;
141 m3(r,c) += 1;
142 VERIFY( (m1.cwise() == m3).any() );
143 VERIFY( !(m1.cwise() == m3).all() );
144 }
145 VERIFY( (m1.cwise().min(m2).cwise() <= m2).all() );
146 VERIFY( (m1.cwise().max(m2).cwise() >= m2).all() );
147 VERIFY( (m1.cwise().min(m2).cwise() < (m1+mones)).all() );
148 VERIFY( (m1.cwise().max(m2).cwise() > (m1-mones)).all() );
149
150 VERIFY( (m1.cwise()<m1.unaryExpr(bind2nd(plus<Scalar>(), Scalar(1)))).all() );
151 VERIFY( !(m1.cwise()<m1.unaryExpr(bind2nd(minus<Scalar>(), Scalar(1)))).all() );
152 VERIFY( !(m1.cwise()>m1.unaryExpr(bind2nd(plus<Scalar>(), Scalar(1)))).any() );
153 }
154
test_cwiseop()155 void test_cwiseop()
156 {
157 for(int i = 0; i < g_repeat ; i++) {
158 CALL_SUBTEST_1( cwiseops(Matrix<float, 1, 1>()) );
159 CALL_SUBTEST_2( cwiseops(Matrix4d()) );
160 CALL_SUBTEST_3( cwiseops(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
161 CALL_SUBTEST_4( cwiseops(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
162 CALL_SUBTEST_5( cwiseops(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
163 CALL_SUBTEST_6( cwiseops(MatrixXd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
164 }
165 }
166