11#ifndef EIGEN_DIAGONALMATRIX_H
12#define EIGEN_DIAGONALMATRIX_H
15#include "./InternalHeaderCheck.h"
32template <
typename Derived>
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;
42 RowsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
43 ColsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
44 MaxRowsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
45 MaxColsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
46 IsVectorAtCompileTime = 0,
52 typedef DenseMatrixType DenseType;
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); }
68 EIGEN_DEVICE_FUNC
inline const DiagonalVectorType&
diagonal()
const {
return derived().diagonal(); }
70 EIGEN_DEVICE_FUNC
inline DiagonalVectorType&
diagonal() {
return derived().diagonal(); }
74 eigen_assert(row >= 0 && col >= 0 && row <
rows() && col <=
cols());
75 return row == col ?
diagonal().coeff(row) : Scalar(0);
84 template <
typename MatrixDerived>
90 template <
typename OtherDerived>
91 using DiagonalProductReturnType =
DiagonalWrapper<
const EIGEN_CWISE_BINARY_RETURN_TYPE(
92 DiagonalVectorType,
typename OtherDerived::DiagonalVectorType, product)>;
95 template <
typename OtherDerived>
96 EIGEN_DEVICE_FUNC
const DiagonalProductReturnType<OtherDerived>
operator*(
101 using DiagonalInverseReturnType =
105 EIGEN_DEVICE_FUNC
inline const DiagonalInverseReturnType
inverse()
const {
106 return diagonal().cwiseInverse().asDiagonal();
109 using DiagonalScaleReturnType =
113 EIGEN_DEVICE_FUNC
inline const DiagonalScaleReturnType
operator*(
const Scalar& scalar)
const {
114 return (
diagonal() * scalar).asDiagonal();
117 using ScaleDiagonalReturnType =
121 EIGEN_DEVICE_FUNC
friend inline const ScaleDiagonalReturnType
operator*(
const Scalar& scalar,
123 return (scalar * other.
diagonal()).asDiagonal();
126 template <
typename OtherDerived>
127 using DiagonalSumReturnType =
DiagonalWrapper<
const EIGEN_CWISE_BINARY_RETURN_TYPE(
128 DiagonalVectorType,
typename OtherDerived::DiagonalVectorType, sum)>;
131 template <
typename OtherDerived>
132 EIGEN_DEVICE_FUNC
inline const DiagonalSumReturnType<OtherDerived>
operator+(
137 template <
typename OtherDerived>
138 using DiagonalDifferenceReturnType =
DiagonalWrapper<
const EIGEN_CWISE_BINARY_RETURN_TYPE(
139 DiagonalVectorType,
typename OtherDerived::DiagonalVectorType, difference)>;
142 template <
typename OtherDerived>
143 EIGEN_DEVICE_FUNC
inline const DiagonalDifferenceReturnType<OtherDerived>
operator-(
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;
171template <
typename Scalar_,
int SizeAtCompileTime,
int MaxSizeAtCompileTime>
174#ifndef EIGEN_PARSED_BY_DOXYGEN
175 typedef typename internal::traits<DiagonalMatrix>::DiagonalVectorType DiagonalVectorType;
177 typedef Scalar_ Scalar;
178 typedef typename internal::traits<DiagonalMatrix>::StorageKind StorageKind;
179 typedef typename internal::traits<DiagonalMatrix>::StorageIndex StorageIndex;
183 DiagonalVectorType m_diagonal;
187 EIGEN_DEVICE_FUNC
inline const DiagonalVectorType&
diagonal()
const {
return m_diagonal; }
189 EIGEN_DEVICE_FUNC
inline DiagonalVectorType&
diagonal() {
return m_diagonal; }
198 EIGEN_DEVICE_FUNC
inline DiagonalMatrix(
const Scalar& x,
const Scalar& y) : m_diagonal(x, y) {}
201 EIGEN_DEVICE_FUNC
inline DiagonalMatrix(
const Scalar& x,
const Scalar& y,
const Scalar& z) : m_diagonal(x, y, z) {}
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...) {}
220 const std::initializer_list<std::initializer_list<Scalar>>& list)
221 : m_diagonal(list) {}
224 EIGEN_DEVICE_FUNC
explicit inline DiagonalMatrix(DiagonalVectorType&& diag) : m_diagonal(std::move(diag)) {}
227 template <
typename OtherDerived>
230#ifndef EIGEN_PARSED_BY_DOXYGEN
236 template <
typename OtherDerived>
240 template <
typename OtherDerived>
246#ifndef EIGEN_PARSED_BY_DOXYGEN
257 InitializeReturnType;
260 ZeroInitializeReturnType;
263 EIGEN_DEVICE_FUNC
static const ZeroInitializeReturnType
Zero() {
return DiagonalVectorType::Zero().asDiagonal(); }
266 return DiagonalVectorType::Zero(
size).asDiagonal();
269 EIGEN_DEVICE_FUNC
static const InitializeReturnType
Identity() {
return DiagonalVectorType::Ones().asDiagonal(); }
272 return DiagonalVectorType::Ones(
size).asDiagonal();
278 EIGEN_DEVICE_FUNC
inline void setZero() { m_diagonal.setZero(); }
282 EIGEN_DEVICE_FUNC
inline void setIdentity() { m_diagonal.setOnes(); }
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;
310 RowsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
311 ColsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
312 MaxRowsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
313 MaxColsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
319template <
typename DiagonalVectorType_>
322#ifndef EIGEN_PARSED_BY_DOXYGEN
323 typedef DiagonalVectorType_ DiagonalVectorType;
328 EIGEN_DEVICE_FUNC
explicit inline DiagonalWrapper(DiagonalVectorType& a_diagonal) : m_diagonal(a_diagonal) {}
331 EIGEN_DEVICE_FUNC
const DiagonalVectorType&
diagonal()
const {
return m_diagonal; }
334 typename DiagonalVectorType::Nested m_diagonal;
346template <
typename Derived>
359template <
typename Derived>
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;
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;
378struct storage_kind_to_shape<DiagonalShape> {
379 typedef DiagonalShape Shape;
382struct Diagonal2Dense {};
385struct AssignmentKind<DenseShape, DiagonalShape> {
386 typedef Diagonal2Dense Kind;
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>& ) {
395 Index dstRows = src.rows();
396 Index dstCols = src.cols();
397 if ((dst.rows() != dstRows) || (dst.cols() != dstCols)) dst.resize(dstRows, dstCols);
400 dst.diagonal() = src.diagonal();
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>& ) {
406 dst.diagonal() += src.diagonal();
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>& ) {
412 dst.diagonal() -= src.diagonal();
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