// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2008 Gael Guennebaud // Copyright (C) 2008 Benoit Jacob // // Eigen is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 3 of the License, or (at your option) any later version. // // Alternatively, you can redistribute it and/or // modify it under the terms of the GNU General Public License as // published by the Free Software Foundation; either version 2 of // the License, or (at your option) any later version. // // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the // GNU General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License and a copy of the GNU General Public License along with // Eigen. If not, see . // work around "uninitialized" warnings and give that option some testing #define EIGEN_INITIALIZE_MATRICES_BY_ZERO #ifndef EIGEN_NO_STATIC_ASSERT #define EIGEN_NO_STATIC_ASSERT // turn static asserts into runtime asserts in order to check them #endif // #ifndef EIGEN_DONT_VECTORIZE // #define EIGEN_DONT_VECTORIZE // SSE intrinsics aren't designed to allow mixing types // #endif #include "main.h" using namespace std; template void mixingtypes(int size = SizeAtCompileType) { typedef Matrix Mat_f; typedef Matrix Mat_d; typedef Matrix, SizeAtCompileType, SizeAtCompileType> Mat_cf; typedef Matrix, SizeAtCompileType, SizeAtCompileType> Mat_cd; typedef Matrix Vec_f; typedef Matrix Vec_d; typedef Matrix, SizeAtCompileType, 1> Vec_cf; typedef Matrix, SizeAtCompileType, 1> Vec_cd; Mat_f mf = Mat_f::Random(size,size); Mat_d md = mf.template cast(); Mat_cf mcf = Mat_cf::Random(size,size); Mat_cd mcd = mcf.template cast >(); Vec_f vf = Vec_f::Random(size,1); Vec_d vd = vf.template cast(); Vec_cf vcf = Vec_cf::Random(size,1); Vec_cd vcd = vcf.template cast >(); float sf = ei_random(); double sd = ei_random(); complex scf = ei_random >(); complex scd = ei_random >(); mf+mf; VERIFY_RAISES_ASSERT(mf+md); VERIFY_RAISES_ASSERT(mf+mcf); VERIFY_RAISES_ASSERT(vf=vd); VERIFY_RAISES_ASSERT(vf+=vd); VERIFY_RAISES_ASSERT(mcd=md); // check scalar products VERIFY_IS_APPROX(vcf * sf , vcf * complex(sf)); VERIFY_IS_APPROX(sd * vcd, complex(sd) * vcd); VERIFY_IS_APPROX(vf * scf , vf.template cast >() * scf); VERIFY_IS_APPROX(scd * vd, scd * vd.template cast >()); // check dot product vf.dot(vf); #if 0 // we get other compilation errors here than just static asserts VERIFY_RAISES_ASSERT(vd.dot(vf)); #endif VERIFY_RAISES_ASSERT(vcf.dot(vf)); // yeah eventually we should allow this but i'm too lazy to make that change now in Dot.h // especially as that might be rewritten as cwise product .sum() which would make that automatic. // check diagonal product VERIFY_IS_APPROX(vf.asDiagonal() * mcf, vf.template cast >().asDiagonal() * mcf); VERIFY_IS_APPROX(vcd.asDiagonal() * md, vcd.asDiagonal() * md.template cast >()); VERIFY_IS_APPROX(mcf * vf.asDiagonal(), mcf * vf.template cast >().asDiagonal()); VERIFY_IS_APPROX(md * vcd.asDiagonal(), md.template cast >() * vcd.asDiagonal()); // vd.asDiagonal() * mf; // does not even compile // vcd.asDiagonal() * mf; // does not even compile // check inner product VERIFY_IS_APPROX((vf.transpose() * vcf).value(), (vf.template cast >().transpose() * vcf).value()); // check outer product VERIFY_IS_APPROX((vf * vcf.transpose()).eval(), (vf.template cast >() * vcf.transpose()).eval()); // coeff wise product VERIFY_IS_APPROX((vf * vcf.transpose()).eval(), (vf.template cast >() * vcf.transpose()).eval()); Mat_cd mcd2 = mcd; VERIFY_IS_APPROX(mcd.array() *= md.array(), mcd2.array() *= md.array().template cast >()); } void mixingtypes_large(int size) { typedef std::complex CF; typedef std::complex CD; static const int SizeAtCompileType = Dynamic; typedef Matrix Mat_f; typedef Matrix Mat_d; typedef Matrix, SizeAtCompileType, SizeAtCompileType> Mat_cf; typedef Matrix, SizeAtCompileType, SizeAtCompileType> Mat_cd; typedef Matrix Vec_f; typedef Matrix Vec_d; typedef Matrix, SizeAtCompileType, 1> Vec_cf; typedef Matrix, SizeAtCompileType, 1> Vec_cd; Mat_f mf(size,size); mf.setRandom(); Mat_d md(size,size); md.setRandom(); Mat_cf mcf(size,size); mcf.setRandom(); Mat_cd mcd(size,size); mcd.setRandom(); Vec_f vf(size,1); vf.setRandom(); Vec_d vd(size,1); vd.setRandom(); Vec_cf vcf(size,1); vcf.setRandom(); Vec_cd vcd(size,1); vcd.setRandom(); float sf = ei_random(); double sd = ei_random(); CF scf = ei_random(); CD scd = ei_random(); // mf*mf; // FIXME large products does not allow mixing types VERIFY_IS_APPROX(sd*md*mcd, (sd*md).cast().eval()*mcd); VERIFY_IS_APPROX(sd*mcd*md, sd*mcd*md.cast()); VERIFY_IS_APPROX(scd*md*mcd, scd*md.cast().eval()*mcd); VERIFY_IS_APPROX(scd*mcd*md, scd*mcd*md.cast()); // std::cerr << (mf*mf).cast() << "\n\n" << mf.cast().eval()*mf.cast().eval() << "\n\n"; // VERIFY_IS_APPROX((mf*mf).cast(), mf.cast().eval()*mf.cast().eval()); VERIFY_IS_APPROX(sf*mf*mcf, sf*mf.cast()*mcf); VERIFY_IS_APPROX(sf*mcf*mf, sf*mcf*mf.cast()); VERIFY_IS_APPROX(scf*mf*mcf, scf*mf.cast()*mcf); VERIFY_IS_APPROX(scf*mcf*mf, scf*mcf*mf.cast()); VERIFY_IS_APPROX(sf*mf*vcf, (sf*mf).cast().eval()*vcf); VERIFY_IS_APPROX(scf*mf*vcf,(scf*mf.cast()).eval()*vcf); VERIFY_IS_APPROX(sf*mcf*vf, sf*mcf*vf.cast()); VERIFY_IS_APPROX(scf*mcf*vf,scf*mcf*vf.cast()); VERIFY_IS_APPROX(sf*vcf.adjoint()*mf, sf*vcf.adjoint()*mf.cast().eval()); VERIFY_IS_APPROX(scf*vcf.adjoint()*mf, scf*vcf.adjoint()*mf.cast().eval()); VERIFY_IS_APPROX(sf*vf.adjoint()*mcf, sf*vf.adjoint().cast().eval()*mcf); VERIFY_IS_APPROX(scf*vf.adjoint()*mcf, scf*vf.adjoint().cast().eval()*mcf); VERIFY_IS_APPROX(sd*md*vcd, (sd*md).cast().eval()*vcd); VERIFY_IS_APPROX(scd*md*vcd,(scd*md.cast()).eval()*vcd); VERIFY_IS_APPROX(sd*mcd*vd, sd*mcd*vd.cast().eval()); VERIFY_IS_APPROX(scd*mcd*vd,scd*mcd*vd.cast().eval()); VERIFY_IS_APPROX(sd*vcd.adjoint()*md, sd*vcd.adjoint()*md.cast().eval()); VERIFY_IS_APPROX(scd*vcd.adjoint()*md, scd*vcd.adjoint()*md.cast().eval()); VERIFY_IS_APPROX(sd*vd.adjoint()*mcd, sd*vd.adjoint().cast().eval()*mcd); VERIFY_IS_APPROX(scd*vd.adjoint()*mcd, scd*vd.adjoint().cast().eval()*mcd); // VERIFY_IS_APPROX(vcf.adjoint() * mf, vcf.adjoint() * mf.cast()); // VERIFY_IS_APPROX(vf.adjoint() * mcf, vf.adjoint().cast() * mcf); // VERIFY_IS_APPROX(md*vcd, md.cast()*vcd); // VERIFY_IS_APPROX(mcd*vd, mcd*vd.cast()); // VERIFY_IS_APPROX(vcd.adjoint() * md, vcd.adjoint() * md.cast()); // VERIFY_IS_APPROX(vd.adjoint() * mcd, vd.adjoint().cast() * mcd); // VERIFY_RAISES_ASSERT(mcf *= mf); // does not even compile // VERIFY_RAISES_ASSERT(vcd = md*vcd); // does not even compile (cannot convert complex to double) // VERIFY_RAISES_ASSERT(vcf = mcf*vf); // VERIFY_RAISES_ASSERT(mf*md); // does not even compile // VERIFY_RAISES_ASSERT(mcf*mcd); // does not even compile // VERIFY_RAISES_ASSERT(mcf*vcd); // does not even compile // VERIFY_RAISES_ASSERT(vcf = mf*vf); } template void mixingtypes_small() { int size = SizeAtCompileType; typedef Matrix Mat_f; typedef Matrix Mat_d; typedef Matrix, SizeAtCompileType, SizeAtCompileType> Mat_cf; typedef Matrix, SizeAtCompileType, SizeAtCompileType> Mat_cd; typedef Matrix Vec_f; typedef Matrix Vec_d; typedef Matrix, SizeAtCompileType, 1> Vec_cf; typedef Matrix, SizeAtCompileType, 1> Vec_cd; Mat_f mf(size,size); Mat_d md(size,size); Mat_cf mcf(size,size); Mat_cd mcd(size,size); Vec_f vf(size,1); Vec_d vd(size,1); Vec_cf vcf(size,1); Vec_cd vcd(size,1); mf*mf; // FIXME shall we discard those products ? // 1) currently they work only if SizeAtCompileType is small enough // 2) in case we vectorize complexes this might be difficult to still allow that md*mcd; mcd*md; mf*vcf; mcf*vf; mcf *= mf; vcd = md*vcd; vcf = mcf*vf; // VERIFY_RAISES_ASSERT(mf*md); // does not even compile // VERIFY_RAISES_ASSERT(mcf*mcd); // does not even compile // VERIFY_RAISES_ASSERT(mcf*vcd); // does not even compile VERIFY_RAISES_ASSERT(vcf = mf*vf); } void test_mixingtypes() { // check that our operator new is indeed called: CALL_SUBTEST_1(mixingtypes<3>()); CALL_SUBTEST_2(mixingtypes<4>()); CALL_SUBTEST_3(mixingtypes(20)); CALL_SUBTEST_4(mixingtypes_small<4>()); CALL_SUBTEST_5(mixingtypes_large(11)); }