Eigen  3.4.90 (git rev a4098ac676528a83cfb73d4d26ce1b42ec05f47c)
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
16namespace Eigen {
17
37namespace internal {
38template<typename MatrixType, int DiagIndex>
39struct traits<Diagonal<MatrixType,DiagIndex> >
40 : traits<MatrixType>
41{
42 typedef typename ref_selector<MatrixType>::type MatrixTypeNested;
43 typedef typename remove_reference<MatrixTypeNested>::type _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
65template<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 typename internal::conditional<
101 internal::is_lvalue<MatrixType>::value,
102 Scalar,
103 const Scalar
104 >::type 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 typename internal::remove_all<typename MatrixType::Nested>::type&
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
187template<typename Derived>
188EIGEN_DEVICE_FUNC inline typename MatrixBase<Derived>::DiagonalReturnType
190{
191 return DiagonalReturnType(derived());
192}
193
195template<typename Derived>
196EIGEN_DEVICE_FUNC inline typename MatrixBase<Derived>::ConstDiagonalReturnType
198{
199 return ConstDiagonalReturnType(derived());
200}
201
213template<typename Derived>
214EIGEN_DEVICE_FUNC inline typename MatrixBase<Derived>::DiagonalDynamicIndexReturnType
216{
217 return DiagonalDynamicIndexReturnType(derived(), index);
218}
219
221template<typename Derived>
222EIGEN_DEVICE_FUNC inline typename MatrixBase<Derived>::ConstDiagonalDynamicIndexReturnType
224{
225 return ConstDiagonalDynamicIndexReturnType(derived(), index);
226}
227
239template<typename Derived>
240template<int Index_>
241EIGEN_DEVICE_FUNC
242inline typename MatrixBase<Derived>::template DiagonalIndexReturnType<Index_>::Type
244{
245 return typename DiagonalIndexReturnType<Index_>::Type(derived());
246}
247
249template<typename Derived>
250template<int Index_>
251EIGEN_DEVICE_FUNC
252inline typename MatrixBase<Derived>::template ConstDiagonalIndexReturnType<Index_>::Type
254{
255 return typename ConstDiagonalIndexReturnType<Index_>::Type(derived());
256}
257
258} // end namespace Eigen
259
260#endif // EIGEN_DIAGONAL_H
Expression of a diagonal/subdiagonal/superdiagonal in a matrix.
Definition: Diagonal.h:67
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
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: B01_Experimental.dox:1
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