product_trsolve.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) 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 #define VERIFY_TRSM(TRI,XB) { \
00028     (XB).setRandom(); ref = (XB); \
00029     (TRI).solveInPlace(XB); \
00030     VERIFY_IS_APPROX((TRI).toDenseMatrix() * (XB), ref); \
00031     (XB).setRandom(); ref = (XB); \
00032     (XB) = (TRI).solve(XB); \
00033     VERIFY_IS_APPROX((TRI).toDenseMatrix() * (XB), ref); \
00034   }
00035 
00036 #define VERIFY_TRSM_ONTHERIGHT(TRI,XB) { \
00037     (XB).setRandom(); ref = (XB); \
00038     (TRI).transpose().template solveInPlace<OnTheRight>(XB.transpose()); \
00039     VERIFY_IS_APPROX((XB).transpose() * (TRI).transpose().toDenseMatrix(), ref.transpose()); \
00040     (XB).setRandom(); ref = (XB); \
00041     (XB).transpose() = (TRI).transpose().template solve<OnTheRight>(XB.transpose()); \
00042     VERIFY_IS_APPROX((XB).transpose() * (TRI).transpose().toDenseMatrix(), ref.transpose()); \
00043   }
00044 
00045 template<typename Scalar,int Size, int Cols> void trsolve(int size=Size,int cols=Cols)
00046 {
00047   typedef typename NumTraits<Scalar>::Real RealScalar;
00048 
00049   Matrix<Scalar,Size,Size,ColMajor> cmLhs(size,size);
00050   Matrix<Scalar,Size,Size,RowMajor> rmLhs(size,size);
00051 
00052   enum {  colmajor = Size==1 ? RowMajor : ColMajor,
00053           rowmajor = Cols==1 ? ColMajor : RowMajor };
00054   Matrix<Scalar,Size,Cols,colmajor> cmRhs(size,cols);
00055   Matrix<Scalar,Size,Cols,rowmajor> rmRhs(size,cols);
00056   Matrix<Scalar,Dynamic,Dynamic,colmajor> ref(size,cols);
00057 
00058   cmLhs.setRandom(); cmLhs *= static_cast<RealScalar>(0.1); cmLhs.diagonal().array() += static_cast<RealScalar>(1);
00059   rmLhs.setRandom(); rmLhs *= static_cast<RealScalar>(0.1); rmLhs.diagonal().array() += static_cast<RealScalar>(1);
00060 
00061   VERIFY_TRSM(cmLhs.conjugate().template triangularView<Lower>(), cmRhs);
00062   VERIFY_TRSM(cmLhs.adjoint()  .template triangularView<Lower>(), cmRhs);
00063   VERIFY_TRSM(cmLhs            .template triangularView<Upper>(), cmRhs);
00064   VERIFY_TRSM(cmLhs            .template triangularView<Lower>(), rmRhs);
00065   VERIFY_TRSM(cmLhs.conjugate().template triangularView<Upper>(), rmRhs);
00066   VERIFY_TRSM(cmLhs.adjoint()  .template triangularView<Upper>(), rmRhs);
00067 
00068   VERIFY_TRSM(cmLhs.conjugate().template triangularView<UnitLower>(), cmRhs);
00069   VERIFY_TRSM(cmLhs            .template triangularView<UnitUpper>(), rmRhs);
00070 
00071   VERIFY_TRSM(rmLhs            .template triangularView<Lower>(), cmRhs);
00072   VERIFY_TRSM(rmLhs.conjugate().template triangularView<UnitUpper>(), rmRhs);
00073 
00074 
00075   VERIFY_TRSM_ONTHERIGHT(cmLhs.conjugate().template triangularView<Lower>(), cmRhs);
00076   VERIFY_TRSM_ONTHERIGHT(cmLhs            .template triangularView<Upper>(), cmRhs);
00077   VERIFY_TRSM_ONTHERIGHT(cmLhs            .template triangularView<Lower>(), rmRhs);
00078   VERIFY_TRSM_ONTHERIGHT(cmLhs.conjugate().template triangularView<Upper>(), rmRhs);
00079 
00080   VERIFY_TRSM_ONTHERIGHT(cmLhs.conjugate().template triangularView<UnitLower>(), cmRhs);
00081   VERIFY_TRSM_ONTHERIGHT(cmLhs            .template triangularView<UnitUpper>(), rmRhs);
00082 
00083   VERIFY_TRSM_ONTHERIGHT(rmLhs            .template triangularView<Lower>(), cmRhs);
00084   VERIFY_TRSM_ONTHERIGHT(rmLhs.conjugate().template triangularView<UnitUpper>(), rmRhs);
00085 
00086   int c = internal::random<int>(0,cols-1);
00087   VERIFY_TRSM(rmLhs.template triangularView<Lower>(), rmRhs.col(c));
00088   VERIFY_TRSM(cmLhs.template triangularView<Lower>(), rmRhs.col(c));
00089 }
00090 
00091 void test_product_trsolve()
00092 {
00093   for(int i = 0; i < g_repeat ; i++)
00094   {
00095     // matrices
00096     CALL_SUBTEST_1((trsolve<float,Dynamic,Dynamic>(internal::random<int>(1,320),internal::random<int>(1,320))));
00097     CALL_SUBTEST_2((trsolve<double,Dynamic,Dynamic>(internal::random<int>(1,320),internal::random<int>(1,320))));
00098     CALL_SUBTEST_3((trsolve<std::complex<float>,Dynamic,Dynamic>(internal::random<int>(1,200),internal::random<int>(1,200))));
00099     CALL_SUBTEST_4((trsolve<std::complex<double>,Dynamic,Dynamic>(internal::random<int>(1,200),internal::random<int>(1,200))));
00100 
00101     // vectors
00102     CALL_SUBTEST_1((trsolve<float,Dynamic,1>(internal::random<int>(1,320))));
00103     CALL_SUBTEST_5((trsolve<std::complex<double>,Dynamic,1>(internal::random<int>(1,320))));
00104     CALL_SUBTEST_6((trsolve<float,1,1>()));
00105     CALL_SUBTEST_7((trsolve<float,1,2>()));
00106     CALL_SUBTEST_8((trsolve<std::complex<float>,4,1>()));
00107   }
00108 }


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