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 namespace Eigen {
15 
16 #ifndef EIGEN_PARSED_BY_DOXYGEN
17 template<typename Derived>
18 class DiagonalBase : public EigenBase<Derived>
19 {
20  public:
22  typedef typename DiagonalVectorType::Scalar Scalar;
23  typedef typename DiagonalVectorType::RealScalar RealScalar;
26 
27  enum {
28  RowsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
29  ColsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
30  MaxRowsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
31  MaxColsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
34  };
35 
37  typedef DenseMatrixType DenseType;
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
61  operator*(const MatrixBase<MatrixDerived> &matrix) const
62  {
63  return Product<Derived, MatrixDerived, LazyProduct>(derived(),matrix.derived());
64  }
65 
67  EIGEN_DEVICE_FUNC
68  inline const InverseReturnType
69  inverse() const
70  {
72  }
73 
74  EIGEN_DEVICE_FUNC
76  operator*(const Scalar& scalar) const
77  {
79  }
80  EIGEN_DEVICE_FUNC
82  operator*(const Scalar& scalar, const DiagonalBase& other)
83  {
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 {
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
122  typedef const DiagonalMatrix& Nested;
123  typedef _Scalar Scalar;
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
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;
234  enum {
235  RowsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
236  ColsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
237  MaxRowsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
238  MaxColsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
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;
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>
278 {
280 }
281 
290 template<typename Derived>
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 
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_DEVICE_FUNC const DiagonalVectorType & diagonal() const
DiagonalMatrix< Scalar, DiagonalVectorType::SizeAtCompileTime, DiagonalVectorType::MaxSizeAtCompileTime > PlainObject
EIGEN_DEVICE_FUNC const Derived & derived() const
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:71
EIGEN_DEVICE_FUNC const DiagonalWrapper< const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DiagonalVectorType, Scalar, product) > operator*(const Scalar &scalar) const
DenseMatrixType DenseType
DiagonalVectorType::Scalar Scalar
EIGEN_DEVICE_FUNC const Product< Derived, MatrixDerived, LazyProduct > operator*(const MatrixBase< MatrixDerived > &matrix) const
EIGEN_DEVICE_FUNC friend const DiagonalWrapper< const EIGEN_SCALAR_BINARYOP_EXPR_RETURN_TYPE(Scalar, DiagonalVectorType, product) > operator*(const Scalar &scalar, const DiagonalBase &other)
EIGEN_DEVICE_FUNC Derived & derived()
EIGEN_DEVICE_FUNC const CwiseInverseReturnType cwiseInverse() const
static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op< typename DstXprType::Scalar, typename SrcXprType::Scalar > &)
const unsigned int LvalueBit
Definition: Constants.h:139
Represents a diagonal matrix with its storage.
Definition: LDLT.h:16
EIGEN_DEVICE_FUNC DiagonalMatrix()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const AbsReturnType abs() const
EIGEN_DEVICE_FUNC DiagonalMatrix(const Scalar &x, const Scalar &y)
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:38
EIGEN_DEVICE_FUNC Index rows() const
internal::traits< Derived >::DiagonalVectorType DiagonalVectorType
static void run(DstXprType &dst, const SrcXprType &src, const internal::sub_assign_op< typename DstXprType::Scalar, typename SrcXprType::Scalar > &)
EIGEN_DEVICE_FUNC DiagonalMatrix(Index dim)
internal::traits< DiagonalMatrix >::StorageIndex StorageIndex
EIGEN_DEVICE_FUNC void setIdentity(Index size)
DiagonalWrapper< const CwiseUnaryOp< internal::scalar_inverse_op< Scalar >, const DiagonalVectorType > > InverseReturnType
EIGEN_DEVICE_FUNC const DiagonalVectorType & diagonal() const
DiagonalVectorType m_diagonal
EIGEN_DEVICE_FUNC DiagonalMatrix & operator=(const DiagonalMatrix &other)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
internal::traits< DiagonalMatrix >::StorageKind StorageKind
const DiagonalMatrix & Nested
EIGEN_DEVICE_FUNC DiagonalMatrix & operator=(const DiagonalBase< OtherDerived > &other)
DiagonalVectorType::RealScalar RealScalar
EIGEN_DEVICE_FUNC void resize(Index size)
EIGEN_DEVICE_FUNC DenseMatrixType toDenseMatrix() const
DiagonalWrapper Nested
const mpreal dim(const mpreal &a, const mpreal &b, mp_rnd_t r=mpreal::get_default_rnd())
Definition: mpreal.h:2201
Matrix< Scalar, RowsAtCompileTime, ColsAtCompileTime, 0, MaxRowsAtCompileTime, MaxColsAtCompileTime > DenseMatrixType
internal::traits< Derived >::StorageKind StorageKind
EIGEN_DEVICE_FUNC DiagonalVectorType & diagonal()
NumTraits< Scalar >::Real RealScalar
Definition: DenseBase.h:73
EIGEN_DEVICE_FUNC DiagonalVectorType & diagonal()
static void run(DstXprType &dst, const SrcXprType &src, const internal::add_assign_op< typename DstXprType::Scalar, typename SrcXprType::Scalar > &)
_DiagonalVectorType DiagonalVectorType
DiagonalVectorType::Nested m_diagonal
Expression of a diagonal matrix.
EIGEN_DEVICE_FUNC const InverseReturnType inverse() const
internal::traits< DiagonalMatrix >::DiagonalVectorType DiagonalVectorType
EIGEN_DEVICE_FUNC Index cols() const
EIGEN_DEVICE_FUNC DiagonalMatrix(const Scalar &x, const Scalar &y, const Scalar &z)
EIGEN_DEVICE_FUNC void setIdentity()
EIGEN_DEVICE_FUNC DiagonalMatrix(const MatrixBase< OtherDerived > &other)
bool isDiagonal(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
EIGEN_DEVICE_FUNC const DiagonalWrapper< const Derived > asDiagonal() const
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:178
EIGEN_DEVICE_FUNC const DiagonalVectorType & diagonal() const
EIGEN_DEVICE_FUNC DiagonalMatrix(const DiagonalBase< OtherDerived > &other)
internal::traits< Derived >::StorageIndex StorageIndex
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
EIGEN_DEVICE_FUNC Index size() const
Definition: EigenBase.h:66
EIGEN_DEVICE_FUNC void setZero(Index size)
EIGEN_DEVICE_FUNC DiagonalWrapper(DiagonalVectorType &a_diagonal)
DiagonalMatrix(const DiagonalMatrix &other)
const T & y
Matrix< _Scalar, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1 > DiagonalVectorType
const unsigned int NoPreferredStorageOrderBit
Definition: Constants.h:173
EIGEN_DEVICE_FUNC void setZero()


hebiros
Author(s): Xavier Artache , Matthew Tesch
autogenerated on Thu Sep 3 2020 04:08:10