Eigen  5.0.1-dev+60122df6
 
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 <gael.guennebaud@inria.fr>
5// Copyright (C) 2007-2009 Benoit Jacob <jacob.benoit.1@gmail.com>
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
51 DenseMatrixType;
52 typedef DenseMatrixType DenseType;
54 PlainObject;
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 constexpr Index rows() const { return diagonal().size(); }
81 EIGEN_DEVICE_FUNC constexpr Index cols() const { return diagonal().size(); }
82
84 template <typename MatrixDerived>
86 const MatrixBase<MatrixDerived>& matrix) const {
87 return Product<Derived, MatrixDerived, LazyProduct>(derived(), matrix.derived());
88 }
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>
96 EIGEN_DEVICE_FUNC const DiagonalProductReturnType<OtherDerived> operator*(
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>
143 EIGEN_DEVICE_FUNC inline const DiagonalDifferenceReturnType<OtherDerived> operator-(
144 const DiagonalBase<OtherDerived>& other) const {
145 return (diagonal() - other.diagonal()).asDiagonal();
146 }
147};
148
161
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 ZeroInitializeReturnType;
261
263 EIGEN_DEVICE_FUNC static const ZeroInitializeReturnType Zero() { return DiagonalVectorType::Zero().asDiagonal(); }
265 EIGEN_DEVICE_FUNC static const ZeroInitializeReturnType Zero(Index size) {
266 return DiagonalVectorType::Zero(size).asDiagonal();
267 }
269 EIGEN_DEVICE_FUNC static const InitializeReturnType Identity() { return DiagonalVectorType::Ones().asDiagonal(); }
271 EIGEN_DEVICE_FUNC static const InitializeReturnType Identity(Index size) {
272 return DiagonalVectorType::Ones(size).asDiagonal();
273 }
274
276 EIGEN_DEVICE_FUNC inline void resize(Index size) { m_diagonal.resize(size); }
278 EIGEN_DEVICE_FUNC inline void setZero() { m_diagonal.setZero(); }
280 EIGEN_DEVICE_FUNC inline void setZero(Index size) { m_diagonal.setZero(size); }
282 EIGEN_DEVICE_FUNC inline void setIdentity() { m_diagonal.setOnes(); }
284 EIGEN_DEVICE_FUNC inline void setIdentity(Index size) { m_diagonal.setOnes(size); }
285};
286
300
301namespace internal {
302template <typename DiagonalVectorType_>
303struct traits<DiagonalWrapper<DiagonalVectorType_>> {
304 typedef DiagonalVectorType_ DiagonalVectorType;
305 typedef typename DiagonalVectorType::Scalar Scalar;
306 typedef typename DiagonalVectorType::StorageIndex StorageIndex;
307 typedef DiagonalShape StorageKind;
308 typedef typename traits<DiagonalVectorType>::XprKind XprKind;
309 enum {
310 RowsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
311 ColsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
312 MaxRowsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
313 MaxColsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
314 Flags = (traits<DiagonalVectorType>::Flags & LvalueBit) | NoPreferredStorageOrderBit
315 };
316};
317} // namespace internal
318
319template <typename DiagonalVectorType_>
320class DiagonalWrapper : public DiagonalBase<DiagonalWrapper<DiagonalVectorType_>>, internal::no_assignment_operator {
321 public:
322#ifndef EIGEN_PARSED_BY_DOXYGEN
323 typedef DiagonalVectorType_ DiagonalVectorType;
324 typedef DiagonalWrapper Nested;
325#endif
326
328 EIGEN_DEVICE_FUNC explicit inline DiagonalWrapper(DiagonalVectorType& a_diagonal) : m_diagonal(a_diagonal) {}
329
331 EIGEN_DEVICE_FUNC const DiagonalVectorType& diagonal() const { return m_diagonal; }
332
333 protected:
334 typename DiagonalVectorType::Nested m_diagonal;
335};
336
346template <typename Derived>
348 return DiagonalWrapper<const Derived>(derived());
349}
350
359template <typename Derived>
360bool MatrixBase<Derived>::isDiagonal(const RealScalar& prec) const {
361 if (cols() != rows()) return false;
362 RealScalar maxAbsOnDiagonal = static_cast<RealScalar>(-1);
363 for (Index j = 0; j < cols(); ++j) {
364 RealScalar absOnDiagonal = numext::abs(coeff(j, j));
365 if (absOnDiagonal > maxAbsOnDiagonal) maxAbsOnDiagonal = absOnDiagonal;
366 }
367 for (Index j = 0; j < cols(); ++j)
368 for (Index i = 0; i < j; ++i) {
369 if (!internal::isMuchSmallerThan(coeff(i, j), maxAbsOnDiagonal, prec)) return false;
370 if (!internal::isMuchSmallerThan(coeff(j, i), maxAbsOnDiagonal, prec)) return false;
371 }
372 return true;
373}
374
375namespace internal {
376
377template <>
378struct storage_kind_to_shape<DiagonalShape> {
379 typedef DiagonalShape Shape;
380};
381
382struct Diagonal2Dense {};
383
384template <>
385struct AssignmentKind<DenseShape, DiagonalShape> {
386 typedef Diagonal2Dense Kind;
387};
388
389// Diagonal matrix to Dense assignment
390template <typename DstXprType, typename SrcXprType, typename Functor>
391struct Assignment<DstXprType, SrcXprType, Functor, Diagonal2Dense> {
392 static EIGEN_DEVICE_FUNC void run(
393 DstXprType& dst, const SrcXprType& src,
394 const internal::assign_op<typename DstXprType::Scalar, typename SrcXprType::Scalar>& /*func*/) {
395 Index dstRows = src.rows();
396 Index dstCols = src.cols();
397 if ((dst.rows() != dstRows) || (dst.cols() != dstCols)) dst.resize(dstRows, dstCols);
398
399 dst.setZero();
400 dst.diagonal() = src.diagonal();
401 }
402
403 static EIGEN_DEVICE_FUNC void run(
404 DstXprType& dst, const SrcXprType& src,
405 const internal::add_assign_op<typename DstXprType::Scalar, typename SrcXprType::Scalar>& /*func*/) {
406 dst.diagonal() += src.diagonal();
407 }
408
409 static EIGEN_DEVICE_FUNC void run(
410 DstXprType& dst, const SrcXprType& src,
411 const internal::sub_assign_op<typename DstXprType::Scalar, typename SrcXprType::Scalar>& /*func*/) {
412 dst.diagonal() -= src.diagonal();
413 }
414};
415
416} // namespace internal
417
418} // end namespace Eigen
419
420#endif // EIGEN_DIAGONALMATRIX_H
Base class for diagonal matrices and expressions.
Definition DiagonalMatrix.h:33
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
Derived & derived()
Definition DiagonalMatrix.h:59
const DiagonalProductReturnType< OtherDerived > operator*(const DiagonalBase< OtherDerived > &other) const
Definition DiagonalMatrix.h:96
constexpr Index cols() const
Definition DiagonalMatrix.h:81
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
constexpr Index rows() const
Definition DiagonalMatrix.h:79
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:271
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:269
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:284
void setZero()
Definition DiagonalMatrix.h:278
void setIdentity()
Definition DiagonalMatrix.h:282
DiagonalMatrix(const DiagonalBase< OtherDerived > &other)
Definition DiagonalMatrix.h:228
DiagonalMatrix(Index dim)
Definition DiagonalMatrix.h:195
void setZero(Index size)
Definition DiagonalMatrix.h:280
void resize(Index size)
Definition DiagonalMatrix.h:276
DiagonalMatrix(const MatrixBase< OtherDerived > &other)
Definition DiagonalMatrix.h:237
const DiagonalVectorType & diagonal() const
Definition DiagonalMatrix.h:187
static const ZeroInitializeReturnType Zero()
Definition DiagonalMatrix.h:263
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:320
DiagonalWrapper(DiagonalVectorType &a_diagonal)
Definition DiagonalMatrix.h:328
const DiagonalVectorType & diagonal() const
Definition DiagonalMatrix.h:331
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:52
const DiagonalWrapper< const Derived > asDiagonal() const
Definition DiagonalMatrix.h:347
bool isDiagonal(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition DiagonalMatrix.h:360
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
const unsigned int LvalueBit
Definition Constants.h:148
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:82
Definition EigenBase.h:33
Eigen::Index Index
The interface type of indices.
Definition EigenBase.h:43
constexpr Index size() const noexcept
Definition EigenBase.h:64