Eigen  3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)
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 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
37 namespace internal {
38 template<typename MatrixType, int DiagIndex>
39 struct traits<Diagonal<MatrixType,DiagIndex> >
40  : traits<MatrixType>
41 {
42  typedef typename ref_selector<MatrixType>::type MatrixTypeNested;
43  typedef std::remove_reference_t<MatrixTypeNested> MatrixTypeNested_;
44  typedef typename MatrixType::StorageKind StorageKind;
45  enum {
46  RowsAtCompileTime = (int(DiagIndex) == DynamicIndex || int(MatrixType::SizeAtCompileTime) == Dynamic) ? 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 = int(MatrixType::MaxSizeAtCompileTime) == Dynamic ? Dynamic
51  : DiagIndex == DynamicIndex ? min_size_prefer_fixed(MatrixType::MaxRowsAtCompileTime,
52  MatrixType::MaxColsAtCompileTime)
53  : (plain_enum_min(MatrixType::MaxRowsAtCompileTime - plain_enum_max(-DiagIndex, 0),
54  MatrixType::MaxColsAtCompileTime - plain_enum_max( DiagIndex, 0))),
55  MaxColsAtCompileTime = 1,
56  MaskLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
57  Flags = (unsigned int)MatrixTypeNested_::Flags & (RowMajorBit | MaskLvalueBit | DirectAccessBit) & ~RowMajorBit, // FIXME DirectAccessBit should not be handled by expressions
58  MatrixTypeOuterStride = outer_stride_at_compile_time<MatrixType>::ret,
59  InnerStrideAtCompileTime = MatrixTypeOuterStride == Dynamic ? Dynamic : MatrixTypeOuterStride+1,
60  OuterStrideAtCompileTime = 0
61  };
62 };
63 }
64 
65 template<typename MatrixType, int DiagIndex_> class Diagonal
66  : public internal::dense_xpr_base< Diagonal<MatrixType,DiagIndex_> >::type
67 {
68  public:
69 
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
75  explicit inline Diagonal(MatrixType& matrix, Index a_index = DiagIndex) : m_matrix(matrix), m_index(a_index)
76  {
77  eigen_assert( a_index <= m_matrix.cols() && -a_index <= m_matrix.rows() );
78  }
79 
80  EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Diagonal)
81 
82  EIGEN_DEVICE_FUNC
83  inline Index rows() const
84  {
85  return m_index.value()<0 ? numext::mini<Index>(m_matrix.cols(),m_matrix.rows()+m_index.value())
86  : numext::mini<Index>(m_matrix.rows(),m_matrix.cols()-m_index.value());
87  }
88 
89  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
90  inline Index cols() const EIGEN_NOEXCEPT { return 1; }
91 
92  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
93  inline Index innerStride() const EIGEN_NOEXCEPT {
94  return m_matrix.outerStride() + 1;
95  }
96 
97  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
98  inline Index outerStride() const EIGEN_NOEXCEPT { return 0; }
99 
100  typedef std::conditional_t<
101  internal::is_lvalue<MatrixType>::value,
102  Scalar,
103  const Scalar
104  > ScalarWithConstIfNotLvalue;
105 
106  EIGEN_DEVICE_FUNC
107  inline ScalarWithConstIfNotLvalue* data() { return &(m_matrix.coeffRef(rowOffset(), colOffset())); }
108  EIGEN_DEVICE_FUNC
109  inline const Scalar* data() const { return &(m_matrix.coeffRef(rowOffset(), colOffset())); }
110 
111  EIGEN_DEVICE_FUNC
112  inline Scalar& coeffRef(Index row, Index)
113  {
114  EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
115  return m_matrix.coeffRef(row+rowOffset(), row+colOffset());
116  }
117 
118  EIGEN_DEVICE_FUNC
119  inline const Scalar& coeffRef(Index row, Index) const
120  {
121  return m_matrix.coeffRef(row+rowOffset(), row+colOffset());
122  }
123 
124  EIGEN_DEVICE_FUNC
125  inline CoeffReturnType coeff(Index row, Index) const
126  {
127  return m_matrix.coeff(row+rowOffset(), row+colOffset());
128  }
129 
130  EIGEN_DEVICE_FUNC
131  inline Scalar& coeffRef(Index idx)
132  {
133  EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
134  return m_matrix.coeffRef(idx+rowOffset(), idx+colOffset());
135  }
136 
137  EIGEN_DEVICE_FUNC
138  inline const Scalar& coeffRef(Index idx) const
139  {
140  return m_matrix.coeffRef(idx+rowOffset(), idx+colOffset());
141  }
142 
143  EIGEN_DEVICE_FUNC
144  inline CoeffReturnType coeff(Index idx) const
145  {
146  return m_matrix.coeff(idx+rowOffset(), idx+colOffset());
147  }
148 
149  EIGEN_DEVICE_FUNC
150  inline const internal::remove_all_t<typename MatrixType::Nested>&
151  nestedExpression() const
152  {
153  return m_matrix;
154  }
155 
156  EIGEN_DEVICE_FUNC
157  inline Index index() const
158  {
159  return m_index.value();
160  }
161 
162  protected:
163  typename internal::ref_selector<MatrixType>::non_const_type m_matrix;
164  const internal::variable_if_dynamicindex<Index, DiagIndex> m_index;
165 
166  private:
167  // some compilers may fail to optimize std::max etc in case of compile-time constants...
168  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
169  Index absDiagIndex() const EIGEN_NOEXCEPT { return m_index.value()>0 ? m_index.value() : -m_index.value(); }
170  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
171  Index rowOffset() const EIGEN_NOEXCEPT { return m_index.value()>0 ? 0 : -m_index.value(); }
172  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
173  Index colOffset() const EIGEN_NOEXCEPT { return m_index.value()>0 ? m_index.value() : 0; }
174  // trigger a compile-time error if someone try to call packet
175  template<int LoadMode> typename MatrixType::PacketReturnType packet(Index) const;
176  template<int LoadMode> typename MatrixType::PacketReturnType packet(Index,Index) const;
177 };
178 
187 template<typename Derived>
188 EIGEN_DEVICE_FUNC inline typename MatrixBase<Derived>::DiagonalReturnType
190 {
191  return DiagonalReturnType(derived());
192 }
193 
195 template<typename Derived>
196 EIGEN_DEVICE_FUNC inline
199 {
200  return ConstDiagonalReturnType(derived());
201 }
202 
214 template<typename Derived>
215 EIGEN_DEVICE_FUNC inline Diagonal<Derived, DynamicIndex>
217 {
218  return Diagonal<Derived, DynamicIndex>(derived(), index);
219 }
220 
222 template<typename Derived>
223 EIGEN_DEVICE_FUNC inline const Diagonal<const Derived, DynamicIndex>
225 {
226  return Diagonal<const Derived, DynamicIndex>(derived(), index);
227 }
228 
240 template<typename Derived>
241 template<int Index_>
242 EIGEN_DEVICE_FUNC
245 {
246  return Diagonal<Derived, Index_>(derived());
247 }
248 
250 template<typename Derived>
251 template<int Index_>
252 EIGEN_DEVICE_FUNC
253 inline const Diagonal<const Derived, Index_>
255 {
256  return Diagonal<const Derived, Index_>(derived());
257 }
258 
259 } // end namespace Eigen
260 
261 #endif // EIGEN_DIAGONAL_H
Expression of a diagonal/subdiagonal/superdiagonal in a matrix.
Definition: Diagonal.h:67
DiagonalReturnType diagonal()
Definition: Diagonal.h:189
const unsigned int DirectAccessBit
Definition: Constants.h:157
const unsigned int LvalueBit
Definition: Constants.h:146
const unsigned int RowMajorBit
Definition: Constants.h:68
Namespace containing all symbols from the Eigen library.
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
const int DynamicIndex
Definition: Constants.h:29
const int Dynamic
Definition: Constants.h:24
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:41