Eigen  3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)
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 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 #ifndef EIGEN_PARSED_BY_DOXYGEN
19 template<typename Derived>
20 class DiagonalBase : public EigenBase<Derived>
21 {
22  public:
23  typedef typename internal::traits<Derived>::DiagonalVectorType DiagonalVectorType;
24  typedef typename DiagonalVectorType::Scalar Scalar;
25  typedef typename DiagonalVectorType::RealScalar RealScalar;
26  typedef typename internal::traits<Derived>::StorageKind StorageKind;
27  typedef typename internal::traits<Derived>::StorageIndex StorageIndex;
28 
29  enum {
30  RowsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
31  ColsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
32  MaxRowsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
33  MaxColsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
34  IsVectorAtCompileTime = 0,
36  };
37 
38  typedef Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime, 0, MaxRowsAtCompileTime, MaxColsAtCompileTime> DenseMatrixType;
39  typedef DenseMatrixType DenseType;
40  typedef DiagonalMatrix<Scalar,DiagonalVectorType::SizeAtCompileTime,DiagonalVectorType::MaxSizeAtCompileTime> PlainObject;
41 
42  EIGEN_DEVICE_FUNC
43  inline const Derived& derived() const { return *static_cast<const Derived*>(this); }
44  EIGEN_DEVICE_FUNC
45  inline Derived& derived() { return *static_cast<Derived*>(this); }
46 
47  EIGEN_DEVICE_FUNC
48  DenseMatrixType toDenseMatrix() const { return derived(); }
49 
50  EIGEN_DEVICE_FUNC
51  inline const DiagonalVectorType& diagonal() const { return derived().diagonal(); }
52  EIGEN_DEVICE_FUNC
53  inline DiagonalVectorType& diagonal() { return derived().diagonal(); }
54 
55  EIGEN_DEVICE_FUNC
56  inline Index rows() const { return diagonal().size(); }
57  EIGEN_DEVICE_FUNC
58  inline Index cols() const { return diagonal().size(); }
59 
60  template<typename MatrixDerived>
61  EIGEN_DEVICE_FUNC
62  const Product<Derived,MatrixDerived,LazyProduct>
63  operator*(const MatrixBase<MatrixDerived> &matrix) const
64  {
65  return Product<Derived, MatrixDerived, LazyProduct>(derived(),matrix.derived());
66  }
67 
68  typedef DiagonalWrapper<const CwiseUnaryOp<internal::scalar_inverse_op<Scalar>, const DiagonalVectorType> > InverseReturnType;
69  EIGEN_DEVICE_FUNC
70  inline const InverseReturnType
71  inverse() const
72  {
73  return InverseReturnType(diagonal().cwiseInverse());
74  }
75 
76  EIGEN_DEVICE_FUNC
77  inline const DiagonalWrapper<const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DiagonalVectorType,Scalar,product) >
78  operator*(const Scalar& scalar) const
79  {
80  return DiagonalWrapper<const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DiagonalVectorType,Scalar,product) >(diagonal() * scalar);
81  }
82  EIGEN_DEVICE_FUNC
83  friend inline const DiagonalWrapper<const EIGEN_SCALAR_BINARYOP_EXPR_RETURN_TYPE(Scalar,DiagonalVectorType,product) >
84  operator*(const Scalar& scalar, const DiagonalBase& other)
85  {
86  return DiagonalWrapper<const EIGEN_SCALAR_BINARYOP_EXPR_RETURN_TYPE(Scalar,DiagonalVectorType,product) >(scalar * other.diagonal());
87  }
88 
89  template<typename OtherDerived>
90  EIGEN_DEVICE_FUNC
91  #ifdef EIGEN_PARSED_BY_DOXYGEN
92  inline unspecified_expression_type
93  #else
94  inline const DiagonalWrapper<const EIGEN_CWISE_BINARY_RETURN_TYPE(DiagonalVectorType,typename OtherDerived::DiagonalVectorType,sum) >
95  #endif
96  operator+(const DiagonalBase<OtherDerived>& other) const
97  {
98  return (diagonal() + other.diagonal()).asDiagonal();
99  }
100 
101  template<typename OtherDerived>
102  EIGEN_DEVICE_FUNC
103  #ifdef EIGEN_PARSED_BY_DOXYGEN
104  inline unspecified_expression_type
105  #else
106  inline const DiagonalWrapper<const EIGEN_CWISE_BINARY_RETURN_TYPE(DiagonalVectorType,typename OtherDerived::DiagonalVectorType,difference) >
107  #endif
108  operator-(const DiagonalBase<OtherDerived>& other) const
109  {
110  return (diagonal() - other.diagonal()).asDiagonal();
111  }
112 };
113 
114 #endif
115 
129 namespace internal {
130 template<typename Scalar_, int SizeAtCompileTime, int MaxSizeAtCompileTime>
131 struct traits<DiagonalMatrix<Scalar_,SizeAtCompileTime,MaxSizeAtCompileTime> >
132  : traits<Matrix<Scalar_,SizeAtCompileTime,SizeAtCompileTime,0,MaxSizeAtCompileTime,MaxSizeAtCompileTime> >
133 {
134  typedef Matrix<Scalar_,SizeAtCompileTime,1,0,MaxSizeAtCompileTime,1> DiagonalVectorType;
135  typedef DiagonalShape StorageKind;
136  enum {
137  Flags = LvalueBit | NoPreferredStorageOrderBit | NestByRefBit
138  };
139 };
140 }
141 template<typename Scalar_, int SizeAtCompileTime, int MaxSizeAtCompileTime>
143  : public DiagonalBase<DiagonalMatrix<Scalar_,SizeAtCompileTime,MaxSizeAtCompileTime> >
144 {
145  public:
146  #ifndef EIGEN_PARSED_BY_DOXYGEN
147  typedef typename internal::traits<DiagonalMatrix>::DiagonalVectorType DiagonalVectorType;
148  typedef const DiagonalMatrix& Nested;
149  typedef Scalar_ Scalar;
150  typedef typename internal::traits<DiagonalMatrix>::StorageKind StorageKind;
151  typedef typename internal::traits<DiagonalMatrix>::StorageIndex StorageIndex;
152  #endif
153 
154  protected:
155 
156  DiagonalVectorType m_diagonal;
157 
158  public:
159 
161  EIGEN_DEVICE_FUNC
162  inline const DiagonalVectorType& diagonal() const { return m_diagonal; }
164  EIGEN_DEVICE_FUNC
165  inline DiagonalVectorType& diagonal() { return m_diagonal; }
166 
168  EIGEN_DEVICE_FUNC
169  inline DiagonalMatrix() {}
170 
172  EIGEN_DEVICE_FUNC
173  explicit inline DiagonalMatrix(Index dim) : m_diagonal(dim) {}
174 
176  EIGEN_DEVICE_FUNC
177  inline DiagonalMatrix(const Scalar& x, const Scalar& y) : m_diagonal(x,y) {}
178 
180  EIGEN_DEVICE_FUNC
181  inline DiagonalMatrix(const Scalar& x, const Scalar& y, const Scalar& z) : m_diagonal(x,y,z) {}
182 
191  template <typename... ArgTypes>
192  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
193  DiagonalMatrix(const Scalar& a0, const Scalar& a1, const Scalar& a2, const ArgTypes&... args)
194  : m_diagonal(a0, a1, a2, args...) {}
195 
199  EIGEN_DEVICE_FUNC
200  explicit EIGEN_STRONG_INLINE DiagonalMatrix(const std::initializer_list<std::initializer_list<Scalar>>& list)
201  : m_diagonal(list) {}
202 
204  EIGEN_DEVICE_FUNC
205  explicit inline DiagonalMatrix(DiagonalVectorType&& diag) : m_diagonal(std::move(diag)) {}
206 
208  template<typename OtherDerived>
209  EIGEN_DEVICE_FUNC
210  inline DiagonalMatrix(const DiagonalBase<OtherDerived>& other) : m_diagonal(other.diagonal()) {}
211 
212  #ifndef EIGEN_PARSED_BY_DOXYGEN
214  inline DiagonalMatrix(const DiagonalMatrix& other) : m_diagonal(other.diagonal()) {}
215  #endif
216 
218  template<typename OtherDerived>
219  EIGEN_DEVICE_FUNC
220  explicit inline DiagonalMatrix(const MatrixBase<OtherDerived>& other) : m_diagonal(other)
221  {}
222 
224  template<typename OtherDerived>
225  EIGEN_DEVICE_FUNC
226  DiagonalMatrix& operator=(const DiagonalBase<OtherDerived>& other)
227  {
228  m_diagonal = other.diagonal();
229  return *this;
230  }
231 
232  #ifndef EIGEN_PARSED_BY_DOXYGEN
236  EIGEN_DEVICE_FUNC
238  {
239  m_diagonal = other.diagonal();
240  return *this;
241  }
242  #endif
243 
245  EIGEN_DEVICE_FUNC
246  inline void resize(Index size) { m_diagonal.resize(size); }
248  EIGEN_DEVICE_FUNC
249  inline void setZero() { m_diagonal.setZero(); }
251  EIGEN_DEVICE_FUNC
252  inline void setZero(Index size) { m_diagonal.setZero(size); }
254  EIGEN_DEVICE_FUNC
255  inline void setIdentity() { m_diagonal.setOnes(); }
257  EIGEN_DEVICE_FUNC
258  inline void setIdentity(Index size) { m_diagonal.setOnes(size); }
259 };
260 
275 namespace internal {
276 template<typename DiagonalVectorType_>
277 struct traits<DiagonalWrapper<DiagonalVectorType_> >
278 {
279  typedef DiagonalVectorType_ DiagonalVectorType;
280  typedef typename DiagonalVectorType::Scalar Scalar;
281  typedef typename DiagonalVectorType::StorageIndex StorageIndex;
282  typedef DiagonalShape StorageKind;
283  typedef typename traits<DiagonalVectorType>::XprKind XprKind;
284  enum {
285  RowsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
286  ColsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
287  MaxRowsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
288  MaxColsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
289  Flags = (traits<DiagonalVectorType>::Flags & LvalueBit) | NoPreferredStorageOrderBit
290  };
291 };
292 }
293 
294 template<typename DiagonalVectorType_>
296  : public DiagonalBase<DiagonalWrapper<DiagonalVectorType_> >, internal::no_assignment_operator
297 {
298  public:
299  #ifndef EIGEN_PARSED_BY_DOXYGEN
300  typedef DiagonalVectorType_ DiagonalVectorType;
301  typedef DiagonalWrapper Nested;
302  #endif
303 
305  EIGEN_DEVICE_FUNC
306  explicit inline DiagonalWrapper(DiagonalVectorType& a_diagonal) : m_diagonal(a_diagonal) {}
307 
309  EIGEN_DEVICE_FUNC
310  const DiagonalVectorType& diagonal() const { return m_diagonal; }
311 
312  protected:
313  typename DiagonalVectorType::Nested m_diagonal;
314 };
315 
325 template<typename Derived>
326 EIGEN_DEVICE_FUNC inline const DiagonalWrapper<const Derived>
328 {
329  return DiagonalWrapper<const Derived>(derived());
330 }
331 
340 template<typename Derived>
341 bool MatrixBase<Derived>::isDiagonal(const RealScalar& prec) const
342 {
343  if(cols() != rows()) return false;
344  RealScalar maxAbsOnDiagonal = static_cast<RealScalar>(-1);
345  for(Index j = 0; j < cols(); ++j)
346  {
347  RealScalar absOnDiagonal = numext::abs(coeff(j,j));
348  if(absOnDiagonal > maxAbsOnDiagonal) maxAbsOnDiagonal = absOnDiagonal;
349  }
350  for(Index j = 0; j < cols(); ++j)
351  for(Index i = 0; i < j; ++i)
352  {
353  if(!internal::isMuchSmallerThan(coeff(i, j), maxAbsOnDiagonal, prec)) return false;
354  if(!internal::isMuchSmallerThan(coeff(j, i), maxAbsOnDiagonal, prec)) return false;
355  }
356  return true;
357 }
358 
359 namespace internal {
360 
361 template<> struct storage_kind_to_shape<DiagonalShape> { typedef DiagonalShape Shape; };
362 
363 struct Diagonal2Dense {};
364 
365 template<> struct AssignmentKind<DenseShape,DiagonalShape> { typedef Diagonal2Dense Kind; };
366 
367 // Diagonal matrix to Dense assignment
368 template< typename DstXprType, typename SrcXprType, typename Functor>
369 struct Assignment<DstXprType, SrcXprType, Functor, Diagonal2Dense>
370 {
371  static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> &/*func*/)
372  {
373  Index dstRows = src.rows();
374  Index dstCols = src.cols();
375  if((dst.rows()!=dstRows) || (dst.cols()!=dstCols))
376  dst.resize(dstRows, dstCols);
377 
378  dst.setZero();
379  dst.diagonal() = src.diagonal();
380  }
381 
382  static void run(DstXprType &dst, const SrcXprType &src, const internal::add_assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> &/*func*/)
383  { dst.diagonal() += src.diagonal(); }
384 
385  static void run(DstXprType &dst, const SrcXprType &src, const internal::sub_assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> &/*func*/)
386  { dst.diagonal() -= src.diagonal(); }
387 };
388 
389 } // namespace internal
390 
391 } // end namespace Eigen
392 
393 #endif // EIGEN_DIAGONALMATRIX_H
internal::traits< Homogeneous< MatrixType, Direction_ > >::StorageIndex StorageIndex
The type used to store indices.
Definition: DenseBase.h:58
internal::traits< Homogeneous< MatrixType, Direction_ > >::Scalar Scalar
Definition: DenseBase.h:61
Represents a diagonal matrix with its storage.
Definition: DiagonalMatrix.h:144
DiagonalVectorType & diagonal()
Definition: DiagonalMatrix.h:165
DiagonalMatrix(const Scalar &x, const Scalar &y)
Definition: DiagonalMatrix.h:177
DiagonalMatrix()
Definition: DiagonalMatrix.h:169
DiagonalMatrix(const Scalar &x, const Scalar &y, const Scalar &z)
Definition: DiagonalMatrix.h:181
DiagonalMatrix(DiagonalVectorType &&diag)
Constructs a DiagonalMatrix from an r-value diagonal vector type.
Definition: DiagonalMatrix.h:205
void setIdentity(Index size)
Definition: DiagonalMatrix.h:258
const DiagonalVectorType & diagonal() const
Definition: DiagonalMatrix.h:162
void setZero()
Definition: DiagonalMatrix.h:249
void setIdentity()
Definition: DiagonalMatrix.h:255
DiagonalMatrix(const DiagonalBase< OtherDerived > &other)
Definition: DiagonalMatrix.h:210
DiagonalMatrix(Index dim)
Definition: DiagonalMatrix.h:173
void setZero(Index size)
Definition: DiagonalMatrix.h:252
void resize(Index size)
Definition: DiagonalMatrix.h:246
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:200
DiagonalMatrix(const MatrixBase< OtherDerived > &other)
Definition: DiagonalMatrix.h:220
DiagonalMatrix & operator=(const DiagonalBase< OtherDerived > &other)
Definition: DiagonalMatrix.h:226
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:193
Expression of a diagonal matrix.
Definition: DiagonalMatrix.h:297
const DiagonalVectorType & diagonal() const
Definition: DiagonalMatrix.h:310
DiagonalWrapper(DiagonalVectorType &a_diagonal)
Definition: DiagonalMatrix.h:306
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
const DiagonalWrapper< const Derived > asDiagonal() const
Definition: DiagonalMatrix.h:327
bool isDiagonal(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: DiagonalMatrix.h:341
const unsigned int NoPreferredStorageOrderBit
Definition: Constants.h:180
const unsigned int LvalueBit
Definition: Constants.h:146
Namespace containing all symbols from the Eigen library.
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
const Product< MatrixDerived, PermutationDerived, AliasFreeProduct > operator*(const MatrixBase< MatrixDerived > &matrix, const PermutationBase< PermutationDerived > &permutation)
Definition: PermutationMatrix.h:517
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_inverse_op< typename Derived::Scalar >, const Derived > inverse(const Eigen::ArrayBase< Derived > &x)
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:41