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) 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 // #ifndef EIGEN_DONT_VECTORIZE
19 // #define EIGEN_DONT_VECTORIZE // SSE intrinsics aren't designed to allow mixing types
20 // #endif
21
22 #include "main.h"
23
24 using namespace std;
25
mixingtypes(int size=SizeAtCompileType)26 template<int SizeAtCompileType> void mixingtypes(int size = SizeAtCompileType)
27 {
28 typedef std::complex<float> CF;
29 typedef std::complex<double> CD;
30 typedef Matrix<float, SizeAtCompileType, SizeAtCompileType> Mat_f;
31 typedef Matrix<double, SizeAtCompileType, SizeAtCompileType> Mat_d;
32 typedef Matrix<std::complex<float>, SizeAtCompileType, SizeAtCompileType> Mat_cf;
33 typedef Matrix<std::complex<double>, SizeAtCompileType, SizeAtCompileType> Mat_cd;
34 typedef Matrix<float, SizeAtCompileType, 1> Vec_f;
35 typedef Matrix<double, SizeAtCompileType, 1> Vec_d;
36 typedef Matrix<std::complex<float>, SizeAtCompileType, 1> Vec_cf;
37 typedef Matrix<std::complex<double>, SizeAtCompileType, 1> Vec_cd;
38
39 Mat_f mf = Mat_f::Random(size,size);
40 Mat_d md = mf.template cast<double>();
41 Mat_cf mcf = Mat_cf::Random(size,size);
42 Mat_cd mcd = mcf.template cast<complex<double> >();
43 Vec_f vf = Vec_f::Random(size,1);
44 Vec_d vd = vf.template cast<double>();
45 Vec_cf vcf = Vec_cf::Random(size,1);
46 Vec_cd vcd = vcf.template cast<complex<double> >();
47 float sf = internal::random<float>();
48 double sd = internal::random<double>();
49 complex<float> scf = internal::random<complex<float> >();
50 complex<double> scd = internal::random<complex<double> >();
51
52
53 mf+mf;
54 VERIFY_RAISES_ASSERT(mf+md);
55 VERIFY_RAISES_ASSERT(mf+mcf);
56 VERIFY_RAISES_ASSERT(vf=vd);
57 VERIFY_RAISES_ASSERT(vf+=vd);
58 VERIFY_RAISES_ASSERT(mcd=md);
59
60 // check scalar products
61 VERIFY_IS_APPROX(vcf * sf , vcf * complex<float>(sf));
62 VERIFY_IS_APPROX(sd * vcd, complex<double>(sd) * vcd);
63 VERIFY_IS_APPROX(vf * scf , vf.template cast<complex<float> >() * scf);
64 VERIFY_IS_APPROX(scd * vd, scd * vd.template cast<complex<double> >());
65
66 // check dot product
67 vf.dot(vf);
68 #if 0 // we get other compilation errors here than just static asserts
69 VERIFY_RAISES_ASSERT(vd.dot(vf));
70 #endif
71 VERIFY_IS_APPROX(vcf.dot(vf), vcf.dot(vf.template cast<complex<float> >()));
72
73 // check diagonal product
74 VERIFY_IS_APPROX(vf.asDiagonal() * mcf, vf.template cast<complex<float> >().asDiagonal() * mcf);
75 VERIFY_IS_APPROX(vcd.asDiagonal() * md, vcd.asDiagonal() * md.template cast<complex<double> >());
76 VERIFY_IS_APPROX(mcf * vf.asDiagonal(), mcf * vf.template cast<complex<float> >().asDiagonal());
77 VERIFY_IS_APPROX(md * vcd.asDiagonal(), md.template cast<complex<double> >() * vcd.asDiagonal());
78 // vd.asDiagonal() * mf; // does not even compile
79 // vcd.asDiagonal() * mf; // does not even compile
80
81 // check inner product
82 VERIFY_IS_APPROX((vf.transpose() * vcf).value(), (vf.template cast<complex<float> >().transpose() * vcf).value());
83
84 // check outer product
85 VERIFY_IS_APPROX((vf * vcf.transpose()).eval(), (vf.template cast<complex<float> >() * vcf.transpose()).eval());
86
87 // coeff wise product
88
89 VERIFY_IS_APPROX((vf * vcf.transpose()).eval(), (vf.template cast<complex<float> >() * vcf.transpose()).eval());
90
91 Mat_cd mcd2 = mcd;
92 VERIFY_IS_APPROX(mcd.array() *= md.array(), mcd2.array() *= md.array().template cast<std::complex<double> >());
93
94 // check matrix-matrix products
95
96 VERIFY_IS_APPROX(sd*md*mcd, (sd*md).template cast<CD>().eval()*mcd);
97 VERIFY_IS_APPROX(sd*mcd*md, sd*mcd*md.template cast<CD>());
98 VERIFY_IS_APPROX(scd*md*mcd, scd*md.template cast<CD>().eval()*mcd);
99 VERIFY_IS_APPROX(scd*mcd*md, scd*mcd*md.template cast<CD>());
100
101 VERIFY_IS_APPROX(sf*mf*mcf, sf*mf.template cast<CF>()*mcf);
102 VERIFY_IS_APPROX(sf*mcf*mf, sf*mcf*mf.template cast<CF>());
103 VERIFY_IS_APPROX(scf*mf*mcf, scf*mf.template cast<CF>()*mcf);
104 VERIFY_IS_APPROX(scf*mcf*mf, scf*mcf*mf.template cast<CF>());
105
106 VERIFY_IS_APPROX(sf*mf*vcf, (sf*mf).template cast<CF>().eval()*vcf);
107 VERIFY_IS_APPROX(scf*mf*vcf,(scf*mf.template cast<CF>()).eval()*vcf);
108 VERIFY_IS_APPROX(sf*mcf*vf, sf*mcf*vf.template cast<CF>());
109 VERIFY_IS_APPROX(scf*mcf*vf,scf*mcf*vf.template cast<CF>());
110
111 VERIFY_IS_APPROX(sf*vcf.adjoint()*mf, sf*vcf.adjoint()*mf.template cast<CF>().eval());
112 VERIFY_IS_APPROX(scf*vcf.adjoint()*mf, scf*vcf.adjoint()*mf.template cast<CF>().eval());
113 VERIFY_IS_APPROX(sf*vf.adjoint()*mcf, sf*vf.adjoint().template cast<CF>().eval()*mcf);
114 VERIFY_IS_APPROX(scf*vf.adjoint()*mcf, scf*vf.adjoint().template cast<CF>().eval()*mcf);
115
116 VERIFY_IS_APPROX(sd*md*vcd, (sd*md).template cast<CD>().eval()*vcd);
117 VERIFY_IS_APPROX(scd*md*vcd,(scd*md.template cast<CD>()).eval()*vcd);
118 VERIFY_IS_APPROX(sd*mcd*vd, sd*mcd*vd.template cast<CD>().eval());
119 VERIFY_IS_APPROX(scd*mcd*vd,scd*mcd*vd.template cast<CD>().eval());
120
121 VERIFY_IS_APPROX(sd*vcd.adjoint()*md, sd*vcd.adjoint()*md.template cast<CD>().eval());
122 VERIFY_IS_APPROX(scd*vcd.adjoint()*md, scd*vcd.adjoint()*md.template cast<CD>().eval());
123 VERIFY_IS_APPROX(sd*vd.adjoint()*mcd, sd*vd.adjoint().template cast<CD>().eval()*mcd);
124 VERIFY_IS_APPROX(scd*vd.adjoint()*mcd, scd*vd.adjoint().template cast<CD>().eval()*mcd);
125 }
126
test_mixingtypes()127 void test_mixingtypes()
128 {
129 CALL_SUBTEST_1(mixingtypes<3>());
130 CALL_SUBTEST_2(mixingtypes<4>());
131 CALL_SUBTEST_3(mixingtypes<Dynamic>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE)));
132 }
133