Eigen  3.3.7
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  EIGEN_DEVICE_FUNC
75  inline const DiagonalWrapper<const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DiagonalVectorType,Scalar,product) >
76  operator*(const Scalar& scalar) const
77  {
78  return DiagonalWrapper<const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DiagonalVectorType,Scalar,product) >(diagonal() * scalar);
79  }
80  EIGEN_DEVICE_FUNC
81  friend inline const DiagonalWrapper<const EIGEN_SCALAR_BINARYOP_EXPR_RETURN_TYPE(Scalar,DiagonalVectorType,product) >
82  operator*(const Scalar& scalar, const DiagonalBase& other)
83  {
84  return DiagonalWrapper<const EIGEN_SCALAR_BINARYOP_EXPR_RETURN_TYPE(Scalar,DiagonalVectorType,product) >(scalar * other.diagonal());
85  }
86 };
87 
88 #endif
89 
103 namespace internal {
104 template<typename _Scalar, int SizeAtCompileTime, int MaxSizeAtCompileTime>
105 struct traits<DiagonalMatrix<_Scalar,SizeAtCompileTime,MaxSizeAtCompileTime> >
106  : traits<Matrix<_Scalar,SizeAtCompileTime,SizeAtCompileTime,0,MaxSizeAtCompileTime,MaxSizeAtCompileTime> >
107 {
108  typedef Matrix<_Scalar,SizeAtCompileTime,1,0,MaxSizeAtCompileTime,1> DiagonalVectorType;
109  typedef DiagonalShape StorageKind;
110  enum {
112  };
113 };
114 }
115 template<typename _Scalar, int SizeAtCompileTime, int MaxSizeAtCompileTime>
117  : public DiagonalBase<DiagonalMatrix<_Scalar,SizeAtCompileTime,MaxSizeAtCompileTime> >
118 {
119  public:
120  #ifndef EIGEN_PARSED_BY_DOXYGEN
121  typedef typename internal::traits<DiagonalMatrix>::DiagonalVectorType DiagonalVectorType;
122  typedef const DiagonalMatrix& Nested;
123  typedef _Scalar Scalar;
124  typedef typename internal::traits<DiagonalMatrix>::StorageKind StorageKind;
125  typedef typename internal::traits<DiagonalMatrix>::StorageIndex StorageIndex;
126  #endif
127 
128  protected:
129 
130  DiagonalVectorType m_diagonal;
131 
132  public:
133 
135  EIGEN_DEVICE_FUNC
136  inline const DiagonalVectorType& diagonal() const { return m_diagonal; }
138  EIGEN_DEVICE_FUNC
139  inline DiagonalVectorType& diagonal() { return m_diagonal; }
140 
142  EIGEN_DEVICE_FUNC
143  inline DiagonalMatrix() {}
144 
146  EIGEN_DEVICE_FUNC
147  explicit inline DiagonalMatrix(Index dim) : m_diagonal(dim) {}
148 
150  EIGEN_DEVICE_FUNC
151  inline DiagonalMatrix(const Scalar& x, const Scalar& y) : m_diagonal(x,y) {}
152 
154  EIGEN_DEVICE_FUNC
155  inline DiagonalMatrix(const Scalar& x, const Scalar& y, const Scalar& z) : m_diagonal(x,y,z) {}
156 
158  template<typename OtherDerived>
159  EIGEN_DEVICE_FUNC
160  inline DiagonalMatrix(const DiagonalBase<OtherDerived>& other) : m_diagonal(other.diagonal()) {}
161 
162  #ifndef EIGEN_PARSED_BY_DOXYGEN
163 
164  inline DiagonalMatrix(const DiagonalMatrix& other) : m_diagonal(other.diagonal()) {}
165  #endif
166 
168  template<typename OtherDerived>
169  EIGEN_DEVICE_FUNC
170  explicit inline DiagonalMatrix(const MatrixBase<OtherDerived>& other) : m_diagonal(other)
171  {}
172 
174  template<typename OtherDerived>
175  EIGEN_DEVICE_FUNC
176  DiagonalMatrix& operator=(const DiagonalBase<OtherDerived>& other)
177  {
178  m_diagonal = other.diagonal();
179  return *this;
180  }
181 
182  #ifndef EIGEN_PARSED_BY_DOXYGEN
183 
186  EIGEN_DEVICE_FUNC
188  {
189  m_diagonal = other.diagonal();
190  return *this;
191  }
192  #endif
193 
195  EIGEN_DEVICE_FUNC
196  inline void resize(Index size) { m_diagonal.resize(size); }
198  EIGEN_DEVICE_FUNC
199  inline void setZero() { m_diagonal.setZero(); }
201  EIGEN_DEVICE_FUNC
202  inline void setZero(Index size) { m_diagonal.setZero(size); }
204  EIGEN_DEVICE_FUNC
205  inline void setIdentity() { m_diagonal.setOnes(); }
207  EIGEN_DEVICE_FUNC
208  inline void setIdentity(Index size) { m_diagonal.setOnes(size); }
209 };
210 
225 namespace internal {
226 template<typename _DiagonalVectorType>
227 struct traits<DiagonalWrapper<_DiagonalVectorType> >
228 {
229  typedef _DiagonalVectorType DiagonalVectorType;
230  typedef typename DiagonalVectorType::Scalar Scalar;
231  typedef typename DiagonalVectorType::StorageIndex StorageIndex;
232  typedef DiagonalShape StorageKind;
233  typedef typename traits<DiagonalVectorType>::XprKind XprKind;
234  enum {
235  RowsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
236  ColsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
237  MaxRowsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
238  MaxColsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
239  Flags = (traits<DiagonalVectorType>::Flags & LvalueBit) | NoPreferredStorageOrderBit
240  };
241 };
242 }
243 
244 template<typename _DiagonalVectorType>
246  : public DiagonalBase<DiagonalWrapper<_DiagonalVectorType> >, internal::no_assignment_operator
247 {
248  public:
249  #ifndef EIGEN_PARSED_BY_DOXYGEN
250  typedef _DiagonalVectorType DiagonalVectorType;
251  typedef DiagonalWrapper Nested;
252  #endif
253 
255  EIGEN_DEVICE_FUNC
256  explicit inline DiagonalWrapper(DiagonalVectorType& a_diagonal) : m_diagonal(a_diagonal) {}
257 
259  EIGEN_DEVICE_FUNC
260  const DiagonalVectorType& diagonal() const { return m_diagonal; }
261 
262  protected:
263  typename DiagonalVectorType::Nested m_diagonal;
264 };
265 
275 template<typename Derived>
276 inline const DiagonalWrapper<const Derived>
278 {
279  return DiagonalWrapper<const Derived>(derived());
280 }
281 
290 template<typename Derived>
291 bool MatrixBase<Derived>::isDiagonal(const RealScalar& prec) const
292 {
293  if(cols() != rows()) return false;
294  RealScalar maxAbsOnDiagonal = static_cast<RealScalar>(-1);
295  for(Index j = 0; j < cols(); ++j)
296  {
297  RealScalar absOnDiagonal = numext::abs(coeff(j,j));
298  if(absOnDiagonal > maxAbsOnDiagonal) maxAbsOnDiagonal = absOnDiagonal;
299  }
300  for(Index j = 0; j < cols(); ++j)
301  for(Index i = 0; i < j; ++i)
302  {
303  if(!internal::isMuchSmallerThan(coeff(i, j), maxAbsOnDiagonal, prec)) return false;
304  if(!internal::isMuchSmallerThan(coeff(j, i), maxAbsOnDiagonal, prec)) return false;
305  }
306  return true;
307 }
308 
309 namespace internal {
310 
311 template<> struct storage_kind_to_shape<DiagonalShape> { typedef DiagonalShape Shape; };
312 
313 struct Diagonal2Dense {};
314 
315 template<> struct AssignmentKind<DenseShape,DiagonalShape> { typedef Diagonal2Dense Kind; };
316 
317 // Diagonal matrix to Dense assignment
318 template< typename DstXprType, typename SrcXprType, typename Functor>
319 struct Assignment<DstXprType, SrcXprType, Functor, Diagonal2Dense>
320 {
321  static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> &/*func*/)
322  {
323  Index dstRows = src.rows();
324  Index dstCols = src.cols();
325  if((dst.rows()!=dstRows) || (dst.cols()!=dstCols))
326  dst.resize(dstRows, dstCols);
327 
328  dst.setZero();
329  dst.diagonal() = src.diagonal();
330  }
331 
332  static void run(DstXprType &dst, const SrcXprType &src, const internal::add_assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> &/*func*/)
333  { dst.diagonal() += src.diagonal(); }
334 
335  static void run(DstXprType &dst, const SrcXprType &src, const internal::sub_assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> &/*func*/)
336  { dst.diagonal() -= src.diagonal(); }
337 };
338 
339 } // namespace internal
340 
341 } // end namespace Eigen
342 
343 #endif // EIGEN_DIAGONALMATRIX_H
Eigen::DiagonalMatrix::operator=
DiagonalMatrix & operator=(const DiagonalBase< OtherDerived > &other)
Definition: DiagonalMatrix.h:176
Eigen
Namespace containing all symbols from the Eigen library.
Definition: Core:306
Eigen::DiagonalMatrix
Represents a diagonal matrix with its storage.
Definition: DiagonalMatrix.h:116
Eigen::DiagonalMatrix::setZero
void setZero(Index size)
Definition: DiagonalMatrix.h:202
Eigen::DiagonalMatrix::setIdentity
void setIdentity()
Definition: DiagonalMatrix.h:205
Eigen::EigenBase::Index
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:38
Eigen::DiagonalMatrix::DiagonalMatrix
DiagonalMatrix(const DiagonalBase< OtherDerived > &other)
Definition: DiagonalMatrix.h:160
Eigen::DiagonalMatrix::resize
void resize(Index size)
Definition: DiagonalMatrix.h:196
Eigen::DiagonalMatrix::DiagonalMatrix
DiagonalMatrix(const Scalar &x, const Scalar &y)
Definition: DiagonalMatrix.h:151
Eigen::DiagonalMatrix::diagonal
const DiagonalVectorType & diagonal() const
Definition: DiagonalMatrix.h:136
Eigen::inverse
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_inverse_op< typename Derived::Scalar >, const Derived > inverse(const Eigen::ArrayBase< Derived > &x)
Eigen::MatrixBase::isDiagonal
bool isDiagonal(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: DiagonalMatrix.h:291
Eigen::LvalueBit
const unsigned int LvalueBit
Definition: Constants.h:139
Eigen::DiagonalMatrix::setZero
void setZero()
Definition: DiagonalMatrix.h:199
Eigen::MatrixBase::asDiagonal
const DiagonalWrapper< const Derived > asDiagonal() const
Definition: DiagonalMatrix.h:277
Eigen::DiagonalMatrix::DiagonalMatrix
DiagonalMatrix()
Definition: DiagonalMatrix.h:143
Eigen::DiagonalWrapper::DiagonalWrapper
DiagonalWrapper(DiagonalVectorType &a_diagonal)
Definition: DiagonalMatrix.h:256
Eigen::DiagonalWrapper::diagonal
const DiagonalVectorType & diagonal() const
Definition: DiagonalMatrix.h:260
Eigen::DiagonalWrapper
Expression of a diagonal matrix.
Definition: DiagonalMatrix.h:245
Eigen::DiagonalMatrix::DiagonalMatrix
DiagonalMatrix(const Scalar &x, const Scalar &y, const Scalar &z)
Definition: DiagonalMatrix.h:155
Eigen::DiagonalMatrix::diagonal
DiagonalVectorType & diagonal()
Definition: DiagonalMatrix.h:139
Eigen::DiagonalMatrix::DiagonalMatrix
DiagonalMatrix(Index dim)
Definition: DiagonalMatrix.h:147
Eigen::MatrixBase
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
Eigen::NoPreferredStorageOrderBit
const unsigned int NoPreferredStorageOrderBit
Definition: Constants.h:173
Eigen::operator*
const Product< MatrixDerived, PermutationDerived, AliasFreeProduct > operator*(const MatrixBase< MatrixDerived > &matrix, const PermutationBase< PermutationDerived > &permutation)
Definition: PermutationMatrix.h:543
Eigen::DiagonalMatrix::DiagonalMatrix
DiagonalMatrix(const MatrixBase< OtherDerived > &other)
Definition: DiagonalMatrix.h:170
Eigen::DiagonalMatrix::setIdentity
void setIdentity(Index size)
Definition: DiagonalMatrix.h:208
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33