25 #ifndef EIGEN_BANDMATRIX_H
26 #define EIGEN_BANDMATRIX_H
32 template<
typename Derived>
33 class BandMatrixBase :
public EigenBase<Derived>
38 Flags = internal::traits<Derived>::Flags,
39 CoeffReadCost = internal::traits<Derived>::CoeffReadCost,
40 RowsAtCompileTime = internal::traits<Derived>::RowsAtCompileTime,
41 ColsAtCompileTime = internal::traits<Derived>::ColsAtCompileTime,
42 MaxRowsAtCompileTime = internal::traits<Derived>::MaxRowsAtCompileTime,
43 MaxColsAtCompileTime = internal::traits<Derived>::MaxColsAtCompileTime,
44 Supers = internal::traits<Derived>::Supers,
45 Subs = internal::traits<Derived>::Subs,
46 Options = internal::traits<Derived>::Options
48 typedef typename internal::traits<Derived>::Scalar Scalar;
49 typedef Matrix<Scalar,RowsAtCompileTime,ColsAtCompileTime> DenseMatrixType;
51 typedef typename internal::traits<Derived>::CoefficientsType CoefficientsType;
52 typedef EigenBase<Derived> Base;
69 inline Index supers()
const {
return derived().supers(); }
75 inline const CoefficientsType& coeffs()
const {
return derived().coeffs(); }
78 inline CoefficientsType& coeffs() {
return derived().coeffs(); }
83 inline Block<CoefficientsType,Dynamic,1>
col(Index i)
87 Index len = coeffs().rows();
91 len = (std::min)(
rows(),std::max<Index>(0,coeffs().rows() - (supers()-i)));
93 else if (i>=
rows()-subs())
94 len = std::max<Index>(0,coeffs().rows() - (i + 1 -
rows() + subs()));
95 return Block<CoefficientsType,Dynamic,1>(coeffs(), start, i, len, 1);
99 inline Block<CoefficientsType,1,SizeAtCompileTime> diagonal()
100 {
return Block<CoefficientsType,1,SizeAtCompileTime>(coeffs(),supers(),0,1,(std::min)(
rows(),
cols())); }
103 inline const Block<const CoefficientsType,1,SizeAtCompileTime> diagonal()
const
104 {
return Block<const CoefficientsType,1,SizeAtCompileTime>(coeffs(),supers(),0,1,(std::min)(
rows(),
cols())); }
106 template<
int Index>
struct DiagonalIntReturnType {
108 ReturnOpposite = (Options&
SelfAdjoint) && (((Index)>0 && Supers==0) || ((Index)<0 && Subs==0)),
110 ActualIndex = ReturnOpposite ? -Index : Index,
111 DiagonalSize = (RowsAtCompileTime==Dynamic || ColsAtCompileTime==Dynamic)
115 : EIGEN_SIZE_MIN_PREFER_DYNAMIC(RowsAtCompileTime, ColsAtCompileTime - ActualIndex))
117 typedef Block<CoefficientsType,1, DiagonalSize> BuildType;
118 typedef typename internal::conditional<Conjugate,
119 CwiseUnaryOp<internal::scalar_conjugate_op<Scalar>,BuildType >,
120 BuildType>::type
Type;
126 return typename DiagonalIntReturnType<N>::BuildType(coeffs(), supers()-N, (std::max)(0,N), 1, diagonalLength(N));
132 return typename DiagonalIntReturnType<N>::BuildType(coeffs(), supers()-N, (std::max)(0,N), 1, diagonalLength(N));
136 inline Block<CoefficientsType,1,Dynamic> diagonal(Index i)
138 eigen_assert((i<0 && -i<=subs()) || (i>=0 && i<=supers()));
139 return Block<CoefficientsType,1,Dynamic>(coeffs(), supers()-i, std::max<Index>(0,i), 1, diagonalLength(i));
143 inline const Block<const CoefficientsType,1,Dynamic> diagonal(Index i)
const
145 eigen_assert((i<0 && -i<=subs()) || (i>=0 && i<=supers()));
146 return Block<const CoefficientsType,1,Dynamic>(coeffs(), supers()-i, std::max<Index>(0,i), 1, diagonalLength(i));
149 template<
typename Dest>
inline void evalTo(Dest& dst)
const
151 dst.resize(rows(),cols());
153 dst.diagonal() = diagonal();
154 for (Index i=1; i<=supers();++i)
155 dst.diagonal(i) = diagonal(i);
156 for (Index i=1; i<=subs();++i)
157 dst.diagonal(-i) = diagonal(-i);
160 DenseMatrixType toDenseMatrix()
const
162 DenseMatrixType res(rows(),cols());
169 inline Index diagonalLength(Index i)
const
170 {
return i<0 ? (std::min)(cols(),rows()+i) : (std::min)(rows(),cols()-i); }
192 template<
typename _Scalar,
int _Rows,
int _Cols,
int _Supers,
int _Subs,
int _Options>
193 struct traits<BandMatrix<_Scalar,_Rows,_Cols,_Supers,_Subs,_Options> >
195 typedef _Scalar Scalar;
196 typedef Dense StorageKind;
200 RowsAtCompileTime = _Rows,
201 ColsAtCompileTime = _Cols,
202 MaxRowsAtCompileTime = _Rows,
203 MaxColsAtCompileTime = _Cols,
208 DataRowsAtCompileTime = ((Supers!=
Dynamic) && (Subs!=Dynamic)) ? 1 + Supers + Subs : Dynamic
210 typedef Matrix<Scalar,DataRowsAtCompileTime,ColsAtCompileTime,Options&RowMajor?RowMajor:ColMajor> CoefficientsType;
213 template<
typename _Scalar,
int Rows,
int Cols,
int Supers,
int Subs,
int Options>
214 class BandMatrix :
public BandMatrixBase<BandMatrix<_Scalar,Rows,Cols,Supers,Subs,Options> >
218 typedef typename internal::traits<BandMatrix>::Scalar Scalar;
219 typedef typename internal::traits<BandMatrix>::Index Index;
220 typedef typename internal::traits<BandMatrix>::CoefficientsType CoefficientsType;
222 inline BandMatrix(Index rows=Rows, Index cols=Cols, Index supers=Supers, Index subs=Subs)
223 : m_coeffs(1+supers+subs,cols),
224 m_rows(rows), m_supers(supers), m_subs(subs)
229 inline Index rows()
const {
return m_rows.value(); }
232 inline Index cols()
const {
return m_coeffs.cols(); }
235 inline Index supers()
const {
return m_supers.value(); }
238 inline Index subs()
const {
return m_subs.value(); }
240 inline const CoefficientsType& coeffs()
const {
return m_coeffs; }
241 inline CoefficientsType& coeffs() {
return m_coeffs; }
245 CoefficientsType m_coeffs;
246 internal::variable_if_dynamic<Index, Rows> m_rows;
247 internal::variable_if_dynamic<Index, Supers> m_supers;
248 internal::variable_if_dynamic<Index, Subs> m_subs;
251 template<
typename _CoefficientsType,
int _Rows,
int _Cols,
int _Supers,
int _Subs,
int _Options>
252 class BandMatrixWrapper;
254 template<
typename _CoefficientsType,
int _Rows,
int _Cols,
int _Supers,
int _Subs,
int _Options>
255 struct traits<BandMatrixWrapper<_CoefficientsType,_Rows,_Cols,_Supers,_Subs,_Options> >
257 typedef typename _CoefficientsType::Scalar Scalar;
258 typedef typename _CoefficientsType::StorageKind StorageKind;
259 typedef typename _CoefficientsType::Index Index;
261 CoeffReadCost = internal::traits<_CoefficientsType>::CoeffReadCost,
262 RowsAtCompileTime = _Rows,
263 ColsAtCompileTime = _Cols,
264 MaxRowsAtCompileTime = _Rows,
265 MaxColsAtCompileTime = _Cols,
270 DataRowsAtCompileTime = ((Supers!=
Dynamic) && (Subs!=Dynamic)) ? 1 + Supers + Subs : Dynamic
272 typedef _CoefficientsType CoefficientsType;
275 template<
typename _CoefficientsType,
int _Rows,
int _Cols,
int _Supers,
int _Subs,
int _Options>
276 class BandMatrixWrapper :
public BandMatrixBase<BandMatrixWrapper<_CoefficientsType,_Rows,_Cols,_Supers,_Subs,_Options> >
280 typedef typename internal::traits<BandMatrixWrapper>::Scalar Scalar;
281 typedef typename internal::traits<BandMatrixWrapper>::CoefficientsType CoefficientsType;
282 typedef typename internal::traits<BandMatrixWrapper>::Index Index;
284 inline BandMatrixWrapper(
const CoefficientsType& coeffs, Index rows=_Rows, Index cols=_Cols, Index supers=_Supers, Index subs=_Subs)
286 m_rows(rows), m_supers(supers), m_subs(subs)
293 inline Index rows()
const {
return m_rows.value(); }
296 inline Index cols()
const {
return m_coeffs.cols(); }
299 inline Index supers()
const {
return m_supers.value(); }
302 inline Index subs()
const {
return m_subs.value(); }
304 inline const CoefficientsType& coeffs()
const {
return m_coeffs; }
308 const CoefficientsType& m_coeffs;
309 internal::variable_if_dynamic<Index, _Rows> m_rows;
310 internal::variable_if_dynamic<Index, _Supers> m_supers;
311 internal::variable_if_dynamic<Index, _Subs> m_subs;
326 template<
typename Scalar,
int Size,
int Options>
327 class TridiagonalMatrix :
public BandMatrix<Scalar,Size,Size,Options&SelfAdjoint?0:1,1,Options|RowMajor>
329 typedef BandMatrix<Scalar,Size,Size,Options&SelfAdjoint?0:1,1,Options|RowMajor> Base;
332 TridiagonalMatrix(Index size = Size) : Base(size,size,Options&
SelfAdjoint?0:1,1) {}
335 {
return Base::template diagonal<1>(); }
337 {
return Base::template diagonal<1>(); }
338 inline typename Base::template DiagonalIntReturnType<-1>
::Type sub()
339 {
return Base::template diagonal<-1>(); }
340 inline const typename Base::template DiagonalIntReturnType<-1>
::Type sub()
const
341 {
return Base::template diagonal<-1>(); }
349 #endif // EIGEN_BANDMATRIX_H