bandmatrix.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 bandmatrix(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,Dynamic,Dynamic> DenseMatrixType;
00033 
00034   Index rows = _m.rows();
00035   Index cols = _m.cols();
00036   Index supers = _m.supers();
00037   Index subs = _m.subs();
00038 
00039   MatrixType m(rows,cols,supers,subs);
00040 
00041   DenseMatrixType dm1(rows,cols);
00042   dm1.setZero();
00043 
00044   m.diagonal().setConstant(123);
00045   dm1.diagonal().setConstant(123);
00046   for (int i=1; i<=m.supers();++i)
00047   {
00048     m.diagonal(i).setConstant(static_cast<RealScalar>(i));
00049     dm1.diagonal(i).setConstant(static_cast<RealScalar>(i));
00050   }
00051   for (int i=1; i<=m.subs();++i)
00052   {
00053     m.diagonal(-i).setConstant(-static_cast<RealScalar>(i));
00054     dm1.diagonal(-i).setConstant(-static_cast<RealScalar>(i));
00055   }
00056   //std::cerr << m.m_data << "\n\n" << m.toDense() << "\n\n" << dm1 << "\n\n\n\n";
00057   VERIFY_IS_APPROX(dm1,m.toDenseMatrix());
00058 
00059   for (int i=0; i<cols; ++i)
00060   {
00061     m.col(i).setConstant(static_cast<RealScalar>(i+1));
00062     dm1.col(i).setConstant(static_cast<RealScalar>(i+1));
00063   }
00064   Index d = (std::min)(rows,cols);
00065   Index a = std::max<Index>(0,cols-d-supers);
00066   Index b = std::max<Index>(0,rows-d-subs);
00067   if(a>0) dm1.block(0,d+supers,rows,a).setZero();
00068   dm1.block(0,supers+1,cols-supers-1-a,cols-supers-1-a).template triangularView<Upper>().setZero();
00069   dm1.block(subs+1,0,rows-subs-1-b,rows-subs-1-b).template triangularView<Lower>().setZero();
00070   if(b>0) dm1.block(d+subs,0,b,cols).setZero();
00071   //std::cerr << m.m_data << "\n\n" << m.toDense() << "\n\n" << dm1 << "\n\n";
00072   VERIFY_IS_APPROX(dm1,m.toDenseMatrix());
00073 
00074 }
00075 
00076 using Eigen::internal::BandMatrix;
00077 
00078 void test_bandmatrix()
00079 {
00080   typedef BandMatrix<float>::Index Index;
00081 
00082   for(int i = 0; i < 10*g_repeat ; i++) {
00083     Index rows = internal::random<Index>(1,10);
00084     Index cols = internal::random<Index>(1,10);
00085     Index sups = internal::random<Index>(0,cols-1);
00086     Index subs = internal::random<Index>(0,rows-1);
00087     CALL_SUBTEST(bandmatrix(BandMatrix<float>(rows,cols,sups,subs)) );
00088   }
00089 }


libicr
Author(s): Robert Krug
autogenerated on Mon Jan 6 2014 11:32:29