11#ifndef EIGEN_DIAGONALMATRIX_H
12#define EIGEN_DIAGONALMATRIX_H
16#ifndef EIGEN_PARSED_BY_DOXYGEN
17template<
typename Derived>
18class DiagonalBase :
public EigenBase<Derived>
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;
28 RowsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
29 ColsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
30 MaxRowsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
31 MaxColsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
32 IsVectorAtCompileTime = 0,
36 typedef Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime, 0, MaxRowsAtCompileTime, MaxColsAtCompileTime> DenseMatrixType;
37 typedef DenseMatrixType DenseType;
38 typedef DiagonalMatrix<Scalar,DiagonalVectorType::SizeAtCompileTime,DiagonalVectorType::MaxSizeAtCompileTime> PlainObject;
41 inline const Derived& derived()
const {
return *
static_cast<const Derived*
>(
this); }
43 inline Derived& derived() {
return *
static_cast<Derived*
>(
this); }
46 DenseMatrixType toDenseMatrix()
const {
return derived(); }
49 inline const DiagonalVectorType& diagonal()
const {
return derived().diagonal(); }
51 inline DiagonalVectorType& diagonal() {
return derived().diagonal(); }
54 inline Index rows()
const {
return diagonal().size(); }
56 inline Index cols()
const {
return diagonal().size(); }
58 template<
typename MatrixDerived>
60 const Product<Derived,MatrixDerived,LazyProduct>
61 operator*(
const MatrixBase<MatrixDerived> &matrix)
const
63 return Product<Derived, MatrixDerived, LazyProduct>(derived(),matrix.derived());
66 typedef DiagonalWrapper<const CwiseUnaryOp<internal::scalar_inverse_op<Scalar>,
const DiagonalVectorType> > InverseReturnType;
68 inline const InverseReturnType
71 return InverseReturnType(diagonal().cwiseInverse());
75 inline const DiagonalWrapper<
const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DiagonalVectorType,Scalar,product) >
76 operator*(
const Scalar& scalar)
const
78 return DiagonalWrapper<const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DiagonalVectorType,Scalar,product) >(diagonal() * scalar);
81 friend inline const DiagonalWrapper<
const EIGEN_SCALAR_BINARYOP_EXPR_RETURN_TYPE(Scalar,DiagonalVectorType,product) >
82 operator*(
const Scalar& scalar,
const DiagonalBase& other)
84 return DiagonalWrapper<const EIGEN_SCALAR_BINARYOP_EXPR_RETURN_TYPE(Scalar,DiagonalVectorType,product) >(scalar * other.diagonal());
87 template<
typename OtherDerived>
89 #ifdef EIGEN_PARSED_BY_DOXYGEN
90 inline unspecified_expression_type
92 inline const DiagonalWrapper<
const EIGEN_CWISE_BINARY_RETURN_TYPE(DiagonalVectorType,
typename OtherDerived::DiagonalVectorType,sum) >
94 operator+(
const DiagonalBase<OtherDerived>& other)
const
96 return (diagonal() + other.diagonal()).asDiagonal();
99 template<
typename OtherDerived>
101 #ifdef EIGEN_PARSED_BY_DOXYGEN
102 inline unspecified_expression_type
104 inline const DiagonalWrapper<
const EIGEN_CWISE_BINARY_RETURN_TYPE(DiagonalVectorType,
typename OtherDerived::DiagonalVectorType,difference) >
106 operator-(
const DiagonalBase<OtherDerived>& other)
const
108 return (diagonal() - other.diagonal()).asDiagonal();
128template<
typename _Scalar,
int SizeAtCompileTime,
int MaxSizeAtCompileTime>
129struct traits<DiagonalMatrix<_Scalar,SizeAtCompileTime,MaxSizeAtCompileTime> >
130 : traits<Matrix<_Scalar,SizeAtCompileTime,SizeAtCompileTime,0,MaxSizeAtCompileTime,MaxSizeAtCompileTime> >
132 typedef Matrix<_Scalar,SizeAtCompileTime,1,0,MaxSizeAtCompileTime,1> DiagonalVectorType;
133 typedef DiagonalShape StorageKind;
139template<
typename _Scalar,
int SizeAtCompileTime,
int MaxSizeAtCompileTime>
141 :
public DiagonalBase<DiagonalMatrix<_Scalar,SizeAtCompileTime,MaxSizeAtCompileTime> >
144 #ifndef EIGEN_PARSED_BY_DOXYGEN
145 typedef typename internal::traits<DiagonalMatrix>::DiagonalVectorType DiagonalVectorType;
147 typedef _Scalar Scalar;
148 typedef typename internal::traits<DiagonalMatrix>::StorageKind StorageKind;
149 typedef typename internal::traits<DiagonalMatrix>::StorageIndex StorageIndex;
154 DiagonalVectorType m_diagonal;
160 inline const DiagonalVectorType&
diagonal()
const {
return m_diagonal; }
163 inline DiagonalVectorType&
diagonal() {
return m_diagonal; }
179 inline DiagonalMatrix(
const Scalar& x,
const Scalar& y,
const Scalar& z) : m_diagonal(x,y,z) {}
192 template <
typename... ArgTypes>
193 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
194 DiagonalMatrix(
const Scalar& a0,
const Scalar& a1,
const Scalar& a2,
const ArgTypes&... args)
195 : m_diagonal(a0, a1, a2, args...) {}
201 explicit EIGEN_STRONG_INLINE
DiagonalMatrix(
const std::initializer_list<std::initializer_list<Scalar>>& list)
202 : m_diagonal(list) {}
206 template<
typename OtherDerived>
210 #ifndef EIGEN_PARSED_BY_DOXYGEN
216 template<
typename OtherDerived>
222 template<
typename OtherDerived>
230 #ifndef EIGEN_PARSED_BY_DOXYGEN
247 inline void setZero() { m_diagonal.setZero(); }
274template<
typename _DiagonalVectorType>
277 typedef _DiagonalVectorType DiagonalVectorType;
278 typedef typename DiagonalVectorType::Scalar
Scalar;
279 typedef typename DiagonalVectorType::StorageIndex
StorageIndex;
280 typedef DiagonalShape StorageKind;
281 typedef typename traits<DiagonalVectorType>::XprKind XprKind;
283 RowsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
284 ColsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
285 MaxRowsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
286 MaxColsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
292template<
typename _DiagonalVectorType>
294 :
public DiagonalBase<DiagonalWrapper<_DiagonalVectorType> >, internal::no_assignment_operator
297 #ifndef EIGEN_PARSED_BY_DOXYGEN
298 typedef _DiagonalVectorType DiagonalVectorType;
304 explicit inline DiagonalWrapper(DiagonalVectorType& a_diagonal) : m_diagonal(a_diagonal) {}
308 const DiagonalVectorType&
diagonal()
const {
return m_diagonal; }
311 typename DiagonalVectorType::Nested m_diagonal;
323template<
typename Derived>
324EIGEN_DEVICE_FUNC
inline const DiagonalWrapper<const Derived>
338template<
typename Derived>
341 if(cols() != rows())
return false;
342 RealScalar maxAbsOnDiagonal =
static_cast<RealScalar
>(-1);
343 for(
Index j = 0; j < cols(); ++j)
345 RealScalar absOnDiagonal = numext::abs(coeff(j,j));
346 if(absOnDiagonal > maxAbsOnDiagonal) maxAbsOnDiagonal = absOnDiagonal;
348 for(
Index j = 0; j < cols(); ++j)
349 for(
Index i = 0; i < j; ++i)
351 if(!internal::isMuchSmallerThan(coeff(i, j), maxAbsOnDiagonal, prec))
return false;
352 if(!internal::isMuchSmallerThan(coeff(j, i), maxAbsOnDiagonal, prec))
return false;
359template<>
struct storage_kind_to_shape<DiagonalShape> {
typedef DiagonalShape Shape; };
361struct Diagonal2Dense {};
363template<>
struct AssignmentKind<DenseShape,DiagonalShape> {
typedef Diagonal2Dense Kind; };
366template<
typename DstXprType,
typename SrcXprType,
typename Functor>
367struct Assignment<DstXprType, SrcXprType, Functor, Diagonal2Dense>
369 static void run(DstXprType &dst,
const SrcXprType &src,
const internal::assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> &)
371 Index dstRows = src.rows();
372 Index dstCols = src.cols();
373 if((dst.rows()!=dstRows) || (dst.cols()!=dstCols))
374 dst.resize(dstRows, dstCols);
377 dst.diagonal() = src.diagonal();
380 static void run(DstXprType &dst,
const SrcXprType &src,
const internal::add_assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> &)
381 { dst.diagonal() += src.diagonal(); }
383 static void run(DstXprType &dst,
const SrcXprType &src,
const internal::sub_assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> &)
384 { dst.diagonal() -= src.diagonal(); }
internal::traits< Derived >::StorageIndex StorageIndex
The type used to store indices.
Definition: DenseBase.h:63
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:66
Represents a diagonal matrix with its storage.
Definition: DiagonalMatrix.h:142
DiagonalMatrix()
Definition: DiagonalMatrix.h:167
DiagonalMatrix(const MatrixBase< OtherDerived > &other)
Definition: DiagonalMatrix.h:218
DiagonalMatrix(const Scalar &x, const Scalar &y, const Scalar &z)
Definition: DiagonalMatrix.h:179
void setIdentity(Index size)
Definition: DiagonalMatrix.h:256
const DiagonalVectorType & diagonal() const
Definition: DiagonalMatrix.h:160
void setZero(Index size)
Definition: DiagonalMatrix.h:250
DiagonalMatrix(const DiagonalBase< OtherDerived > &other)
Definition: DiagonalMatrix.h:208
void setIdentity()
Definition: DiagonalMatrix.h:253
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:194
DiagonalMatrix(const Scalar &x, const Scalar &y)
Definition: DiagonalMatrix.h:175
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:201
DiagonalMatrix(Index dim)
Definition: DiagonalMatrix.h:171
DiagonalMatrix & operator=(const DiagonalBase< OtherDerived > &other)
Definition: DiagonalMatrix.h:224
void setZero()
Definition: DiagonalMatrix.h:247
DiagonalVectorType & diagonal()
Definition: DiagonalMatrix.h:163
void resize(Index size)
Definition: DiagonalMatrix.h:244
Expression of a diagonal matrix.
Definition: DiagonalMatrix.h:295
const DiagonalVectorType & diagonal() const
Definition: DiagonalMatrix.h:308
DiagonalWrapper(DiagonalVectorType &a_diagonal)
Definition: DiagonalMatrix.h:304
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:50
const unsigned int NoPreferredStorageOrderBit
Definition: Constants.h:178
const unsigned int LvalueBit
Definition: Constants.h:144
Namespace containing all symbols from the Eigen library.
Definition: Core:141
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_inverse_op< typename Derived::Scalar >, const Derived > inverse(const Eigen::ArrayBase< Derived > &x)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74