11#ifndef EIGEN_BANDMATRIX_H
12#define EIGEN_BANDMATRIX_H
15#include "./InternalHeaderCheck.h"
21template <
typename Derived>
22class BandMatrixBase :
public EigenBase<Derived> {
25 Flags = internal::traits<Derived>::Flags,
26 CoeffReadCost = internal::traits<Derived>::CoeffReadCost,
27 RowsAtCompileTime = internal::traits<Derived>::RowsAtCompileTime,
28 ColsAtCompileTime = internal::traits<Derived>::ColsAtCompileTime,
29 MaxRowsAtCompileTime = internal::traits<Derived>::MaxRowsAtCompileTime,
30 MaxColsAtCompileTime = internal::traits<Derived>::MaxColsAtCompileTime,
31 Supers = internal::traits<Derived>::Supers,
32 Subs = internal::traits<Derived>::Subs,
33 Options = internal::traits<Derived>::Options
35 using Scalar =
typename internal::traits<Derived>::Scalar;
36 using DenseMatrixType = Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime>;
37 using StorageIndex =
typename DenseMatrixType::StorageIndex;
38 using CoefficientsType =
typename internal::traits<Derived>::CoefficientsType;
39 using Base = EigenBase<Derived>;
43 DataRowsAtCompileTime = ((Supers != Dynamic) && (Subs != Dynamic)) ? 1 + Supers + Subs : Dynamic,
44 SizeAtCompileTime = min_size_prefer_dynamic(RowsAtCompileTime, ColsAtCompileTime)
53 inline Index supers()
const {
return derived().supers(); }
56 inline Index subs()
const {
return derived().subs(); }
59 inline const CoefficientsType& coeffs()
const {
return derived().coeffs(); }
62 inline CoefficientsType& coeffs() {
return derived().coeffs(); }
67 inline Block<CoefficientsType, Dynamic, 1> col(
Index i) {
68 EIGEN_STATIC_ASSERT((
int(Options) &
int(
RowMajor)) == 0, THIS_METHOD_IS_ONLY_FOR_COLUMN_MAJOR_MATRICES);
70 Index len = coeffs().rows();
73 len = (std::min)(rows(), std::max<Index>(0, coeffs().rows() - (supers() - i)));
74 }
else if (i >= rows() - subs())
75 len = std::max<Index>(0, coeffs().rows() - (i + 1 - rows() + subs()));
76 return Block<CoefficientsType, Dynamic, 1>(coeffs(), start, i, len, 1);
80 inline Block<CoefficientsType, 1, SizeAtCompileTime> diagonal() {
81 return Block<CoefficientsType, 1, SizeAtCompileTime>(coeffs(), supers(), 0, 1, (std::min)(rows(), cols()));
85 inline const Block<const CoefficientsType, 1, SizeAtCompileTime> diagonal()
const {
86 return Block<const CoefficientsType, 1, SizeAtCompileTime>(coeffs(), supers(), 0, 1, (std::min)(rows(), cols()));
90 struct DiagonalIntReturnType {
94 Conjugate = ReturnOpposite && NumTraits<Scalar>::IsComplex,
97 (RowsAtCompileTime == Dynamic || ColsAtCompileTime == Dynamic)
99 : (ActualIndex < 0 ? min_size_prefer_dynamic(ColsAtCompileTime, RowsAtCompileTime + ActualIndex)
100 : min_size_prefer_dynamic(RowsAtCompileTime, ColsAtCompileTime - ActualIndex))
102 using BuildType = Block<CoefficientsType, 1, DiagonalSize>;
104 std::conditional_t<Conjugate, CwiseUnaryOp<internal::scalar_conjugate_op<Scalar>, BuildType>, BuildType>;
105 using ConstBuildType = Block<const CoefficientsType, 1, DiagonalSize>;
106 using ConstType = std::conditional_t<Conjugate, CwiseUnaryOp<internal::scalar_conjugate_op<Scalar>, ConstBuildType>,
113 inline typename DiagonalIntReturnType<N>::Type diagonal() {
114 constexpr int ActualN = DiagonalIntReturnType<N>::ActualIndex;
115 return typename DiagonalIntReturnType<N>::Type(
typename DiagonalIntReturnType<N>::BuildType(
116 coeffs(), supers() - ActualN, (std::max)(0, ActualN), 1, diagonalLength(ActualN)));
122 inline const typename DiagonalIntReturnType<N>::ConstType diagonal()
const {
123 constexpr int ActualN = DiagonalIntReturnType<N>::ActualIndex;
124 return typename DiagonalIntReturnType<N>::ConstType(
typename DiagonalIntReturnType<N>::ConstBuildType(
125 coeffs(), supers() - ActualN, (std::max)(0, ActualN), 1, diagonalLength(ActualN)));
129 inline Block<CoefficientsType, 1, Dynamic> diagonal(
Index i) {
130 eigen_assert((i < 0 && -i <= subs()) || (i >= 0 && i <= supers()));
131 return Block<CoefficientsType, 1, Dynamic>(coeffs(), supers() - i, std::max<Index>(0, i), 1, diagonalLength(i));
135 inline const Block<const CoefficientsType, 1, Dynamic> diagonal(
Index i)
const {
136 eigen_assert((i < 0 && -i <= subs()) || (i >= 0 && i <= supers()));
137 return Block<const CoefficientsType, 1, Dynamic>(coeffs(), supers() - i, std::max<Index>(0, i), 1,
141 template <
typename Dest>
142 inline void evalTo(Dest& dst)
const {
143 dst.resize(rows(), cols());
145 dst.diagonal() = diagonal();
146 for (
Index i = 1; i <= supers(); ++i) dst.diagonal(i) = diagonal(i);
147 for (
Index i = 1; i <= subs(); ++i) dst.diagonal(-i) = diagonal(-i);
150 DenseMatrixType toDenseMatrix()
const {
151 DenseMatrixType res(rows(), cols());
158 return i < 0 ? (std::min)(cols(), rows() + i) : (std::min)(rows(), cols() - i);
181template <
typename Scalar_,
int Rows_,
int Cols_,
int Supers_,
int Subs_,
int Options_>
182struct traits<BandMatrix<Scalar_, Rows_, Cols_, Supers_, Subs_, Options_> > {
183 using Scalar = Scalar_;
184 using StorageKind = Dense;
185 using StorageIndex = Eigen::Index;
187 CoeffReadCost = NumTraits<Scalar>::ReadCost,
188 RowsAtCompileTime = Rows_,
189 ColsAtCompileTime = Cols_,
190 MaxRowsAtCompileTime = Rows_,
191 MaxColsAtCompileTime = Cols_,
196 DataRowsAtCompileTime = ((Supers != Dynamic) && (Subs != Dynamic)) ? 1 + Supers + Subs : Dynamic
198 using CoefficientsType =
199 Matrix<Scalar, DataRowsAtCompileTime, ColsAtCompileTime, int(Options) & int(RowMajor) ?
RowMajor :
ColMajor>;
202template <
typename Scalar_,
int Rows,
int Cols,
int Supers,
int Subs,
int Options>
203class BandMatrix :
public BandMatrixBase<BandMatrix<Scalar_, Rows, Cols, Supers, Subs, Options> > {
205 using Scalar =
typename internal::traits<BandMatrix>::Scalar;
206 using StorageIndex =
typename internal::traits<BandMatrix>::StorageIndex;
207 using CoefficientsType =
typename internal::traits<BandMatrix>::CoefficientsType;
216 constexpr Index cols()
const {
return m_coeffs.cols(); }
224 inline const CoefficientsType& coeffs()
const {
return m_coeffs; }
225 inline CoefficientsType& coeffs() {
return m_coeffs; }
228 CoefficientsType m_coeffs;
229 internal::variable_if_dynamic<Index, Rows> m_rows;
230 internal::variable_if_dynamic<Index, Supers> m_supers;
231 internal::variable_if_dynamic<Index, Subs> m_subs;
234template <
typename CoefficientsType_,
int Rows_,
int Cols_,
int Supers_,
int Subs_,
int Options_>
235class BandMatrixWrapper;
237template <
typename CoefficientsType_,
int Rows_,
int Cols_,
int Supers_,
int Subs_,
int Options_>
238struct traits<BandMatrixWrapper<CoefficientsType_, Rows_, Cols_, Supers_, Subs_, Options_> > {
239 using Scalar =
typename CoefficientsType_::Scalar;
240 using StorageKind =
typename CoefficientsType_::StorageKind;
241 using StorageIndex =
typename CoefficientsType_::StorageIndex;
243 CoeffReadCost = internal::traits<CoefficientsType_>::CoeffReadCost,
244 RowsAtCompileTime = Rows_,
245 ColsAtCompileTime = Cols_,
246 MaxRowsAtCompileTime = Rows_,
247 MaxColsAtCompileTime = Cols_,
252 DataRowsAtCompileTime = ((Supers != Dynamic) && (Subs != Dynamic)) ? 1 + Supers + Subs : Dynamic
254 using CoefficientsType = CoefficientsType_;
257template <
typename CoefficientsType_,
int Rows_,
int Cols_,
int Supers_,
int Subs_,
int Options_>
258class BandMatrixWrapper
259 :
public BandMatrixBase<BandMatrixWrapper<CoefficientsType_, Rows_, Cols_, Supers_, Subs_, Options_> > {
261 using Scalar =
typename internal::traits<BandMatrixWrapper>::Scalar;
262 using CoefficientsType =
typename internal::traits<BandMatrixWrapper>::CoefficientsType;
263 using StorageIndex =
typename internal::traits<BandMatrixWrapper>::StorageIndex;
265 explicit inline BandMatrixWrapper(
const CoefficientsType& coeffs, Index rows = Rows_, Index cols = Cols_,
266 Index supers = Supers_, Index subs = Subs_)
267 : m_coeffs(coeffs), m_rows(rows), m_supers(supers), m_subs(subs) {
268 EIGEN_UNUSED_VARIABLE(cols);
273 constexpr Index rows()
const {
return m_rows.value(); }
276 constexpr Index cols()
const {
return m_coeffs.cols(); }
279 constexpr Index supers()
const {
return m_supers.value(); }
282 constexpr Index subs()
const {
return m_subs.value(); }
284 inline const CoefficientsType& coeffs()
const {
return m_coeffs; }
287 const CoefficientsType& m_coeffs;
288 internal::variable_if_dynamic<Index, Rows_> m_rows;
289 internal::variable_if_dynamic<Index, Supers_> m_supers;
290 internal::variable_if_dynamic<Index, Subs_> m_subs;
305template <
typename Scalar,
int Size,
int Options>
306class TridiagonalMatrix :
public BandMatrix<Scalar, Size, Size, Options & SelfAdjoint ? 0 : 1, 1, Options | RowMajor> {
307 using Base = BandMatrix<Scalar, Size, Size, Options & SelfAdjoint ? 0 : 1, 1, Options | RowMajor>;
308 using StorageIndex =
typename Base::StorageIndex;
313 inline typename Base::template DiagonalIntReturnType<1>::Type super() {
return Base::template diagonal<1>(); }
314 inline const typename Base::template DiagonalIntReturnType<1>::ConstType super()
const {
315 return Base::template diagonal<1>();
317 inline typename Base::template DiagonalIntReturnType<-1>::Type sub() {
return Base::template diagonal<-1>(); }
318 inline const typename Base::template DiagonalIntReturnType<-1>::ConstType sub()
const {
319 return Base::template diagonal<-1>();
323template <
typename Scalar_,
int Rows_,
int Cols_,
int Supers_,
int Subs_,
int Options_>
324struct evaluator_traits<
BandMatrix<Scalar_, Rows_, Cols_, Supers_, Subs_, Options_> >
325 :
public evaluator_traits_base<BandMatrix<Scalar_, Rows_, Cols_, Supers_, Subs_, Options_> > {
326 using Shape = BandShape;
329template <
typename CoefficientsType_,
int Rows_,
int Cols_,
int Supers_,
int Subs_,
int Options_>
330struct evaluator_traits<BandMatrixWrapper<CoefficientsType_, Rows_, Cols_, Supers_, Subs_, Options_> >
331 :
public evaluator_traits_base<BandMatrixWrapper<CoefficientsType_, Rows_, Cols_, Supers_, Subs_, Options_> > {
332 using Shape = BandShape;
336struct AssignmentKind<DenseShape, BandShape> {
337 using Kind = EigenBase2EigenBase;
Represents a rectangular matrix with a banded storage.
Definition BandMatrix.h:203
constexpr Index cols() const
Definition BandMatrix.h:216
constexpr Index supers() const
Definition BandMatrix.h:219
constexpr Index subs() const
Definition BandMatrix.h:222
constexpr Index rows() const
Definition BandMatrix.h:213
@ SelfAdjoint
Definition Constants.h:228
@ ColMajor
Definition Constants.h:319
@ RowMajor
Definition Constants.h:321
constexpr unsigned int LvalueBit
Definition Constants.h:149
constexpr Index cols() const noexcept
Definition EigenBase.h:62
constexpr Derived & derived()
Definition EigenBase.h:50
constexpr Index rows() const noexcept
Definition EigenBase.h:60
constexpr Index size() const noexcept
Definition EigenBase.h:65
Eigen::Index Index
The interface type of indices.
Definition EigenBase.h:44