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,
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>
101 using DiagonalInverseReturnType =
106 return diagonal().cwiseInverse().asDiagonal();
109 using DiagonalScaleReturnType =
110 DiagonalWrapper<
const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DiagonalVectorType, Scalar, product)>;
114 return (
diagonal() * scalar).asDiagonal();
117 using ScaleDiagonalReturnType =
118 DiagonalWrapper<
const EIGEN_SCALAR_BINARYOP_EXPR_RETURN_TYPE(Scalar, DiagonalVectorType, product)>;
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>
137 template <
typename OtherDerived>
138 using DiagonalDifferenceReturnType =
DiagonalWrapper<
const EIGEN_CWISE_BINARY_RETURN_TYPE(
139 DiagonalVectorType,
typename OtherDerived::DiagonalVectorType, difference)>;
142 template <
typename OtherDerived>
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 };
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;
263 return DiagonalVectorType::Zero(
size).asDiagonal();
269 return DiagonalVectorType::Ones(
size).asDiagonal();
275 EIGEN_DEVICE_FUNC
inline void setZero() { m_diagonal.setZero(); }
279 EIGEN_DEVICE_FUNC
inline void setIdentity() { m_diagonal.setOnes(); }
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;
307 RowsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
308 ColsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
309 MaxRowsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
310 MaxColsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
311 Flags = (traits<DiagonalVectorType>::Flags & LvalueBit) | NoPreferredStorageOrderBit
316template <
typename DiagonalVectorType_>
319#ifndef EIGEN_PARSED_BY_DOXYGEN
320 typedef DiagonalVectorType_ DiagonalVectorType;
325 EIGEN_DEVICE_FUNC
explicit inline DiagonalWrapper(DiagonalVectorType& a_diagonal) : m_diagonal(a_diagonal) {}
328 EIGEN_DEVICE_FUNC
const DiagonalVectorType&
diagonal()
const {
return m_diagonal; }
331 typename DiagonalVectorType::Nested m_diagonal;
343template <
typename Derived>
356template <
typename Derived>
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;
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;
375struct storage_kind_to_shape<DiagonalShape> {
376 typedef DiagonalShape Shape;
379struct Diagonal2Dense {};
382struct AssignmentKind<DenseShape, DiagonalShape> {
383 typedef Diagonal2Dense Kind;
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>& ) {
391 Index dstRows = src.rows();
392 Index dstCols = src.cols();
393 if ((dst.rows() != dstRows) || (dst.cols() != dstCols)) dst.resize(dstRows, dstCols);
396 dst.diagonal() = src.diagonal();
399 static void run(DstXprType& dst,
const SrcXprType& src,
400 const internal::add_assign_op<typename DstXprType::Scalar, typename SrcXprType::Scalar>& ) {
401 dst.diagonal() += src.diagonal();
404 static void run(DstXprType& dst,
const SrcXprType& src,
405 const internal::sub_assign_op<typename DstXprType::Scalar, typename SrcXprType::Scalar>& ) {
406 dst.diagonal() -= src.diagonal();
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