Medial Code Documentation
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 <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
14namespace Eigen {
15
35namespace internal {
36template<typename MatrixType, int DiagIndex>
37struct traits<Diagonal<MatrixType,DiagIndex> >
38 : traits<MatrixType>
39{
41 typedef typename remove_reference<MatrixTypeNested>::type _MatrixTypeNested;
42 typedef typename MatrixType::StorageKind StorageKind;
43 enum {
44 RowsAtCompileTime = (int(DiagIndex) == DynamicIndex || int(MatrixType::SizeAtCompileTime) == Dynamic) ? Dynamic
45 : (EIGEN_PLAIN_ENUM_MIN(MatrixType::RowsAtCompileTime - EIGEN_PLAIN_ENUM_MAX(-DiagIndex, 0),
46 MatrixType::ColsAtCompileTime - EIGEN_PLAIN_ENUM_MAX( DiagIndex, 0))),
47 ColsAtCompileTime = 1,
48 MaxRowsAtCompileTime = int(MatrixType::MaxSizeAtCompileTime) == Dynamic ? Dynamic
49 : DiagIndex == DynamicIndex ? EIGEN_SIZE_MIN_PREFER_FIXED(MatrixType::MaxRowsAtCompileTime,
50 MatrixType::MaxColsAtCompileTime)
51 : (EIGEN_PLAIN_ENUM_MIN(MatrixType::MaxRowsAtCompileTime - EIGEN_PLAIN_ENUM_MAX(-DiagIndex, 0),
52 MatrixType::MaxColsAtCompileTime - EIGEN_PLAIN_ENUM_MAX( DiagIndex, 0))),
53 MaxColsAtCompileTime = 1,
54 MaskLvalueBit = is_lvalue<MatrixType>::value ? LvalueBit : 0,
55 Flags = (unsigned int)_MatrixTypeNested::Flags & (RowMajorBit | MaskLvalueBit | DirectAccessBit) & ~RowMajorBit, // FIXME DirectAccessBit should not be handled by expressions
57 InnerStrideAtCompileTime = MatrixTypeOuterStride == Dynamic ? Dynamic : MatrixTypeOuterStride+1,
58 OuterStrideAtCompileTime = 0
59 };
60};
61}
62
63template<typename MatrixType, int _DiagIndex> class Diagonal
64 : public internal::dense_xpr_base< Diagonal<MatrixType,_DiagIndex> >::type
65{
66 public:
67
68 enum { DiagIndex = _DiagIndex };
69 typedef typename internal::dense_xpr_base<Diagonal>::type Base;
70 EIGEN_DENSE_PUBLIC_INTERFACE(Diagonal)
71
72 EIGEN_DEVICE_FUNC
73 explicit inline Diagonal(MatrixType& matrix, Index a_index = DiagIndex) : m_matrix(matrix), m_index(a_index)
74 {
75 eigen_assert( a_index <= m_matrix.cols() && -a_index <= m_matrix.rows() );
76 }
77
78 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Diagonal)
79
80 EIGEN_DEVICE_FUNC
81 inline Index rows() const
82 {
83 return m_index.value()<0 ? numext::mini<Index>(m_matrix.cols(),m_matrix.rows()+m_index.value())
84 : numext::mini<Index>(m_matrix.rows(),m_matrix.cols()-m_index.value());
85 }
86
87 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
88 inline Index cols() const EIGEN_NOEXCEPT { return 1; }
89
90 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
91 inline Index innerStride() const EIGEN_NOEXCEPT {
92 return m_matrix.outerStride() + 1;
93 }
94
95 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
96 inline Index outerStride() const EIGEN_NOEXCEPT { return 0; }
97
98 typedef typename internal::conditional<
100 Scalar,
101 const Scalar
103
104 EIGEN_DEVICE_FUNC
105 inline ScalarWithConstIfNotLvalue* data() { return &(m_matrix.coeffRef(rowOffset(), colOffset())); }
106 EIGEN_DEVICE_FUNC
107 inline const Scalar* data() const { return &(m_matrix.coeffRef(rowOffset(), colOffset())); }
108
109 EIGEN_DEVICE_FUNC
110 inline Scalar& coeffRef(Index row, Index)
111 {
112 EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
113 return m_matrix.coeffRef(row+rowOffset(), row+colOffset());
114 }
115
116 EIGEN_DEVICE_FUNC
117 inline const Scalar& coeffRef(Index row, Index) const
118 {
119 return m_matrix.coeffRef(row+rowOffset(), row+colOffset());
120 }
121
122 EIGEN_DEVICE_FUNC
123 inline CoeffReturnType coeff(Index row, Index) const
124 {
125 return m_matrix.coeff(row+rowOffset(), row+colOffset());
126 }
127
128 EIGEN_DEVICE_FUNC
129 inline Scalar& coeffRef(Index idx)
130 {
131 EIGEN_STATIC_ASSERT_LVALUE(MatrixType)
132 return m_matrix.coeffRef(idx+rowOffset(), idx+colOffset());
133 }
134
135 EIGEN_DEVICE_FUNC
136 inline const Scalar& coeffRef(Index idx) const
137 {
138 return m_matrix.coeffRef(idx+rowOffset(), idx+colOffset());
139 }
140
141 EIGEN_DEVICE_FUNC
142 inline CoeffReturnType coeff(Index idx) const
143 {
144 return m_matrix.coeff(idx+rowOffset(), idx+colOffset());
145 }
146
147 EIGEN_DEVICE_FUNC
148 inline const typename internal::remove_all<typename MatrixType::Nested>::type&
149 nestedExpression() const
150 {
151 return m_matrix;
152 }
153
154 EIGEN_DEVICE_FUNC
155 inline Index index() const
156 {
157 return m_index.value();
158 }
159
160 protected:
163
164 private:
165 // some compilers may fail to optimize std::max etc in case of compile-time constants...
166 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
167 Index absDiagIndex() const EIGEN_NOEXCEPT { return m_index.value()>0 ? m_index.value() : -m_index.value(); }
168 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
169 Index rowOffset() const EIGEN_NOEXCEPT { return m_index.value()>0 ? 0 : -m_index.value(); }
170 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
171 Index colOffset() const EIGEN_NOEXCEPT { return m_index.value()>0 ? m_index.value() : 0; }
172 // trigger a compile-time error if someone try to call packet
173 template<int LoadMode> typename MatrixType::PacketReturnType packet(Index) const;
174 template<int LoadMode> typename MatrixType::PacketReturnType packet(Index,Index) const;
175};
176
185template<typename Derived>
186EIGEN_DEVICE_FUNC inline typename MatrixBase<Derived>::DiagonalReturnType
188{
189 return DiagonalReturnType(derived());
190}
191
193template<typename Derived>
194EIGEN_DEVICE_FUNC inline
197{
198 return ConstDiagonalReturnType(derived());
199}
200
212template<typename Derived>
213EIGEN_DEVICE_FUNC inline Diagonal<Derived, DynamicIndex>
215{
216 return Diagonal<Derived, DynamicIndex>(derived(), index);
217}
218
220template<typename Derived>
221EIGEN_DEVICE_FUNC inline const Diagonal<const Derived, DynamicIndex>
226
238template<typename Derived>
239template<int Index_>
240EIGEN_DEVICE_FUNC
246
248template<typename Derived>
249template<int Index_>
250EIGEN_DEVICE_FUNC
253{
254 return Diagonal<const Derived, Index_>(derived());
255}
256
257} // end namespace Eigen
258
259#endif // EIGEN_DIAGONAL_H
EIGEN_DEVICE_FUNC CoeffReturnType value() const
Definition DenseBase.h:526
Expression of a diagonal/subdiagonal/superdiagonal in a matrix.
Definition Diagonal.h:65
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:50
const unsigned int DirectAccessBit
Means that the underlying array of coefficients can be directly accessed as a plain strided array.
Definition Constants.h:155
const unsigned int LvalueBit
Means the expression has a coeffRef() method, i.e.
Definition Constants.h:144
const unsigned int RowMajorBit
for a matrix, this means that the storage order is row-major.
Definition Constants.h:66
Namespace containing all symbols from the Eigen library.
Definition LDLT.h:16
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:74
const int DynamicIndex
This value means that a signed quantity (e.g., a signed index) is not known at compile-time,...
Definition Constants.h:27
const int Dynamic
This value means that a positive quantity (e.g., a size) is not known at compile-time,...
Definition Constants.h:22
Definition Meta.h:109
Definition XprHelper.h:484
Definition XprHelper.h:660
Definition DenseCoeffsBase.h:671
Definition ForwardDeclarations.h:17
Definition Meta.h:96