compbio
BandMatrix.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_BANDMATRIX_H
11 #define EIGEN_BANDMATRIX_H
12 
13 namespace Eigen {
14 
15 namespace internal {
16 
17 template<typename Derived>
18 class BandMatrixBase : public EigenBase<Derived>
19 {
20  public:
21 
22  enum {
32  };
33  typedef typename internal::traits<Derived>::Scalar Scalar;
35  typedef typename DenseMatrixType::StorageIndex StorageIndex;
36  typedef typename internal::traits<Derived>::CoefficientsType CoefficientsType;
37  typedef EigenBase<Derived> Base;
38 
39  protected:
40  enum {
41  DataRowsAtCompileTime = ((Supers!=Dynamic) && (Subs!=Dynamic))
42  ? 1 + Supers + Subs
43  : Dynamic,
44  SizeAtCompileTime = EIGEN_SIZE_MIN_PREFER_DYNAMIC(RowsAtCompileTime,ColsAtCompileTime)
45  };
46 
47  public:
48 
49  using Base::derived;
50  using Base::rows;
51  using Base::cols;
52 
54  inline Index supers() const { return derived().supers(); }
55 
57  inline Index subs() const { return derived().subs(); }
58 
60  inline const CoefficientsType& coeffs() const { return derived().coeffs(); }
61 
63  inline CoefficientsType& coeffs() { return derived().coeffs(); }
64 
69  {
70  EIGEN_STATIC_ASSERT((Options&RowMajor)==0,THIS_METHOD_IS_ONLY_FOR_COLUMN_MAJOR_MATRICES);
71  Index start = 0;
72  Index len = coeffs().rows();
73  if (i<=supers())
74  {
75  start = supers()-i;
76  len = (std::min)(rows(),std::max<Index>(0,coeffs().rows() - (supers()-i)));
77  }
78  else if (i>=rows()-subs())
79  len = std::max<Index>(0,coeffs().rows() - (i + 1 - rows() + subs()));
80  return Block<CoefficientsType,Dynamic,1>(coeffs(), start, i, len, 1);
81  }
82 
85  { return Block<CoefficientsType,1,SizeAtCompileTime>(coeffs(),supers(),0,1,(std::min)(rows(),cols())); }
86 
89  { return Block<const CoefficientsType,1,SizeAtCompileTime>(coeffs(),supers(),0,1,(std::min)(rows(),cols())); }
90 
91  template<int Index> struct DiagonalIntReturnType {
92  enum {
93  ReturnOpposite = (Options&SelfAdjoint) && (((Index)>0 && Supers==0) || ((Index)<0 && Subs==0)),
94  Conjugate = ReturnOpposite && NumTraits<Scalar>::IsComplex,
95  ActualIndex = ReturnOpposite ? -Index : Index,
96  DiagonalSize = (RowsAtCompileTime==Dynamic || ColsAtCompileTime==Dynamic)
97  ? Dynamic
98  : (ActualIndex<0
99  ? EIGEN_SIZE_MIN_PREFER_DYNAMIC(ColsAtCompileTime, RowsAtCompileTime + ActualIndex)
100  : EIGEN_SIZE_MIN_PREFER_DYNAMIC(RowsAtCompileTime, ColsAtCompileTime - ActualIndex))
101  };
103  typedef typename internal::conditional<Conjugate,
105  BuildType>::type Type;
106  };
107 
109  template<int N> inline typename DiagonalIntReturnType<N>::Type diagonal()
110  {
111  return typename DiagonalIntReturnType<N>::BuildType(coeffs(), supers()-N, (std::max)(0,N), 1, diagonalLength(N));
112  }
113 
115  template<int N> inline const typename DiagonalIntReturnType<N>::Type diagonal() const
116  {
117  return typename DiagonalIntReturnType<N>::BuildType(coeffs(), supers()-N, (std::max)(0,N), 1, diagonalLength(N));
118  }
119 
122  {
123  eigen_assert((i<0 && -i<=subs()) || (i>=0 && i<=supers()));
124  return Block<CoefficientsType,1,Dynamic>(coeffs(), supers()-i, std::max<Index>(0,i), 1, diagonalLength(i));
125  }
126 
129  {
130  eigen_assert((i<0 && -i<=subs()) || (i>=0 && i<=supers()));
131  return Block<const CoefficientsType,1,Dynamic>(coeffs(), supers()-i, std::max<Index>(0,i), 1, diagonalLength(i));
132  }
133 
134  template<typename Dest> inline void evalTo(Dest& dst) const
135  {
136  dst.resize(rows(),cols());
137  dst.setZero();
138  dst.diagonal() = diagonal();
139  for (Index i=1; i<=supers();++i)
140  dst.diagonal(i) = diagonal(i);
141  for (Index i=1; i<=subs();++i)
142  dst.diagonal(-i) = diagonal(-i);
143  }
144 
145  DenseMatrixType toDenseMatrix() const
146  {
147  DenseMatrixType res(rows(),cols());
148  evalTo(res);
149  return res;
150  }
151 
152  protected:
153 
154  inline Index diagonalLength(Index i) const
155  { return i<0 ? (std::min)(cols(),rows()+i) : (std::min)(rows(),cols()-i); }
156 };
157 
177 template<typename _Scalar, int _Rows, int _Cols, int _Supers, int _Subs, int _Options>
178 struct traits<BandMatrix<_Scalar,_Rows,_Cols,_Supers,_Subs,_Options> >
179 {
180  typedef _Scalar Scalar;
181  typedef Dense StorageKind;
182  typedef Eigen::Index StorageIndex;
183  enum {
184  CoeffReadCost = NumTraits<Scalar>::ReadCost,
185  RowsAtCompileTime = _Rows,
186  ColsAtCompileTime = _Cols,
187  MaxRowsAtCompileTime = _Rows,
188  MaxColsAtCompileTime = _Cols,
189  Flags = LvalueBit,
190  Supers = _Supers,
191  Subs = _Subs,
192  Options = _Options,
193  DataRowsAtCompileTime = ((Supers!=Dynamic) && (Subs!=Dynamic)) ? 1 + Supers + Subs : Dynamic
194  };
196 };
197 
198 template<typename _Scalar, int Rows, int Cols, int Supers, int Subs, int Options>
199 class BandMatrix : public BandMatrixBase<BandMatrix<_Scalar,Rows,Cols,Supers,Subs,Options> >
200 {
201  public:
202 
203  typedef typename internal::traits<BandMatrix>::Scalar Scalar;
204  typedef typename internal::traits<BandMatrix>::StorageIndex StorageIndex;
205  typedef typename internal::traits<BandMatrix>::CoefficientsType CoefficientsType;
206 
207  explicit inline BandMatrix(Index rows=Rows, Index cols=Cols, Index supers=Supers, Index subs=Subs)
208  : m_coeffs(1+supers+subs,cols),
209  m_rows(rows), m_supers(supers), m_subs(subs)
210  {
211  }
212 
214  inline Index rows() const { return m_rows.value(); }
215 
217  inline Index cols() const { return m_coeffs.cols(); }
218 
220  inline Index supers() const { return m_supers.value(); }
221 
223  inline Index subs() const { return m_subs.value(); }
224 
225  inline const CoefficientsType& coeffs() const { return m_coeffs; }
226  inline CoefficientsType& coeffs() { return m_coeffs; }
227 
228  protected:
229 
230  CoefficientsType m_coeffs;
234 };
235 
236 template<typename _CoefficientsType,int _Rows, int _Cols, int _Supers, int _Subs,int _Options>
238 
239 template<typename _CoefficientsType,int _Rows, int _Cols, int _Supers, int _Subs,int _Options>
240 struct traits<BandMatrixWrapper<_CoefficientsType,_Rows,_Cols,_Supers,_Subs,_Options> >
241 {
242  typedef typename _CoefficientsType::Scalar Scalar;
243  typedef typename _CoefficientsType::StorageKind StorageKind;
244  typedef typename _CoefficientsType::StorageIndex StorageIndex;
245  enum {
247  RowsAtCompileTime = _Rows,
248  ColsAtCompileTime = _Cols,
249  MaxRowsAtCompileTime = _Rows,
250  MaxColsAtCompileTime = _Cols,
251  Flags = LvalueBit,
252  Supers = _Supers,
253  Subs = _Subs,
254  Options = _Options,
255  DataRowsAtCompileTime = ((Supers!=Dynamic) && (Subs!=Dynamic)) ? 1 + Supers + Subs : Dynamic
256  };
257  typedef _CoefficientsType CoefficientsType;
258 };
259 
260 template<typename _CoefficientsType,int _Rows, int _Cols, int _Supers, int _Subs,int _Options>
261 class BandMatrixWrapper : public BandMatrixBase<BandMatrixWrapper<_CoefficientsType,_Rows,_Cols,_Supers,_Subs,_Options> >
262 {
263  public:
264 
265  typedef typename internal::traits<BandMatrixWrapper>::Scalar Scalar;
266  typedef typename internal::traits<BandMatrixWrapper>::CoefficientsType CoefficientsType;
267  typedef typename internal::traits<BandMatrixWrapper>::StorageIndex StorageIndex;
268 
269  explicit inline BandMatrixWrapper(const CoefficientsType& coeffs, Index rows=_Rows, Index cols=_Cols, Index supers=_Supers, Index subs=_Subs)
270  : m_coeffs(coeffs),
271  m_rows(rows), m_supers(supers), m_subs(subs)
272  {
273  EIGEN_UNUSED_VARIABLE(cols);
274  //internal::assert(coeffs.cols()==cols() && (supers()+subs()+1)==coeffs.rows());
275  }
276 
278  inline Index rows() const { return m_rows.value(); }
279 
281  inline Index cols() const { return m_coeffs.cols(); }
282 
284  inline Index supers() const { return m_supers.value(); }
285 
287  inline Index subs() const { return m_subs.value(); }
288 
289  inline const CoefficientsType& coeffs() const { return m_coeffs; }
290 
291  protected:
292 
293  const CoefficientsType& m_coeffs;
297 };
298 
311 template<typename Scalar, int Size, int Options>
312 class TridiagonalMatrix : public BandMatrix<Scalar,Size,Size,Options&SelfAdjoint?0:1,1,Options|RowMajor>
313 {
315  typedef typename Base::StorageIndex StorageIndex;
316  public:
317  explicit TridiagonalMatrix(Index size = Size) : Base(size,size,Options&SelfAdjoint?0:1,1) {}
318 
319  inline typename Base::template DiagonalIntReturnType<1>::Type super()
320  { return Base::template diagonal<1>(); }
321  inline const typename Base::template DiagonalIntReturnType<1>::Type super() const
322  { return Base::template diagonal<1>(); }
323  inline typename Base::template DiagonalIntReturnType<-1>::Type sub()
324  { return Base::template diagonal<-1>(); }
325  inline const typename Base::template DiagonalIntReturnType<-1>::Type sub() const
326  { return Base::template diagonal<-1>(); }
327  protected:
328 };
329 
330 
331 struct BandShape {};
332 
333 template<typename _Scalar, int _Rows, int _Cols, int _Supers, int _Subs, int _Options>
334 struct evaluator_traits<BandMatrix<_Scalar,_Rows,_Cols,_Supers,_Subs,_Options> >
335  : public evaluator_traits_base<BandMatrix<_Scalar,_Rows,_Cols,_Supers,_Subs,_Options> >
336 {
337  typedef BandShape Shape;
338 };
339 
340 template<typename _CoefficientsType,int _Rows, int _Cols, int _Supers, int _Subs,int _Options>
341 struct evaluator_traits<BandMatrixWrapper<_CoefficientsType,_Rows,_Cols,_Supers,_Subs,_Options> >
342  : public evaluator_traits_base<BandMatrixWrapper<_CoefficientsType,_Rows,_Cols,_Supers,_Subs,_Options> >
343 {
344  typedef BandShape Shape;
345 };
346 
348 
349 } // end namespace internal
350 
351 } // end namespace Eigen
352 
353 #endif // EIGEN_BANDMATRIX_H
Index subs() const
Definition: BandMatrix.h:223
Index supers() const
Definition: BandMatrix.h:54
EIGEN_DEVICE_FUNC Index rows() const
Definition: EigenBase.h:58
Index subs() const
Definition: BandMatrix.h:287
Index subs() const
Definition: BandMatrix.h:57
const unsigned int LvalueBit
Means the expression has a coeffRef() method, i.e.
Definition: Constants.h:139
Represents a rectangular matrix with a banded storage.
Definition: BandMatrix.h:199
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:85
Holds information about the various numeric (i.e.
Definition: NumTraits.h:150
Definition: Meta.h:58
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:37
Definition: Constants.h:512
Block< CoefficientsType, 1, SizeAtCompileTime > diagonal()
Definition: BandMatrix.h:84
Definition: BandMatrix.h:237
Common base class for all classes T such that MatrixBase has an operator=(T) and a constructor Matrix...
Definition: EigenBase.h:28
Index rows() const
Definition: BandMatrix.h:278
const Block< const CoefficientsType, 1, Dynamic > diagonal(Index i) const
Definition: BandMatrix.h:128
DiagonalIntReturnType< N >::Type diagonal()
Definition: BandMatrix.h:109
Represents a tridiagonal matrix with a compact banded storage.
Definition: BandMatrix.h:312
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
Definition: ForwardDeclarations.h:89
Index cols() const
Definition: BandMatrix.h:217
const CoefficientsType & coeffs() const
Definition: BandMatrix.h:60
Definition: AssignEvaluator.h:744
Definition: BandMatrix.h:331
const Block< const CoefficientsType, 1, SizeAtCompileTime > diagonal() const
Definition: BandMatrix.h:88
Index supers() const
Definition: BandMatrix.h:284
Definition: BandTriangularSolver.h:13
Definition: CoreEvaluators.h:79
CoefficientsType & coeffs()
Definition: BandMatrix.h:63
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:103
Used in BandMatrix and SelfAdjointView to indicate that the matrix is self-adjoint.
Definition: Constants.h:220
Definition: AssignEvaluator.h:740
Index cols() const
Definition: BandMatrix.h:281
Storage order is row major (see TopicStorageOrders).
Definition: Constants.h:322
EIGEN_DEVICE_FUNC Index cols() const
Definition: EigenBase.h:61
Block< CoefficientsType, Dynamic, 1 > col(Index i)
Definition: BandMatrix.h:68
Index rows() const
Definition: BandMatrix.h:214
The type used to identify a dense storage.
Definition: Constants.h:491
Index supers() const
Definition: BandMatrix.h:220
Block< CoefficientsType, 1, Dynamic > diagonal(Index i)
Definition: BandMatrix.h:121
const int Dynamic
This value means that a positive quantity (e.g., a size) is not known at compile-time, and that instead the value is stored in some runtime variable.
Definition: Constants.h:21
Definition: BandMatrix.h:18
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:55
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:178
EIGEN_DEVICE_FUNC Index size() const
Definition: EigenBase.h:65
EIGEN_DEVICE_FUNC Derived & derived()
Definition: EigenBase.h:44
Definition: ForwardDeclarations.h:17
const DiagonalIntReturnType< N >::Type diagonal() const
Definition: BandMatrix.h:115
Definition: CoreEvaluators.h:70