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 
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;
73 
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 
81 
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 
90  inline Index cols() const EIGEN_NOEXCEPT { return 1; }
91 
93  inline Index innerStride() const EIGEN_NOEXCEPT {
94  return m_matrix.outerStride() + 1;
95  }
96 
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
105 
107  inline ScalarWithConstIfNotLvalue* data() { return &(m_matrix.coeffRef(rowOffset(), colOffset())); }
109  inline const Scalar* data() const { return &(m_matrix.coeffRef(rowOffset(), colOffset())); }
110 
112  inline Scalar& coeffRef(Index row, Index)
113  {
115  return m_matrix.coeffRef(row+rowOffset(), row+colOffset());
116  }
117 
119  inline const Scalar& coeffRef(Index row, Index) const
120  {
121  return m_matrix.coeffRef(row+rowOffset(), row+colOffset());
122  }
123 
125  inline CoeffReturnType coeff(Index row, Index) const
126  {
127  return m_matrix.coeff(row+rowOffset(), row+colOffset());
128  }
129 
131  inline Scalar& coeffRef(Index idx)
132  {
134  return m_matrix.coeffRef(idx+rowOffset(), idx+colOffset());
135  }
136 
138  inline const Scalar& coeffRef(Index idx) const
139  {
140  return m_matrix.coeffRef(idx+rowOffset(), idx+colOffset());
141  }
142 
144  inline CoeffReturnType coeff(Index idx) const
145  {
146  return m_matrix.coeff(idx+rowOffset(), idx+colOffset());
147  }
148 
152  {
153  return m_matrix;
154  }
155 
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>
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>
217 {
218  return Diagonal<Derived, DynamicIndex>(derived(), index);
219 }
220 
222 template<typename Derived>
225 {
226  return Diagonal<const Derived, DynamicIndex>(derived(), index);
227 }
228 
240 template<typename Derived>
241 template<int Index_>
245 {
246  return Diagonal<Derived, Index_>(derived());
247 }
248 
250 template<typename Derived>
251 template<int Index_>
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
RowXpr row(Index i)
This is the const version of row(). *‍/.
#define EIGEN_NOEXCEPT
Definition: Macros.h:1260
#define EIGEN_CONSTEXPR
Definition: Macros.h:747
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:883
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1168
#define eigen_assert(x)
Definition: Macros.h:902
#define EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Derived)
Definition: Macros.h:1122
#define EIGEN_STATIC_ASSERT_LVALUE(Derived)
Definition: StaticAssert.h:96
Matrix< float, 1, Dynamic > MatrixType
internal::traits< Derived >::StorageKind StorageKind
Definition: DenseBase.h:50
internal::add_const_on_value_type_if_arithmetic< typename internal::packet_traits< Scalar >::type >::type PacketReturnType
Expression of a diagonal/subdiagonal/superdiagonal in a matrix.
Definition: Diagonal.h:67
const Scalar & coeffRef(Index row, Index) const
Definition: Diagonal.h:119
EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: Diagonal.h:90
EIGEN_CONSTEXPR Index innerStride() const EIGEN_NOEXCEPT
Definition: Diagonal.h:93
internal::dense_xpr_base< Diagonal >::type Base
Definition: Diagonal.h:71
CoeffReturnType coeff(Index row, Index) const
Definition: Diagonal.h:125
Index index() const
Definition: Diagonal.h:157
Scalar & coeffRef(Index row, Index)
Definition: Diagonal.h:112
MatrixType::PacketReturnType packet(Index, Index) const
const internal::variable_if_dynamicindex< Index, DiagIndex > m_index
Definition: Diagonal.h:164
const internal::remove_all_t< typename MatrixType::Nested > & nestedExpression() const
Definition: Diagonal.h:151
EIGEN_CONSTEXPR Index absDiagIndex() const EIGEN_NOEXCEPT
Definition: Diagonal.h:169
MatrixType::PacketReturnType packet(Index) const
std::conditional_t< internal::is_lvalue< MatrixType >::value, Scalar, const Scalar > ScalarWithConstIfNotLvalue
Definition: Diagonal.h:104
EIGEN_CONSTEXPR Index rowOffset() const EIGEN_NOEXCEPT
Definition: Diagonal.h:171
EIGEN_CONSTEXPR Index outerStride() const EIGEN_NOEXCEPT
Definition: Diagonal.h:98
ScalarWithConstIfNotLvalue * data()
Definition: Diagonal.h:107
CoeffReturnType coeff(Index idx) const
Definition: Diagonal.h:144
Scalar & coeffRef(Index idx)
Definition: Diagonal.h:131
Diagonal(MatrixType &matrix, Index a_index=DiagIndex)
Definition: Diagonal.h:75
internal::ref_selector< MatrixType >::non_const_type m_matrix
Definition: Diagonal.h:163
const Scalar & coeffRef(Index idx) const
Definition: Diagonal.h:138
const Scalar * data() const
Definition: Diagonal.h:109
Index rows() const
Definition: Diagonal.h:83
EIGEN_CONSTEXPR Index colOffset() const EIGEN_NOEXCEPT
Definition: Diagonal.h:173
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
constexpr int plain_enum_min(A a, B b)
Definition: Meta.h:516
constexpr int plain_enum_max(A a, B b)
Definition: Meta.h:524
constexpr int min_size_prefer_fixed(A a, B b)
Definition: Meta.h:553
typename remove_all< T >::type remove_all_t
Definition: Meta.h:119
: InteropHeaders
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
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