Home | History | Annotate | Download | only in test
      1 // This file is part of Eigen, a lightweight C++ template library
      2 // for linear algebra.
      3 //
      4 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1 (at) gmail.com>
      5 //
      6 // This Source Code Form is subject to the terms of the Mozilla
      7 // Public License v. 2.0. If a copy of the MPL was not distributed
      8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
      9 
     10 #include "main.h"
     11 
     12 template<typename MatrixType> void product_extra(const MatrixType& m)
     13 {
     14   typedef typename MatrixType::Index Index;
     15   typedef typename MatrixType::Scalar Scalar;
     16   typedef typename NumTraits<Scalar>::NonInteger NonInteger;
     17   typedef Matrix<Scalar, 1, Dynamic> RowVectorType;
     18   typedef Matrix<Scalar, Dynamic, 1> ColVectorType;
     19   typedef Matrix<Scalar, Dynamic, Dynamic,
     20                          MatrixType::Flags&RowMajorBit> OtherMajorMatrixType;
     21 
     22   Index rows = m.rows();
     23   Index cols = m.cols();
     24 
     25   MatrixType m1 = MatrixType::Random(rows, cols),
     26              m2 = MatrixType::Random(rows, cols),
     27              m3(rows, cols),
     28              mzero = MatrixType::Zero(rows, cols),
     29              identity = MatrixType::Identity(rows, rows),
     30              square = MatrixType::Random(rows, rows),
     31              res = MatrixType::Random(rows, rows),
     32              square2 = MatrixType::Random(cols, cols),
     33              res2 = MatrixType::Random(cols, cols);
     34   RowVectorType v1 = RowVectorType::Random(rows), vrres(rows);
     35   ColVectorType vc2 = ColVectorType::Random(cols), vcres(cols);
     36   OtherMajorMatrixType tm1 = m1;
     37 
     38   Scalar s1 = internal::random<Scalar>(),
     39          s2 = internal::random<Scalar>(),
     40          s3 = internal::random<Scalar>();
     41 
     42   VERIFY_IS_APPROX(m3.noalias() = m1 * m2.adjoint(),                 m1 * m2.adjoint().eval());
     43   VERIFY_IS_APPROX(m3.noalias() = m1.adjoint() * square.adjoint(),   m1.adjoint().eval() * square.adjoint().eval());
     44   VERIFY_IS_APPROX(m3.noalias() = m1.adjoint() * m2,                 m1.adjoint().eval() * m2);
     45   VERIFY_IS_APPROX(m3.noalias() = (s1 * m1.adjoint()) * m2,          (s1 * m1.adjoint()).eval() * m2);
     46   VERIFY_IS_APPROX(m3.noalias() = ((s1 * m1).adjoint()) * m2,        (internal::conj(s1) * m1.adjoint()).eval() * m2);
     47   VERIFY_IS_APPROX(m3.noalias() = (- m1.adjoint() * s1) * (s3 * m2), (- m1.adjoint()  * s1).eval() * (s3 * m2).eval());
     48   VERIFY_IS_APPROX(m3.noalias() = (s2 * m1.adjoint() * s1) * m2,     (s2 * m1.adjoint()  * s1).eval() * m2);
     49   VERIFY_IS_APPROX(m3.noalias() = (-m1*s2) * s1*m2.adjoint(),        (-m1*s2).eval() * (s1*m2.adjoint()).eval());
     50 
     51   // a very tricky case where a scale factor has to be automatically conjugated:
     52   VERIFY_IS_APPROX( m1.adjoint() * (s1*m2).conjugate(), (m1.adjoint()).eval() * ((s1*m2).conjugate()).eval());
     53 
     54 
     55   // test all possible conjugate combinations for the four matrix-vector product cases:
     56 
     57   VERIFY_IS_APPROX((-m1.conjugate() * s2) * (s1 * vc2),
     58                    (-m1.conjugate()*s2).eval() * (s1 * vc2).eval());
     59   VERIFY_IS_APPROX((-m1 * s2) * (s1 * vc2.conjugate()),
     60                    (-m1*s2).eval() * (s1 * vc2.conjugate()).eval());
     61   VERIFY_IS_APPROX((-m1.conjugate() * s2) * (s1 * vc2.conjugate()),
     62                    (-m1.conjugate()*s2).eval() * (s1 * vc2.conjugate()).eval());
     63 
     64   VERIFY_IS_APPROX((s1 * vc2.transpose()) * (-m1.adjoint() * s2),
     65                    (s1 * vc2.transpose()).eval() * (-m1.adjoint()*s2).eval());
     66   VERIFY_IS_APPROX((s1 * vc2.adjoint()) * (-m1.transpose() * s2),
     67                    (s1 * vc2.adjoint()).eval() * (-m1.transpose()*s2).eval());
     68   VERIFY_IS_APPROX((s1 * vc2.adjoint()) * (-m1.adjoint() * s2),
     69                    (s1 * vc2.adjoint()).eval() * (-m1.adjoint()*s2).eval());
     70 
     71   VERIFY_IS_APPROX((-m1.adjoint() * s2) * (s1 * v1.transpose()),
     72                    (-m1.adjoint()*s2).eval() * (s1 * v1.transpose()).eval());
     73   VERIFY_IS_APPROX((-m1.transpose() * s2) * (s1 * v1.adjoint()),
     74                    (-m1.transpose()*s2).eval() * (s1 * v1.adjoint()).eval());
     75   VERIFY_IS_APPROX((-m1.adjoint() * s2) * (s1 * v1.adjoint()),
     76                    (-m1.adjoint()*s2).eval() * (s1 * v1.adjoint()).eval());
     77 
     78   VERIFY_IS_APPROX((s1 * v1) * (-m1.conjugate() * s2),
     79                    (s1 * v1).eval() * (-m1.conjugate()*s2).eval());
     80   VERIFY_IS_APPROX((s1 * v1.conjugate()) * (-m1 * s2),
     81                    (s1 * v1.conjugate()).eval() * (-m1*s2).eval());
     82   VERIFY_IS_APPROX((s1 * v1.conjugate()) * (-m1.conjugate() * s2),
     83                    (s1 * v1.conjugate()).eval() * (-m1.conjugate()*s2).eval());
     84 
     85   VERIFY_IS_APPROX((-m1.adjoint() * s2) * (s1 * v1.adjoint()),
     86                    (-m1.adjoint()*s2).eval() * (s1 * v1.adjoint()).eval());
     87 
     88   // test the vector-matrix product with non aligned starts
     89   Index i = internal::random<Index>(0,m1.rows()-2);
     90   Index j = internal::random<Index>(0,m1.cols()-2);
     91   Index r = internal::random<Index>(1,m1.rows()-i);
     92   Index c = internal::random<Index>(1,m1.cols()-j);
     93   Index i2 = internal::random<Index>(0,m1.rows()-1);
     94   Index j2 = internal::random<Index>(0,m1.cols()-1);
     95 
     96   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());
     97   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());
     98 
     99   // regression test
    100   MatrixType tmp = m1 * m1.adjoint() * s1;
    101   VERIFY_IS_APPROX(tmp, m1 * m1.adjoint() * s1);
    102 }
    103 
    104 // Regression test for bug reported at http://forum.kde.org/viewtopic.php?f=74&t=96947
    105 void mat_mat_scalar_scalar_product()
    106 {
    107   Eigen::Matrix2Xd dNdxy(2, 3);
    108   dNdxy << -0.5, 0.5, 0,
    109            -0.3, 0, 0.3;
    110   double det = 6.0, wt = 0.5;
    111   VERIFY_IS_APPROX(dNdxy.transpose()*dNdxy*det*wt, det*wt*dNdxy.transpose()*dNdxy);
    112 }
    113 
    114 void zero_sized_objects()
    115 {
    116   // Bug 127
    117   //
    118   // a product of the form lhs*rhs with
    119   //
    120   // lhs:
    121   // rows = 1, cols = 4
    122   // RowsAtCompileTime = 1, ColsAtCompileTime = -1
    123   // MaxRowsAtCompileTime = 1, MaxColsAtCompileTime = 5
    124   //
    125   // rhs:
    126   // rows = 4, cols = 0
    127   // RowsAtCompileTime = -1, ColsAtCompileTime = -1
    128   // MaxRowsAtCompileTime = 5, MaxColsAtCompileTime = 1
    129   //
    130   // was failing on a runtime assertion, because it had been mis-compiled as a dot product because Product.h was using the
    131   // max-sizes to detect size 1 indicating vectors, and that didn't account for 0-sized object with max-size 1.
    132 
    133   Matrix<float,1,Dynamic,RowMajor,1,5> a(1,4);
    134   Matrix<float,Dynamic,Dynamic,ColMajor,5,1> b(4,0);
    135   a*b;
    136 }
    137 
    138 void test_product_extra()
    139 {
    140   for(int i = 0; i < g_repeat; i++) {
    141     CALL_SUBTEST_1( product_extra(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
    142     CALL_SUBTEST_2( product_extra(MatrixXd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
    143     CALL_SUBTEST_2( mat_mat_scalar_scalar_product() );
    144     CALL_SUBTEST_3( product_extra(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
    145     CALL_SUBTEST_4( product_extra(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2), internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
    146     CALL_SUBTEST_5( zero_sized_objects() );
    147   }
    148 }
    149