BandMatrix.h
Go to the documentation of this file.
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 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 
20 template <typename Derived>
21 class BandMatrixBase : public EigenBase<Derived> {
22  public:
23  enum {
33  };
36  typedef typename DenseMatrixType::StorageIndex StorageIndex;
39 
40  protected:
41  enum {
44  };
45 
46  public:
47  using Base::cols;
48  using Base::derived;
49  using Base::rows;
50 
52  inline Index supers() const { return derived().supers(); }
53 
55  inline Index subs() const { return derived().subs(); }
56 
58  inline const CoefficientsType& coeffs() const { return derived().coeffs(); }
59 
61  inline CoefficientsType& coeffs() { return derived().coeffs(); }
62 
67  EIGEN_STATIC_ASSERT((int(Options) & int(RowMajor)) == 0, THIS_METHOD_IS_ONLY_FOR_COLUMN_MAJOR_MATRICES);
68  Index start = 0;
69  Index len = coeffs().rows();
70  if (i <= supers()) {
71  start = supers() - i;
72  len = (std::min)(rows(), std::max<Index>(0, coeffs().rows() - (supers() - i)));
73  } else if (i >= rows() - subs())
74  len = std::max<Index>(0, coeffs().rows() - (i + 1 - rows() + subs()));
76  }
77 
81  }
82 
86  }
87 
88  template <int Index>
90  enum {
92  (int(Options) & int(SelfAdjoint)) && (((Index) > 0 && Supers == 0) || ((Index) < 0 && Subs == 0)),
95  DiagonalSize =
97  ? Dynamic
100  };
102  typedef std::conditional_t<Conjugate, CwiseUnaryOp<internal::scalar_conjugate_op<Scalar>, BuildType>, BuildType>
104  };
105 
107  template <int N>
109  return typename DiagonalIntReturnType<N>::BuildType(coeffs(), supers() - N, (std::max)(0, N), 1, diagonalLength(N));
110  }
111 
113  template <int N>
114  inline const typename DiagonalIntReturnType<N>::Type diagonal() const {
115  return typename DiagonalIntReturnType<N>::BuildType(coeffs(), supers() - N, (std::max)(0, N), 1, diagonalLength(N));
116  }
117 
120  eigen_assert((i < 0 && -i <= subs()) || (i >= 0 && i <= supers()));
121  return Block<CoefficientsType, 1, Dynamic>(coeffs(), supers() - i, std::max<Index>(0, i), 1, diagonalLength(i));
122  }
123 
126  eigen_assert((i < 0 && -i <= subs()) || (i >= 0 && i <= supers()));
127  return Block<const CoefficientsType, 1, Dynamic>(coeffs(), supers() - i, std::max<Index>(0, i), 1,
128  diagonalLength(i));
129  }
130 
131  template <typename Dest>
132  inline void evalTo(Dest& dst) const {
133  dst.resize(rows(), cols());
134  dst.setZero();
135  dst.diagonal() = diagonal();
136  for (Index i = 1; i <= supers(); ++i) dst.diagonal(i) = diagonal(i);
137  for (Index i = 1; i <= subs(); ++i) dst.diagonal(-i) = diagonal(-i);
138  }
139 
142  evalTo(res);
143  return res;
144  }
145 
146  protected:
147  inline Index diagonalLength(Index i) const {
148  return i < 0 ? (std::min)(cols(), rows() + i) : (std::min)(rows(), cols() - i);
149  }
150 };
151 
171 template <typename Scalar_, int Rows_, int Cols_, int Supers_, int Subs_, int Options_>
172 struct traits<BandMatrix<Scalar_, Rows_, Cols_, Supers_, Subs_, Options_> > {
173  typedef Scalar_ Scalar;
176  enum {
183  Supers = Supers_,
184  Subs = Subs_,
185  Options = Options_,
187  };
190 };
191 
192 template <typename Scalar_, int Rows, int Cols, int Supers, int Subs, int Options>
193 class BandMatrix : public BandMatrixBase<BandMatrix<Scalar_, Rows, Cols, Supers, Subs, Options> > {
194  public:
198 
199  explicit inline BandMatrix(Index rows = Rows, Index cols = Cols, Index supers = Supers, Index subs = Subs)
200  : m_coeffs(1 + supers + subs, cols), m_rows(rows), m_supers(supers), m_subs(subs) {}
201 
203  inline EIGEN_CONSTEXPR Index rows() const { return m_rows.value(); }
204 
206  inline EIGEN_CONSTEXPR Index cols() const { return m_coeffs.cols(); }
207 
209  inline EIGEN_CONSTEXPR Index supers() const { return m_supers.value(); }
210 
212  inline EIGEN_CONSTEXPR Index subs() const { return m_subs.value(); }
213 
214  inline const CoefficientsType& coeffs() const { return m_coeffs; }
215  inline CoefficientsType& coeffs() { return m_coeffs; }
216 
217  protected:
222 };
223 
224 template <typename CoefficientsType_, int Rows_, int Cols_, int Supers_, int Subs_, int Options_>
225 class BandMatrixWrapper;
226 
227 template <typename CoefficientsType_, int Rows_, int Cols_, int Supers_, int Subs_, int Options_>
228 struct traits<BandMatrixWrapper<CoefficientsType_, Rows_, Cols_, Supers_, Subs_, Options_> > {
230  typedef typename CoefficientsType_::StorageKind StorageKind;
231  typedef typename CoefficientsType_::StorageIndex StorageIndex;
232  enum {
239  Supers = Supers_,
240  Subs = Subs_,
241  Options = Options_,
243  };
244  typedef CoefficientsType_ CoefficientsType;
245 };
246 
247 template <typename CoefficientsType_, int Rows_, int Cols_, int Supers_, int Subs_, int Options_>
249  : public BandMatrixBase<BandMatrixWrapper<CoefficientsType_, Rows_, Cols_, Supers_, Subs_, Options_> > {
250  public:
254 
255  explicit inline BandMatrixWrapper(const CoefficientsType& coeffs, Index rows = Rows_, Index cols = Cols_,
256  Index supers = Supers_, Index subs = Subs_)
257  : m_coeffs(coeffs), m_rows(rows), m_supers(supers), m_subs(subs) {
259  // eigen_assert(coeffs.cols()==cols() && (supers()+subs()+1)==coeffs.rows());
260  }
261 
263  inline EIGEN_CONSTEXPR Index rows() const { return m_rows.value(); }
264 
266  inline EIGEN_CONSTEXPR Index cols() const { return m_coeffs.cols(); }
267 
269  inline EIGEN_CONSTEXPR Index supers() const { return m_supers.value(); }
270 
272  inline EIGEN_CONSTEXPR Index subs() const { return m_subs.value(); }
273 
274  inline const CoefficientsType& coeffs() const { return m_coeffs; }
275 
276  protected:
281 };
282 
295 template <typename Scalar, int Size, int Options>
296 class TridiagonalMatrix : public BandMatrix<Scalar, Size, Size, Options & SelfAdjoint ? 0 : 1, 1, Options | RowMajor> {
298  typedef typename Base::StorageIndex StorageIndex;
299 
300  public:
301  explicit TridiagonalMatrix(Index size = Size) : Base(size, size, Options & SelfAdjoint ? 0 : 1, 1) {}
302 
303  inline typename Base::template DiagonalIntReturnType<1>::Type super() { return Base::template diagonal<1>(); }
304  inline const typename Base::template DiagonalIntReturnType<1>::Type super() const {
305  return Base::template diagonal<1>();
306  }
307  inline typename Base::template DiagonalIntReturnType<-1>::Type sub() { return Base::template diagonal<-1>(); }
308  inline const typename Base::template DiagonalIntReturnType<-1>::Type sub() const {
309  return Base::template diagonal<-1>();
310  }
311 
312  protected:
313 };
314 
315 struct BandShape {};
316 
317 template <typename Scalar_, int Rows_, int Cols_, int Supers_, int Subs_, int Options_>
318 struct evaluator_traits<BandMatrix<Scalar_, Rows_, Cols_, Supers_, Subs_, Options_> >
319  : public evaluator_traits_base<BandMatrix<Scalar_, Rows_, Cols_, Supers_, Subs_, Options_> > {
320  typedef BandShape Shape;
321 };
322 
323 template <typename CoefficientsType_, int Rows_, int Cols_, int Supers_, int Subs_, int Options_>
324 struct evaluator_traits<BandMatrixWrapper<CoefficientsType_, Rows_, Cols_, Supers_, Subs_, Options_> >
325  : public evaluator_traits_base<BandMatrixWrapper<CoefficientsType_, Rows_, Cols_, Supers_, Subs_, Options_> > {
326  typedef BandShape Shape;
327 };
328 
329 template <>
332 };
333 
334 } // end namespace internal
335 
336 } // end namespace Eigen
337 
338 #endif // EIGEN_BANDMATRIX_H
int i
Definition: BiCGSTAB_step_by_step.cpp:9
#define EIGEN_CONSTEXPR
Definition: Macros.h:758
#define EIGEN_UNUSED_VARIABLE(var)
Definition: Macros.h:966
#define eigen_assert(x)
Definition: Macros.h:910
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Definition: PartialRedux_count.cpp:3
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
SCALAR Scalar
Definition: bench_gemm.cpp:45
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:110
Definition: ForwardDeclarations.h:102
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
Definition: BandMatrix.h:21
Index subs() const
Definition: BandMatrix.h:55
internal::traits< Derived >::CoefficientsType CoefficientsType
Definition: BandMatrix.h:37
constexpr EIGEN_DEVICE_FUNC Derived & derived()
Definition: EigenBase.h:49
DiagonalIntReturnType< N >::Type diagonal()
Definition: BandMatrix.h:108
DenseMatrixType::StorageIndex StorageIndex
Definition: BandMatrix.h:36
@ MaxColsAtCompileTime
Definition: BandMatrix.h:29
@ RowsAtCompileTime
Definition: BandMatrix.h:26
@ Supers
Definition: BandMatrix.h:30
@ CoeffReadCost
Definition: BandMatrix.h:25
@ Subs
Definition: BandMatrix.h:31
@ MaxRowsAtCompileTime
Definition: BandMatrix.h:28
@ ColsAtCompileTime
Definition: BandMatrix.h:27
@ Options
Definition: BandMatrix.h:32
Block< CoefficientsType, 1, Dynamic > diagonal(Index i)
Definition: BandMatrix.h:119
void evalTo(Dest &dst) const
Definition: BandMatrix.h:132
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: EigenBase.h:61
const Block< const CoefficientsType, 1, Dynamic > diagonal(Index i) const
Definition: BandMatrix.h:125
Index supers() const
Definition: BandMatrix.h:52
Index diagonalLength(Index i) const
Definition: BandMatrix.h:147
@ SizeAtCompileTime
Definition: BandMatrix.h:43
@ DataRowsAtCompileTime
Definition: BandMatrix.h:42
Block< CoefficientsType, Dynamic, 1 > col(Index i)
Definition: BandMatrix.h:66
const Block< const CoefficientsType, 1, SizeAtCompileTime > diagonal() const
Definition: BandMatrix.h:84
Block< CoefficientsType, 1, SizeAtCompileTime > diagonal()
Definition: BandMatrix.h:79
const DiagonalIntReturnType< N >::Type diagonal() const
Definition: BandMatrix.h:114
DenseMatrixType toDenseMatrix() const
Definition: BandMatrix.h:140
CoefficientsType & coeffs()
Definition: BandMatrix.h:61
const CoefficientsType & coeffs() const
Definition: BandMatrix.h:58
Matrix< Scalar, RowsAtCompileTime, ColsAtCompileTime > DenseMatrixType
Definition: BandMatrix.h:35
EigenBase< Derived > Base
Definition: BandMatrix.h:38
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: EigenBase.h:59
internal::traits< Derived >::Scalar Scalar
Definition: BandMatrix.h:34
Definition: BandMatrix.h:249
internal::traits< BandMatrixWrapper >::StorageIndex StorageIndex
Definition: BandMatrix.h:253
BandMatrixWrapper(const CoefficientsType &coeffs, Index rows=Rows_, Index cols=Cols_, Index supers=Supers_, Index subs=Subs_)
Definition: BandMatrix.h:255
internal::variable_if_dynamic< Index, Supers_ > m_supers
Definition: BandMatrix.h:279
EIGEN_CONSTEXPR Index subs() const
Definition: BandMatrix.h:272
EIGEN_CONSTEXPR Index supers() const
Definition: BandMatrix.h:269
internal::variable_if_dynamic< Index, Rows_ > m_rows
Definition: BandMatrix.h:278
internal::traits< BandMatrixWrapper >::Scalar Scalar
Definition: BandMatrix.h:251
const CoefficientsType & m_coeffs
Definition: BandMatrix.h:277
const CoefficientsType & coeffs() const
Definition: BandMatrix.h:274
EIGEN_CONSTEXPR Index rows() const
Definition: BandMatrix.h:263
internal::variable_if_dynamic< Index, Subs_ > m_subs
Definition: BandMatrix.h:280
internal::traits< BandMatrixWrapper >::CoefficientsType CoefficientsType
Definition: BandMatrix.h:252
EIGEN_CONSTEXPR Index cols() const
Definition: BandMatrix.h:266
Represents a rectangular matrix with a banded storage.
Definition: BandMatrix.h:193
EIGEN_CONSTEXPR Index subs() const
Definition: BandMatrix.h:212
CoefficientsType & coeffs()
Definition: BandMatrix.h:215
BandMatrix(Index rows=Rows, Index cols=Cols, Index supers=Supers, Index subs=Subs)
Definition: BandMatrix.h:199
internal::traits< BandMatrix >::StorageIndex StorageIndex
Definition: BandMatrix.h:196
internal::variable_if_dynamic< Index, Supers > m_supers
Definition: BandMatrix.h:220
internal::variable_if_dynamic< Index, Rows > m_rows
Definition: BandMatrix.h:219
CoefficientsType m_coeffs
Definition: BandMatrix.h:218
internal::traits< BandMatrix >::CoefficientsType CoefficientsType
Definition: BandMatrix.h:197
internal::traits< BandMatrix >::Scalar Scalar
Definition: BandMatrix.h:195
EIGEN_CONSTEXPR Index supers() const
Definition: BandMatrix.h:209
const CoefficientsType & coeffs() const
Definition: BandMatrix.h:214
EIGEN_CONSTEXPR Index cols() const
Definition: BandMatrix.h:206
internal::variable_if_dynamic< Index, Subs > m_subs
Definition: BandMatrix.h:221
EIGEN_CONSTEXPR Index rows() const
Definition: BandMatrix.h:203
Represents a tridiagonal matrix with a compact banded storage.
Definition: BandMatrix.h:296
const Base::template DiagonalIntReturnType< 1 >::Type super() const
Definition: BandMatrix.h:304
Base::template DiagonalIntReturnType< 1 >::Type super()
Definition: BandMatrix.h:303
const Base::template DiagonalIntReturnType<-1 >::Type sub() const
Definition: BandMatrix.h:308
TridiagonalMatrix(Index size=Size)
Definition: BandMatrix.h:301
Base::StorageIndex StorageIndex
Definition: BandMatrix.h:298
BandMatrix< Scalar, Size, Size, Options &SelfAdjoint ? 0 :1, 1, Options|RowMajor > Base
Definition: BandMatrix.h:297
Base::template DiagonalIntReturnType<-1 >::Type sub()
Definition: BandMatrix.h:307
@ N
Definition: constructor.cpp:22
#define min(a, b)
Definition: datatypes.h:22
#define max(a, b)
Definition: datatypes.h:23
@ SelfAdjoint
Definition: Constants.h:227
@ ColMajor
Definition: Constants.h:318
@ RowMajor
Definition: Constants.h:320
const unsigned int LvalueBit
Definition: Constants.h:148
return int(ret)+1
constexpr int min_size_prefer_dynamic(A a, B b)
Definition: Meta.h:668
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
const int Dynamic
Definition: Constants.h:25
Extend namespace for flags.
Definition: fsi_chan_precond_driver.cc:56
Definition: Eigen_Colamd.h:49
void start(const unsigned &i)
(Re-)start i-th timer
Definition: oomph_utilities.cc:243
Type
Type of JSON value.
Definition: rapidjson.h:513
Definition: Constants.h:540
Definition: Constants.h:519
Definition: EigenBase.h:33
constexpr EIGEN_DEVICE_FUNC Derived & derived()
Definition: EigenBase.h:49
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: EigenBase.h:61
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:43
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index size() const EIGEN_NOEXCEPT
Definition: EigenBase.h:64
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: EigenBase.h:59
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
EigenBase2EigenBase Kind
Definition: BandMatrix.h:331
Definition: AssignEvaluator.h:760
Block< CoefficientsType, 1, DiagonalSize > BuildType
Definition: BandMatrix.h:101
std::conditional_t< Conjugate, CwiseUnaryOp< internal::scalar_conjugate_op< Scalar >, BuildType >, BuildType > Type
Definition: BandMatrix.h:103
Definition: BandMatrix.h:315
Definition: AssignEvaluator.h:757
Definition: CoreEvaluators.h:87
Definition: CoreEvaluators.h:95
Matrix< Scalar, DataRowsAtCompileTime, ColsAtCompileTime, int(Options) &int(RowMajor) ? RowMajor :ColMajor > CoefficientsType
Definition: BandMatrix.h:189
Definition: ForwardDeclarations.h:21