Medial Code Documentation
Loading...
Searching...
No Matches
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
14namespace Eigen {
15
16#ifndef EIGEN_PARSED_BY_DOXYGEN
17template<typename Derived>
18class 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
39
41 inline const Derived& derived() const { return *static_cast<const Derived*>(this); }
43 inline Derived& derived() { return *static_cast<Derived*>(this); }
44
46 DenseMatrixType toDenseMatrix() const { return derived(); }
47
49 inline const DiagonalVectorType& diagonal() const { return derived().diagonal(); }
51 inline DiagonalVectorType& diagonal() { return derived().diagonal(); }
52
54 inline Index rows() const { return diagonal().size(); }
56 inline Index cols() const { return diagonal().size(); }
57
58 template<typename MatrixDerived>
61 operator*(const MatrixBase<MatrixDerived> &matrix) const
62 {
63 return Product<Derived, MatrixDerived, LazyProduct>(derived(),matrix.derived());
64 }
65
68 inline const InverseReturnType
69 inverse() const
70 {
71 return InverseReturnType(diagonal().cwiseInverse());
72 }
73
76 inline const ScalarMultipleReturnType
77 operator*(const Scalar& scalar) const
78 {
79 return ScalarMultipleReturnType(diagonal() * scalar);
80 }
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
104namespace internal {
105template<typename _Scalar, int SizeAtCompileTime, int MaxSizeAtCompileTime>
106struct traits<DiagonalMatrix<_Scalar,SizeAtCompileTime,MaxSizeAtCompileTime> >
107 : traits<Matrix<_Scalar,SizeAtCompileTime,SizeAtCompileTime,0,MaxSizeAtCompileTime,MaxSizeAtCompileTime> >
108{
111 enum {
113 };
114};
115}
116template<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
137 inline const DiagonalVectorType& diagonal() const { return m_diagonal; }
140 inline DiagonalVectorType& diagonal() { return m_diagonal; }
141
144 inline DiagonalMatrix() {}
145
148 explicit inline DiagonalMatrix(Index dim) : m_diagonal(dim) {}
149
152 inline DiagonalMatrix(const Scalar& x, const Scalar& y) : m_diagonal(x,y) {}
153
156 inline DiagonalMatrix(const Scalar& x, const Scalar& y, const Scalar& z) : m_diagonal(x,y,z) {}
157
159 template<typename OtherDerived>
161 inline DiagonalMatrix(const DiagonalBase<OtherDerived>& other) : m_diagonal(other.diagonal()) {}
162
163 #ifndef EIGEN_PARSED_BY_DOXYGEN
165 inline DiagonalMatrix(const DiagonalMatrix& other) : m_diagonal(other.diagonal()) {}
166 #endif
167
169 template<typename OtherDerived>
171 explicit inline DiagonalMatrix(const MatrixBase<OtherDerived>& other) : m_diagonal(other)
172 {}
173
175 template<typename OtherDerived>
178 {
179 m_diagonal = other.diagonal();
180 return *this;
181 }
182
183 #ifndef EIGEN_PARSED_BY_DOXYGEN
189 {
190 m_diagonal = other.diagonal();
191 return *this;
192 }
193 #endif
194
197 inline void resize(Index size) { m_diagonal.resize(size); }
200 inline void setZero() { m_diagonal.setZero(); }
203 inline void setZero(Index size) { m_diagonal.setZero(size); }
206 inline void setIdentity() { m_diagonal.setOnes(); }
209 inline void setIdentity(Index size) { m_diagonal.setOnes(size); }
210};
211
226namespace internal {
227template<typename _DiagonalVectorType>
229{
231 typedef typename DiagonalVectorType::Scalar Scalar;
232 typedef typename DiagonalVectorType::StorageIndex StorageIndex;
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,
241 };
242};
243}
244
245template<typename _DiagonalVectorType>
247 : public DiagonalBase<DiagonalWrapper<_DiagonalVectorType> >, internal::no_assignment_operator
248{
249 public:
250 #ifndef EIGEN_PARSED_BY_DOXYGEN
252 typedef DiagonalWrapper Nested;
253 #endif
254
257 explicit inline DiagonalWrapper(DiagonalVectorType& a_diagonal) : m_diagonal(a_diagonal) {}
258
261 const DiagonalVectorType& diagonal() const { return m_diagonal; }
262
263 protected:
264 typename DiagonalVectorType::Nested m_diagonal;
265};
266
276template<typename Derived>
277inline const DiagonalWrapper<const Derived>
279{
281}
282
291template<typename Derived>
292bool 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));
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
311namespace internal {
312
314
316
318
319// Diagonal matrix to Dense assignment
320template< typename DstXprType, typename SrcXprType, typename Functor, typename Scalar>
321struct 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
Definition DiagonalMatrix.h:19
Represents a diagonal matrix with its storage.
Definition DiagonalMatrix.h:119
EIGEN_DEVICE_FUNC const DiagonalVectorType & diagonal() const
const version of diagonal().
Definition DiagonalMatrix.h:137
EIGEN_DEVICE_FUNC DiagonalMatrix(const Scalar &x, const Scalar &y)
2D constructor.
Definition DiagonalMatrix.h:152
EIGEN_DEVICE_FUNC DiagonalMatrix(const MatrixBase< OtherDerived > &other)
generic constructor from expression of the diagonal coefficients
Definition DiagonalMatrix.h:171
EIGEN_DEVICE_FUNC void setIdentity()
Sets this matrix to be the identity matrix of the current size.
Definition DiagonalMatrix.h:206
EIGEN_DEVICE_FUNC void setZero()
Sets all coefficients to zero.
Definition DiagonalMatrix.h:200
EIGEN_DEVICE_FUNC void resize(Index size)
Resizes to given size.
Definition DiagonalMatrix.h:197
EIGEN_DEVICE_FUNC DiagonalVectorType & diagonal()
Definition DiagonalMatrix.h:140
EIGEN_DEVICE_FUNC DiagonalMatrix & operator=(const DiagonalMatrix &other)
This is a special case of the templated operator=.
Definition DiagonalMatrix.h:188
EIGEN_DEVICE_FUNC DiagonalMatrix()
Default constructor without initialization.
Definition DiagonalMatrix.h:144
EIGEN_DEVICE_FUNC DiagonalMatrix(Index dim)
Constructs a diagonal matrix with given dimension
Definition DiagonalMatrix.h:148
EIGEN_DEVICE_FUNC void setZero(Index size)
Resizes and sets all coefficients to zero.
Definition DiagonalMatrix.h:203
EIGEN_DEVICE_FUNC DiagonalMatrix & operator=(const DiagonalBase< OtherDerived > &other)
Copy operator.
Definition DiagonalMatrix.h:177
DiagonalMatrix(const DiagonalMatrix &other)
copy constructor.
Definition DiagonalMatrix.h:165
EIGEN_DEVICE_FUNC DiagonalMatrix(const DiagonalBase< OtherDerived > &other)
Copy constructor.
Definition DiagonalMatrix.h:161
EIGEN_DEVICE_FUNC void setIdentity(Index size)
Sets this matrix to be the identity matrix of the given size.
Definition DiagonalMatrix.h:209
EIGEN_DEVICE_FUNC DiagonalMatrix(const Scalar &x, const Scalar &y, const Scalar &z)
3D constructor.
Definition DiagonalMatrix.h:156
Expression of a diagonal matrix.
Definition DiagonalMatrix.h:248
EIGEN_DEVICE_FUNC const DiagonalVectorType & diagonal() const
Definition DiagonalMatrix.h:261
EIGEN_DEVICE_FUNC DiagonalWrapper(DiagonalVectorType &a_diagonal)
Constructor from expression of diagonal coefficients to wrap.
Definition DiagonalMatrix.h:257
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:50
Pseudo expression representing a solving operation.
Definition Solve.h:63
const unsigned int NoPreferredStorageOrderBit
for an expression, this means that the storage order can be either row-major or column-major.
Definition Constants.h:172
const unsigned int LvalueBit
Means the expression has a coeffRef() method, i.e.
Definition Constants.h:138
Definition Constants.h:511
Definition Constants.h:514
Common base class for all classes T such that MatrixBase has an operator=(T) and a constructor Matrix...
Definition EigenBase.h:29
Eigen::Index Index
The interface type of indices.
Definition EigenBase.h:37
EIGEN_DEVICE_FUNC Index size() const
Definition EigenBase.h:65
Definition AssignEvaluator.h:677
Definition AssignEvaluator.h:684
Definition DiagonalMatrix.h:315
Definition AssignmentFunctors.h:42
Definition AssignmentFunctors.h:21
Definition CoreEvaluators.h:29
Definition AssignmentFunctors.h:63
Definition ForwardDeclarations.h:17