• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2015 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 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 // work around "uninitialized" warnings and give that option some testing
12 #define EIGEN_INITIALIZE_MATRICES_BY_ZERO
13 
14 #ifndef EIGEN_NO_STATIC_ASSERT
15 #define EIGEN_NO_STATIC_ASSERT // turn static asserts into runtime asserts in order to check them
16 #endif
17 
18 #if defined(EIGEN_TEST_PART_1) || defined(EIGEN_TEST_PART_2) || defined(EIGEN_TEST_PART_3)
19 
20 #ifndef EIGEN_DONT_VECTORIZE
21 #define EIGEN_DONT_VECTORIZE
22 #endif
23 
24 #endif
25 
26 static bool g_called;
27 #define EIGEN_SCALAR_BINARY_OP_PLUGIN { g_called |= (!internal::is_same<LhsScalar,RhsScalar>::value); }
28 
29 #include "main.h"
30 
31 using namespace std;
32 
33 #define VERIFY_MIX_SCALAR(XPR,REF) \
34   g_called = false; \
35   VERIFY_IS_APPROX(XPR,REF); \
36   VERIFY( g_called && #XPR" not properly optimized");
37 
mixingtypes(int size=SizeAtCompileType)38 template<int SizeAtCompileType> void mixingtypes(int size = SizeAtCompileType)
39 {
40   typedef std::complex<float>   CF;
41   typedef std::complex<double>  CD;
42   typedef Matrix<float, SizeAtCompileType, SizeAtCompileType> Mat_f;
43   typedef Matrix<double, SizeAtCompileType, SizeAtCompileType> Mat_d;
44   typedef Matrix<std::complex<float>, SizeAtCompileType, SizeAtCompileType> Mat_cf;
45   typedef Matrix<std::complex<double>, SizeAtCompileType, SizeAtCompileType> Mat_cd;
46   typedef Matrix<float, SizeAtCompileType, 1> Vec_f;
47   typedef Matrix<double, SizeAtCompileType, 1> Vec_d;
48   typedef Matrix<std::complex<float>, SizeAtCompileType, 1> Vec_cf;
49   typedef Matrix<std::complex<double>, SizeAtCompileType, 1> Vec_cd;
50 
51   Mat_f mf    = Mat_f::Random(size,size);
52   Mat_d md    = mf.template cast<double>();
53   //Mat_d rd    = md;
54   Mat_cf mcf  = Mat_cf::Random(size,size);
55   Mat_cd mcd  = mcf.template cast<complex<double> >();
56   Mat_cd rcd = mcd;
57   Vec_f vf    = Vec_f::Random(size,1);
58   Vec_d vd    = vf.template cast<double>();
59   Vec_cf vcf  = Vec_cf::Random(size,1);
60   Vec_cd vcd  = vcf.template cast<complex<double> >();
61   float           sf  = internal::random<float>();
62   double          sd  = internal::random<double>();
63   complex<float>  scf = internal::random<complex<float> >();
64   complex<double> scd = internal::random<complex<double> >();
65 
66   mf+mf;
67 
68   float  epsf = std::sqrt(std::numeric_limits<float> ::min EIGEN_EMPTY ());
69   double epsd = std::sqrt(std::numeric_limits<double>::min EIGEN_EMPTY ());
70 
71   while(std::abs(sf )<epsf) sf  = internal::random<float>();
72   while(std::abs(sd )<epsd) sf  = internal::random<double>();
73   while(std::abs(scf)<epsf) scf = internal::random<CF>();
74   while(std::abs(scd)<epsd) scd = internal::random<CD>();
75 
76 //   VERIFY_RAISES_ASSERT(mf+md); // does not even compile
77 
78 #ifdef EIGEN_DONT_VECTORIZE
79   VERIFY_RAISES_ASSERT(vf=vd);
80   VERIFY_RAISES_ASSERT(vf+=vd);
81 #endif
82 
83   // check scalar products
84   VERIFY_MIX_SCALAR(vcf * sf , vcf * complex<float>(sf));
85   VERIFY_MIX_SCALAR(sd * vcd , complex<double>(sd) * vcd);
86   VERIFY_MIX_SCALAR(vf * scf , vf.template cast<complex<float> >() * scf);
87   VERIFY_MIX_SCALAR(scd * vd , scd * vd.template cast<complex<double> >());
88 
89   VERIFY_MIX_SCALAR(vcf * 2 , vcf * complex<float>(2));
90   VERIFY_MIX_SCALAR(vcf * 2.1 , vcf * complex<float>(2.1));
91   VERIFY_MIX_SCALAR(2 * vcf, vcf * complex<float>(2));
92   VERIFY_MIX_SCALAR(2.1 * vcf , vcf * complex<float>(2.1));
93 
94   // check scalar quotients
95   VERIFY_MIX_SCALAR(vcf / sf , vcf / complex<float>(sf));
96   VERIFY_MIX_SCALAR(vf / scf , vf.template cast<complex<float> >() / scf);
97   VERIFY_MIX_SCALAR(vf.array()  / scf, vf.template cast<complex<float> >().array() / scf);
98   VERIFY_MIX_SCALAR(scd / vd.array() , scd / vd.template cast<complex<double> >().array());
99 
100   // check scalar increment
101   VERIFY_MIX_SCALAR(vcf.array() + sf , vcf.array() + complex<float>(sf));
102   VERIFY_MIX_SCALAR(sd  + vcd.array(), complex<double>(sd) + vcd.array());
103   VERIFY_MIX_SCALAR(vf.array()  + scf, vf.template cast<complex<float> >().array() + scf);
104   VERIFY_MIX_SCALAR(scd + vd.array() , scd + vd.template cast<complex<double> >().array());
105 
106   // check scalar subtractions
107   VERIFY_MIX_SCALAR(vcf.array() - sf , vcf.array() - complex<float>(sf));
108   VERIFY_MIX_SCALAR(sd  - vcd.array(), complex<double>(sd) - vcd.array());
109   VERIFY_MIX_SCALAR(vf.array()  - scf, vf.template cast<complex<float> >().array() - scf);
110   VERIFY_MIX_SCALAR(scd - vd.array() , scd - vd.template cast<complex<double> >().array());
111 
112   // check scalar powers
113   VERIFY_MIX_SCALAR( pow(vcf.array(), sf),        Eigen::pow(vcf.array(), complex<float>(sf)) );
114   VERIFY_MIX_SCALAR( vcf.array().pow(sf) ,        Eigen::pow(vcf.array(), complex<float>(sf)) );
115   VERIFY_MIX_SCALAR( pow(sd, vcd.array()),        Eigen::pow(complex<double>(sd), vcd.array()) );
116   VERIFY_MIX_SCALAR( Eigen::pow(vf.array(), scf), Eigen::pow(vf.template cast<complex<float> >().array(), scf) );
117   VERIFY_MIX_SCALAR( vf.array().pow(scf) ,        Eigen::pow(vf.template cast<complex<float> >().array(), scf) );
118   VERIFY_MIX_SCALAR( Eigen::pow(scd, vd.array()), Eigen::pow(scd, vd.template cast<complex<double> >().array()) );
119 
120   // check dot product
121   vf.dot(vf);
122 #if 0 // we get other compilation errors here than just static asserts
123   VERIFY_RAISES_ASSERT(vd.dot(vf));
124 #endif
125   VERIFY_IS_APPROX(vcf.dot(vf), vcf.dot(vf.template cast<complex<float> >()));
126 
127   // check diagonal product
128   VERIFY_IS_APPROX(vf.asDiagonal() * mcf, vf.template cast<complex<float> >().asDiagonal() * mcf);
129   VERIFY_IS_APPROX(vcd.asDiagonal() * md, vcd.asDiagonal() * md.template cast<complex<double> >());
130   VERIFY_IS_APPROX(mcf * vf.asDiagonal(), mcf * vf.template cast<complex<float> >().asDiagonal());
131   VERIFY_IS_APPROX(md * vcd.asDiagonal(), md.template cast<complex<double> >() * vcd.asDiagonal());
132 
133 //   vd.asDiagonal() * mf;    // does not even compile
134 //   vcd.asDiagonal() * mf;   // does not even compile
135 
136   // check inner product
137   VERIFY_IS_APPROX((vf.transpose() * vcf).value(), (vf.template cast<complex<float> >().transpose() * vcf).value());
138 
139   // check outer product
140   VERIFY_IS_APPROX((vf * vcf.transpose()).eval(), (vf.template cast<complex<float> >() * vcf.transpose()).eval());
141 
142   // coeff wise product
143 
144   VERIFY_IS_APPROX((vf * vcf.transpose()).eval(), (vf.template cast<complex<float> >() * vcf.transpose()).eval());
145 
146   Mat_cd mcd2 = mcd;
147   VERIFY_IS_APPROX(mcd.array() *= md.array(), mcd2.array() *= md.array().template cast<std::complex<double> >());
148 
149   // check matrix-matrix products
150   VERIFY_IS_APPROX(sd*md*mcd, (sd*md).template cast<CD>().eval()*mcd);
151   VERIFY_IS_APPROX(sd*mcd*md, sd*mcd*md.template cast<CD>());
152   VERIFY_IS_APPROX(scd*md*mcd, scd*md.template cast<CD>().eval()*mcd);
153   VERIFY_IS_APPROX(scd*mcd*md, scd*mcd*md.template cast<CD>());
154 
155   VERIFY_IS_APPROX(sf*mf*mcf, sf*mf.template cast<CF>()*mcf);
156   VERIFY_IS_APPROX(sf*mcf*mf, sf*mcf*mf.template cast<CF>());
157   VERIFY_IS_APPROX(scf*mf*mcf, scf*mf.template cast<CF>()*mcf);
158   VERIFY_IS_APPROX(scf*mcf*mf, scf*mcf*mf.template cast<CF>());
159 
160   VERIFY_IS_APPROX(sd*md.adjoint()*mcd, (sd*md).template cast<CD>().eval().adjoint()*mcd);
161   VERIFY_IS_APPROX(sd*mcd.adjoint()*md, sd*mcd.adjoint()*md.template cast<CD>());
162   VERIFY_IS_APPROX(sd*md.adjoint()*mcd.adjoint(), (sd*md).template cast<CD>().eval().adjoint()*mcd.adjoint());
163   VERIFY_IS_APPROX(sd*mcd.adjoint()*md.adjoint(), sd*mcd.adjoint()*md.template cast<CD>().adjoint());
164   VERIFY_IS_APPROX(sd*md*mcd.adjoint(), (sd*md).template cast<CD>().eval()*mcd.adjoint());
165   VERIFY_IS_APPROX(sd*mcd*md.adjoint(), sd*mcd*md.template cast<CD>().adjoint());
166 
167   VERIFY_IS_APPROX(sf*mf.adjoint()*mcf, (sf*mf).template cast<CF>().eval().adjoint()*mcf);
168   VERIFY_IS_APPROX(sf*mcf.adjoint()*mf, sf*mcf.adjoint()*mf.template cast<CF>());
169   VERIFY_IS_APPROX(sf*mf.adjoint()*mcf.adjoint(), (sf*mf).template cast<CF>().eval().adjoint()*mcf.adjoint());
170   VERIFY_IS_APPROX(sf*mcf.adjoint()*mf.adjoint(), sf*mcf.adjoint()*mf.template cast<CF>().adjoint());
171   VERIFY_IS_APPROX(sf*mf*mcf.adjoint(), (sf*mf).template cast<CF>().eval()*mcf.adjoint());
172   VERIFY_IS_APPROX(sf*mcf*mf.adjoint(), sf*mcf*mf.template cast<CF>().adjoint());
173 
174   VERIFY_IS_APPROX(sf*mf*vcf, (sf*mf).template cast<CF>().eval()*vcf);
175   VERIFY_IS_APPROX(scf*mf*vcf,(scf*mf.template cast<CF>()).eval()*vcf);
176   VERIFY_IS_APPROX(sf*mcf*vf, sf*mcf*vf.template cast<CF>());
177   VERIFY_IS_APPROX(scf*mcf*vf,scf*mcf*vf.template cast<CF>());
178 
179   VERIFY_IS_APPROX(sf*vcf.adjoint()*mf,  sf*vcf.adjoint()*mf.template cast<CF>().eval());
180   VERIFY_IS_APPROX(scf*vcf.adjoint()*mf, scf*vcf.adjoint()*mf.template cast<CF>().eval());
181   VERIFY_IS_APPROX(sf*vf.adjoint()*mcf,  sf*vf.adjoint().template cast<CF>().eval()*mcf);
182   VERIFY_IS_APPROX(scf*vf.adjoint()*mcf, scf*vf.adjoint().template cast<CF>().eval()*mcf);
183 
184   VERIFY_IS_APPROX(sd*md*vcd, (sd*md).template cast<CD>().eval()*vcd);
185   VERIFY_IS_APPROX(scd*md*vcd,(scd*md.template cast<CD>()).eval()*vcd);
186   VERIFY_IS_APPROX(sd*mcd*vd, sd*mcd*vd.template cast<CD>().eval());
187   VERIFY_IS_APPROX(scd*mcd*vd,scd*mcd*vd.template cast<CD>().eval());
188 
189   VERIFY_IS_APPROX(sd*vcd.adjoint()*md,  sd*vcd.adjoint()*md.template cast<CD>().eval());
190   VERIFY_IS_APPROX(scd*vcd.adjoint()*md, scd*vcd.adjoint()*md.template cast<CD>().eval());
191   VERIFY_IS_APPROX(sd*vd.adjoint()*mcd,  sd*vd.adjoint().template cast<CD>().eval()*mcd);
192   VERIFY_IS_APPROX(scd*vd.adjoint()*mcd, scd*vd.adjoint().template cast<CD>().eval()*mcd);
193 
194   VERIFY_IS_APPROX( sd*vcd.adjoint()*md.template triangularView<Upper>(),  sd*vcd.adjoint()*md.template cast<CD>().eval().template triangularView<Upper>());
195   VERIFY_IS_APPROX(scd*vcd.adjoint()*md.template triangularView<Lower>(), scd*vcd.adjoint()*md.template cast<CD>().eval().template triangularView<Lower>());
196   VERIFY_IS_APPROX( sd*vcd.adjoint()*md.transpose().template triangularView<Upper>(),  sd*vcd.adjoint()*md.transpose().template cast<CD>().eval().template triangularView<Upper>());
197   VERIFY_IS_APPROX(scd*vcd.adjoint()*md.transpose().template triangularView<Lower>(), scd*vcd.adjoint()*md.transpose().template cast<CD>().eval().template triangularView<Lower>());
198   VERIFY_IS_APPROX( sd*vd.adjoint()*mcd.template triangularView<Lower>(),  sd*vd.adjoint().template cast<CD>().eval()*mcd.template triangularView<Lower>());
199   VERIFY_IS_APPROX(scd*vd.adjoint()*mcd.template triangularView<Upper>(), scd*vd.adjoint().template cast<CD>().eval()*mcd.template triangularView<Upper>());
200   VERIFY_IS_APPROX( sd*vd.adjoint()*mcd.transpose().template triangularView<Lower>(),  sd*vd.adjoint().template cast<CD>().eval()*mcd.transpose().template triangularView<Lower>());
201   VERIFY_IS_APPROX(scd*vd.adjoint()*mcd.transpose().template triangularView<Upper>(), scd*vd.adjoint().template cast<CD>().eval()*mcd.transpose().template triangularView<Upper>());
202 
203   // Not supported yet: trmm
204 //   VERIFY_IS_APPROX(sd*mcd*md.template triangularView<Lower>(),  sd*mcd*md.template cast<CD>().eval().template triangularView<Lower>());
205 //   VERIFY_IS_APPROX(scd*mcd*md.template triangularView<Upper>(), scd*mcd*md.template cast<CD>().eval().template triangularView<Upper>());
206 //   VERIFY_IS_APPROX(sd*md*mcd.template triangularView<Lower>(),  sd*md.template cast<CD>().eval()*mcd.template triangularView<Lower>());
207 //   VERIFY_IS_APPROX(scd*md*mcd.template triangularView<Upper>(), scd*md.template cast<CD>().eval()*mcd.template triangularView<Upper>());
208 
209   // Not supported yet: symv
210 //   VERIFY_IS_APPROX(sd*vcd.adjoint()*md.template selfadjointView<Upper>(),  sd*vcd.adjoint()*md.template cast<CD>().eval().template selfadjointView<Upper>());
211 //   VERIFY_IS_APPROX(scd*vcd.adjoint()*md.template selfadjointView<Lower>(), scd*vcd.adjoint()*md.template cast<CD>().eval().template selfadjointView<Lower>());
212 //   VERIFY_IS_APPROX(sd*vd.adjoint()*mcd.template selfadjointView<Lower>(),  sd*vd.adjoint().template cast<CD>().eval()*mcd.template selfadjointView<Lower>());
213 //   VERIFY_IS_APPROX(scd*vd.adjoint()*mcd.template selfadjointView<Upper>(), scd*vd.adjoint().template cast<CD>().eval()*mcd.template selfadjointView<Upper>());
214 
215   // Not supported yet: symm
216 //   VERIFY_IS_APPROX(sd*vcd.adjoint()*md.template selfadjointView<Upper>(),  sd*vcd.adjoint()*md.template cast<CD>().eval().template selfadjointView<Upper>());
217 //   VERIFY_IS_APPROX(scd*vcd.adjoint()*md.template selfadjointView<Upper>(), scd*vcd.adjoint()*md.template cast<CD>().eval().template selfadjointView<Upper>());
218 //   VERIFY_IS_APPROX(sd*vd.adjoint()*mcd.template selfadjointView<Upper>(),  sd*vd.adjoint().template cast<CD>().eval()*mcd.template selfadjointView<Upper>());
219 //   VERIFY_IS_APPROX(scd*vd.adjoint()*mcd.template selfadjointView<Upper>(), scd*vd.adjoint().template cast<CD>().eval()*mcd.template selfadjointView<Upper>());
220 
221   rcd.setZero();
222   VERIFY_IS_APPROX(Mat_cd(rcd.template triangularView<Upper>() = sd * mcd * md),
223                    Mat_cd((sd * mcd * md.template cast<CD>().eval()).template triangularView<Upper>()));
224   VERIFY_IS_APPROX(Mat_cd(rcd.template triangularView<Upper>() = sd * md * mcd),
225                    Mat_cd((sd * md.template cast<CD>().eval() * mcd).template triangularView<Upper>()));
226   VERIFY_IS_APPROX(Mat_cd(rcd.template triangularView<Upper>() = scd * mcd * md),
227                    Mat_cd((scd * mcd * md.template cast<CD>().eval()).template triangularView<Upper>()));
228   VERIFY_IS_APPROX(Mat_cd(rcd.template triangularView<Upper>() = scd * md * mcd),
229                    Mat_cd((scd * md.template cast<CD>().eval() * mcd).template triangularView<Upper>()));
230 
231 
232   VERIFY_IS_APPROX( md.array()  * mcd.array(), md.template cast<CD>().eval().array() * mcd.array() );
233   VERIFY_IS_APPROX( mcd.array() * md.array(),  mcd.array() * md.template cast<CD>().eval().array() );
234 
235   VERIFY_IS_APPROX( md.array()  + mcd.array(), md.template cast<CD>().eval().array() + mcd.array() );
236   VERIFY_IS_APPROX( mcd.array() + md.array(),  mcd.array() + md.template cast<CD>().eval().array() );
237 
238   VERIFY_IS_APPROX( md.array()  - mcd.array(), md.template cast<CD>().eval().array() - mcd.array() );
239   VERIFY_IS_APPROX( mcd.array() - md.array(),  mcd.array() - md.template cast<CD>().eval().array() );
240 
241   if(mcd.array().abs().minCoeff()>epsd)
242   {
243     VERIFY_IS_APPROX( md.array() / mcd.array(), md.template cast<CD>().eval().array() / mcd.array() );
244   }
245   if(md.array().abs().minCoeff()>epsd)
246   {
247     VERIFY_IS_APPROX( mcd.array() / md.array(), mcd.array() / md.template cast<CD>().eval().array() );
248   }
249 
250   if(md.array().abs().minCoeff()>epsd || mcd.array().abs().minCoeff()>epsd)
251   {
252     VERIFY_IS_APPROX( md.array().pow(mcd.array()), md.template cast<CD>().eval().array().pow(mcd.array()) );
253     VERIFY_IS_APPROX( mcd.array().pow(md.array()),  mcd.array().pow(md.template cast<CD>().eval().array()) );
254 
255     VERIFY_IS_APPROX( pow(md.array(),mcd.array()), md.template cast<CD>().eval().array().pow(mcd.array()) );
256     VERIFY_IS_APPROX( pow(mcd.array(),md.array()),  mcd.array().pow(md.template cast<CD>().eval().array()) );
257   }
258 
259   rcd = mcd;
260   VERIFY_IS_APPROX( rcd = md, md.template cast<CD>().eval() );
261   rcd = mcd;
262   VERIFY_IS_APPROX( rcd += md, mcd + md.template cast<CD>().eval() );
263   rcd = mcd;
264   VERIFY_IS_APPROX( rcd -= md, mcd - md.template cast<CD>().eval() );
265   rcd = mcd;
266   VERIFY_IS_APPROX( rcd.array() *= md.array(), mcd.array() * md.template cast<CD>().eval().array() );
267   rcd = mcd;
268   if(md.array().abs().minCoeff()>epsd)
269   {
270     VERIFY_IS_APPROX( rcd.array() /= md.array(), mcd.array() / md.template cast<CD>().eval().array() );
271   }
272 
273   rcd = mcd;
274   VERIFY_IS_APPROX( rcd.noalias() += md + mcd*md, mcd + (md.template cast<CD>().eval()) + mcd*(md.template cast<CD>().eval()));
275 
276   VERIFY_IS_APPROX( rcd.noalias()  = md*md,       ((md*md).eval().template cast<CD>()) );
277   rcd = mcd;
278   VERIFY_IS_APPROX( rcd.noalias() += md*md, mcd + ((md*md).eval().template cast<CD>()) );
279   rcd = mcd;
280   VERIFY_IS_APPROX( rcd.noalias() -= md*md, mcd - ((md*md).eval().template cast<CD>()) );
281 
282   VERIFY_IS_APPROX( rcd.noalias()  = mcd + md*md,       mcd + ((md*md).eval().template cast<CD>()) );
283   rcd = mcd;
284   VERIFY_IS_APPROX( rcd.noalias() += mcd + md*md, mcd + mcd + ((md*md).eval().template cast<CD>()) );
285   rcd = mcd;
286   VERIFY_IS_APPROX( rcd.noalias() -= mcd + md*md,           - ((md*md).eval().template cast<CD>()) );
287 }
288 
test_mixingtypes()289 void test_mixingtypes()
290 {
291   for(int i = 0; i < g_repeat; i++) {
292     CALL_SUBTEST_1(mixingtypes<3>());
293     CALL_SUBTEST_2(mixingtypes<4>());
294     CALL_SUBTEST_3(mixingtypes<Dynamic>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE)));
295 
296     CALL_SUBTEST_4(mixingtypes<3>());
297     CALL_SUBTEST_5(mixingtypes<4>());
298     CALL_SUBTEST_6(mixingtypes<Dynamic>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE)));
299   }
300 }
301