WPILibC++ 2023.4.3-108-ge5452e3
Diagonal.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) 2007-2009 Benoit Jacob <jacob.benoit.1@gmail.com>
5// Copyright (C) 2009-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10
11#ifndef EIGEN_DIAGONAL_H
12#define EIGEN_DIAGONAL_H
13
14namespace Eigen {
15
16/** \class Diagonal
17 * \ingroup Core_Module
18 *
19 * \brief Expression of a diagonal/subdiagonal/superdiagonal in a matrix
20 *
21 * \param MatrixType the type of the object in which we are taking a sub/main/super diagonal
22 * \param DiagIndex the index of the sub/super diagonal. The default is 0 and it means the main diagonal.
23 * A positive value means a superdiagonal, a negative value means a subdiagonal.
24 * You can also use DynamicIndex so the index can be set at runtime.
25 *
26 * The matrix is not required to be square.
27 *
28 * This class represents an expression of the main diagonal, or any sub/super diagonal
29 * of a square matrix. It is the return type of MatrixBase::diagonal() and MatrixBase::diagonal(Index) and most of the
30 * time this is the only way it is used.
31 *
32 * \sa MatrixBase::diagonal(), MatrixBase::diagonal(Index)
33 */
34
35namespace internal {
36template<typename MatrixType, int DiagIndex>
37struct traits<Diagonal<MatrixType,DiagIndex> >
38 : traits<MatrixType>
39{
42 typedef typename MatrixType::StorageKind StorageKind;
43 enum {
44 RowsAtCompileTime = (int(DiagIndex) == DynamicIndex || int(MatrixType::SizeAtCompileTime) == Dynamic) ? Dynamic
45 : (EIGEN_PLAIN_ENUM_MIN(MatrixType::RowsAtCompileTime - EIGEN_PLAIN_ENUM_MAX(-DiagIndex, 0),
46 MatrixType::ColsAtCompileTime - EIGEN_PLAIN_ENUM_MAX( DiagIndex, 0))),
47 ColsAtCompileTime = 1,
48 MaxRowsAtCompileTime = int(MatrixType::MaxSizeAtCompileTime) == Dynamic ? Dynamic
49 : DiagIndex == DynamicIndex ? EIGEN_SIZE_MIN_PREFER_FIXED(MatrixType::MaxRowsAtCompileTime,
50 MatrixType::MaxColsAtCompileTime)
51 : (EIGEN_PLAIN_ENUM_MIN(MatrixType::MaxRowsAtCompileTime - EIGEN_PLAIN_ENUM_MAX(-DiagIndex, 0),
52 MatrixType::MaxColsAtCompileTime - EIGEN_PLAIN_ENUM_MAX( DiagIndex, 0))),
53 MaxColsAtCompileTime = 1,
54 MaskLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
55 Flags = (unsigned int)_MatrixTypeNested::Flags & (RowMajorBit | MaskLvalueBit | DirectAccessBit) & ~RowMajorBit, // FIXME DirectAccessBit should not be handled by expressions
56 MatrixTypeOuterStride = outer_stride_at_compile_time<MatrixType>::ret,
57 InnerStrideAtCompileTime = MatrixTypeOuterStride == Dynamic ? Dynamic : MatrixTypeOuterStride+1,
58 OuterStrideAtCompileTime = 0
59 };
60};
61}
62
63template<typename MatrixType, int _DiagIndex> class Diagonal
64 : public internal::dense_xpr_base< Diagonal<MatrixType,_DiagIndex> >::type
65{
66 public:
67
68 enum { DiagIndex = _DiagIndex };
71
73 explicit inline Diagonal(MatrixType& matrix, Index a_index = DiagIndex) : m_matrix(matrix), m_index(a_index)
74 {
75 eigen_assert( a_index <= m_matrix.cols() && -a_index <= m_matrix.rows() );
76 }
77
79
81 inline Index rows() const
82 {
83 return m_index.value()<0 ? numext::mini<Index>(m_matrix.cols(),m_matrix.rows()+m_index.value())
84 : numext::mini<Index>(m_matrix.rows(),m_matrix.cols()-m_index.value());
85 }
86
88 inline Index cols() const EIGEN_NOEXCEPT { return 1; }
89
92 return m_matrix.outerStride() + 1;
93 }
94
96 inline Index outerStride() const EIGEN_NOEXCEPT { return 0; }
97
98 typedef typename internal::conditional<
100 Scalar,
101 const Scalar
103
105 inline ScalarWithConstIfNotLvalue* data() { return &(m_matrix.coeffRef(rowOffset(), colOffset())); }
107 inline const Scalar* data() const { return &(m_matrix.coeffRef(rowOffset(), colOffset())); }
108
110 inline Scalar& coeffRef(Index row, Index)
111 {
113 return m_matrix.coeffRef(row+rowOffset(), row+colOffset());
114 }
115
117 inline const Scalar& coeffRef(Index row, Index) const
118 {
119 return m_matrix.coeffRef(row+rowOffset(), row+colOffset());
120 }
121
123 inline CoeffReturnType coeff(Index row, Index) const
124 {
125 return m_matrix.coeff(row+rowOffset(), row+colOffset());
126 }
127
129 inline Scalar& coeffRef(Index idx)
130 {
132 return m_matrix.coeffRef(idx+rowOffset(), idx+colOffset());
133 }
134
136 inline const Scalar& coeffRef(Index idx) const
137 {
138 return m_matrix.coeffRef(idx+rowOffset(), idx+colOffset());
139 }
140
142 inline CoeffReturnType coeff(Index idx) const
143 {
144 return m_matrix.coeff(idx+rowOffset(), idx+colOffset());
145 }
146
150 {
151 return m_matrix;
152 }
153
155 inline Index index() const
156 {
157 return m_index.value();
158 }
159
160 protected:
163
164 private:
165 // some compilers may fail to optimize std::max etc in case of compile-time constants...
167 Index absDiagIndex() const EIGEN_NOEXCEPT { return m_index.value()>0 ? m_index.value() : -m_index.value(); }
169 Index rowOffset() const EIGEN_NOEXCEPT { return m_index.value()>0 ? 0 : -m_index.value(); }
171 Index colOffset() const EIGEN_NOEXCEPT { return m_index.value()>0 ? m_index.value() : 0; }
172 // trigger a compile-time error if someone try to call packet
173 template<int LoadMode> typename MatrixType::PacketReturnType packet(Index) const;
174 template<int LoadMode> typename MatrixType::PacketReturnType packet(Index,Index) const;
175};
176
177/** \returns an expression of the main diagonal of the matrix \c *this
178 *
179 * \c *this is not required to be square.
180 *
181 * Example: \include MatrixBase_diagonal.cpp
182 * Output: \verbinclude MatrixBase_diagonal.out
183 *
184 * \sa class Diagonal */
185template<typename Derived>
188{
189 return DiagonalReturnType(derived());
190}
191
192/** This is the const version of diagonal(). */
193template<typename Derived>
196{
197 return ConstDiagonalReturnType(derived());
198}
199
200/** \returns an expression of the \a DiagIndex-th sub or super diagonal of the matrix \c *this
201 *
202 * \c *this is not required to be square.
203 *
204 * The template parameter \a DiagIndex represent a super diagonal if \a DiagIndex > 0
205 * and a sub diagonal otherwise. \a DiagIndex == 0 is equivalent to the main diagonal.
206 *
207 * Example: \include MatrixBase_diagonal_int.cpp
208 * Output: \verbinclude MatrixBase_diagonal_int.out
209 *
210 * \sa MatrixBase::diagonal(), class Diagonal */
211template<typename Derived>
214{
215 return DiagonalDynamicIndexReturnType(derived(), index);
216}
217
218/** This is the const version of diagonal(Index). */
219template<typename Derived>
222{
224}
225
226/** \returns an expression of the \a DiagIndex-th sub or super diagonal of the matrix \c *this
227 *
228 * \c *this is not required to be square.
229 *
230 * The template parameter \a DiagIndex represent a super diagonal if \a DiagIndex > 0
231 * and a sub diagonal otherwise. \a DiagIndex == 0 is equivalent to the main diagonal.
232 *
233 * Example: \include MatrixBase_diagonal_template_int.cpp
234 * Output: \verbinclude MatrixBase_diagonal_template_int.out
235 *
236 * \sa MatrixBase::diagonal(), class Diagonal */
237template<typename Derived>
238template<int Index_>
242{
243 return typename DiagonalIndexReturnType<Index_>::Type(derived());
244}
245
246/** This is the const version of diagonal<int>(). */
247template<typename Derived>
248template<int Index_>
252{
253 return typename ConstDiagonalIndexReturnType<Index_>::Type(derived());
254}
255
256} // end namespace Eigen
257
258#endif // EIGEN_DIAGONAL_H
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE RowXpr row(Index i)
This is the const version of row(). *‍/.
Definition: BlockMethods.h:1118
#define EIGEN_PLAIN_ENUM_MAX(a, b)
Definition: Macros.h:1299
#define EIGEN_PLAIN_ENUM_MIN(a, b)
Definition: Macros.h:1298
#define EIGEN_NOEXCEPT
Definition: Macros.h:1428
#define EIGEN_CONSTEXPR
Definition: Macros.h:797
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:986
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1293
#define eigen_assert(x)
Definition: Macros.h:1047
#define EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Derived)
Definition: Macros.h:1241
#define EIGEN_STRONG_INLINE
Definition: Macros.h:927
#define EIGEN_SIZE_MIN_PREFER_FIXED(a, b)
Definition: Macros.h:1312
#define EIGEN_STATIC_ASSERT_LVALUE(Derived)
Definition: StaticAssert.h:202
Expression of a diagonal/subdiagonal/superdiagonal in a matrix.
Definition: Diagonal.h:65
EIGEN_DEVICE_FUNC const internal::remove_all< typenameMatrixType::Nested >::type & nestedExpression() const
Definition: Diagonal.h:149
EIGEN_DEVICE_FUNC const Scalar & coeffRef(Index row, Index) const
Definition: Diagonal.h:117
EIGEN_DEVICE_FUNC Scalar & coeffRef(Index idx)
Definition: Diagonal.h:129
const internal::variable_if_dynamicindex< Index, DiagIndex > m_index
Definition: Diagonal.h:162
EIGEN_DEVICE_FUNC CoeffReturnType coeff(Index row, Index) const
Definition: Diagonal.h:123
EIGEN_DEVICE_FUNC Diagonal(MatrixType &matrix, Index a_index=DiagIndex)
Definition: Diagonal.h:73
EIGEN_DEVICE_FUNC Scalar & coeffRef(Index row, Index)
Definition: Diagonal.h:110
EIGEN_DEVICE_FUNC Index index() const
Definition: Diagonal.h:155
EIGEN_DEVICE_FUNC Index rows() const
Definition: Diagonal.h:81
EIGEN_DEVICE_FUNC CoeffReturnType coeff(Index idx) const
Definition: Diagonal.h:142
EIGEN_DEVICE_FUNC ScalarWithConstIfNotLvalue * data()
Definition: Diagonal.h:105
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: Diagonal.h:88
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index innerStride() const EIGEN_NOEXCEPT
Definition: Diagonal.h:91
internal::ref_selector< MatrixType >::non_const_type m_matrix
Definition: Diagonal.h:161
internal::dense_xpr_base< Diagonal >::type Base
Definition: Diagonal.h:69
internal::conditional< internal::is_lvalue< MatrixType >::value, Scalar, constScalar >::type ScalarWithConstIfNotLvalue
Definition: Diagonal.h:102
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index outerStride() const EIGEN_NOEXCEPT
Definition: Diagonal.h:96
EIGEN_DEVICE_FUNC const Scalar * data() const
Definition: Diagonal.h:107
EIGEN_DEVICE_FUNC const Scalar & coeffRef(Index idx) const
Definition: Diagonal.h:136
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:50
EIGEN_DEVICE_FUNC DiagonalReturnType diagonal()
Definition: Diagonal.h:187
Diagonal< Derived > DiagonalReturnType
Definition: MatrixBase.h:205
Expression of the transpose of a matrix.
Definition: Transpose.h:54
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR T value()
Definition: XprHelper.h:160
Definition: core.h:1240
type
Definition: core.h:575
const unsigned int DirectAccessBit
Means that the underlying array of coefficients can be directly accessed as a plain strided array.
Definition: Constants.h:155
const unsigned int LvalueBit
Means the expression has a coeffRef() method, i.e.
Definition: Constants.h:144
const unsigned int RowMajorBit
for a matrix, this means that the storage order is row-major.
Definition: Constants.h:66
Type
Definition: Constants.h:471
Namespace containing all symbols from the Eigen library.
Definition: Core:141
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
const int DynamicIndex
This value means that a signed quantity (e.g., a signed index) is not known at compile-time,...
Definition: Constants.h:27
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
Definition: Meta.h:109
Definition: XprHelper.h:484
Definition: XprHelper.h:660
Definition: DenseCoeffsBase.h:671
Definition: XprHelper.h:417
T type
Definition: Meta.h:126
T type
Definition: Meta.h:114
remove_reference< MatrixTypeNested >::type _MatrixTypeNested
Definition: Diagonal.h:41
MatrixType::StorageKind StorageKind
Definition: Diagonal.h:42
ref_selector< MatrixType >::type MatrixTypeNested
Definition: Diagonal.h:40
Definition: ForwardDeclarations.h:17
Definition: Meta.h:96