Eigen  3.2.92
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 namespace Eigen {
15 
16 #ifndef EIGEN_PARSED_BY_DOXYGEN
17 template<typename Derived>
18 class DiagonalBase : public EigenBase<Derived>
19 {
20  public:
21  typedef typename internal::traits<Derived>::DiagonalVectorType DiagonalVectorType;
22  typedef typename DiagonalVectorType::Scalar Scalar;
23  typedef typename DiagonalVectorType::RealScalar RealScalar;
24  typedef typename internal::traits<Derived>::StorageKind StorageKind;
25  typedef typename internal::traits<Derived>::StorageIndex StorageIndex;
26 
27  enum {
28  RowsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
29  ColsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
30  MaxRowsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
31  MaxColsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
32  IsVectorAtCompileTime = 0,
34  };
35 
36  typedef Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime, 0, MaxRowsAtCompileTime, MaxColsAtCompileTime> DenseMatrixType;
37  typedef DenseMatrixType DenseType;
38  typedef DiagonalMatrix<Scalar,DiagonalVectorType::SizeAtCompileTime,DiagonalVectorType::MaxSizeAtCompileTime> PlainObject;
39 
40  EIGEN_DEVICE_FUNC
41  inline const Derived& derived() const { return *static_cast<const Derived*>(this); }
42  EIGEN_DEVICE_FUNC
43  inline Derived& derived() { return *static_cast<Derived*>(this); }
44 
45  EIGEN_DEVICE_FUNC
46  DenseMatrixType toDenseMatrix() const { return derived(); }
47 
48  EIGEN_DEVICE_FUNC
49  inline const DiagonalVectorType& diagonal() const { return derived().diagonal(); }
50  EIGEN_DEVICE_FUNC
51  inline DiagonalVectorType& diagonal() { return derived().diagonal(); }
52 
53  EIGEN_DEVICE_FUNC
54  inline Index rows() const { return diagonal().size(); }
55  EIGEN_DEVICE_FUNC
56  inline Index cols() const { return diagonal().size(); }
57 
58  template<typename MatrixDerived>
59  EIGEN_DEVICE_FUNC
60  const Product<Derived,MatrixDerived,LazyProduct>
61  operator*(const MatrixBase<MatrixDerived> &matrix) const
62  {
63  return Product<Derived, MatrixDerived, LazyProduct>(derived(),matrix.derived());
64  }
65 
66  typedef DiagonalWrapper<const CwiseUnaryOp<internal::scalar_inverse_op<Scalar>, const DiagonalVectorType> > InverseReturnType;
67  EIGEN_DEVICE_FUNC
68  inline const InverseReturnType
69  inverse() const
70  {
71  return InverseReturnType(diagonal().cwiseInverse());
72  }
73 
74  typedef DiagonalWrapper<const CwiseUnaryOp<internal::scalar_multiple_op<Scalar>, const DiagonalVectorType> > ScalarMultipleReturnType;
75  EIGEN_DEVICE_FUNC
76  inline const ScalarMultipleReturnType
77  operator*(const Scalar& scalar) const
78  {
79  return ScalarMultipleReturnType(diagonal() * scalar);
80  }
81  EIGEN_DEVICE_FUNC
82  friend inline const ScalarMultipleReturnType
83  operator*(const Scalar& scalar, const DiagonalBase& other)
84  {
85  return ScalarMultipleReturnType(other.diagonal() * scalar);
86  }
87 };
88 
89 #endif
90 
104 namespace internal {
105 template<typename _Scalar, int SizeAtCompileTime, int MaxSizeAtCompileTime>
106 struct traits<DiagonalMatrix<_Scalar,SizeAtCompileTime,MaxSizeAtCompileTime> >
107  : traits<Matrix<_Scalar,SizeAtCompileTime,SizeAtCompileTime,0,MaxSizeAtCompileTime,MaxSizeAtCompileTime> >
108 {
109  typedef Matrix<_Scalar,SizeAtCompileTime,1,0,MaxSizeAtCompileTime,1> DiagonalVectorType;
110  typedef DiagonalShape StorageKind;
111  enum {
113  };
114 };
115 }
116 template<typename _Scalar, int SizeAtCompileTime, int MaxSizeAtCompileTime>
118  : public DiagonalBase<DiagonalMatrix<_Scalar,SizeAtCompileTime,MaxSizeAtCompileTime> >
119 {
120  public:
121  #ifndef EIGEN_PARSED_BY_DOXYGEN
122  typedef typename internal::traits<DiagonalMatrix>::DiagonalVectorType DiagonalVectorType;
123  typedef const DiagonalMatrix& Nested;
124  typedef _Scalar Scalar;
125  typedef typename internal::traits<DiagonalMatrix>::StorageKind StorageKind;
126  typedef typename internal::traits<DiagonalMatrix>::StorageIndex StorageIndex;
127  #endif
128 
129  protected:
130 
131  DiagonalVectorType m_diagonal;
132 
133  public:
134 
136  EIGEN_DEVICE_FUNC
137  inline const DiagonalVectorType& diagonal() const { return m_diagonal; }
139  EIGEN_DEVICE_FUNC
140  inline DiagonalVectorType& diagonal() { return m_diagonal; }
141 
143  EIGEN_DEVICE_FUNC
144  inline DiagonalMatrix() {}
145 
147  EIGEN_DEVICE_FUNC
148  explicit inline DiagonalMatrix(Index dim) : m_diagonal(dim) {}
149 
151  EIGEN_DEVICE_FUNC
152  inline DiagonalMatrix(const Scalar& x, const Scalar& y) : m_diagonal(x,y) {}
153 
155  EIGEN_DEVICE_FUNC
156  inline DiagonalMatrix(const Scalar& x, const Scalar& y, const Scalar& z) : m_diagonal(x,y,z) {}
157 
159  template<typename OtherDerived>
160  EIGEN_DEVICE_FUNC
161  inline DiagonalMatrix(const DiagonalBase<OtherDerived>& other) : m_diagonal(other.diagonal()) {}
162 
163  #ifndef EIGEN_PARSED_BY_DOXYGEN
164 
165  inline DiagonalMatrix(const DiagonalMatrix& other) : m_diagonal(other.diagonal()) {}
166  #endif
167 
169  template<typename OtherDerived>
170  EIGEN_DEVICE_FUNC
171  explicit inline DiagonalMatrix(const MatrixBase<OtherDerived>& other) : m_diagonal(other)
172  {}
173 
175  template<typename OtherDerived>
176  EIGEN_DEVICE_FUNC
177  DiagonalMatrix& operator=(const DiagonalBase<OtherDerived>& other)
178  {
179  m_diagonal = other.diagonal();
180  return *this;
181  }
182 
183  #ifndef EIGEN_PARSED_BY_DOXYGEN
184 
187  EIGEN_DEVICE_FUNC
189  {
190  m_diagonal = other.diagonal();
191  return *this;
192  }
193  #endif
194 
196  EIGEN_DEVICE_FUNC
197  inline void resize(Index size) { m_diagonal.resize(size); }
199  EIGEN_DEVICE_FUNC
200  inline void setZero() { m_diagonal.setZero(); }
202  EIGEN_DEVICE_FUNC
203  inline void setZero(Index size) { m_diagonal.setZero(size); }
205  EIGEN_DEVICE_FUNC
206  inline void setIdentity() { m_diagonal.setOnes(); }
208  EIGEN_DEVICE_FUNC
209  inline void setIdentity(Index size) { m_diagonal.setOnes(size); }
210 };
211 
226 namespace internal {
227 template<typename _DiagonalVectorType>
228 struct traits<DiagonalWrapper<_DiagonalVectorType> >
229 {
230  typedef _DiagonalVectorType DiagonalVectorType;
231  typedef typename DiagonalVectorType::Scalar Scalar;
232  typedef typename DiagonalVectorType::StorageIndex StorageIndex;
233  typedef DiagonalShape StorageKind;
234  typedef typename traits<DiagonalVectorType>::XprKind XprKind;
235  enum {
236  RowsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
237  ColsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
238  MaxRowsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
239  MaxColsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
240  Flags = (traits<DiagonalVectorType>::Flags & LvalueBit) | NoPreferredStorageOrderBit
241  };
242 };
243 }
244 
245 template<typename _DiagonalVectorType>
247  : public DiagonalBase<DiagonalWrapper<_DiagonalVectorType> >, internal::no_assignment_operator
248 {
249  public:
250  #ifndef EIGEN_PARSED_BY_DOXYGEN
251  typedef _DiagonalVectorType DiagonalVectorType;
252  typedef DiagonalWrapper Nested;
253  #endif
254 
256  EIGEN_DEVICE_FUNC
257  explicit inline DiagonalWrapper(DiagonalVectorType& a_diagonal) : m_diagonal(a_diagonal) {}
258 
260  EIGEN_DEVICE_FUNC
261  const DiagonalVectorType& diagonal() const { return m_diagonal; }
262 
263  protected:
264  typename DiagonalVectorType::Nested m_diagonal;
265 };
266 
276 template<typename Derived>
277 inline const DiagonalWrapper<const Derived>
279 {
280  return DiagonalWrapper<const Derived>(derived());
281 }
282 
291 template<typename Derived>
292 bool MatrixBase<Derived>::isDiagonal(const RealScalar& prec) const
293 {
294  using std::abs;
295  if(cols() != rows()) return false;
296  RealScalar maxAbsOnDiagonal = static_cast<RealScalar>(-1);
297  for(Index j = 0; j < cols(); ++j)
298  {
299  RealScalar absOnDiagonal = abs(coeff(j,j));
300  if(absOnDiagonal > maxAbsOnDiagonal) maxAbsOnDiagonal = absOnDiagonal;
301  }
302  for(Index j = 0; j < cols(); ++j)
303  for(Index i = 0; i < j; ++i)
304  {
305  if(!internal::isMuchSmallerThan(coeff(i, j), maxAbsOnDiagonal, prec)) return false;
306  if(!internal::isMuchSmallerThan(coeff(j, i), maxAbsOnDiagonal, prec)) return false;
307  }
308  return true;
309 }
310 
311 namespace internal {
312 
313 template<> struct storage_kind_to_shape<DiagonalShape> { typedef DiagonalShape Shape; };
314 
315 struct Diagonal2Dense {};
316 
317 template<> struct AssignmentKind<DenseShape,DiagonalShape> { typedef Diagonal2Dense Kind; };
318 
319 // Diagonal matrix to Dense assignment
320 template< typename DstXprType, typename SrcXprType, typename Functor, typename Scalar>
321 struct Assignment<DstXprType, SrcXprType, Functor, Diagonal2Dense, Scalar>
322 {
323  static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op<typename DstXprType::Scalar> &/*func*/)
324  {
325  dst.setZero();
326  dst.diagonal() = src.diagonal();
327  }
328 
329  static void run(DstXprType &dst, const SrcXprType &src, const internal::add_assign_op<typename DstXprType::Scalar> &/*func*/)
330  { dst.diagonal() += src.diagonal(); }
331 
332  static void run(DstXprType &dst, const SrcXprType &src, const internal::sub_assign_op<typename DstXprType::Scalar> &/*func*/)
333  { dst.diagonal() -= src.diagonal(); }
334 };
335 
336 } // namespace internal
337 
338 } // end namespace Eigen
339 
340 #endif // EIGEN_DIAGONALMATRIX_H
DiagonalMatrix & operator=(const DiagonalBase< OtherDerived > &other)
Definition: DiagonalMatrix.h:177
DiagonalMatrix(const DiagonalBase< OtherDerived > &other)
Definition: DiagonalMatrix.h:161
void setIdentity()
Definition: DiagonalMatrix.h:206
const unsigned int LvalueBit
Definition: Constants.h:138
DiagonalMatrix(const Scalar &x, const Scalar &y)
Definition: DiagonalMatrix.h:152
const DiagonalVectorType & diagonal() const
Definition: DiagonalMatrix.h:261
Represents a diagonal matrix with its storage.
Definition: DiagonalMatrix.h:117
Definition: LDLT.h:16
void resize(Index size)
Definition: DiagonalMatrix.h:197
DiagonalWrapper(DiagonalVectorType &a_diagonal)
Definition: DiagonalMatrix.h:257
void setZero()
Definition: DiagonalMatrix.h:200
DiagonalMatrix()
Definition: DiagonalMatrix.h:144
DiagonalMatrix(const Scalar &x, const Scalar &y, const Scalar &z)
Definition: DiagonalMatrix.h:156
const DiagonalVectorType & diagonal() const
Definition: DiagonalMatrix.h:137
DiagonalMatrix(Index dim)
Definition: DiagonalMatrix.h:148
Definition: Eigen_Colamd.h:54
DiagonalVectorType & diagonal()
Definition: DiagonalMatrix.h:140
Expression of a diagonal matrix.
Definition: DiagonalMatrix.h:246
void setIdentity(Index size)
Definition: DiagonalMatrix.h:209
DiagonalMatrix(const MatrixBase< OtherDerived > &other)
Definition: DiagonalMatrix.h:171
bool isDiagonal(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: DiagonalMatrix.h:292
const DiagonalWrapper< const Derived > asDiagonal() const
Definition: DiagonalMatrix.h:278
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
void setZero(Index size)
Definition: DiagonalMatrix.h:203
const unsigned int NoPreferredStorageOrderBit
Definition: Constants.h:172