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>;
109 inline typename DiagonalIntReturnType<N>::Type diagonal() {
110 return typename DiagonalIntReturnType<N>::BuildType(coeffs(), supers() - N, (std::max)(0, N), 1, diagonalLength(N));
115 inline const typename DiagonalIntReturnType<N>::Type diagonal()
const {
116 return typename DiagonalIntReturnType<N>::BuildType(coeffs(), supers() - N, (std::max)(0, N), 1, diagonalLength(N));
120 inline Block<CoefficientsType, 1, Dynamic> diagonal(
Index i) {
121 eigen_assert((i < 0 && -i <= subs()) || (i >= 0 && i <= supers()));
122 return Block<CoefficientsType, 1, Dynamic>(coeffs(), supers() - i, std::max<Index>(0, i), 1, diagonalLength(i));
126 inline const Block<const CoefficientsType, 1, Dynamic> diagonal(
Index i)
const {
127 eigen_assert((i < 0 && -i <= subs()) || (i >= 0 && i <= supers()));
128 return Block<const CoefficientsType, 1, Dynamic>(coeffs(), supers() - i, std::max<Index>(0, i), 1,
132 template <
typename Dest>
133 inline void evalTo(Dest& dst)
const {
134 dst.resize(rows(), cols());
136 dst.diagonal() = diagonal();
137 for (
Index i = 1; i <= supers(); ++i) dst.diagonal(i) = diagonal(i);
138 for (
Index i = 1; i <= subs(); ++i) dst.diagonal(-i) = diagonal(-i);
141 DenseMatrixType toDenseMatrix()
const {
142 DenseMatrixType res(rows(), cols());
149 return i < 0 ? (std::min)(cols(), rows() + i) : (std::min)(rows(), cols() - i);
172template <
typename Scalar_,
int Rows_,
int Cols_,
int Supers_,
int Subs_,
int Options_>
173struct traits<BandMatrix<Scalar_, Rows_, Cols_, Supers_, Subs_, Options_> > {
174 using Scalar = Scalar_;
175 using StorageKind = Dense;
176 using StorageIndex = Eigen::Index;
178 CoeffReadCost = NumTraits<Scalar>::ReadCost,
179 RowsAtCompileTime = Rows_,
180 ColsAtCompileTime = Cols_,
181 MaxRowsAtCompileTime = Rows_,
182 MaxColsAtCompileTime = Cols_,
187 DataRowsAtCompileTime = ((Supers != Dynamic) && (Subs != Dynamic)) ? 1 + Supers + Subs : Dynamic
189 using CoefficientsType =
190 Matrix<Scalar, DataRowsAtCompileTime, ColsAtCompileTime, int(Options) & int(RowMajor) ?
RowMajor :
ColMajor>;
193template <
typename Scalar_,
int Rows,
int Cols,
int Supers,
int Subs,
int Options>
194class BandMatrix :
public BandMatrixBase<BandMatrix<Scalar_, Rows, Cols, Supers, Subs, Options> > {
196 using Scalar =
typename internal::traits<BandMatrix>::Scalar;
197 using StorageIndex =
typename internal::traits<BandMatrix>::StorageIndex;
198 using CoefficientsType =
typename internal::traits<BandMatrix>::CoefficientsType;
207 constexpr Index cols()
const {
return m_coeffs.cols(); }
215 inline const CoefficientsType& coeffs()
const {
return m_coeffs; }
216 inline CoefficientsType& coeffs() {
return m_coeffs; }
219 CoefficientsType m_coeffs;
220 internal::variable_if_dynamic<Index, Rows> m_rows;
221 internal::variable_if_dynamic<Index, Supers> m_supers;
222 internal::variable_if_dynamic<Index, Subs> m_subs;
225template <
typename CoefficientsType_,
int Rows_,
int Cols_,
int Supers_,
int Subs_,
int Options_>
226class BandMatrixWrapper;
228template <
typename CoefficientsType_,
int Rows_,
int Cols_,
int Supers_,
int Subs_,
int Options_>
229struct traits<BandMatrixWrapper<CoefficientsType_, Rows_, Cols_, Supers_, Subs_, Options_> > {
230 using Scalar =
typename CoefficientsType_::Scalar;
231 using StorageKind =
typename CoefficientsType_::StorageKind;
232 using StorageIndex =
typename CoefficientsType_::StorageIndex;
234 CoeffReadCost = internal::traits<CoefficientsType_>::CoeffReadCost,
235 RowsAtCompileTime = Rows_,
236 ColsAtCompileTime = Cols_,
237 MaxRowsAtCompileTime = Rows_,
238 MaxColsAtCompileTime = Cols_,
243 DataRowsAtCompileTime = ((Supers != Dynamic) && (Subs != Dynamic)) ? 1 + Supers + Subs : Dynamic
245 using CoefficientsType = CoefficientsType_;
248template <
typename CoefficientsType_,
int Rows_,
int Cols_,
int Supers_,
int Subs_,
int Options_>
249class BandMatrixWrapper
250 :
public BandMatrixBase<BandMatrixWrapper<CoefficientsType_, Rows_, Cols_, Supers_, Subs_, Options_> > {
252 using Scalar =
typename internal::traits<BandMatrixWrapper>::Scalar;
253 using CoefficientsType =
typename internal::traits<BandMatrixWrapper>::CoefficientsType;
254 using StorageIndex =
typename internal::traits<BandMatrixWrapper>::StorageIndex;
256 explicit inline BandMatrixWrapper(
const CoefficientsType& coeffs, Index rows = Rows_, Index cols = Cols_,
257 Index supers = Supers_, Index subs = Subs_)
258 : m_coeffs(coeffs), m_rows(rows), m_supers(supers), m_subs(subs) {
259 EIGEN_UNUSED_VARIABLE(cols);
264 constexpr Index rows()
const {
return m_rows.value(); }
267 constexpr Index cols()
const {
return m_coeffs.cols(); }
270 constexpr Index supers()
const {
return m_supers.value(); }
273 constexpr Index subs()
const {
return m_subs.value(); }
275 inline const CoefficientsType& coeffs()
const {
return m_coeffs; }
278 const CoefficientsType& m_coeffs;
279 internal::variable_if_dynamic<Index, Rows_> m_rows;
280 internal::variable_if_dynamic<Index, Supers_> m_supers;
281 internal::variable_if_dynamic<Index, Subs_> m_subs;
296template <
typename Scalar,
int Size,
int Options>
297class TridiagonalMatrix :
public BandMatrix<Scalar, Size, Size, Options & SelfAdjoint ? 0 : 1, 1, Options | RowMajor> {
298 using Base = BandMatrix<Scalar, Size, Size, Options & SelfAdjoint ? 0 : 1, 1, Options | RowMajor>;
299 using StorageIndex =
typename Base::StorageIndex;
304 inline typename Base::template DiagonalIntReturnType<1>::Type super() {
return Base::template diagonal<1>(); }
305 inline const typename Base::template DiagonalIntReturnType<1>::Type super()
const {
306 return Base::template diagonal<1>();
308 inline typename Base::template DiagonalIntReturnType<-1>::Type sub() {
return Base::template diagonal<-1>(); }
309 inline const typename Base::template DiagonalIntReturnType<-1>::Type sub()
const {
310 return Base::template diagonal<-1>();
314template <
typename Scalar_,
int Rows_,
int Cols_,
int Supers_,
int Subs_,
int Options_>
315struct evaluator_traits<
BandMatrix<Scalar_, Rows_, Cols_, Supers_, Subs_, Options_> >
316 :
public evaluator_traits_base<BandMatrix<Scalar_, Rows_, Cols_, Supers_, Subs_, Options_> > {
317 using Shape = BandShape;
320template <
typename CoefficientsType_,
int Rows_,
int Cols_,
int Supers_,
int Subs_,
int Options_>
321struct evaluator_traits<BandMatrixWrapper<CoefficientsType_, Rows_, Cols_, Supers_, Subs_, Options_> >
322 :
public evaluator_traits_base<BandMatrixWrapper<CoefficientsType_, Rows_, Cols_, Supers_, Subs_, Options_> > {
323 using Shape = BandShape;
327struct AssignmentKind<DenseShape, BandShape> {
328 using Kind = EigenBase2EigenBase;
Represents a rectangular matrix with a banded storage.
Definition BandMatrix.h:194
constexpr Index cols() const
Definition BandMatrix.h:207
constexpr Index supers() const
Definition BandMatrix.h:210
constexpr Index subs() const
Definition BandMatrix.h:213
constexpr Index rows() const
Definition BandMatrix.h:204
@ 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