Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_BANDMATRIX_H
12#define EIGEN_BANDMATRIX_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20
21template <typename Derived>
22class BandMatrixBase : public EigenBase<Derived> {
23 public:
24 enum {
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
34 };
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>;
40
41 protected:
42 enum {
43 DataRowsAtCompileTime = ((Supers != Dynamic) && (Subs != Dynamic)) ? 1 + Supers + Subs : Dynamic,
44 SizeAtCompileTime = min_size_prefer_dynamic(RowsAtCompileTime, ColsAtCompileTime)
45 };
46
47 public:
48 using Base::cols;
49 using Base::derived;
50 using Base::rows;
51
53 inline Index supers() const { return derived().supers(); }
54
56 inline Index subs() const { return derived().subs(); }
57
59 inline const CoefficientsType& coeffs() const { return derived().coeffs(); }
60
62 inline CoefficientsType& coeffs() { return derived().coeffs(); }
63
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);
69 Index start = 0;
70 Index len = coeffs().rows();
71 if (i <= supers()) {
72 start = supers() - i;
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);
77 }
78
80 inline Block<CoefficientsType, 1, SizeAtCompileTime> diagonal() {
81 return Block<CoefficientsType, 1, SizeAtCompileTime>(coeffs(), supers(), 0, 1, (std::min)(rows(), cols()));
82 }
83
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()));
87 }
88
89 template <int Index>
90 struct DiagonalIntReturnType {
91 enum {
92 ReturnOpposite =
93 (int(Options) & int(SelfAdjoint)) && (((Index) > 0 && Supers == 0) || ((Index) < 0 && Subs == 0)),
94 Conjugate = ReturnOpposite && NumTraits<Scalar>::IsComplex,
95 ActualIndex = ReturnOpposite ? -Index : Index,
96 DiagonalSize =
97 (RowsAtCompileTime == Dynamic || ColsAtCompileTime == Dynamic)
98 ? Dynamic
99 : (ActualIndex < 0 ? min_size_prefer_dynamic(ColsAtCompileTime, RowsAtCompileTime + ActualIndex)
100 : min_size_prefer_dynamic(RowsAtCompileTime, ColsAtCompileTime - ActualIndex))
101 };
102 using BuildType = Block<CoefficientsType, 1, DiagonalSize>;
103 using Type =
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>,
107 ConstBuildType>;
108 };
109
112 template <int N>
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)));
117 }
118
121 template <int N>
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)));
126 }
127
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));
132 }
133
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,
138 diagonalLength(i));
139 }
140
141 template <typename Dest>
142 inline void evalTo(Dest& dst) const {
143 dst.resize(rows(), cols());
144 dst.setZero();
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);
148 }
149
150 DenseMatrixType toDenseMatrix() const {
151 DenseMatrixType res(rows(), cols());
152 evalTo(res);
153 return res;
154 }
155
156 protected:
157 inline Index diagonalLength(Index i) const {
158 return i < 0 ? (std::min)(cols(), rows() + i) : (std::min)(rows(), cols() - i);
159 }
160};
161
180
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;
186 enum {
187 CoeffReadCost = NumTraits<Scalar>::ReadCost,
188 RowsAtCompileTime = Rows_,
189 ColsAtCompileTime = Cols_,
190 MaxRowsAtCompileTime = Rows_,
191 MaxColsAtCompileTime = Cols_,
192 Flags = LvalueBit,
193 Supers = Supers_,
194 Subs = Subs_,
195 Options = Options_,
196 DataRowsAtCompileTime = ((Supers != Dynamic) && (Subs != Dynamic)) ? 1 + Supers + Subs : Dynamic
197 };
198 using CoefficientsType =
199 Matrix<Scalar, DataRowsAtCompileTime, ColsAtCompileTime, int(Options) & int(RowMajor) ? RowMajor : ColMajor>;
200};
201
202template <typename Scalar_, int Rows, int Cols, int Supers, int Subs, int Options>
203class BandMatrix : public BandMatrixBase<BandMatrix<Scalar_, Rows, Cols, Supers, Subs, Options> > {
204 public:
205 using Scalar = typename internal::traits<BandMatrix>::Scalar;
206 using StorageIndex = typename internal::traits<BandMatrix>::StorageIndex;
207 using CoefficientsType = typename internal::traits<BandMatrix>::CoefficientsType;
208
209 explicit inline BandMatrix(Index rows = Rows, Index cols = Cols, Index supers = Supers, Index subs = Subs)
210 : m_coeffs(1 + supers + subs, cols), m_rows(rows), m_supers(supers), m_subs(subs) {}
211
213 constexpr Index rows() const { return m_rows.value(); }
214
216 constexpr Index cols() const { return m_coeffs.cols(); }
217
219 constexpr Index supers() const { return m_supers.value(); }
220
222 constexpr Index subs() const { return m_subs.value(); }
223
224 inline const CoefficientsType& coeffs() const { return m_coeffs; }
225 inline CoefficientsType& coeffs() { return m_coeffs; }
226
227 protected:
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;
232};
233
234template <typename CoefficientsType_, int Rows_, int Cols_, int Supers_, int Subs_, int Options_>
235class BandMatrixWrapper;
236
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;
242 enum {
243 CoeffReadCost = internal::traits<CoefficientsType_>::CoeffReadCost,
244 RowsAtCompileTime = Rows_,
245 ColsAtCompileTime = Cols_,
246 MaxRowsAtCompileTime = Rows_,
247 MaxColsAtCompileTime = Cols_,
248 Flags = LvalueBit,
249 Supers = Supers_,
250 Subs = Subs_,
251 Options = Options_,
252 DataRowsAtCompileTime = ((Supers != Dynamic) && (Subs != Dynamic)) ? 1 + Supers + Subs : Dynamic
253 };
254 using CoefficientsType = CoefficientsType_;
255};
256
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_> > {
260 public:
261 using Scalar = typename internal::traits<BandMatrixWrapper>::Scalar;
262 using CoefficientsType = typename internal::traits<BandMatrixWrapper>::CoefficientsType;
263 using StorageIndex = typename internal::traits<BandMatrixWrapper>::StorageIndex;
264
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);
269 // eigen_assert(coeffs.cols()==cols() && (supers()+subs()+1)==coeffs.rows());
270 }
271
273 constexpr Index rows() const { return m_rows.value(); }
274
276 constexpr Index cols() const { return m_coeffs.cols(); }
277
279 constexpr Index supers() const { return m_supers.value(); }
280
282 constexpr Index subs() const { return m_subs.value(); }
283
284 inline const CoefficientsType& coeffs() const { return m_coeffs; }
285
286 protected:
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;
291};
292
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;
309
310 public:
311 explicit TridiagonalMatrix(Index size = Size) : Base(size, size, Options & SelfAdjoint ? 0 : 1, 1) {}
312
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>();
316 }
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>();
320 }
321};
322
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;
327};
328
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;
333};
334
335template <>
336struct AssignmentKind<DenseShape, BandShape> {
337 using Kind = EigenBase2EigenBase;
338};
339
340} // end namespace internal
341
342} // end namespace Eigen
343
344#endif // EIGEN_BANDMATRIX_H
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