$darkmode
Eigen  5.0.1-dev
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 <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 
14 // IWYU pragma: private
15 #include "./InternalHeaderCheck.h"
16 
17 namespace Eigen {
18 
38 namespace internal {
39 template <typename MatrixType, int DiagIndex>
40 struct 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 
67 template <typename MatrixType, int DiagIndex_>
68 class 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 constexpr Index cols() const noexcept { return 1; }
87 
88  EIGEN_DEVICE_FUNC constexpr Index innerStride() const noexcept { return m_matrix.outerStride() + 1; }
89 
90  EIGEN_DEVICE_FUNC constexpr Index outerStride() const noexcept { return 0; }
91 
92  typedef std::conditional_t<internal::is_lvalue<MatrixType>::value, Scalar, const Scalar> ScalarWithConstIfNotLvalue;
93 
94  EIGEN_DEVICE_FUNC inline ScalarWithConstIfNotLvalue* data() { return &(m_matrix.coeffRef(rowOffset(), colOffset())); }
95  EIGEN_DEVICE_FUNC inline const Scalar* data() const { return &(m_matrix.coeffRef(rowOffset(), colOffset())); }
96 
97  EIGEN_DEVICE_FUNC inline Scalar& coeffRef(Index row, Index) {
98  EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
99  return m_matrix.coeffRef(row + rowOffset(), row + colOffset());
100  }
101 
102  EIGEN_DEVICE_FUNC inline const Scalar& coeffRef(Index row, Index) const {
103  return m_matrix.coeffRef(row + rowOffset(), row + colOffset());
104  }
105 
106  EIGEN_DEVICE_FUNC inline CoeffReturnType coeff(Index row, Index) const {
107  return m_matrix.coeff(row + rowOffset(), row + colOffset());
108  }
109 
110  EIGEN_DEVICE_FUNC inline Scalar& coeffRef(Index idx) {
111  EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
112  return m_matrix.coeffRef(idx + rowOffset(), idx + colOffset());
113  }
114 
115  EIGEN_DEVICE_FUNC inline const Scalar& coeffRef(Index idx) const {
116  return m_matrix.coeffRef(idx + rowOffset(), idx + colOffset());
117  }
118 
119  EIGEN_DEVICE_FUNC inline CoeffReturnType coeff(Index idx) const {
120  return m_matrix.coeff(idx + rowOffset(), idx + colOffset());
121  }
122 
123  EIGEN_DEVICE_FUNC inline const internal::remove_all_t<typename MatrixType::Nested>& nestedExpression() const {
124  return m_matrix;
125  }
126 
127  EIGEN_DEVICE_FUNC inline Index index() const { return m_index.value(); }
128 
129  protected:
130  typename internal::ref_selector<MatrixType>::non_const_type m_matrix;
131  const internal::variable_if_dynamicindex<Index, DiagIndex> m_index;
132 
133  private:
134  // some compilers may fail to optimize std::max etc in case of compile-time constants...
135  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr Index absDiagIndex() const noexcept {
136  return m_index.value() > 0 ? m_index.value() : -m_index.value();
137  }
138  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr Index rowOffset() const noexcept {
139  return m_index.value() > 0 ? 0 : -m_index.value();
140  }
141  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr Index colOffset() const noexcept {
142  return m_index.value() > 0 ? m_index.value() : 0;
143  }
144  // trigger a compile-time error if someone try to call packet
145  template <int LoadMode>
146  typename MatrixType::PacketReturnType packet(Index) const;
147  template <int LoadMode>
148  typename MatrixType::PacketReturnType packet(Index, Index) const;
149 };
150 
159 template <typename Derived>
161  return DiagonalReturnType(derived());
162 }
163 
165 template <typename Derived>
167  const {
168  return ConstDiagonalReturnType(derived());
169 }
170 
182 template <typename Derived>
184  return Diagonal<Derived, DynamicIndex>(derived(), index);
185 }
186 
188 template <typename Derived>
189 EIGEN_DEVICE_FUNC inline const Diagonal<const Derived, DynamicIndex> MatrixBase<Derived>::diagonal(Index index) const {
190  return Diagonal<const Derived, DynamicIndex>(derived(), index);
191 }
192 
204 template <typename Derived>
205 template <int Index_>
206 EIGEN_DEVICE_FUNC inline Diagonal<Derived, Index_> MatrixBase<Derived>::diagonal() {
207  return Diagonal<Derived, Index_>(derived());
208 }
209 
211 template <typename Derived>
212 template <int Index_>
213 EIGEN_DEVICE_FUNC inline const Diagonal<const Derived, Index_> MatrixBase<Derived>::diagonal() const {
214  return Diagonal<const Derived, Index_>(derived());
215 }
216 
217 } // end namespace Eigen
218 
219 #endif // EIGEN_DIAGONAL_H
const unsigned int DirectAccessBit
Definition: Constants.h:159
const unsigned int LvalueBit
Definition: Constants.h:148
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
const int DynamicIndex
Definition: Constants.h:30
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:43
const unsigned int RowMajorBit
Definition: Constants.h:70
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
Expression of a diagonal/subdiagonal/superdiagonal in a matrix.
Definition: Diagonal.h:68
const int Dynamic
Definition: Constants.h:25
DiagonalReturnType diagonal()
Definition: Diagonal.h:160