Eigen  3.4.90 (git rev 5a9f66fb35d03a4da9ef8976e67a61b30aa16dcf)
 
Loading...
Searching...
No Matches
DiagonalMatrix.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2009 Gael Guennebaud <[email protected]>
5// Copyright (C) 2007-2009 Benoit Jacob <[email protected]>
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_DIAGONALMATRIX_H
12#define EIGEN_DIAGONALMATRIX_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
32template <typename Derived>
33class DiagonalBase : public EigenBase<Derived> {
34 public:
35 typedef typename internal::traits<Derived>::DiagonalVectorType DiagonalVectorType;
36 typedef typename DiagonalVectorType::Scalar Scalar;
37 typedef typename DiagonalVectorType::RealScalar RealScalar;
38 typedef typename internal::traits<Derived>::StorageKind StorageKind;
39 typedef typename internal::traits<Derived>::StorageIndex StorageIndex;
40
41 enum {
42 RowsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
43 ColsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
44 MaxRowsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
45 MaxColsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
46 IsVectorAtCompileTime = 0,
48 };
49
55
57 EIGEN_DEVICE_FUNC inline const Derived& derived() const { return *static_cast<const Derived*>(this); }
59 EIGEN_DEVICE_FUNC inline Derived& derived() { return *static_cast<Derived*>(this); }
60
65 EIGEN_DEVICE_FUNC DenseMatrixType toDenseMatrix() const { return derived(); }
66
68 EIGEN_DEVICE_FUNC inline const DiagonalVectorType& diagonal() const { return derived().diagonal(); }
70 EIGEN_DEVICE_FUNC inline DiagonalVectorType& diagonal() { return derived().diagonal(); }
71
73 EIGEN_DEVICE_FUNC inline Scalar coeff(Index row, Index col) const {
74 eigen_assert(row >= 0 && col >= 0 && row < rows() && col <= cols());
75 return row == col ? diagonal().coeff(row) : Scalar(0);
76 }
77
79 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index rows() const { return diagonal().size(); }
81 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index cols() const { return diagonal().size(); }
82
84 template <typename MatrixDerived>
89
90 template <typename OtherDerived>
91 using DiagonalProductReturnType = DiagonalWrapper<const EIGEN_CWISE_BINARY_RETURN_TYPE(
92 DiagonalVectorType, typename OtherDerived::DiagonalVectorType, product)>;
93
95 template <typename OtherDerived>
97 const DiagonalBase<OtherDerived>& other) const {
98 return diagonal().cwiseProduct(other.diagonal()).asDiagonal();
99 }
100
101 using DiagonalInverseReturnType =
103
105 EIGEN_DEVICE_FUNC inline const DiagonalInverseReturnType inverse() const {
106 return diagonal().cwiseInverse().asDiagonal();
107 }
108
109 using DiagonalScaleReturnType =
110 DiagonalWrapper<const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DiagonalVectorType, Scalar, product)>;
111
113 EIGEN_DEVICE_FUNC inline const DiagonalScaleReturnType operator*(const Scalar& scalar) const {
114 return (diagonal() * scalar).asDiagonal();
115 }
116
117 using ScaleDiagonalReturnType =
118 DiagonalWrapper<const EIGEN_SCALAR_BINARYOP_EXPR_RETURN_TYPE(Scalar, DiagonalVectorType, product)>;
119
121 EIGEN_DEVICE_FUNC friend inline const ScaleDiagonalReturnType operator*(const Scalar& scalar,
122 const DiagonalBase& other) {
123 return (scalar * other.diagonal()).asDiagonal();
124 }
125
126 template <typename OtherDerived>
127 using DiagonalSumReturnType = DiagonalWrapper<const EIGEN_CWISE_BINARY_RETURN_TYPE(
128 DiagonalVectorType, typename OtherDerived::DiagonalVectorType, sum)>;
129
131 template <typename OtherDerived>
132 EIGEN_DEVICE_FUNC inline const DiagonalSumReturnType<OtherDerived> operator+(
133 const DiagonalBase<OtherDerived>& other) const {
134 return (diagonal() + other.diagonal()).asDiagonal();
135 }
136
137 template <typename OtherDerived>
138 using DiagonalDifferenceReturnType = DiagonalWrapper<const EIGEN_CWISE_BINARY_RETURN_TYPE(
139 DiagonalVectorType, typename OtherDerived::DiagonalVectorType, difference)>;
140
142 template <typename OtherDerived>
144 const DiagonalBase<OtherDerived>& other) const {
145 return (diagonal() - other.diagonal()).asDiagonal();
146 }
147};
148
162namespace internal {
163template <typename Scalar_, int SizeAtCompileTime, int MaxSizeAtCompileTime>
164struct traits<DiagonalMatrix<Scalar_, SizeAtCompileTime, MaxSizeAtCompileTime>>
165 : traits<Matrix<Scalar_, SizeAtCompileTime, SizeAtCompileTime, 0, MaxSizeAtCompileTime, MaxSizeAtCompileTime>> {
166 typedef Matrix<Scalar_, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1> DiagonalVectorType;
167 typedef DiagonalShape StorageKind;
168 enum { Flags = LvalueBit | NoPreferredStorageOrderBit | NestByRefBit };
169};
170} // namespace internal
171template <typename Scalar_, int SizeAtCompileTime, int MaxSizeAtCompileTime>
172class DiagonalMatrix : public DiagonalBase<DiagonalMatrix<Scalar_, SizeAtCompileTime, MaxSizeAtCompileTime>> {
173 public:
174#ifndef EIGEN_PARSED_BY_DOXYGEN
175 typedef typename internal::traits<DiagonalMatrix>::DiagonalVectorType DiagonalVectorType;
176 typedef const DiagonalMatrix& Nested;
177 typedef Scalar_ Scalar;
178 typedef typename internal::traits<DiagonalMatrix>::StorageKind StorageKind;
179 typedef typename internal::traits<DiagonalMatrix>::StorageIndex StorageIndex;
180#endif
181
182 protected:
183 DiagonalVectorType m_diagonal;
184
185 public:
187 EIGEN_DEVICE_FUNC inline const DiagonalVectorType& diagonal() const { return m_diagonal; }
189 EIGEN_DEVICE_FUNC inline DiagonalVectorType& diagonal() { return m_diagonal; }
190
192 EIGEN_DEVICE_FUNC inline DiagonalMatrix() {}
193
195 EIGEN_DEVICE_FUNC explicit inline DiagonalMatrix(Index dim) : m_diagonal(dim) {}
196
198 EIGEN_DEVICE_FUNC inline DiagonalMatrix(const Scalar& x, const Scalar& y) : m_diagonal(x, y) {}
199
201 EIGEN_DEVICE_FUNC inline DiagonalMatrix(const Scalar& x, const Scalar& y, const Scalar& z) : m_diagonal(x, y, z) {}
202
211 template <typename... ArgTypes>
212 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DiagonalMatrix(const Scalar& a0, const Scalar& a1, const Scalar& a2,
213 const ArgTypes&... args)
214 : m_diagonal(a0, a1, a2, args...) {}
215
219 EIGEN_DEVICE_FUNC explicit EIGEN_STRONG_INLINE DiagonalMatrix(
220 const std::initializer_list<std::initializer_list<Scalar>>& list)
221 : m_diagonal(list) {}
222
224 EIGEN_DEVICE_FUNC explicit inline DiagonalMatrix(DiagonalVectorType&& diag) : m_diagonal(std::move(diag)) {}
225
227 template <typename OtherDerived>
228 EIGEN_DEVICE_FUNC inline DiagonalMatrix(const DiagonalBase<OtherDerived>& other) : m_diagonal(other.diagonal()) {}
229
230#ifndef EIGEN_PARSED_BY_DOXYGEN
232 inline DiagonalMatrix(const DiagonalMatrix& other) : m_diagonal(other.diagonal()) {}
233#endif
234
236 template <typename OtherDerived>
237 EIGEN_DEVICE_FUNC explicit inline DiagonalMatrix(const MatrixBase<OtherDerived>& other) : m_diagonal(other) {}
238
240 template <typename OtherDerived>
241 EIGEN_DEVICE_FUNC DiagonalMatrix& operator=(const DiagonalBase<OtherDerived>& other) {
242 m_diagonal = other.diagonal();
243 return *this;
244 }
245
246#ifndef EIGEN_PARSED_BY_DOXYGEN
250 EIGEN_DEVICE_FUNC DiagonalMatrix& operator=(const DiagonalMatrix& other) {
251 m_diagonal = other.diagonal();
252 return *this;
253 }
254#endif
257 InitializeReturnType;
258
260 EIGEN_DEVICE_FUNC static const InitializeReturnType Zero() { return DiagonalVectorType::Zero().asDiagonal(); }
262 EIGEN_DEVICE_FUNC static const InitializeReturnType Zero(Index size) {
263 return DiagonalVectorType::Zero(size).asDiagonal();
264 }
266 EIGEN_DEVICE_FUNC static const InitializeReturnType Identity() { return DiagonalVectorType::Ones().asDiagonal(); }
268 EIGEN_DEVICE_FUNC static const InitializeReturnType Identity(Index size) {
269 return DiagonalVectorType::Ones(size).asDiagonal();
270 }
271
273 EIGEN_DEVICE_FUNC inline void resize(Index size) { m_diagonal.resize(size); }
275 EIGEN_DEVICE_FUNC inline void setZero() { m_diagonal.setZero(); }
277 EIGEN_DEVICE_FUNC inline void setZero(Index size) { m_diagonal.setZero(size); }
279 EIGEN_DEVICE_FUNC inline void setIdentity() { m_diagonal.setOnes(); }
281 EIGEN_DEVICE_FUNC inline void setIdentity(Index size) { m_diagonal.setOnes(size); }
282};
283
298namespace internal {
299template <typename DiagonalVectorType_>
300struct traits<DiagonalWrapper<DiagonalVectorType_>> {
301 typedef DiagonalVectorType_ DiagonalVectorType;
302 typedef typename DiagonalVectorType::Scalar Scalar;
303 typedef typename DiagonalVectorType::StorageIndex StorageIndex;
304 typedef DiagonalShape StorageKind;
305 typedef typename traits<DiagonalVectorType>::XprKind XprKind;
306 enum {
307 RowsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
308 ColsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
309 MaxRowsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
310 MaxColsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
311 Flags = (traits<DiagonalVectorType>::Flags & LvalueBit) | NoPreferredStorageOrderBit
312 };
313};
314} // namespace internal
315
316template <typename DiagonalVectorType_>
317class DiagonalWrapper : public DiagonalBase<DiagonalWrapper<DiagonalVectorType_>>, internal::no_assignment_operator {
318 public:
319#ifndef EIGEN_PARSED_BY_DOXYGEN
320 typedef DiagonalVectorType_ DiagonalVectorType;
321 typedef DiagonalWrapper Nested;
322#endif
323
325 EIGEN_DEVICE_FUNC explicit inline DiagonalWrapper(DiagonalVectorType& a_diagonal) : m_diagonal(a_diagonal) {}
326
328 EIGEN_DEVICE_FUNC const DiagonalVectorType& diagonal() const { return m_diagonal; }
329
330 protected:
331 typename DiagonalVectorType::Nested m_diagonal;
332};
333
343template <typename Derived>
345 return DiagonalWrapper<const Derived>(derived());
346}
347
356template <typename Derived>
357bool MatrixBase<Derived>::isDiagonal(const RealScalar& prec) const {
358 if (cols() != rows()) return false;
359 RealScalar maxAbsOnDiagonal = static_cast<RealScalar>(-1);
360 for (Index j = 0; j < cols(); ++j) {
361 RealScalar absOnDiagonal = numext::abs(coeff(j, j));
362 if (absOnDiagonal > maxAbsOnDiagonal) maxAbsOnDiagonal = absOnDiagonal;
363 }
364 for (Index j = 0; j < cols(); ++j)
365 for (Index i = 0; i < j; ++i) {
366 if (!internal::isMuchSmallerThan(coeff(i, j), maxAbsOnDiagonal, prec)) return false;
367 if (!internal::isMuchSmallerThan(coeff(j, i), maxAbsOnDiagonal, prec)) return false;
368 }
369 return true;
370}
371
372namespace internal {
373
374template <>
375struct storage_kind_to_shape<DiagonalShape> {
376 typedef DiagonalShape Shape;
377};
378
379struct Diagonal2Dense {};
380
381template <>
382struct AssignmentKind<DenseShape, DiagonalShape> {
383 typedef Diagonal2Dense Kind;
384};
385
386// Diagonal matrix to Dense assignment
387template <typename DstXprType, typename SrcXprType, typename Functor>
388struct Assignment<DstXprType, SrcXprType, Functor, Diagonal2Dense> {
389 static void run(DstXprType& dst, const SrcXprType& src,
390 const internal::assign_op<typename DstXprType::Scalar, typename SrcXprType::Scalar>& /*func*/) {
391 Index dstRows = src.rows();
392 Index dstCols = src.cols();
393 if ((dst.rows() != dstRows) || (dst.cols() != dstCols)) dst.resize(dstRows, dstCols);
394
395 dst.setZero();
396 dst.diagonal() = src.diagonal();
397 }
398
399 static void run(DstXprType& dst, const SrcXprType& src,
400 const internal::add_assign_op<typename DstXprType::Scalar, typename SrcXprType::Scalar>& /*func*/) {
401 dst.diagonal() += src.diagonal();
402 }
403
404 static void run(DstXprType& dst, const SrcXprType& src,
405 const internal::sub_assign_op<typename DstXprType::Scalar, typename SrcXprType::Scalar>& /*func*/) {
406 dst.diagonal() -= src.diagonal();
407 }
408};
409
410} // namespace internal
411
412} // end namespace Eigen
413
414#endif // EIGEN_DIAGONALMATRIX_H
Derived & derived()
Definition EigenBase.h:49
Base class for diagonal matrices and expressions.
Definition DiagonalMatrix.h:33
EIGEN_CONSTEXPR Index cols() const
Definition DiagonalMatrix.h:81
const Product< Derived, MatrixDerived, LazyProduct > operator*(const MatrixBase< MatrixDerived > &matrix) const
Definition DiagonalMatrix.h:85
friend const ScaleDiagonalReturnType operator*(const Scalar &scalar, const DiagonalBase &other)
Definition DiagonalMatrix.h:121
DiagonalVectorType & diagonal()
Definition DiagonalMatrix.h:70
const Derived & derived() const
Definition DiagonalMatrix.h:57
DenseMatrixType toDenseMatrix() const
Definition DiagonalMatrix.h:65
Scalar coeff(Index row, Index col) const
Definition DiagonalMatrix.h:73
const DiagonalInverseReturnType inverse() const
Definition DiagonalMatrix.h:105
EIGEN_CONSTEXPR Index rows() const
Definition DiagonalMatrix.h:79
Derived & derived()
Definition DiagonalMatrix.h:59
const DiagonalProductReturnType< OtherDerived > operator*(const DiagonalBase< OtherDerived > &other) const
Definition DiagonalMatrix.h:96
const DiagonalVectorType & diagonal() const
Definition DiagonalMatrix.h:68
const DiagonalScaleReturnType operator*(const Scalar &scalar) const
Definition DiagonalMatrix.h:113
const DiagonalSumReturnType< OtherDerived > operator+(const DiagonalBase< OtherDerived > &other) const
Definition DiagonalMatrix.h:132
const DiagonalDifferenceReturnType< OtherDerived > operator-(const DiagonalBase< OtherDerived > &other) const
Definition DiagonalMatrix.h:143
Represents a diagonal matrix with its storage.
Definition DiagonalMatrix.h:172
DiagonalMatrix & operator=(const DiagonalBase< OtherDerived > &other)
Definition DiagonalMatrix.h:241
static const InitializeReturnType Identity(Index size)
Definition DiagonalMatrix.h:268
DiagonalMatrix(const Scalar &x, const Scalar &y)
Definition DiagonalMatrix.h:198
DiagonalMatrix(const std::initializer_list< std::initializer_list< Scalar > > &list)
Constructs a DiagonalMatrix and initializes it by elements given by an initializer list of initialize...
Definition DiagonalMatrix.h:219
DiagonalMatrix()
Definition DiagonalMatrix.h:192
static const InitializeReturnType Identity()
Definition DiagonalMatrix.h:266
DiagonalMatrix(const Scalar &x, const Scalar &y, const Scalar &z)
Definition DiagonalMatrix.h:201
DiagonalMatrix(DiagonalVectorType &&diag)
Constructs a DiagonalMatrix from an r-value diagonal vector type.
Definition DiagonalMatrix.h:224
void setIdentity(Index size)
Definition DiagonalMatrix.h:281
void setZero()
Definition DiagonalMatrix.h:275
void setIdentity()
Definition DiagonalMatrix.h:279
DiagonalMatrix(const DiagonalBase< OtherDerived > &other)
Definition DiagonalMatrix.h:228
DiagonalMatrix(Index dim)
Definition DiagonalMatrix.h:195
void setZero(Index size)
Definition DiagonalMatrix.h:277
void resize(Index size)
Definition DiagonalMatrix.h:273
static const InitializeReturnType Zero()
Definition DiagonalMatrix.h:260
static const InitializeReturnType Zero(Index size)
Definition DiagonalMatrix.h:262
DiagonalMatrix(const MatrixBase< OtherDerived > &other)
Definition DiagonalMatrix.h:237
const DiagonalVectorType & diagonal() const
Definition DiagonalMatrix.h:187
DiagonalVectorType & diagonal()
Definition DiagonalMatrix.h:189
DiagonalMatrix(const Scalar &a0, const Scalar &a1, const Scalar &a2, const ArgTypes &... args)
Construct a diagonal matrix with fixed size from an arbitrary number of coefficients.
Definition DiagonalMatrix.h:212
Expression of a diagonal matrix.
Definition DiagonalMatrix.h:317
DiagonalWrapper(DiagonalVectorType &a_diagonal)
Definition DiagonalMatrix.h:325
const DiagonalVectorType & diagonal() const
Definition DiagonalMatrix.h:328
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:52
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:186
Expression of the product of two arbitrary matrices or vectors.
Definition Product.h:202
const unsigned int NoPreferredStorageOrderBit
Definition Constants.h:182
Namespace containing all symbols from the Eigen library.
Definition Core:137
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:83
Definition EigenBase.h:33
Eigen::Index Index
The interface type of indices.
Definition EigenBase.h:43
EIGEN_CONSTEXPR Index size() const EIGEN_NOEXCEPT
Definition EigenBase.h:64