2009-07-14 23:27:37 +02:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
|
// for linear algebra.
|
|
|
|
|
//
|
|
|
|
|
// Copyright (C) 2009 Gael Guennebaud <g.gael@free.fr>
|
|
|
|
|
//
|
|
|
|
|
// 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
#ifndef EIGEN_BANDMATRIX_H
|
|
|
|
|
#define EIGEN_BANDMATRIX_H
|
|
|
|
|
|
|
|
|
|
/** \nonstableyet
|
|
|
|
|
* \class BandMatrix
|
|
|
|
|
*
|
2009-07-15 17:00:49 +02:00
|
|
|
* \brief
|
2009-07-14 23:27:37 +02:00
|
|
|
*
|
|
|
|
|
* \param
|
|
|
|
|
*
|
2009-07-15 17:00:49 +02:00
|
|
|
* \sa
|
2009-07-14 23:27:37 +02:00
|
|
|
*/
|
2009-07-15 17:00:49 +02:00
|
|
|
template<typename _Scalar, int Rows, int Cols, int Supers, int Subs, int Options>
|
|
|
|
|
struct ei_traits<BandMatrix<_Scalar,Rows,Cols,Supers,Subs,Options> >
|
2009-07-14 23:27:37 +02:00
|
|
|
{
|
|
|
|
|
typedef _Scalar Scalar;
|
|
|
|
|
enum {
|
|
|
|
|
CoeffReadCost = NumTraits<Scalar>::ReadCost,
|
2009-07-15 17:00:49 +02:00
|
|
|
RowsAtCompileTime = Rows,
|
|
|
|
|
ColsAtCompileTime = Cols,
|
|
|
|
|
MaxRowsAtCompileTime = Rows,
|
|
|
|
|
MaxColsAtCompileTime = Cols,
|
2009-07-14 23:27:37 +02:00
|
|
|
Flags = 0
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2009-07-15 17:00:49 +02:00
|
|
|
template<typename _Scalar, int Rows, int Cols, int Supers, int Subs, int Options>
|
|
|
|
|
class BandMatrix : public MultiplierBase<BandMatrix<_Scalar,Rows,Cols,Supers,Subs,Options> >
|
2009-07-14 23:27:37 +02:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
|
Flags = ei_traits<BandMatrix>::Flags,
|
|
|
|
|
CoeffReadCost = ei_traits<BandMatrix>::CoeffReadCost,
|
|
|
|
|
RowsAtCompileTime = ei_traits<BandMatrix>::RowsAtCompileTime,
|
|
|
|
|
ColsAtCompileTime = ei_traits<BandMatrix>::ColsAtCompileTime,
|
|
|
|
|
MaxRowsAtCompileTime = ei_traits<BandMatrix>::MaxRowsAtCompileTime,
|
|
|
|
|
MaxColsAtCompileTime = ei_traits<BandMatrix>::MaxColsAtCompileTime
|
|
|
|
|
};
|
|
|
|
|
typedef typename ei_traits<BandMatrix>::Scalar Scalar;
|
|
|
|
|
typedef Matrix<Scalar,RowsAtCompileTime,ColsAtCompileTime> PlainMatrixType;
|
2009-07-15 17:00:49 +02:00
|
|
|
|
2009-07-14 23:27:37 +02:00
|
|
|
protected:
|
|
|
|
|
enum {
|
2009-07-15 17:00:49 +02:00
|
|
|
DataRowsAtCompileTime = ((Supers!=Dynamic) && (Subs!=Dynamic))
|
|
|
|
|
? 1 + Supers + Subs
|
|
|
|
|
: Dynamic,
|
|
|
|
|
SizeAtCompileTime = EIGEN_ENUM_MIN(Rows,Cols)
|
2009-07-14 23:27:37 +02:00
|
|
|
};
|
2009-07-15 17:00:49 +02:00
|
|
|
typedef Matrix<Scalar,DataRowsAtCompileTime,ColsAtCompileTime,Options&RowMajor?RowMajor:ColMajor> DataType;
|
2009-07-14 23:27:37 +02:00
|
|
|
|
2009-07-15 17:00:49 +02:00
|
|
|
public:
|
2009-07-14 23:27:37 +02:00
|
|
|
|
2009-07-15 17:00:49 +02:00
|
|
|
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)
|
2009-07-14 23:27:37 +02:00
|
|
|
{ }
|
|
|
|
|
|
2009-07-15 17:00:49 +02:00
|
|
|
/** \returns the number of columns */
|
|
|
|
|
inline int rows() const { return m_rows.value(); }
|
2009-07-14 23:27:37 +02:00
|
|
|
|
2009-07-15 17:00:49 +02:00
|
|
|
/** \returns the number of rows */
|
|
|
|
|
inline int cols() const { return m_data.cols(); }
|
|
|
|
|
|
|
|
|
|
/** \returns the number of super diagonals */
|
2009-07-14 23:27:37 +02:00
|
|
|
inline int supers() const { return m_supers.value(); }
|
|
|
|
|
|
2009-07-15 17:00:49 +02:00
|
|
|
/** \returns the number of sub diagonals */
|
|
|
|
|
inline int subs() const { return m_subs.value(); }
|
2009-07-14 23:27:37 +02:00
|
|
|
|
2009-07-15 17:00:49 +02:00
|
|
|
/** \returns a vector expression of the main diagonal */
|
|
|
|
|
inline Block<DataType,1,SizeAtCompileTime> diagonal()
|
|
|
|
|
{ return Block<DataType,1,SizeAtCompileTime>(m_data,supers(),0,1,std::min(rows(),cols())); }
|
|
|
|
|
|
|
|
|
|
/** \returns a vector expression of the main diagonal (const version) */
|
|
|
|
|
inline const Block<DataType,1,SizeAtCompileTime> diagonal() const
|
|
|
|
|
{ return Block<DataType,1,SizeAtCompileTime>(m_data,supers(),0,1,std::min(rows(),cols())); }
|
|
|
|
|
|
|
|
|
|
template<int Index> struct DiagonalIntReturnType {
|
|
|
|
|
enum {
|
|
|
|
|
DiagonalSize = RowsAtCompileTime==Dynamic || ColsAtCompileTime==Dynamic
|
|
|
|
|
? Dynamic
|
|
|
|
|
: Index<0
|
|
|
|
|
? EIGEN_ENUM_MIN(ColsAtCompileTime, RowsAtCompileTime + Index)
|
|
|
|
|
: EIGEN_ENUM_MIN(RowsAtCompileTime, ColsAtCompileTime - Index)
|
|
|
|
|
};
|
|
|
|
|
typedef Block<DataType,1, DiagonalSize> Type;
|
|
|
|
|
};
|
2009-07-14 23:27:37 +02:00
|
|
|
|
2009-07-15 17:00:49 +02:00
|
|
|
/** \returns a vector expression of the \a Index -th sub or super diagonal */
|
|
|
|
|
template<int Index> inline typename DiagonalIntReturnType<Index>::Type diagonal()
|
2009-07-14 23:27:37 +02:00
|
|
|
{
|
2009-07-15 17:00:49 +02:00
|
|
|
return typename DiagonalIntReturnType<Index>::Type(m_data, supers()-Index, ei_abs(Index), 1, diagonalLength(Index));
|
2009-07-14 23:27:37 +02:00
|
|
|
}
|
|
|
|
|
|
2009-07-15 17:00:49 +02:00
|
|
|
/** \returns a vector expression of the \a Index -th sub or super diagonal */
|
|
|
|
|
template<int Index> inline const typename DiagonalIntReturnType<Index>::Type diagonal() const
|
2009-07-14 23:27:37 +02:00
|
|
|
{
|
2009-07-15 17:00:49 +02:00
|
|
|
return typename DiagonalIntReturnType<Index>::Type(m_data, supers()-Index, ei_abs(Index), 1, diagonalLength(Index));
|
2009-07-14 23:27:37 +02:00
|
|
|
}
|
|
|
|
|
|
2009-07-15 17:00:49 +02:00
|
|
|
/** \returns a vector expression of the \a i -th sub or super diagonal */
|
|
|
|
|
inline Block<DataType,1,Dynamic> diagonal(int i)
|
2009-07-14 23:27:37 +02:00
|
|
|
{
|
2009-07-15 17:00:49 +02:00
|
|
|
ei_assert((i<0 && -i<=subs()) || (i>=0 && i<=supers()));
|
|
|
|
|
return Block<DataType,1,Dynamic>(m_data, supers()-i, ei_abs(i), 1, diagonalLength(i));
|
2009-07-14 23:27:37 +02:00
|
|
|
}
|
2009-07-15 17:00:49 +02:00
|
|
|
|
|
|
|
|
/** \returns a vector expression of the \a i -th sub or super diagonal */
|
|
|
|
|
inline const Block<DataType,1,Dynamic> diagonal(int i) const
|
2009-07-14 23:27:37 +02:00
|
|
|
{
|
2009-07-15 17:00:49 +02:00
|
|
|
ei_assert((i<0 && -i<=subs()) || (i>=0 && i<=supers()));
|
|
|
|
|
return Block<DataType,1,Dynamic>(m_data, supers()-i, ei_abs(i), 1, diagonalLength(i));
|
2009-07-14 23:27:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PlainMatrixType toDense() const
|
|
|
|
|
{
|
|
|
|
|
PlainMatrixType res(rows(),cols());
|
|
|
|
|
res.setZero();
|
|
|
|
|
res.diagonal() = diagonal();
|
|
|
|
|
for (int i=1; i<=supers();++i)
|
|
|
|
|
res.diagonal(i) = diagonal(i);
|
|
|
|
|
for (int i=1; i<=subs();++i)
|
|
|
|
|
res.diagonal(-i) = diagonal(-i);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
2009-07-15 17:00:49 +02:00
|
|
|
inline int diagonalLength(int i) const
|
|
|
|
|
{ return i<0 ? std::min(cols(),rows()+i) : std::min(rows(),cols()-i); }
|
2009-07-14 23:27:37 +02:00
|
|
|
|
|
|
|
|
DataType m_data;
|
2009-07-15 17:00:49 +02:00
|
|
|
ei_int_if_dynamic<Rows> m_rows;
|
2009-07-14 23:27:37 +02:00
|
|
|
ei_int_if_dynamic<Supers> m_supers;
|
|
|
|
|
ei_int_if_dynamic<Subs> m_subs;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif // EIGEN_BANDMATRIX_H
|