WPILibC++ 2023.4.3
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
13namespace Eigen {
14
15namespace internal {
16
17template<typename Derived>
18class BandMatrixBase : public EigenBase<Derived>
19{
20 public:
21
22 enum {
32 };
35 typedef typename DenseMatrixType::StorageIndex StorageIndex;
38
39 protected:
40 enum {
42 ? 1 + Supers + Subs
45 };
46
47 public:
48
49 using Base::derived;
50 using Base::rows;
51 using Base::cols;
52
53 /** \returns the number of super diagonals */
54 inline Index supers() const { return derived().supers(); }
55
56 /** \returns the number of sub diagonals */
57 inline Index subs() const { return derived().subs(); }
58
59 /** \returns an expression of the underlying coefficient matrix */
60 inline const CoefficientsType& coeffs() const { return derived().coeffs(); }
61
62 /** \returns an expression of the underlying coefficient matrix */
63 inline CoefficientsType& coeffs() { return derived().coeffs(); }
64
65 /** \returns a vector expression of the \a i -th column,
66 * only the meaningful part is returned.
67 * \warning the internal storage must be column major. */
69 {
70 EIGEN_STATIC_ASSERT((int(Options) & int(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
83 /** \returns a vector expression of the main diagonal */
86
87 /** \returns a vector expression of the main diagonal (const version) */
90
91 template<int Index> struct DiagonalIntReturnType {
92 enum {
93 ReturnOpposite = (int(Options) & int(SelfAdjoint)) && (((Index) > 0 && Supers == 0) || ((Index) < 0 && Subs == 0)),
97 ? Dynamic
98 : (ActualIndex<0
101 };
103 typedef typename internal::conditional<Conjugate,
106 };
107
108 /** \returns a vector expression of the \a N -th sub or super diagonal */
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
114 /** \returns a vector expression of the \a N -th sub or super diagonal */
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
120 /** \returns a vector expression of the \a i -th sub or super diagonal */
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
127 /** \returns a vector expression of the \a i -th sub or super diagonal */
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
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
158/**
159 * \class BandMatrix
160 * \ingroup Core_Module
161 *
162 * \brief Represents a rectangular matrix with a banded storage
163 *
164 * \tparam _Scalar Numeric type, i.e. float, double, int
165 * \tparam _Rows Number of rows, or \b Dynamic
166 * \tparam _Cols Number of columns, or \b Dynamic
167 * \tparam _Supers Number of super diagonal
168 * \tparam _Subs Number of sub diagonal
169 * \tparam _Options A combination of either \b #RowMajor or \b #ColMajor, and of \b #SelfAdjoint
170 * The former controls \ref TopicStorageOrders "storage order", and defaults to
171 * column-major. The latter controls whether the matrix represents a selfadjoint
172 * matrix in which case either Supers of Subs have to be null.
173 *
174 * \sa class TridiagonalMatrix
175 */
176
177template<typename _Scalar, int _Rows, int _Cols, int _Supers, int _Subs, int _Options>
178struct traits<BandMatrix<_Scalar,_Rows,_Cols,_Supers,_Subs,_Options> >
179{
180 typedef _Scalar Scalar;
183 enum {
190 Supers = _Supers,
191 Subs = _Subs,
192 Options = _Options,
194 };
196};
197
198template<typename _Scalar, int Rows, int Cols, int Supers, int Subs, int Options>
199class BandMatrix : public BandMatrixBase<BandMatrix<_Scalar,Rows,Cols,Supers,Subs,Options> >
200{
201 public:
202
206
208 : m_coeffs(1+supers+subs,cols),
209 m_rows(rows), m_supers(supers), m_subs(subs)
210 {
211 }
212
213 /** \returns the number of columns */
214 inline EIGEN_CONSTEXPR Index rows() const { return m_rows.value(); }
215
216 /** \returns the number of rows */
217 inline EIGEN_CONSTEXPR Index cols() const { return m_coeffs.cols(); }
218
219 /** \returns the number of super diagonals */
220 inline EIGEN_CONSTEXPR Index supers() const { return m_supers.value(); }
221
222 /** \returns the number of sub diagonals */
223 inline EIGEN_CONSTEXPR 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
234};
235
236template<typename _CoefficientsType,int _Rows, int _Cols, int _Supers, int _Subs,int _Options>
238
239template<typename _CoefficientsType,int _Rows, int _Cols, int _Supers, int _Subs,int _Options>
240struct 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 {
252 Supers = _Supers,
253 Subs = _Subs,
254 Options = _Options,
256 };
257 typedef _CoefficientsType CoefficientsType;
258};
259
260template<typename _CoefficientsType,int _Rows, int _Cols, int _Supers, int _Subs,int _Options>
261class BandMatrixWrapper : public BandMatrixBase<BandMatrixWrapper<_CoefficientsType,_Rows,_Cols,_Supers,_Subs,_Options> >
262{
263 public:
264
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 {
274 //internal::assert(coeffs.cols()==cols() && (supers()+subs()+1)==coeffs.rows());
275 }
276
277 /** \returns the number of columns */
278 inline EIGEN_CONSTEXPR Index rows() const { return m_rows.value(); }
279
280 /** \returns the number of rows */
281 inline EIGEN_CONSTEXPR Index cols() const { return m_coeffs.cols(); }
282
283 /** \returns the number of super diagonals */
284 inline EIGEN_CONSTEXPR Index supers() const { return m_supers.value(); }
285
286 /** \returns the number of sub diagonals */
287 inline EIGEN_CONSTEXPR Index subs() const { return m_subs.value(); }
288
289 inline const CoefficientsType& coeffs() const { return m_coeffs; }
290
291 protected:
292
297};
298
299/**
300 * \class TridiagonalMatrix
301 * \ingroup Core_Module
302 *
303 * \brief Represents a tridiagonal matrix with a compact banded storage
304 *
305 * \tparam Scalar Numeric type, i.e. float, double, int
306 * \tparam Size Number of rows and cols, or \b Dynamic
307 * \tparam Options Can be 0 or \b SelfAdjoint
308 *
309 * \sa class BandMatrix
310 */
311template<typename Scalar, int Size, int Options>
312class TridiagonalMatrix : public BandMatrix<Scalar,Size,Size,Options&SelfAdjoint?0:1,1,Options|RowMajor>
313{
315 typedef typename Base::StorageIndex StorageIndex;
316 public:
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
331struct BandShape {};
332
333template<typename _Scalar, int _Rows, int _Cols, int _Supers, int _Subs, int _Options>
334struct evaluator_traits<BandMatrix<_Scalar,_Rows,_Cols,_Supers,_Subs,_Options> >
335 : public evaluator_traits_base<BandMatrix<_Scalar,_Rows,_Cols,_Supers,_Subs,_Options> >
336{
338};
339
340template<typename _CoefficientsType,int _Rows, int _Cols, int _Supers, int _Subs,int _Options>
341struct evaluator_traits<BandMatrixWrapper<_CoefficientsType,_Rows,_Cols,_Supers,_Subs,_Options> >
342 : public evaluator_traits_base<BandMatrixWrapper<_CoefficientsType,_Rows,_Cols,_Supers,_Subs,_Options> >
343{
345};
346
348
349} // end namespace internal
350
351} // end namespace Eigen
352
353#endif // EIGEN_BANDMATRIX_H
#define EIGEN_SIZE_MIN_PREFER_DYNAMIC(a, b)
Definition: Macros.h:1304
#define EIGEN_CONSTEXPR
Definition: Macros.h:797
#define EIGEN_UNUSED_VARIABLE(var)
Definition: Macros.h:1086
#define eigen_assert(x)
Definition: Macros.h:1047
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition: StaticAssert.h:127
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:105
Definition: ForwardDeclarations.h:87
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:56
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:180
Definition: BandMatrix.h:19
Index subs() const
Definition: BandMatrix.h:57
internal::traits< Derived >::CoefficientsType CoefficientsType
Definition: BandMatrix.h:36
DenseMatrixType::StorageIndex StorageIndex
Definition: BandMatrix.h:35
Block< CoefficientsType, Dynamic, 1 > col(Index i)
Definition: BandMatrix.h:68
void evalTo(Dest &dst) const
Definition: BandMatrix.h:134
Matrix< Scalar, RowsAtCompileTime, ColsAtCompileTime > DenseMatrixType
Definition: BandMatrix.h:34
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: EigenBase.h:63
Index supers() const
Definition: BandMatrix.h:54
Index diagonalLength(Index i) const
Definition: BandMatrix.h:154
const Block< const CoefficientsType, 1, Dynamic > diagonal(Index i) const
Definition: BandMatrix.h:128
const DiagonalIntReturnType< N >::Type diagonal() const
Definition: BandMatrix.h:115
const CoefficientsType & coeffs() const
Definition: BandMatrix.h:60
CoefficientsType & coeffs()
Definition: BandMatrix.h:63
@ SizeAtCompileTime
Definition: BandMatrix.h:44
@ DataRowsAtCompileTime
Definition: BandMatrix.h:41
DiagonalIntReturnType< N >::Type diagonal()
Definition: BandMatrix.h:109
DenseMatrixType toDenseMatrix() const
Definition: BandMatrix.h:145
EIGEN_DEVICE_FUNC Derived & derived()
Definition: EigenBase.h:46
Block< CoefficientsType, 1, SizeAtCompileTime > diagonal()
Definition: BandMatrix.h:84
Block< CoefficientsType, 1, Dynamic > diagonal(Index i)
Definition: BandMatrix.h:121
@ MaxColsAtCompileTime
Definition: BandMatrix.h:28
@ RowsAtCompileTime
Definition: BandMatrix.h:25
@ Supers
Definition: BandMatrix.h:29
@ Flags
Definition: BandMatrix.h:23
@ CoeffReadCost
Definition: BandMatrix.h:24
@ Subs
Definition: BandMatrix.h:30
@ MaxRowsAtCompileTime
Definition: BandMatrix.h:27
@ ColsAtCompileTime
Definition: BandMatrix.h:26
@ Options
Definition: BandMatrix.h:31
EigenBase< Derived > Base
Definition: BandMatrix.h:37
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: EigenBase.h:60
internal::traits< Derived >::Scalar Scalar
Definition: BandMatrix.h:33
const Block< const CoefficientsType, 1, SizeAtCompileTime > diagonal() const
Definition: BandMatrix.h:88
Represents a rectangular matrix with a banded storage.
Definition: BandMatrix.h:200
internal::variable_if_dynamic< Index, Rows > m_rows
Definition: BandMatrix.h:231
internal::traits< BandMatrix >::Scalar Scalar
Definition: BandMatrix.h:203
internal::traits< BandMatrix >::StorageIndex StorageIndex
Definition: BandMatrix.h:204
const CoefficientsType & coeffs() const
Definition: BandMatrix.h:225
internal::variable_if_dynamic< Index, Supers > m_supers
Definition: BandMatrix.h:232
internal::traits< BandMatrix >::CoefficientsType CoefficientsType
Definition: BandMatrix.h:205
EIGEN_CONSTEXPR Index supers() const
Definition: BandMatrix.h:220
internal::variable_if_dynamic< Index, Subs > m_subs
Definition: BandMatrix.h:233
CoefficientsType & coeffs()
Definition: BandMatrix.h:226
CoefficientsType m_coeffs
Definition: BandMatrix.h:230
EIGEN_CONSTEXPR Index rows() const
Definition: BandMatrix.h:214
EIGEN_CONSTEXPR Index subs() const
Definition: BandMatrix.h:223
BandMatrix(Index rows=Rows, Index cols=Cols, Index supers=Supers, Index subs=Subs)
Definition: BandMatrix.h:207
EIGEN_CONSTEXPR Index cols() const
Definition: BandMatrix.h:217
Definition: BandMatrix.h:262
BandMatrixWrapper(const CoefficientsType &coeffs, Index rows=_Rows, Index cols=_Cols, Index supers=_Supers, Index subs=_Subs)
Definition: BandMatrix.h:269
const CoefficientsType & coeffs() const
Definition: BandMatrix.h:289
EIGEN_CONSTEXPR Index supers() const
Definition: BandMatrix.h:284
internal::traits< BandMatrixWrapper >::StorageIndex StorageIndex
Definition: BandMatrix.h:267
internal::traits< BandMatrixWrapper >::Scalar Scalar
Definition: BandMatrix.h:265
internal::variable_if_dynamic< Index, _Supers > m_supers
Definition: BandMatrix.h:295
internal::variable_if_dynamic< Index, _Rows > m_rows
Definition: BandMatrix.h:294
EIGEN_CONSTEXPR Index rows() const
Definition: BandMatrix.h:278
internal::variable_if_dynamic< Index, _Subs > m_subs
Definition: BandMatrix.h:296
const CoefficientsType & m_coeffs
Definition: BandMatrix.h:293
internal::traits< BandMatrixWrapper >::CoefficientsType CoefficientsType
Definition: BandMatrix.h:266
EIGEN_CONSTEXPR Index cols() const
Definition: BandMatrix.h:281
EIGEN_CONSTEXPR Index subs() const
Definition: BandMatrix.h:287
Represents a tridiagonal matrix with a compact banded storage.
Definition: BandMatrix.h:313
Base::template DiagonalIntReturnType< 1 >::Type super()
Definition: BandMatrix.h:319
const Base::template DiagonalIntReturnType< 1 >::Type super() const
Definition: BandMatrix.h:321
Base::template DiagonalIntReturnType<-1 >::Type sub()
Definition: BandMatrix.h:323
const Base::template DiagonalIntReturnType<-1 >::Type sub() const
Definition: BandMatrix.h:325
TridiagonalMatrix(Index size=Size)
Definition: BandMatrix.h:317
type
Definition: core.h:575
@ SelfAdjoint
Used in BandMatrix and SelfAdjointView to indicate that the matrix is self-adjoint.
Definition: Constants.h:225
@ ColMajor
Storage order is column major (see TopicStorageOrders).
Definition: Constants.h:319
@ RowMajor
Storage order is row major (see TopicStorageOrders).
Definition: Constants.h:321
const unsigned int LvalueBit
Means the expression has a coeffRef() method, i.e.
Definition: Constants.h:144
constexpr common_t< T1, T2 > max(const T1 x, const T2 y) noexcept
Compile-time pairwise maximum function.
Definition: max.hpp:35
constexpr common_t< T1, T2 > min(const T1 x, const T2 y) noexcept
Compile-time pairwise minimum function.
Definition: min.hpp:35
Type
Definition: Constants.h:471
Namespace containing all symbols from the Eigen library.
Definition: MatrixExponential.h:16
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
const int Dynamic
This value means that a positive quantity (e.g., a size) is not known at compile-time,...
Definition: Constants.h:22
Definition: Eigen_Colamd.h:50
The type used to identify a dense storage.
Definition: Constants.h:507
Definition: Constants.h:528
Common base class for all classes T such that MatrixBase has an operator=(T) and a constructor Matrix...
Definition: EigenBase.h:30
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: EigenBase.h:63
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:39
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index size() const EIGEN_NOEXCEPT
Definition: EigenBase.h:67
EIGEN_DEVICE_FUNC Derived & derived()
Definition: EigenBase.h:46
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: EigenBase.h:60
Holds information about the various numeric (i.e.
Definition: NumTraits.h:233
EigenBase2EigenBase Kind
Definition: BandMatrix.h:347
Definition: AssignEvaluator.h:817
internal::conditional< Conjugate, CwiseUnaryOp< internal::scalar_conjugate_op< Scalar >, BuildType >, BuildType >::type Type
Definition: BandMatrix.h:105
Block< CoefficientsType, 1, DiagonalSize > BuildType
Definition: BandMatrix.h:102
Definition: BandMatrix.h:331
Definition: AssignEvaluator.h:815
Definition: Meta.h:109
Definition: CoreEvaluators.h:71
Definition: CoreEvaluators.h:80
Matrix< Scalar, DataRowsAtCompileTime, ColsAtCompileTime, int(Options) &int(RowMajor) ? RowMajor :ColMajor > CoefficientsType
Definition: BandMatrix.h:195
Definition: ForwardDeclarations.h:17
Definition: Meta.h:96