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 // 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> {
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,
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_ };
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 
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 
87 
89  return m_matrix.outerStride() + 1;
90  }
91 
93 
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 
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 
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 
126  return m_matrix;
127  }
128 
129  EIGEN_DEVICE_FUNC inline Index index() const { return m_index.value(); }
130 
131  protected:
134 
135  private:
136  // some compilers may fail to optimize std::max etc in case of compile-time constants...
138  return m_index.value() > 0 ? m_index.value() : -m_index.value();
139  }
141  return m_index.value() > 0 ? 0 : -m_index.value();
142  }
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 
161 template <typename Derived>
163  return DiagonalReturnType(derived());
164 }
165 
167 template <typename Derived>
169  const {
170  return ConstDiagonalReturnType(derived());
171 }
172 
184 template <typename Derived>
186  return Diagonal<Derived, DynamicIndex>(derived(), index);
187 }
188 
190 template <typename Derived>
192  return Diagonal<const Derived, DynamicIndex>(derived(), index);
193 }
194 
206 template <typename Derived>
207 template <int Index_>
209  return Diagonal<Derived, Index_>(derived());
210 }
211 
213 template <typename Derived>
214 template <int Index_>
216  return Diagonal<const Derived, Index_>(derived());
217 }
218 
219 } // end namespace Eigen
220 
221 #endif // EIGEN_DIAGONAL_H
#define EIGEN_NOEXCEPT
Definition: Macros.h:1267
#define EIGEN_CONSTEXPR
Definition: Macros.h:758
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1171
#define eigen_assert(x)
Definition: Macros.h:910
#define EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Derived)
Macro to manually inherit assignment operators. This is necessary, because the implicitly defined ass...
Definition: Macros.h:1126
#define EIGEN_STRONG_INLINE
Definition: Macros.h:834
m row(1)
#define EIGEN_STATIC_ASSERT_LVALUE(Derived)
Definition: StaticAssert.h:87
int rows
Definition: Tutorial_commainit_02.cpp:1
SCALAR Scalar
Definition: bench_gemm.cpp:45
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
Expression of a diagonal/subdiagonal/superdiagonal in a matrix.
Definition: Diagonal.h:68
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index innerStride() const EIGEN_NOEXCEPT
Definition: Diagonal.h:88
EIGEN_DEVICE_FUNC Scalar & coeffRef(Index row, Index)
Definition: Diagonal.h:99
internal::dense_xpr_base< Diagonal >::type Base
Definition: Diagonal.h:71
EIGEN_DEVICE_FUNC const internal::remove_all_t< typename MatrixType::Nested > & nestedExpression() const
Definition: Diagonal.h:125
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index rowOffset() const EIGEN_NOEXCEPT
Definition: Diagonal.h:140
EIGEN_DEVICE_FUNC CoeffReturnType coeff(Index row, Index) const
Definition: Diagonal.h:108
MatrixType::PacketReturnType packet(Index, Index) const
const internal::variable_if_dynamicindex< Index, DiagIndex > m_index
Definition: Diagonal.h:133
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index colOffset() const EIGEN_NOEXCEPT
Definition: Diagonal.h:143
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index absDiagIndex() const EIGEN_NOEXCEPT
Definition: Diagonal.h:137
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index outerStride() const EIGEN_NOEXCEPT
Definition: Diagonal.h:92
EIGEN_DEVICE_FUNC ScalarWithConstIfNotLvalue * data()
Definition: Diagonal.h:96
MatrixType::PacketReturnType packet(Index) const
EIGEN_DEVICE_FUNC const Scalar & coeffRef(Index row, Index) const
Definition: Diagonal.h:104
std::conditional_t< internal::is_lvalue< MatrixType >::value, Scalar, const Scalar > ScalarWithConstIfNotLvalue
Definition: Diagonal.h:94
EIGEN_DEVICE_FUNC const Scalar * data() const
Definition: Diagonal.h:97
EIGEN_DEVICE_FUNC Index index() const
Definition: Diagonal.h:129
EIGEN_DEVICE_FUNC CoeffReturnType coeff(Index idx) const
Definition: Diagonal.h:121
EIGEN_DEVICE_FUNC Scalar & coeffRef(Index idx)
Definition: Diagonal.h:112
EIGEN_DEVICE_FUNC const Scalar & coeffRef(Index idx) const
Definition: Diagonal.h:117
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: Diagonal.h:86
internal::ref_selector< MatrixType >::non_const_type m_matrix
Definition: Diagonal.h:132
EIGEN_DEVICE_FUNC DiagonalReturnType diagonal()
Definition: Diagonal.h:162
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR T value()
Definition: XprHelper.h:189
Eigen::Map< Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor >, 0, Eigen::OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: common.h:85
const unsigned int DirectAccessBit
Definition: Constants.h:159
const unsigned int LvalueBit
Definition: Constants.h:148
const unsigned int RowMajorBit
Definition: Constants.h:70
return int(ret)+1
Eigen::DenseIndex ret
Definition: level1_cplx_impl.h:43
constexpr int plain_enum_min(A a, B b)
Definition: Meta.h:649
constexpr int plain_enum_max(A a, B b)
Definition: Meta.h:656
constexpr int min_size_prefer_fixed(A a, B b)
Definition: Meta.h:683
typename remove_all< T >::type remove_all_t
Definition: Meta.h:142
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
squared absolute value
Definition: GlobalFunctions.h:87
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
Extend namespace for flags.
Definition: fsi_chan_precond_driver.cc:56
type
Definition: compute_granudrum_aor.py:141
Definition: Eigen_Colamd.h:49
Definition: XprHelper.h:558
Definition: XprHelper.h:819
Definition: DenseCoeffsBase.h:556
Definition: XprHelper.h:506
std::conditional_t< bool(traits< T >::Flags &NestByRefBit), T &, T > non_const_type
Definition: XprHelper.h:509
std::remove_reference_t< MatrixTypeNested > MatrixTypeNested_
Definition: Diagonal.h:42
MatrixType::StorageKind StorageKind
Definition: Diagonal.h:43
ref_selector< MatrixType >::type MatrixTypeNested
Definition: Diagonal.h:41
Definition: ForwardDeclarations.h:21