product.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra. Eigen itself is part of the KDE project.
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 #include <Eigen/Array>
00027 #include <Eigen/QR>
00028 
00029 template<typename Derived1, typename Derived2>
00030 bool areNotApprox(const MatrixBase<Derived1>& m1, const MatrixBase<Derived2>& m2, typename Derived1::RealScalar epsilon = precision<typename Derived1::RealScalar>())
00031 {
00032   return !((m1-m2).cwise().abs2().maxCoeff() < epsilon * epsilon
00033                           * std::max(m1.cwise().abs2().maxCoeff(), m2.cwise().abs2().maxCoeff()));
00034 }
00035 
00036 template<typename MatrixType> void product(const MatrixType& m)
00037 {
00038   /* this test covers the following files:
00039      Identity.h Product.h
00040   */
00041 
00042   typedef typename MatrixType::Scalar Scalar;
00043   typedef typename NumTraits<Scalar>::FloatingPoint FloatingPoint;
00044   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> RowVectorType;
00045   typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, 1> ColVectorType;
00046   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> RowSquareMatrixType;
00047   typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, MatrixType::ColsAtCompileTime> ColSquareMatrixType;
00048   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime,
00049                          MatrixType::Options^RowMajor> OtherMajorMatrixType;
00050 
00051   int rows = m.rows();
00052   int cols = m.cols();
00053 
00054   // this test relies a lot on Random.h, and there's not much more that we can do
00055   // to test it, hence I consider that we will have tested Random.h
00056   MatrixType m1 = MatrixType::Random(rows, cols),
00057              m2 = MatrixType::Random(rows, cols),
00058              m3(rows, cols),
00059              mzero = MatrixType::Zero(rows, cols);
00060   RowSquareMatrixType
00061              identity = RowSquareMatrixType::Identity(rows, rows),
00062              square = RowSquareMatrixType::Random(rows, rows),
00063              res = RowSquareMatrixType::Random(rows, rows);
00064   ColSquareMatrixType
00065              square2 = ColSquareMatrixType::Random(cols, cols),
00066              res2 = ColSquareMatrixType::Random(cols, cols);
00067   RowVectorType v1 = RowVectorType::Random(rows),
00068              v2 = RowVectorType::Random(rows),
00069              vzero = RowVectorType::Zero(rows);
00070   ColVectorType vc2 = ColVectorType::Random(cols), vcres(cols);
00071   OtherMajorMatrixType tm1 = m1;
00072 
00073   Scalar s1 = ei_random<Scalar>();
00074 
00075   int r = ei_random<int>(0, rows-1),
00076       c = ei_random<int>(0, cols-1);
00077 
00078   // begin testing Product.h: only associativity for now
00079   // (we use Transpose.h but this doesn't count as a test for it)
00080 
00081   VERIFY_IS_APPROX((m1*m1.transpose())*m2,  m1*(m1.transpose()*m2));
00082   m3 = m1;
00083   m3 *= m1.transpose() * m2;
00084   VERIFY_IS_APPROX(m3,                      m1 * (m1.transpose()*m2));
00085   VERIFY_IS_APPROX(m3,                      m1.lazy() * (m1.transpose()*m2));
00086 
00087   // continue testing Product.h: distributivity
00088   VERIFY_IS_APPROX(square*(m1 + m2),        square*m1+square*m2);
00089   VERIFY_IS_APPROX(square*(m1 - m2),        square*m1-square*m2);
00090 
00091   // continue testing Product.h: compatibility with ScalarMultiple.h
00092   VERIFY_IS_APPROX(s1*(square*m1),          (s1*square)*m1);
00093   VERIFY_IS_APPROX(s1*(square*m1),          square*(m1*s1));
00094 
00095   // again, test operator() to check const-qualification
00096   s1 += (square.lazy() * m1)(r,c);
00097 
00098   // test Product.h together with Identity.h
00099   VERIFY_IS_APPROX(v1,                      identity*v1);
00100   VERIFY_IS_APPROX(v1.transpose(),          v1.transpose() * identity);
00101   // again, test operator() to check const-qualification
00102   VERIFY_IS_APPROX(MatrixType::Identity(rows, cols)(r,c), static_cast<Scalar>(r==c));
00103 
00104   if (rows!=cols)
00105      VERIFY_RAISES_ASSERT(m3 = m1*m1);
00106 
00107   // test the previous tests were not screwed up because operator* returns 0
00108   // (we use the more accurate default epsilon)
00109   if (NumTraits<Scalar>::HasFloatingPoint && std::min(rows,cols)>1)
00110   {
00111     VERIFY(areNotApprox(m1.transpose()*m2,m2.transpose()*m1));
00112   }
00113 
00114   // test optimized operator+= path
00115   res = square;
00116   res += (m1 * m2.transpose()).lazy();
00117   VERIFY_IS_APPROX(res, square + m1 * m2.transpose());
00118   if (NumTraits<Scalar>::HasFloatingPoint && std::min(rows,cols)>1)
00119   {
00120     VERIFY(areNotApprox(res,square + m2 * m1.transpose()));
00121   }
00122   vcres = vc2;
00123   vcres += (m1.transpose() * v1).lazy();
00124   VERIFY_IS_APPROX(vcres, vc2 + m1.transpose() * v1);
00125   tm1 = m1;
00126   VERIFY_IS_APPROX(tm1.transpose() * v1, m1.transpose() * v1);
00127   VERIFY_IS_APPROX(v1.transpose() * tm1, v1.transpose() * m1);
00128 
00129   // test submatrix and matrix/vector product
00130   for (int i=0; i<rows; ++i)
00131     res.row(i) = m1.row(i) * m2.transpose();
00132   VERIFY_IS_APPROX(res, m1 * m2.transpose());
00133   // the other way round:
00134   for (int i=0; i<rows; ++i)
00135     res.col(i) = m1 * m2.transpose().col(i);
00136   VERIFY_IS_APPROX(res, m1 * m2.transpose());
00137 
00138   res2 = square2;
00139   res2 += (m1.transpose() * m2).lazy();
00140   VERIFY_IS_APPROX(res2, square2 + m1.transpose() * m2);
00141 
00142   if (NumTraits<Scalar>::HasFloatingPoint && std::min(rows,cols)>1)
00143   {
00144     VERIFY(areNotApprox(res2,square2 + m2.transpose() * m1));
00145   }
00146 }
00147 


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:32:09