mixingtypes.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
00006 //
00007 // Eigen is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 3 of the License, or (at your option) any later version.
00011 //
00012 // Alternatively, you can redistribute it and/or
00013 // modify it under the terms of the GNU General Public License as
00014 // published by the Free Software Foundation; either version 2 of
00015 // the License, or (at your option) any later version.
00016 //
00017 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00018 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00019 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00020 // GNU General Public License for more details.
00021 //
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License and a copy of the GNU General Public License along with
00024 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00025 
00026 // work around "uninitialized" warnings and give that option some testing
00027 #define EIGEN_INITIALIZE_MATRICES_BY_ZERO
00028 
00029 #ifndef EIGEN_NO_STATIC_ASSERT
00030 #define EIGEN_NO_STATIC_ASSERT // turn static asserts into runtime asserts in order to check them
00031 #endif
00032 
00033 // #ifndef EIGEN_DONT_VECTORIZE
00034 // #define EIGEN_DONT_VECTORIZE // SSE intrinsics aren't designed to allow mixing types
00035 // #endif
00036 
00037 #include "main.h"
00038 
00039 using namespace std;
00040 
00041 template<int SizeAtCompileType> void mixingtypes(int size = SizeAtCompileType)
00042 {
00043   typedef std::complex<float>   CF;
00044   typedef std::complex<double>  CD;
00045   typedef Matrix<float, SizeAtCompileType, SizeAtCompileType> Mat_f;
00046   typedef Matrix<double, SizeAtCompileType, SizeAtCompileType> Mat_d;
00047   typedef Matrix<std::complex<float>, SizeAtCompileType, SizeAtCompileType> Mat_cf;
00048   typedef Matrix<std::complex<double>, SizeAtCompileType, SizeAtCompileType> Mat_cd;
00049   typedef Matrix<float, SizeAtCompileType, 1> Vec_f;
00050   typedef Matrix<double, SizeAtCompileType, 1> Vec_d;
00051   typedef Matrix<std::complex<float>, SizeAtCompileType, 1> Vec_cf;
00052   typedef Matrix<std::complex<double>, SizeAtCompileType, 1> Vec_cd;
00053 
00054   Mat_f mf    = Mat_f::Random(size,size);
00055   Mat_d md    = mf.template cast<double>();
00056   Mat_cf mcf  = Mat_cf::Random(size,size);
00057   Mat_cd mcd  = mcf.template cast<complex<double> >();
00058   Vec_f vf    = Vec_f::Random(size,1);
00059   Vec_d vd    = vf.template cast<double>();
00060   Vec_cf vcf  = Vec_cf::Random(size,1);
00061   Vec_cd vcd  = vcf.template cast<complex<double> >();
00062   float           sf  = internal::random<float>();
00063   double          sd  = internal::random<double>();
00064   complex<float>  scf = internal::random<complex<float> >();
00065   complex<double> scd = internal::random<complex<double> >();
00066 
00067 
00068   mf+mf;
00069   VERIFY_RAISES_ASSERT(mf+md);
00070   VERIFY_RAISES_ASSERT(mf+mcf);
00071   VERIFY_RAISES_ASSERT(vf=vd);
00072   VERIFY_RAISES_ASSERT(vf+=vd);
00073   VERIFY_RAISES_ASSERT(mcd=md);
00074 
00075   // check scalar products
00076   VERIFY_IS_APPROX(vcf * sf , vcf * complex<float>(sf));
00077   VERIFY_IS_APPROX(sd * vcd, complex<double>(sd) * vcd);
00078   VERIFY_IS_APPROX(vf * scf , vf.template cast<complex<float> >() * scf);
00079   VERIFY_IS_APPROX(scd * vd, scd * vd.template cast<complex<double> >());
00080 
00081   // check dot product
00082   vf.dot(vf);
00083 #if 0 // we get other compilation errors here than just static asserts
00084   VERIFY_RAISES_ASSERT(vd.dot(vf));
00085 #endif
00086   VERIFY_IS_APPROX(vcf.dot(vf), vcf.dot(vf.template cast<complex<float> >()));
00087 
00088   // check diagonal product
00089   VERIFY_IS_APPROX(vf.asDiagonal() * mcf, vf.template cast<complex<float> >().asDiagonal() * mcf);
00090   VERIFY_IS_APPROX(vcd.asDiagonal() * md, vcd.asDiagonal() * md.template cast<complex<double> >());
00091   VERIFY_IS_APPROX(mcf * vf.asDiagonal(), mcf * vf.template cast<complex<float> >().asDiagonal());
00092   VERIFY_IS_APPROX(md * vcd.asDiagonal(), md.template cast<complex<double> >() * vcd.asDiagonal());
00093 //   vd.asDiagonal() * mf;    // does not even compile
00094 //   vcd.asDiagonal() * mf;   // does not even compile
00095 
00096   // check inner product
00097   VERIFY_IS_APPROX((vf.transpose() * vcf).value(), (vf.template cast<complex<float> >().transpose() * vcf).value());
00098 
00099   // check outer product
00100   VERIFY_IS_APPROX((vf * vcf.transpose()).eval(), (vf.template cast<complex<float> >() * vcf.transpose()).eval());
00101 
00102   // coeff wise product
00103 
00104   VERIFY_IS_APPROX((vf * vcf.transpose()).eval(), (vf.template cast<complex<float> >() * vcf.transpose()).eval());
00105 
00106   Mat_cd mcd2 = mcd;
00107   VERIFY_IS_APPROX(mcd.array() *= md.array(), mcd2.array() *= md.array().template cast<std::complex<double> >());
00108   
00109   // check matrix-matrix products
00110 
00111   VERIFY_IS_APPROX(sd*md*mcd, (sd*md).template cast<CD>().eval()*mcd);
00112   VERIFY_IS_APPROX(sd*mcd*md, sd*mcd*md.template cast<CD>());
00113   VERIFY_IS_APPROX(scd*md*mcd, scd*md.template cast<CD>().eval()*mcd);
00114   VERIFY_IS_APPROX(scd*mcd*md, scd*mcd*md.template cast<CD>());
00115 
00116   VERIFY_IS_APPROX(sf*mf*mcf, sf*mf.template cast<CF>()*mcf);
00117   VERIFY_IS_APPROX(sf*mcf*mf, sf*mcf*mf.template cast<CF>());
00118   VERIFY_IS_APPROX(scf*mf*mcf, scf*mf.template cast<CF>()*mcf);
00119   VERIFY_IS_APPROX(scf*mcf*mf, scf*mcf*mf.template cast<CF>());
00120 
00121   VERIFY_IS_APPROX(sf*mf*vcf, (sf*mf).template cast<CF>().eval()*vcf);
00122   VERIFY_IS_APPROX(scf*mf*vcf,(scf*mf.template cast<CF>()).eval()*vcf);
00123   VERIFY_IS_APPROX(sf*mcf*vf, sf*mcf*vf.template cast<CF>());
00124   VERIFY_IS_APPROX(scf*mcf*vf,scf*mcf*vf.template cast<CF>());
00125 
00126   VERIFY_IS_APPROX(sf*vcf.adjoint()*mf,  sf*vcf.adjoint()*mf.template cast<CF>().eval());
00127   VERIFY_IS_APPROX(scf*vcf.adjoint()*mf, scf*vcf.adjoint()*mf.template cast<CF>().eval());
00128   VERIFY_IS_APPROX(sf*vf.adjoint()*mcf,  sf*vf.adjoint().template cast<CF>().eval()*mcf);
00129   VERIFY_IS_APPROX(scf*vf.adjoint()*mcf, scf*vf.adjoint().template cast<CF>().eval()*mcf);
00130 
00131   VERIFY_IS_APPROX(sd*md*vcd, (sd*md).template cast<CD>().eval()*vcd);
00132   VERIFY_IS_APPROX(scd*md*vcd,(scd*md.template cast<CD>()).eval()*vcd);
00133   VERIFY_IS_APPROX(sd*mcd*vd, sd*mcd*vd.template cast<CD>().eval());
00134   VERIFY_IS_APPROX(scd*mcd*vd,scd*mcd*vd.template cast<CD>().eval());
00135 
00136   VERIFY_IS_APPROX(sd*vcd.adjoint()*md,  sd*vcd.adjoint()*md.template cast<CD>().eval());
00137   VERIFY_IS_APPROX(scd*vcd.adjoint()*md, scd*vcd.adjoint()*md.template cast<CD>().eval());
00138   VERIFY_IS_APPROX(sd*vd.adjoint()*mcd,  sd*vd.adjoint().template cast<CD>().eval()*mcd);
00139   VERIFY_IS_APPROX(scd*vd.adjoint()*mcd, scd*vd.adjoint().template cast<CD>().eval()*mcd);
00140 }
00141 
00142 void test_mixingtypes()
00143 {
00144   CALL_SUBTEST_1(mixingtypes<3>());
00145   CALL_SUBTEST_2(mixingtypes<4>());
00146   CALL_SUBTEST_3(mixingtypes<Dynamic>(internal::random<int>(1,310)));
00147 }


libicr
Author(s): Robert Krug
autogenerated on Mon Jan 6 2014 11:33:07