product_trmv.cpp
Go to the documentation of this file.
00001 // This file is triangularView of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
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 trmv(const MatrixType& m)
00028 {
00029   typedef typename MatrixType::Index Index;
00030   typedef typename MatrixType::Scalar Scalar;
00031   typedef typename NumTraits<Scalar>::Real RealScalar;
00032   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
00033 
00034   RealScalar largerEps = 10*test_precision<RealScalar>();
00035 
00036   Index rows = m.rows();
00037   Index cols = m.cols();
00038 
00039   MatrixType m1 = MatrixType::Random(rows, cols),
00040              m3(rows, cols);
00041   VectorType v1 = VectorType::Random(rows);
00042 
00043   Scalar s1 = internal::random<Scalar>();
00044 
00045   m1 = MatrixType::Random(rows, cols);
00046 
00047   // check with a column-major matrix
00048   m3 = m1.template triangularView<Eigen::Lower>();
00049   VERIFY((m3 * v1).isApprox(m1.template triangularView<Eigen::Lower>() * v1, largerEps));
00050   m3 = m1.template triangularView<Eigen::Upper>();
00051   VERIFY((m3 * v1).isApprox(m1.template triangularView<Eigen::Upper>() * v1, largerEps));
00052   m3 = m1.template triangularView<Eigen::UnitLower>();
00053   VERIFY((m3 * v1).isApprox(m1.template triangularView<Eigen::UnitLower>() * v1, largerEps));
00054   m3 = m1.template triangularView<Eigen::UnitUpper>();
00055   VERIFY((m3 * v1).isApprox(m1.template triangularView<Eigen::UnitUpper>() * v1, largerEps));
00056 
00057   // check conjugated and scalar multiple expressions (col-major)
00058   m3 = m1.template triangularView<Eigen::Lower>();
00059   VERIFY(((s1*m3).conjugate() * v1).isApprox((s1*m1).conjugate().template triangularView<Eigen::Lower>() * v1, largerEps));
00060   m3 = m1.template triangularView<Eigen::Upper>();
00061   VERIFY((m3.conjugate() * v1.conjugate()).isApprox(m1.conjugate().template triangularView<Eigen::Upper>() * v1.conjugate(), largerEps));
00062 
00063   // check with a row-major matrix
00064   m3 = m1.template triangularView<Eigen::Upper>();
00065   VERIFY((m3.transpose() * v1).isApprox(m1.transpose().template triangularView<Eigen::Lower>() * v1, largerEps));
00066   m3 = m1.template triangularView<Eigen::Lower>();
00067   VERIFY((m3.transpose() * v1).isApprox(m1.transpose().template triangularView<Eigen::Upper>() * v1, largerEps));
00068   m3 = m1.template triangularView<Eigen::UnitUpper>();
00069   VERIFY((m3.transpose() * v1).isApprox(m1.transpose().template triangularView<Eigen::UnitLower>() * v1, largerEps));
00070   m3 = m1.template triangularView<Eigen::UnitLower>();
00071   VERIFY((m3.transpose() * v1).isApprox(m1.transpose().template triangularView<Eigen::UnitUpper>() * v1, largerEps));
00072 
00073   // check conjugated and scalar multiple expressions (row-major)
00074   m3 = m1.template triangularView<Eigen::Upper>();
00075   VERIFY((m3.adjoint() * v1).isApprox(m1.adjoint().template triangularView<Eigen::Lower>() * v1, largerEps));
00076   m3 = m1.template triangularView<Eigen::Lower>();
00077   VERIFY((m3.adjoint() * (s1*v1.conjugate())).isApprox(m1.adjoint().template triangularView<Eigen::Upper>() * (s1*v1.conjugate()), largerEps));
00078   m3 = m1.template triangularView<Eigen::UnitUpper>();
00079 
00080   // check transposed cases:
00081   m3 = m1.template triangularView<Eigen::Lower>();
00082   VERIFY((v1.transpose() * m3).isApprox(v1.transpose() * m1.template triangularView<Eigen::Lower>(), largerEps));
00083   VERIFY((v1.adjoint() * m3).isApprox(v1.adjoint() * m1.template triangularView<Eigen::Lower>(), largerEps));
00084   VERIFY((v1.adjoint() * m3.adjoint()).isApprox(v1.adjoint() * m1.template triangularView<Eigen::Lower>().adjoint(), largerEps));
00085 
00086   // TODO check with sub-matrices
00087 }
00088 
00089 void test_product_trmv()
00090 {
00091   int s;
00092   for(int i = 0; i < g_repeat ; i++) {
00093     CALL_SUBTEST_1( trmv(Matrix<float, 1, 1>()) );
00094     CALL_SUBTEST_2( trmv(Matrix<float, 2, 2>()) );
00095     CALL_SUBTEST_3( trmv(Matrix3d()) );
00096     s = internal::random<int>(1,200);
00097     CALL_SUBTEST_4( trmv(MatrixXcf(s,s)) );
00098     s = internal::random<int>(1,200);
00099     CALL_SUBTEST_5( trmv(MatrixXcd(s,s)) );
00100     s = internal::random<int>(1,320);
00101     CALL_SUBTEST_6( trmv(Matrix<float,Dynamic,Dynamic,RowMajor>(s, s)) );
00102   }
00103 }


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