BandMatrix.h
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) 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 #ifndef EIGEN_BANDMATRIX_H
00026 #define EIGEN_BANDMATRIX_H
00027 
00028 namespace Eigen { 
00029 
00030 namespace internal {
00031 
00032 template<typename Derived>
00033 class BandMatrixBase : public EigenBase<Derived>
00034 {
00035   public:
00036 
00037     enum {
00038       Flags = internal::traits<Derived>::Flags,
00039       CoeffReadCost = internal::traits<Derived>::CoeffReadCost,
00040       RowsAtCompileTime = internal::traits<Derived>::RowsAtCompileTime,
00041       ColsAtCompileTime = internal::traits<Derived>::ColsAtCompileTime,
00042       MaxRowsAtCompileTime = internal::traits<Derived>::MaxRowsAtCompileTime,
00043       MaxColsAtCompileTime = internal::traits<Derived>::MaxColsAtCompileTime,
00044       Supers = internal::traits<Derived>::Supers,
00045       Subs   = internal::traits<Derived>::Subs,
00046       Options = internal::traits<Derived>::Options
00047     };
00048     typedef typename internal::traits<Derived>::Scalar Scalar;
00049     typedef Matrix<Scalar,RowsAtCompileTime,ColsAtCompileTime> DenseMatrixType;
00050     typedef typename DenseMatrixType::Index Index;
00051     typedef typename internal::traits<Derived>::CoefficientsType CoefficientsType;
00052     typedef EigenBase<Derived> Base;
00053 
00054   protected:
00055     enum {
00056       DataRowsAtCompileTime = ((Supers!=Dynamic) && (Subs!=Dynamic))
00057                             ? 1 + Supers + Subs
00058                             : Dynamic,
00059       SizeAtCompileTime = EIGEN_SIZE_MIN_PREFER_DYNAMIC(RowsAtCompileTime,ColsAtCompileTime)
00060     };
00061 
00062   public:
00063     
00064     using Base::derived;
00065     using Base::rows;
00066     using Base::cols;
00067 
00069     inline Index supers() const { return derived().supers(); }
00070 
00072     inline Index subs() const { return derived().subs(); }
00073     
00075     inline const CoefficientsType& coeffs() const { return derived().coeffs(); }
00076     
00078     inline CoefficientsType& coeffs() { return derived().coeffs(); }
00079 
00083     inline Block<CoefficientsType,Dynamic,1> col(Index i)
00084     {
00085       EIGEN_STATIC_ASSERT((Options&RowMajor)==0,THIS_METHOD_IS_ONLY_FOR_COLUMN_MAJOR_MATRICES);
00086       Index start = 0;
00087       Index len = coeffs().rows();
00088       if (i<=supers())
00089       {
00090         start = supers()-i;
00091         len = (std::min)(rows(),std::max<Index>(0,coeffs().rows() - (supers()-i)));
00092       }
00093       else if (i>=rows()-subs())
00094         len = std::max<Index>(0,coeffs().rows() - (i + 1 - rows() + subs()));
00095       return Block<CoefficientsType,Dynamic,1>(coeffs(), start, i, len, 1);
00096     }
00097 
00099     inline Block<CoefficientsType,1,SizeAtCompileTime> diagonal()
00100     { return Block<CoefficientsType,1,SizeAtCompileTime>(coeffs(),supers(),0,1,(std::min)(rows(),cols())); }
00101 
00103     inline const Block<const CoefficientsType,1,SizeAtCompileTime> diagonal() const
00104     { return Block<const CoefficientsType,1,SizeAtCompileTime>(coeffs(),supers(),0,1,(std::min)(rows(),cols())); }
00105 
00106     template<int Index> struct DiagonalIntReturnType {
00107       enum {
00108         ReturnOpposite = (Options&SelfAdjoint) && (((Index)>0 && Supers==0) || ((Index)<0 && Subs==0)),
00109         Conjugate = ReturnOpposite && NumTraits<Scalar>::IsComplex,
00110         ActualIndex = ReturnOpposite ? -Index : Index,
00111         DiagonalSize = (RowsAtCompileTime==Dynamic || ColsAtCompileTime==Dynamic)
00112                      ? Dynamic
00113                      : (ActualIndex<0
00114                      ? EIGEN_SIZE_MIN_PREFER_DYNAMIC(ColsAtCompileTime, RowsAtCompileTime + ActualIndex)
00115                      : EIGEN_SIZE_MIN_PREFER_DYNAMIC(RowsAtCompileTime, ColsAtCompileTime - ActualIndex))
00116       };
00117       typedef Block<CoefficientsType,1, DiagonalSize> BuildType;
00118       typedef typename internal::conditional<Conjugate,
00119                  CwiseUnaryOp<internal::scalar_conjugate_op<Scalar>,BuildType >,
00120                  BuildType>::type Type;
00121     };
00122 
00124     template<int N> inline typename DiagonalIntReturnType<N>::Type diagonal()
00125     {
00126       return typename DiagonalIntReturnType<N>::BuildType(coeffs(), supers()-N, (std::max)(0,N), 1, diagonalLength(N));
00127     }
00128 
00130     template<int N> inline const typename DiagonalIntReturnType<N>::Type diagonal() const
00131     {
00132       return typename DiagonalIntReturnType<N>::BuildType(coeffs(), supers()-N, (std::max)(0,N), 1, diagonalLength(N));
00133     }
00134 
00136     inline Block<CoefficientsType,1,Dynamic> diagonal(Index i)
00137     {
00138       eigen_assert((i<0 && -i<=subs()) || (i>=0 && i<=supers()));
00139       return Block<CoefficientsType,1,Dynamic>(coeffs(), supers()-i, std::max<Index>(0,i), 1, diagonalLength(i));
00140     }
00141 
00143     inline const Block<const CoefficientsType,1,Dynamic> diagonal(Index i) const
00144     {
00145       eigen_assert((i<0 && -i<=subs()) || (i>=0 && i<=supers()));
00146       return Block<const CoefficientsType,1,Dynamic>(coeffs(), supers()-i, std::max<Index>(0,i), 1, diagonalLength(i));
00147     }
00148     
00149     template<typename Dest> inline void evalTo(Dest& dst) const
00150     {
00151       dst.resize(rows(),cols());
00152       dst.setZero();
00153       dst.diagonal() = diagonal();
00154       for (Index i=1; i<=supers();++i)
00155         dst.diagonal(i) = diagonal(i);
00156       for (Index i=1; i<=subs();++i)
00157         dst.diagonal(-i) = diagonal(-i);
00158     }
00159 
00160     DenseMatrixType toDenseMatrix() const
00161     {
00162       DenseMatrixType res(rows(),cols());
00163       evalTo(res);
00164       return res;
00165     }
00166 
00167   protected:
00168 
00169     inline Index diagonalLength(Index i) const
00170     { return i<0 ? (std::min)(cols(),rows()+i) : (std::min)(rows(),cols()-i); }
00171 };
00172 
00192 template<typename _Scalar, int _Rows, int _Cols, int _Supers, int _Subs, int _Options>
00193 struct traits<BandMatrix<_Scalar,_Rows,_Cols,_Supers,_Subs,_Options> >
00194 {
00195   typedef _Scalar Scalar;
00196   typedef Dense StorageKind;
00197   typedef DenseIndex Index;
00198   enum {
00199     CoeffReadCost = NumTraits<Scalar>::ReadCost,
00200     RowsAtCompileTime = _Rows,
00201     ColsAtCompileTime = _Cols,
00202     MaxRowsAtCompileTime = _Rows,
00203     MaxColsAtCompileTime = _Cols,
00204     Flags = LvalueBit,
00205     Supers = _Supers,
00206     Subs = _Subs,
00207     Options = _Options,
00208     DataRowsAtCompileTime = ((Supers!=Dynamic) && (Subs!=Dynamic)) ? 1 + Supers + Subs : Dynamic
00209   };
00210   typedef Matrix<Scalar,DataRowsAtCompileTime,ColsAtCompileTime,Options&RowMajor?RowMajor:ColMajor> CoefficientsType;
00211 };
00212 
00213 template<typename _Scalar, int Rows, int Cols, int Supers, int Subs, int Options>
00214 class BandMatrix : public BandMatrixBase<BandMatrix<_Scalar,Rows,Cols,Supers,Subs,Options> >
00215 {
00216   public:
00217 
00218     typedef typename internal::traits<BandMatrix>::Scalar Scalar;
00219     typedef typename internal::traits<BandMatrix>::Index Index;
00220     typedef typename internal::traits<BandMatrix>::CoefficientsType CoefficientsType;
00221 
00222     inline BandMatrix(Index rows=Rows, Index cols=Cols, Index supers=Supers, Index subs=Subs)
00223       : m_coeffs(1+supers+subs,cols),
00224         m_rows(rows), m_supers(supers), m_subs(subs)
00225     {
00226     }
00227 
00229     inline Index rows() const { return m_rows.value(); }
00230 
00232     inline Index cols() const { return m_coeffs.cols(); }
00233 
00235     inline Index supers() const { return m_supers.value(); }
00236 
00238     inline Index subs() const { return m_subs.value(); }
00239 
00240     inline const CoefficientsType& coeffs() const { return m_coeffs; }
00241     inline CoefficientsType& coeffs() { return m_coeffs; }
00242 
00243   protected:
00244 
00245     CoefficientsType m_coeffs;
00246     internal::variable_if_dynamic<Index, Rows>   m_rows;
00247     internal::variable_if_dynamic<Index, Supers> m_supers;
00248     internal::variable_if_dynamic<Index, Subs>   m_subs;
00249 };
00250 
00251 template<typename _CoefficientsType,int _Rows, int _Cols, int _Supers, int _Subs,int _Options>
00252 class BandMatrixWrapper;
00253 
00254 template<typename _CoefficientsType,int _Rows, int _Cols, int _Supers, int _Subs,int _Options>
00255 struct traits<BandMatrixWrapper<_CoefficientsType,_Rows,_Cols,_Supers,_Subs,_Options> >
00256 {
00257   typedef typename _CoefficientsType::Scalar Scalar;
00258   typedef typename _CoefficientsType::StorageKind StorageKind;
00259   typedef typename _CoefficientsType::Index Index;
00260   enum {
00261     CoeffReadCost = internal::traits<_CoefficientsType>::CoeffReadCost,
00262     RowsAtCompileTime = _Rows,
00263     ColsAtCompileTime = _Cols,
00264     MaxRowsAtCompileTime = _Rows,
00265     MaxColsAtCompileTime = _Cols,
00266     Flags = LvalueBit,
00267     Supers = _Supers,
00268     Subs = _Subs,
00269     Options = _Options,
00270     DataRowsAtCompileTime = ((Supers!=Dynamic) && (Subs!=Dynamic)) ? 1 + Supers + Subs : Dynamic
00271   };
00272   typedef _CoefficientsType CoefficientsType;
00273 };
00274 
00275 template<typename _CoefficientsType,int _Rows, int _Cols, int _Supers, int _Subs,int _Options>
00276 class BandMatrixWrapper : public BandMatrixBase<BandMatrixWrapper<_CoefficientsType,_Rows,_Cols,_Supers,_Subs,_Options> >
00277 {
00278   public:
00279 
00280     typedef typename internal::traits<BandMatrixWrapper>::Scalar Scalar;
00281     typedef typename internal::traits<BandMatrixWrapper>::CoefficientsType CoefficientsType;
00282     typedef typename internal::traits<BandMatrixWrapper>::Index Index;
00283 
00284     inline BandMatrixWrapper(const CoefficientsType& coeffs, Index rows=_Rows, Index cols=_Cols, Index supers=_Supers, Index subs=_Subs)
00285       : m_coeffs(coeffs),
00286         m_rows(rows), m_supers(supers), m_subs(subs)
00287     {
00288       EIGEN_UNUSED_VARIABLE(cols);
00289       //internal::assert(coeffs.cols()==cols() && (supers()+subs()+1)==coeffs.rows());
00290     }
00291 
00293     inline Index rows() const { return m_rows.value(); }
00294 
00296     inline Index cols() const { return m_coeffs.cols(); }
00297 
00299     inline Index supers() const { return m_supers.value(); }
00300 
00302     inline Index subs() const { return m_subs.value(); }
00303 
00304     inline const CoefficientsType& coeffs() const { return m_coeffs; }
00305 
00306   protected:
00307 
00308     const CoefficientsType& m_coeffs;
00309     internal::variable_if_dynamic<Index, _Rows>   m_rows;
00310     internal::variable_if_dynamic<Index, _Supers> m_supers;
00311     internal::variable_if_dynamic<Index, _Subs>   m_subs;
00312 };
00313 
00326 template<typename Scalar, int Size, int Options>
00327 class TridiagonalMatrix : public BandMatrix<Scalar,Size,Size,Options&SelfAdjoint?0:1,1,Options|RowMajor>
00328 {
00329     typedef BandMatrix<Scalar,Size,Size,Options&SelfAdjoint?0:1,1,Options|RowMajor> Base;
00330     typedef typename Base::Index Index;
00331   public:
00332     TridiagonalMatrix(Index size = Size) : Base(size,size,Options&SelfAdjoint?0:1,1) {}
00333 
00334     inline typename Base::template DiagonalIntReturnType<1>::Type super()
00335     { return Base::template diagonal<1>(); }
00336     inline const typename Base::template DiagonalIntReturnType<1>::Type super() const
00337     { return Base::template diagonal<1>(); }
00338     inline typename Base::template DiagonalIntReturnType<-1>::Type sub()
00339     { return Base::template diagonal<-1>(); }
00340     inline const typename Base::template DiagonalIntReturnType<-1>::Type sub() const
00341     { return Base::template diagonal<-1>(); }
00342   protected:
00343 };
00344 
00345 } // end namespace internal
00346 
00347 } // end namespace Eigen
00348 
00349 #endif // EIGEN_BANDMATRIX_H