// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2009 Gael Guennebaud // // Eigen is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 3 of the License, or (at your option) any later version. // // Alternatively, you can redistribute it and/or // modify it under the terms of the GNU General Public License as // published by the Free Software Foundation; either version 2 of // the License, or (at your option) any later version. // // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the // GNU General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License and a copy of the GNU General Public License along with // Eigen. If not, see . #ifndef EIGEN_BANDMATRIX_H #define EIGEN_BANDMATRIX_H /** \nonstableyet * \class BandMatrix * * \brief Represents a rectangular matrix with a banded storage * * \param _Scalar Numeric type, i.e. float, double, int * \param Rows Number of rows, or \b Dynamic * \param Cols Number of columns, or \b Dynamic * \param Supers Number of super diagonal * \param Subs Number of sub diagonal * \param _Options A combination of either \b RowMajor or \b ColMajor, and of \b SelfAdjoint * The former controls storage order, and defaults to column-major. The latter controls * whether the matrix represent a selfadjoint matrix in which case either Supers of Subs * have to be null. * * \sa class TridiagonalMatrix */ template struct ei_traits > { typedef _Scalar Scalar; enum { CoeffReadCost = NumTraits::ReadCost, RowsAtCompileTime = Rows, ColsAtCompileTime = Cols, MaxRowsAtCompileTime = Rows, MaxColsAtCompileTime = Cols, Flags = 0 }; }; template class BandMatrix : public EigenBase > { public: enum { Flags = ei_traits::Flags, CoeffReadCost = ei_traits::CoeffReadCost, RowsAtCompileTime = ei_traits::RowsAtCompileTime, ColsAtCompileTime = ei_traits::ColsAtCompileTime, MaxRowsAtCompileTime = ei_traits::MaxRowsAtCompileTime, MaxColsAtCompileTime = ei_traits::MaxColsAtCompileTime }; typedef typename ei_traits::Scalar Scalar; typedef Matrix DenseMatrixType; protected: enum { DataRowsAtCompileTime = ((Supers!=Dynamic) && (Subs!=Dynamic)) ? 1 + Supers + Subs : Dynamic, SizeAtCompileTime = EIGEN_SIZE_MIN(Rows,Cols) }; typedef Matrix DataType; public: inline BandMatrix(int rows=Rows, int cols=Cols, int supers=Supers, int subs=Subs) : m_data(1+supers+subs,cols), m_rows(rows), m_supers(supers), m_subs(subs) { //m_data.setConstant(666); } /** \returns the number of columns */ inline int rows() const { return m_rows.value(); } /** \returns the number of rows */ inline int cols() const { return m_data.cols(); } /** \returns the number of super diagonals */ inline int supers() const { return m_supers.value(); } /** \returns the number of sub diagonals */ inline int subs() const { return m_subs.value(); } /** \returns a vector expression of the \a i -th column, * only the meaningful part is returned. * \warning the internal storage must be column major. */ inline Block col(int i) { EIGEN_STATIC_ASSERT((Options&RowMajor)==0,THIS_METHOD_IS_ONLY_FOR_COLUMN_MAJOR_MATRICES); int start = 0; int len = m_data.rows(); if (i<=supers()) { start = supers()-i; len = std::min(rows(),std::max(0,m_data.rows() - (supers()-i))); } else if (i>=rows()-subs()) len = std::max(0,m_data.rows() - (i + 1 - rows() + subs())); return Block(m_data, start, i, len, 1); } /** \returns a vector expression of the main diagonal */ inline Block diagonal() { return Block(m_data,supers(),0,1,std::min(rows(),cols())); } /** \returns a vector expression of the main diagonal (const version) */ inline const Block diagonal() const { return Block(m_data,supers(),0,1,std::min(rows(),cols())); } template struct DiagonalIntReturnType { enum { ReturnOpposite = (Options&SelfAdjoint) && (((Index)>0 && Supers==0) || ((Index)<0 && Subs==0)), Conjugate = ReturnOpposite && NumTraits::IsComplex, ActualIndex = ReturnOpposite ? -Index : Index, DiagonalSize = (RowsAtCompileTime==Dynamic || ColsAtCompileTime==Dynamic) ? Dynamic : (ActualIndex<0 // we handled Dynamic already, so can use EIGEN_ENUM_MIN safely here. ? EIGEN_ENUM_MIN(ColsAtCompileTime, RowsAtCompileTime + ActualIndex) : EIGEN_ENUM_MIN(RowsAtCompileTime, ColsAtCompileTime - ActualIndex)) }; typedef Block BuildType; typedef typename ei_meta_if,BuildType >, BuildType>::ret Type; }; /** \returns a vector expression of the \a Index -th sub or super diagonal */ template inline typename DiagonalIntReturnType::Type diagonal() { return typename DiagonalIntReturnType::BuildType(m_data, supers()-Index, std::max(0,Index), 1, diagonalLength(Index)); } /** \returns a vector expression of the \a Index -th sub or super diagonal */ template inline const typename DiagonalIntReturnType::Type diagonal() const { return typename DiagonalIntReturnType::BuildType(m_data, supers()-Index, std::max(0,Index), 1, diagonalLength(Index)); } /** \returns a vector expression of the \a i -th sub or super diagonal */ inline Block diagonal(int i) { ei_assert((i<0 && -i<=subs()) || (i>=0 && i<=supers())); return Block(m_data, supers()-i, std::max(0,i), 1, diagonalLength(i)); } /** \returns a vector expression of the \a i -th sub or super diagonal */ inline const Block diagonal(int i) const { ei_assert((i<0 && -i<=subs()) || (i>=0 && i<=supers())); return Block(m_data, supers()-i, std::max(0,i), 1, diagonalLength(i)); } template inline void evalTo(Dest& dst) const { dst.resize(rows(),cols()); dst.setZero(); dst.diagonal() = diagonal(); for (int i=1; i<=supers();++i) dst.diagonal(i) = diagonal(i); for (int i=1; i<=subs();++i) dst.diagonal(-i) = diagonal(-i); } DenseMatrixType toDenseMatrix() const { DenseMatrixType res(rows(),cols()); evalTo(res); return res; } protected: inline int diagonalLength(int i) const { return i<0 ? std::min(cols(),rows()+i) : std::min(rows(),cols()-i); } DataType m_data; ei_int_if_dynamic m_rows; ei_int_if_dynamic m_supers; ei_int_if_dynamic m_subs; }; /** \nonstableyet * \class TridiagonalMatrix * * \brief Represents a tridiagonal matrix * * \param _Scalar Numeric type, i.e. float, double, int * \param Size Number of rows and cols, or \b Dynamic * \param _Options Can be 0 or \b SelfAdjoint * * \sa class BandMatrix */ template class TridiagonalMatrix : public BandMatrix { typedef BandMatrix Base; public: TridiagonalMatrix(int size = Size) : Base(size,size,1,1) {} inline typename Base::template DiagonalIntReturnType<1>::Type super() { return Base::template diagonal<1>(); } inline const typename Base::template DiagonalIntReturnType<1>::Type super() const { return Base::template diagonal<1>(); } inline typename Base::template DiagonalIntReturnType<-1>::Type sub() { return Base::template diagonal<-1>(); } inline const typename Base::template DiagonalIntReturnType<-1>::Type sub() const { return Base::template diagonal<-1>(); } protected: }; #endif // EIGEN_BANDMATRIX_H