product_extra.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) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #include "main.h"
00026 
00027 template<typename MatrixType> void product_extra(const MatrixType& m)
00028 {
00029   typedef typename MatrixType::Index Index;
00030   typedef typename MatrixType::Scalar Scalar;
00031   typedef typename NumTraits<Scalar>::NonInteger NonInteger;
00032   typedef Matrix<Scalar, 1, Dynamic> RowVectorType;
00033   typedef Matrix<Scalar, Dynamic, 1> ColVectorType;
00034   typedef Matrix<Scalar, Dynamic, Dynamic,
00035                          MatrixType::Flags&RowMajorBit> OtherMajorMatrixType;
00036 
00037   Index rows = m.rows();
00038   Index cols = m.cols();
00039 
00040   MatrixType m1 = MatrixType::Random(rows, cols),
00041              m2 = MatrixType::Random(rows, cols),
00042              m3(rows, cols),
00043              mzero = MatrixType::Zero(rows, cols),
00044              identity = MatrixType::Identity(rows, rows),
00045              square = MatrixType::Random(rows, rows),
00046              res = MatrixType::Random(rows, rows),
00047              square2 = MatrixType::Random(cols, cols),
00048              res2 = MatrixType::Random(cols, cols);
00049   RowVectorType v1 = RowVectorType::Random(rows), vrres(rows);
00050   ColVectorType vc2 = ColVectorType::Random(cols), vcres(cols);
00051   OtherMajorMatrixType tm1 = m1;
00052 
00053   Scalar s1 = internal::random<Scalar>(),
00054          s2 = internal::random<Scalar>(),
00055          s3 = internal::random<Scalar>();
00056 
00057   VERIFY_IS_APPROX(m3.noalias() = m1 * m2.adjoint(),                 m1 * m2.adjoint().eval());
00058   VERIFY_IS_APPROX(m3.noalias() = m1.adjoint() * square.adjoint(),   m1.adjoint().eval() * square.adjoint().eval());
00059   VERIFY_IS_APPROX(m3.noalias() = m1.adjoint() * m2,                 m1.adjoint().eval() * m2);
00060   VERIFY_IS_APPROX(m3.noalias() = (s1 * m1.adjoint()) * m2,          (s1 * m1.adjoint()).eval() * m2);
00061   VERIFY_IS_APPROX(m3.noalias() = ((s1 * m1).adjoint()) * m2,        (internal::conj(s1) * m1.adjoint()).eval() * m2);
00062   VERIFY_IS_APPROX(m3.noalias() = (- m1.adjoint() * s1) * (s3 * m2), (- m1.adjoint()  * s1).eval() * (s3 * m2).eval());
00063   VERIFY_IS_APPROX(m3.noalias() = (s2 * m1.adjoint() * s1) * m2,     (s2 * m1.adjoint()  * s1).eval() * m2);
00064   VERIFY_IS_APPROX(m3.noalias() = (-m1*s2) * s1*m2.adjoint(),        (-m1*s2).eval() * (s1*m2.adjoint()).eval());
00065 
00066   // a very tricky case where a scale factor has to be automatically conjugated:
00067   VERIFY_IS_APPROX( m1.adjoint() * (s1*m2).conjugate(), (m1.adjoint()).eval() * ((s1*m2).conjugate()).eval());
00068 
00069 
00070   // test all possible conjugate combinations for the four matrix-vector product cases:
00071 
00072   VERIFY_IS_APPROX((-m1.conjugate() * s2) * (s1 * vc2),
00073                    (-m1.conjugate()*s2).eval() * (s1 * vc2).eval());
00074   VERIFY_IS_APPROX((-m1 * s2) * (s1 * vc2.conjugate()),
00075                    (-m1*s2).eval() * (s1 * vc2.conjugate()).eval());
00076   VERIFY_IS_APPROX((-m1.conjugate() * s2) * (s1 * vc2.conjugate()),
00077                    (-m1.conjugate()*s2).eval() * (s1 * vc2.conjugate()).eval());
00078 
00079   VERIFY_IS_APPROX((s1 * vc2.transpose()) * (-m1.adjoint() * s2),
00080                    (s1 * vc2.transpose()).eval() * (-m1.adjoint()*s2).eval());
00081   VERIFY_IS_APPROX((s1 * vc2.adjoint()) * (-m1.transpose() * s2),
00082                    (s1 * vc2.adjoint()).eval() * (-m1.transpose()*s2).eval());
00083   VERIFY_IS_APPROX((s1 * vc2.adjoint()) * (-m1.adjoint() * s2),
00084                    (s1 * vc2.adjoint()).eval() * (-m1.adjoint()*s2).eval());
00085 
00086   VERIFY_IS_APPROX((-m1.adjoint() * s2) * (s1 * v1.transpose()),
00087                    (-m1.adjoint()*s2).eval() * (s1 * v1.transpose()).eval());
00088   VERIFY_IS_APPROX((-m1.transpose() * s2) * (s1 * v1.adjoint()),
00089                    (-m1.transpose()*s2).eval() * (s1 * v1.adjoint()).eval());
00090   VERIFY_IS_APPROX((-m1.adjoint() * s2) * (s1 * v1.adjoint()),
00091                    (-m1.adjoint()*s2).eval() * (s1 * v1.adjoint()).eval());
00092 
00093   VERIFY_IS_APPROX((s1 * v1) * (-m1.conjugate() * s2),
00094                    (s1 * v1).eval() * (-m1.conjugate()*s2).eval());
00095   VERIFY_IS_APPROX((s1 * v1.conjugate()) * (-m1 * s2),
00096                    (s1 * v1.conjugate()).eval() * (-m1*s2).eval());
00097   VERIFY_IS_APPROX((s1 * v1.conjugate()) * (-m1.conjugate() * s2),
00098                    (s1 * v1.conjugate()).eval() * (-m1.conjugate()*s2).eval());
00099 
00100   VERIFY_IS_APPROX((-m1.adjoint() * s2) * (s1 * v1.adjoint()),
00101                    (-m1.adjoint()*s2).eval() * (s1 * v1.adjoint()).eval());
00102 
00103   // test the vector-matrix product with non aligned starts
00104   Index i = internal::random<Index>(0,m1.rows()-2);
00105   Index j = internal::random<Index>(0,m1.cols()-2);
00106   Index r = internal::random<Index>(1,m1.rows()-i);
00107   Index c = internal::random<Index>(1,m1.cols()-j);
00108   Index i2 = internal::random<Index>(0,m1.rows()-1);
00109   Index j2 = internal::random<Index>(0,m1.cols()-1);
00110 
00111   VERIFY_IS_APPROX(m1.col(j2).adjoint() * m1.block(0,j,m1.rows(),c), m1.col(j2).adjoint().eval() * m1.block(0,j,m1.rows(),c).eval());
00112   VERIFY_IS_APPROX(m1.block(i,0,r,m1.cols()) * m1.row(i2).adjoint(), m1.block(i,0,r,m1.cols()).eval() * m1.row(i2).adjoint().eval());
00113   
00114   // regression test
00115   MatrixType tmp = m1 * m1.adjoint() * s1;
00116   VERIFY_IS_APPROX(tmp, m1 * m1.adjoint() * s1);
00117 }
00118 
00119 void zero_sized_objects()
00120 {
00121   // Bug 127
00122   //
00123   // a product of the form lhs*rhs with
00124   //
00125   // lhs:
00126   // rows = 1, cols = 4
00127   // RowsAtCompileTime = 1, ColsAtCompileTime = -1
00128   // MaxRowsAtCompileTime = 1, MaxColsAtCompileTime = 5
00129   //
00130   // rhs:
00131   // rows = 4, cols = 0
00132   // RowsAtCompileTime = -1, ColsAtCompileTime = -1
00133   // MaxRowsAtCompileTime = 5, MaxColsAtCompileTime = 1
00134   //
00135   // was failing on a runtime assertion, because it had been mis-compiled as a dot product because Product.h was using the
00136   // max-sizes to detect size 1 indicating vectors, and that didn't account for 0-sized object with max-size 1.
00137 
00138   Matrix<float,1,Dynamic,RowMajor,1,5> a(1,4);
00139   Matrix<float,Dynamic,Dynamic,ColMajor,5,1> b(4,0);
00140   a*b;
00141 }
00142 
00143 void test_product_extra()
00144 {
00145   for(int i = 0; i < g_repeat; i++) {
00146     CALL_SUBTEST_1( product_extra(MatrixXf(internal::random<int>(1,320), internal::random<int>(1,320))) );
00147     CALL_SUBTEST_2( product_extra(MatrixXd(internal::random<int>(1,320), internal::random<int>(1,320))) );
00148     CALL_SUBTEST_3( product_extra(MatrixXcf(internal::random<int>(1,150), internal::random<int>(1,150))) );
00149     CALL_SUBTEST_4( product_extra(MatrixXcd(internal::random<int>(1,150), internal::random<int>(1,150))) );
00150     CALL_SUBTEST_5( zero_sized_objects() );
00151   }
00152 }


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