Eigen  3.4.90 (git rev 5a9f66fb35d03a4da9ef8976e67a61b30aa16dcf)
 
Loading...
Searching...
No Matches
Diagonal.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2007-2009 Benoit Jacob <[email protected]>
5// Copyright (C) 2009-2010 Gael Guennebaud <[email protected]>
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
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
38namespace internal {
39template <typename MatrixType, int DiagIndex>
40struct traits<Diagonal<MatrixType, DiagIndex> > : traits<MatrixType> {
41 typedef typename ref_selector<MatrixType>::type MatrixTypeNested;
42 typedef std::remove_reference_t<MatrixTypeNested> MatrixTypeNested_;
43 typedef typename MatrixType::StorageKind StorageKind;
44 enum {
45 RowsAtCompileTime = (int(DiagIndex) == DynamicIndex || int(MatrixType::SizeAtCompileTime) == Dynamic)
46 ? Dynamic
47 : (plain_enum_min(MatrixType::RowsAtCompileTime - plain_enum_max(-DiagIndex, 0),
48 MatrixType::ColsAtCompileTime - plain_enum_max(DiagIndex, 0))),
49 ColsAtCompileTime = 1,
50 MaxRowsAtCompileTime =
51 int(MatrixType::MaxSizeAtCompileTime) == Dynamic ? Dynamic
52 : DiagIndex == DynamicIndex
53 ? min_size_prefer_fixed(MatrixType::MaxRowsAtCompileTime, MatrixType::MaxColsAtCompileTime)
54 : (plain_enum_min(MatrixType::MaxRowsAtCompileTime - plain_enum_max(-DiagIndex, 0),
55 MatrixType::MaxColsAtCompileTime - plain_enum_max(DiagIndex, 0))),
56 MaxColsAtCompileTime = 1,
57 MaskLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
58 Flags = (unsigned int)MatrixTypeNested_::Flags & (RowMajorBit | MaskLvalueBit | DirectAccessBit) &
59 ~RowMajorBit, // FIXME DirectAccessBit should not be handled by expressions
60 MatrixTypeOuterStride = outer_stride_at_compile_time<MatrixType>::ret,
61 InnerStrideAtCompileTime = MatrixTypeOuterStride == Dynamic ? Dynamic : MatrixTypeOuterStride + 1,
62 OuterStrideAtCompileTime = 0
63 };
64};
65} // namespace internal
66
67template <typename MatrixType, int DiagIndex_>
68class Diagonal : public internal::dense_xpr_base<Diagonal<MatrixType, DiagIndex_> >::type {
69 public:
70 enum { DiagIndex = DiagIndex_ };
71 typedef typename internal::dense_xpr_base<Diagonal>::type Base;
72 EIGEN_DENSE_PUBLIC_INTERFACE(Diagonal)
73
74 EIGEN_DEVICE_FUNC explicit inline Diagonal(MatrixType& matrix, Index a_index = DiagIndex)
75 : m_matrix(matrix), m_index(a_index) {
76 eigen_assert(a_index <= m_matrix.cols() && -a_index <= m_matrix.rows());
77 }
78
79 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Diagonal)
80
81 EIGEN_DEVICE_FUNC inline Index rows() const {
82 return m_index.value() < 0 ? numext::mini<Index>(m_matrix.cols(), m_matrix.rows() + m_index.value())
83 : numext::mini<Index>(m_matrix.rows(), m_matrix.cols() - m_index.value());
84 }
85
86 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index cols() const EIGEN_NOEXCEPT { return 1; }
87
88 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index innerStride() const EIGEN_NOEXCEPT {
89 return m_matrix.outerStride() + 1;
90 }
91
92 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index outerStride() const EIGEN_NOEXCEPT { return 0; }
93
94 typedef std::conditional_t<internal::is_lvalue<MatrixType>::value, Scalar, const Scalar> ScalarWithConstIfNotLvalue;
95
96 EIGEN_DEVICE_FUNC inline ScalarWithConstIfNotLvalue* data() { return &(m_matrix.coeffRef(rowOffset(), colOffset())); }
97 EIGEN_DEVICE_FUNC inline const Scalar* data() const { return &(m_matrix.coeffRef(rowOffset(), colOffset())); }
98
99 EIGEN_DEVICE_FUNC inline Scalar& coeffRef(Index row, Index) {
100 EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
101 return m_matrix.coeffRef(row + rowOffset(), row + colOffset());
102 }
103
104 EIGEN_DEVICE_FUNC inline const Scalar& coeffRef(Index row, Index) const {
105 return m_matrix.coeffRef(row + rowOffset(), row + colOffset());
106 }
107
108 EIGEN_DEVICE_FUNC inline CoeffReturnType coeff(Index row, Index) const {
109 return m_matrix.coeff(row + rowOffset(), row + colOffset());
110 }
111
112 EIGEN_DEVICE_FUNC inline Scalar& coeffRef(Index idx) {
113 EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
114 return m_matrix.coeffRef(idx + rowOffset(), idx + colOffset());
115 }
116
117 EIGEN_DEVICE_FUNC inline const Scalar& coeffRef(Index idx) const {
118 return m_matrix.coeffRef(idx + rowOffset(), idx + colOffset());
119 }
120
121 EIGEN_DEVICE_FUNC inline CoeffReturnType coeff(Index idx) const {
122 return m_matrix.coeff(idx + rowOffset(), idx + colOffset());
123 }
124
125 EIGEN_DEVICE_FUNC inline const internal::remove_all_t<typename MatrixType::Nested>& nestedExpression() const {
126 return m_matrix;
127 }
128
129 EIGEN_DEVICE_FUNC inline Index index() const { return m_index.value(); }
130
131 protected:
132 typename internal::ref_selector<MatrixType>::non_const_type m_matrix;
133 const internal::variable_if_dynamicindex<Index, DiagIndex> m_index;
134
135 private:
136 // some compilers may fail to optimize std::max etc in case of compile-time constants...
137 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index absDiagIndex() const EIGEN_NOEXCEPT {
138 return m_index.value() > 0 ? m_index.value() : -m_index.value();
139 }
140 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index rowOffset() const EIGEN_NOEXCEPT {
141 return m_index.value() > 0 ? 0 : -m_index.value();
142 }
143 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index colOffset() const EIGEN_NOEXCEPT {
144 return m_index.value() > 0 ? m_index.value() : 0;
145 }
146 // trigger a compile-time error if someone try to call packet
147 template <int LoadMode>
148 typename MatrixType::PacketReturnType packet(Index) const;
149 template <int LoadMode>
150 typename MatrixType::PacketReturnType packet(Index, Index) const;
151};
152
161template <typename Derived>
163 return DiagonalReturnType(derived());
164}
165
167template <typename Derived>
169 const {
170 return ConstDiagonalReturnType(derived());
171}
172
184template <typename Derived>
186 return Diagonal<Derived, DynamicIndex>(derived(), index);
187}
188
190template <typename Derived>
192 return Diagonal<const Derived, DynamicIndex>(derived(), index);
193}
194
206template <typename Derived>
207template <int Index_>
209 return Diagonal<Derived, Index_>(derived());
210}
211
213template <typename Derived>
214template <int Index_>
216 return Diagonal<const Derived, Index_>(derived());
217}
218
219} // end namespace Eigen
220
221#endif // EIGEN_DIAGONAL_H
Expression of a diagonal/subdiagonal/superdiagonal in a matrix.
Definition Diagonal.h:68
DiagonalReturnType diagonal()
Definition Diagonal.h:162
const unsigned int DirectAccessBit
Definition Constants.h:159
const unsigned int LvalueBit
Definition Constants.h:148
const unsigned int RowMajorBit
Definition Constants.h:70
Namespace containing all symbols from the Eigen library.
Definition Core:137
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:83
const int DynamicIndex
Definition Constants.h:30
const int Dynamic
Definition Constants.h:25