DiagonalMatrix.h
Go to the documentation of this file.
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 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
31 template<typename Derived>
32 class DiagonalBase : public EigenBase<Derived>
33 {
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,
48  };
49 
53 
56  inline const Derived& derived() const { return *static_cast<const Derived*>(this); }
59  inline Derived& derived() { return *static_cast<Derived*>(this); }
60 
66  DenseMatrixType toDenseMatrix() const { return derived(); }
67 
70  inline const DiagonalVectorType& diagonal() const { return derived().diagonal(); }
73  inline DiagonalVectorType& diagonal() { return derived().diagonal(); }
74 
77  inline Scalar coeff(Index row, Index col) const {
78  eigen_assert(row >= 0 && col >= 0 && row < rows() && col <= cols());
79  return row == col ? diagonal().coeff(row) : Scalar(0);
80  }
81 
84  inline Index rows() const { return diagonal().size(); }
87  inline Index cols() const { return diagonal().size(); }
88 
90  template<typename MatrixDerived>
93  operator*(const MatrixBase<MatrixDerived> &matrix) const
94  {
96  }
97 
98  template <typename OtherDerived>
100  DiagonalVectorType, typename OtherDerived::DiagonalVectorType, product)>;
101 
103  template <typename OtherDerived>
105  const DiagonalBase<OtherDerived>& other) const {
106  return diagonal().cwiseProduct(other.diagonal()).asDiagonal();
107  }
108 
111 
114  inline const DiagonalInverseReturnType inverse() const { return diagonal().cwiseInverse().asDiagonal(); }
115 
118 
121  inline const DiagonalScaleReturnType operator*(const Scalar& scalar) const {
122  return (diagonal() * scalar).asDiagonal();
123  }
124 
127 
130  friend inline const ScaleDiagonalReturnType operator*(const Scalar& scalar, const DiagonalBase& other) {
131  return (scalar * other.diagonal()).asDiagonal();
132  }
133 
134  template <typename OtherDerived>
136  DiagonalVectorType, typename OtherDerived::DiagonalVectorType, sum)>;
137 
139  template <typename OtherDerived>
141  const DiagonalBase<OtherDerived>& other) const {
142  return (diagonal() + other.diagonal()).asDiagonal();
143  }
144 
145  template <typename OtherDerived>
147  DiagonalVectorType, typename OtherDerived::DiagonalVectorType, difference)>;
148 
150  template <typename OtherDerived>
152  const DiagonalBase<OtherDerived>& other) const {
153  return (diagonal() - other.diagonal()).asDiagonal();
154  }
155 };
156 
170 namespace internal {
171 template<typename Scalar_, int SizeAtCompileTime, int MaxSizeAtCompileTime>
172 struct traits<DiagonalMatrix<Scalar_,SizeAtCompileTime,MaxSizeAtCompileTime> >
173  : traits<Matrix<Scalar_,SizeAtCompileTime,SizeAtCompileTime,0,MaxSizeAtCompileTime,MaxSizeAtCompileTime> >
174 {
175  typedef Matrix<Scalar_,SizeAtCompileTime,1,0,MaxSizeAtCompileTime,1> DiagonalVectorType;
176  typedef DiagonalShape StorageKind;
177  enum {
179  };
180 };
181 }
182 template<typename Scalar_, int SizeAtCompileTime, int MaxSizeAtCompileTime>
184  : public DiagonalBase<DiagonalMatrix<Scalar_,SizeAtCompileTime,MaxSizeAtCompileTime> >
185 {
186  public:
187  #ifndef EIGEN_PARSED_BY_DOXYGEN
188  typedef typename internal::traits<DiagonalMatrix>::DiagonalVectorType DiagonalVectorType;
189  typedef const DiagonalMatrix& Nested;
190  typedef Scalar_ Scalar;
191  typedef typename internal::traits<DiagonalMatrix>::StorageKind StorageKind;
192  typedef typename internal::traits<DiagonalMatrix>::StorageIndex StorageIndex;
193  #endif
194 
195  protected:
196 
198 
199  public:
200 
203  inline const DiagonalVectorType& diagonal() const { return m_diagonal; }
206  inline DiagonalVectorType& diagonal() { return m_diagonal; }
207 
210  inline DiagonalMatrix() {}
211 
214  explicit inline DiagonalMatrix(Index dim) : m_diagonal(dim) {}
215 
218  inline DiagonalMatrix(const Scalar& x, const Scalar& y) : m_diagonal(x,y) {}
219 
222  inline DiagonalMatrix(const Scalar& x, const Scalar& y, const Scalar& z) : m_diagonal(x,y,z) {}
223 
232  template <typename... ArgTypes>
233  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
234  DiagonalMatrix(const Scalar& a0, const Scalar& a1, const Scalar& a2, const ArgTypes&... args)
235  : m_diagonal(a0, a1, a2, args...) {}
236 
241  explicit EIGEN_STRONG_INLINE DiagonalMatrix(const std::initializer_list<std::initializer_list<Scalar>>& list)
242  : m_diagonal(list) {}
243 
246  explicit inline DiagonalMatrix(DiagonalVectorType&& diag) : m_diagonal(std::move(diag)) {}
247 
249  template<typename OtherDerived>
252 
253  #ifndef EIGEN_PARSED_BY_DOXYGEN
255  inline DiagonalMatrix(const DiagonalMatrix& other) : m_diagonal(other.diagonal()) {}
256  #endif
257 
259  template<typename OtherDerived>
261  explicit inline DiagonalMatrix(const MatrixBase<OtherDerived>& other) : m_diagonal(other)
262  {}
263 
265  template<typename OtherDerived>
268  {
269  m_diagonal = other.diagonal();
270  return *this;
271  }
272 
273  #ifndef EIGEN_PARSED_BY_DOXYGEN
279  {
280  m_diagonal = other.diagonal();
281  return *this;
282  }
283  #endif
284 
287 
290  static const InitializeReturnType Zero() { return DiagonalVectorType::Zero().asDiagonal(); }
293  static const InitializeReturnType Zero(Index size) { return DiagonalVectorType::Zero(size).asDiagonal(); }
296  static const InitializeReturnType Identity() { return DiagonalVectorType::Ones().asDiagonal(); }
299  static const InitializeReturnType Identity(Index size) { return DiagonalVectorType::Ones(size).asDiagonal(); }
300 
303  inline void resize(Index size) { m_diagonal.resize(size); }
306  inline void setZero() { m_diagonal.setZero(); }
309  inline void setZero(Index size) { m_diagonal.setZero(size); }
312  inline void setIdentity() { m_diagonal.setOnes(); }
315  inline void setIdentity(Index size) { m_diagonal.setOnes(size); }
316 };
317 
332 namespace internal {
333 template<typename DiagonalVectorType_>
334 struct traits<DiagonalWrapper<DiagonalVectorType_> >
335 {
336  typedef DiagonalVectorType_ DiagonalVectorType;
337  typedef typename DiagonalVectorType::Scalar Scalar;
338  typedef typename DiagonalVectorType::StorageIndex StorageIndex;
339  typedef DiagonalShape StorageKind;
340  typedef typename traits<DiagonalVectorType>::XprKind XprKind;
341  enum {
342  RowsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
343  ColsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
344  MaxRowsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
345  MaxColsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
346  Flags = (traits<DiagonalVectorType>::Flags & LvalueBit) | NoPreferredStorageOrderBit
347  };
348 };
349 }
350 
351 template<typename DiagonalVectorType_>
353  : public DiagonalBase<DiagonalWrapper<DiagonalVectorType_> >, internal::no_assignment_operator
354 {
355  public:
356  #ifndef EIGEN_PARSED_BY_DOXYGEN
357  typedef DiagonalVectorType_ DiagonalVectorType;
358  typedef DiagonalWrapper Nested;
359  #endif
360 
363  explicit inline DiagonalWrapper(DiagonalVectorType& a_diagonal) : m_diagonal(a_diagonal) {}
364 
367  const DiagonalVectorType& diagonal() const { return m_diagonal; }
368 
369  protected:
370  typename DiagonalVectorType::Nested m_diagonal;
371 };
372 
382 template<typename Derived>
385 {
386  return DiagonalWrapper<const Derived>(derived());
387 }
388 
397 template<typename Derived>
399 {
400  if(cols() != rows()) return false;
401  RealScalar maxAbsOnDiagonal = static_cast<RealScalar>(-1);
402  for(Index j = 0; j < cols(); ++j)
403  {
404  RealScalar absOnDiagonal = numext::abs(coeff(j,j));
405  if(absOnDiagonal > maxAbsOnDiagonal) maxAbsOnDiagonal = absOnDiagonal;
406  }
407  for(Index j = 0; j < cols(); ++j)
408  for(Index i = 0; i < j; ++i)
409  {
410  if(!internal::isMuchSmallerThan(coeff(i, j), maxAbsOnDiagonal, prec)) return false;
411  if(!internal::isMuchSmallerThan(coeff(j, i), maxAbsOnDiagonal, prec)) return false;
412  }
413  return true;
414 }
415 
416 namespace internal {
417 
418 template<> struct storage_kind_to_shape<DiagonalShape> { typedef DiagonalShape Shape; };
419 
420 struct Diagonal2Dense {};
421 
422 template<> struct AssignmentKind<DenseShape,DiagonalShape> { typedef Diagonal2Dense Kind; };
423 
424 // Diagonal matrix to Dense assignment
425 template< typename DstXprType, typename SrcXprType, typename Functor>
426 struct Assignment<DstXprType, SrcXprType, Functor, Diagonal2Dense>
427 {
428  static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> &/*func*/)
429  {
430  Index dstRows = src.rows();
431  Index dstCols = src.cols();
432  if((dst.rows()!=dstRows) || (dst.cols()!=dstCols))
433  dst.resize(dstRows, dstCols);
434 
435  dst.setZero();
436  dst.diagonal() = src.diagonal();
437  }
438 
439  static void run(DstXprType &dst, const SrcXprType &src, const internal::add_assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> &/*func*/)
440  { dst.diagonal() += src.diagonal(); }
441 
442  static void run(DstXprType &dst, const SrcXprType &src, const internal::sub_assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> &/*func*/)
443  { dst.diagonal() -= src.diagonal(); }
444 };
445 
446 } // namespace internal
447 
448 } // end namespace Eigen
449 
450 #endif // EIGEN_DIAGONALMATRIX_H
RowXpr row(Index i)
This is the const version of row(). *‍/.
ColXpr col(Index i)
This is the const version of col().
#define EIGEN_CONSTEXPR
Definition: Macros.h:747
#define EIGEN_CWISE_BINARY_RETURN_TYPE(LHS, RHS, OPNAME)
Definition: Macros.h:1182
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:883
#define EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(EXPR, SCALAR, OPNAME)
Definition: Macros.h:1203
#define eigen_assert(x)
Definition: Macros.h:902
#define EIGEN_SCALAR_BINARYOP_EXPR_RETURN_TYPE(SCALAR, EXPR, OPNAME)
Definition: Macros.h:1207
NumTraits< Scalar >::Real RealScalar
Definition: DenseBase.h:68
Base class for diagonal matrices and expressions.
EIGEN_CONSTEXPR Index cols() const
internal::traits< Derived >::StorageIndex StorageIndex
internal::traits< Derived >::StorageKind StorageKind
DiagonalMatrix< Scalar, DiagonalVectorType::SizeAtCompileTime, DiagonalVectorType::MaxSizeAtCompileTime > PlainObject
const DiagonalProductReturnType< OtherDerived > operator*(const DiagonalBase< OtherDerived > &other) const
DiagonalVectorType::Scalar Scalar
internal::traits< Derived >::DiagonalVectorType DiagonalVectorType
const DiagonalDifferenceReturnType< OtherDerived > operator-(const DiagonalBase< OtherDerived > &other) const
friend const ScaleDiagonalReturnType operator*(const Scalar &scalar, const DiagonalBase &other)
const DiagonalVectorType & diagonal() const
DenseMatrixType toDenseMatrix() const
Matrix< Scalar, RowsAtCompileTime, ColsAtCompileTime, 0, MaxRowsAtCompileTime, MaxColsAtCompileTime > DenseMatrixType
DiagonalVectorType & diagonal()
const Product< Derived, MatrixDerived, LazyProduct > operator*(const MatrixBase< MatrixDerived > &matrix) const
Scalar coeff(Index row, Index col) const
const DiagonalSumReturnType< OtherDerived > operator+(const DiagonalBase< OtherDerived > &other) const
const Derived & derived() const
const DiagonalInverseReturnType inverse() const
DiagonalVectorType::RealScalar RealScalar
EIGEN_CONSTEXPR Index rows() const
DenseMatrixType DenseType
const DiagonalScaleReturnType operator*(const Scalar &scalar) const
Represents a diagonal matrix with its storage.
static const InitializeReturnType Identity(Index size)
DiagonalVectorType & diagonal()
DiagonalWrapper< const CwiseNullaryOp< internal::scalar_constant_op< Scalar >, DiagonalVectorType > > InitializeReturnType
DiagonalMatrix(const Scalar &x, const Scalar &y)
static const InitializeReturnType Identity()
DiagonalMatrix(const Scalar &x, const Scalar &y, const Scalar &z)
DiagonalMatrix(DiagonalVectorType &&diag)
Constructs a DiagonalMatrix from an r-value diagonal vector type.
void setIdentity(Index size)
const DiagonalVectorType & diagonal() const
DiagonalMatrix(const DiagonalBase< OtherDerived > &other)
void setZero(Index size)
void resize(Index size)
static const InitializeReturnType Zero()
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...
static const InitializeReturnType Zero(Index size)
DiagonalMatrix(const MatrixBase< OtherDerived > &other)
DiagonalVectorType m_diagonal
DiagonalMatrix & operator=(const DiagonalBase< OtherDerived > &other)
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.
Expression of a diagonal matrix.
DiagonalVectorType::Nested m_diagonal
const DiagonalVectorType & diagonal() const
DiagonalWrapper(DiagonalVectorType &a_diagonal)
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
const DiagonalWrapper< const Derived > asDiagonal() const
bool isDiagonal(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:182
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:77
const unsigned int NoPreferredStorageOrderBit
Definition: Constants.h:180
const unsigned int LvalueBit
Definition: Constants.h:146
const Scalar & y
bool isMuchSmallerThan(const Scalar &x, const OtherScalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
EIGEN_ALWAYS_INLINE std::enable_if_t< NumTraits< T >::IsSigned||NumTraits< T >::IsComplex, typename NumTraits< T >::Real > abs(const T &x)
: InteropHeaders
Definition: Core:139
const unsigned int NestByRefBit
Definition: Constants.h:171
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
Definition: BFloat16.h:222
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:41
EIGEN_CONSTEXPR Index size() const EIGEN_NOEXCEPT
Definition: EigenBase.h:69
std::ptrdiff_t j