11 #ifndef EIGEN_DIAGONALMATRIX_H
12 #define EIGEN_DIAGONALMATRIX_H
14 #include "./InternalHeaderCheck.h"
18 #ifndef EIGEN_PARSED_BY_DOXYGEN
19 template<
typename Derived>
20 class DiagonalBase :
public EigenBase<Derived>
23 typedef typename internal::traits<Derived>::DiagonalVectorType DiagonalVectorType;
24 typedef typename DiagonalVectorType::Scalar Scalar;
25 typedef typename DiagonalVectorType::RealScalar RealScalar;
26 typedef typename internal::traits<Derived>::StorageKind StorageKind;
27 typedef typename internal::traits<Derived>::StorageIndex StorageIndex;
30 RowsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
31 ColsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
32 MaxRowsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
33 MaxColsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
34 IsVectorAtCompileTime = 0,
38 typedef Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime, 0, MaxRowsAtCompileTime, MaxColsAtCompileTime> DenseMatrixType;
39 typedef DenseMatrixType DenseType;
40 typedef DiagonalMatrix<Scalar,DiagonalVectorType::SizeAtCompileTime,DiagonalVectorType::MaxSizeAtCompileTime> PlainObject;
43 inline const Derived& derived()
const {
return *
static_cast<const Derived*
>(
this); }
45 inline Derived& derived() {
return *
static_cast<Derived*
>(
this); }
48 DenseMatrixType toDenseMatrix()
const {
return derived(); }
51 inline const DiagonalVectorType& diagonal()
const {
return derived().diagonal(); }
53 inline DiagonalVectorType& diagonal() {
return derived().diagonal(); }
56 inline Index rows()
const {
return diagonal().size(); }
58 inline Index cols()
const {
return diagonal().size(); }
60 template<
typename MatrixDerived>
62 const Product<Derived,MatrixDerived,LazyProduct>
63 operator*(
const MatrixBase<MatrixDerived> &matrix)
const
65 return Product<Derived, MatrixDerived, LazyProduct>(derived(),matrix.derived());
68 typedef DiagonalWrapper<const CwiseUnaryOp<internal::scalar_inverse_op<Scalar>,
const DiagonalVectorType> > InverseReturnType;
70 inline const InverseReturnType
73 return InverseReturnType(diagonal().cwiseInverse());
77 inline const DiagonalWrapper<
const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DiagonalVectorType,Scalar,product) >
80 return DiagonalWrapper<const EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(DiagonalVectorType,Scalar,product) >(diagonal() * scalar);
83 friend inline const DiagonalWrapper<
const EIGEN_SCALAR_BINARYOP_EXPR_RETURN_TYPE(Scalar,DiagonalVectorType,product) >
84 operator*(
const Scalar& scalar,
const DiagonalBase& other)
86 return DiagonalWrapper<const EIGEN_SCALAR_BINARYOP_EXPR_RETURN_TYPE(Scalar,DiagonalVectorType,product) >(scalar * other.diagonal());
89 template<
typename OtherDerived>
91 #ifdef EIGEN_PARSED_BY_DOXYGEN
92 inline unspecified_expression_type
94 inline const DiagonalWrapper<
const EIGEN_CWISE_BINARY_RETURN_TYPE(DiagonalVectorType,
typename OtherDerived::DiagonalVectorType,sum) >
96 operator+(
const DiagonalBase<OtherDerived>& other)
const
98 return (diagonal() + other.diagonal()).asDiagonal();
101 template<
typename OtherDerived>
103 #ifdef EIGEN_PARSED_BY_DOXYGEN
104 inline unspecified_expression_type
106 inline const DiagonalWrapper<
const EIGEN_CWISE_BINARY_RETURN_TYPE(DiagonalVectorType,
typename OtherDerived::DiagonalVectorType,difference) >
108 operator-(
const DiagonalBase<OtherDerived>& other)
const
110 return (diagonal() - other.diagonal()).asDiagonal();
130 template<
typename Scalar_,
int SizeAtCompileTime,
int MaxSizeAtCompileTime>
131 struct traits<DiagonalMatrix<Scalar_,SizeAtCompileTime,MaxSizeAtCompileTime> >
132 : traits<Matrix<Scalar_,SizeAtCompileTime,SizeAtCompileTime,0,MaxSizeAtCompileTime,MaxSizeAtCompileTime> >
134 typedef Matrix<Scalar_,SizeAtCompileTime,1,0,MaxSizeAtCompileTime,1> DiagonalVectorType;
135 typedef DiagonalShape StorageKind;
141 template<
typename Scalar_,
int SizeAtCompileTime,
int MaxSizeAtCompileTime>
143 :
public DiagonalBase<DiagonalMatrix<Scalar_,SizeAtCompileTime,MaxSizeAtCompileTime> >
146 #ifndef EIGEN_PARSED_BY_DOXYGEN
147 typedef typename internal::traits<DiagonalMatrix>::DiagonalVectorType DiagonalVectorType;
149 typedef Scalar_ Scalar;
150 typedef typename internal::traits<DiagonalMatrix>::StorageKind StorageKind;
151 typedef typename internal::traits<DiagonalMatrix>::StorageIndex StorageIndex;
156 DiagonalVectorType m_diagonal;
162 inline const DiagonalVectorType&
diagonal()
const {
return m_diagonal; }
165 inline DiagonalVectorType&
diagonal() {
return m_diagonal; }
181 inline DiagonalMatrix(
const Scalar& x,
const Scalar& y,
const Scalar& z) : m_diagonal(x,y,z) {}
191 template <
typename... ArgTypes>
192 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
193 DiagonalMatrix(
const Scalar& a0,
const Scalar& a1,
const Scalar& a2,
const ArgTypes&... args)
194 : m_diagonal(a0, a1, a2, args...) {}
200 explicit EIGEN_STRONG_INLINE
DiagonalMatrix(
const std::initializer_list<std::initializer_list<Scalar>>& list)
201 : m_diagonal(list) {}
205 explicit inline DiagonalMatrix(DiagonalVectorType&& diag) : m_diagonal(std::move(diag)) {}
208 template<
typename OtherDerived>
212 #ifndef EIGEN_PARSED_BY_DOXYGEN
218 template<
typename OtherDerived>
224 template<
typename OtherDerived>
232 #ifndef EIGEN_PARSED_BY_DOXYGEN
249 inline void setZero() { m_diagonal.setZero(); }
276 template<
typename DiagonalVectorType_>
279 typedef DiagonalVectorType_ DiagonalVectorType;
280 typedef typename DiagonalVectorType::Scalar
Scalar;
281 typedef typename DiagonalVectorType::StorageIndex
StorageIndex;
282 typedef DiagonalShape StorageKind;
283 typedef typename traits<DiagonalVectorType>::XprKind XprKind;
285 RowsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
286 ColsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
287 MaxRowsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
288 MaxColsAtCompileTime = DiagonalVectorType::MaxSizeAtCompileTime,
294 template<
typename DiagonalVectorType_>
296 :
public DiagonalBase<DiagonalWrapper<DiagonalVectorType_> >, internal::no_assignment_operator
299 #ifndef EIGEN_PARSED_BY_DOXYGEN
300 typedef DiagonalVectorType_ DiagonalVectorType;
306 explicit inline DiagonalWrapper(DiagonalVectorType& a_diagonal) : m_diagonal(a_diagonal) {}
310 const DiagonalVectorType&
diagonal()
const {
return m_diagonal; }
313 typename DiagonalVectorType::Nested m_diagonal;
325 template<
typename Derived>
326 EIGEN_DEVICE_FUNC
inline const DiagonalWrapper<const Derived>
340 template<
typename Derived>
343 if(cols() != rows())
return false;
344 RealScalar maxAbsOnDiagonal =
static_cast<RealScalar
>(-1);
345 for(
Index j = 0; j < cols(); ++j)
347 RealScalar absOnDiagonal = numext::abs(coeff(j,j));
348 if(absOnDiagonal > maxAbsOnDiagonal) maxAbsOnDiagonal = absOnDiagonal;
350 for(
Index j = 0; j < cols(); ++j)
351 for(
Index i = 0; i < j; ++i)
353 if(!internal::isMuchSmallerThan(coeff(i, j), maxAbsOnDiagonal, prec))
return false;
354 if(!internal::isMuchSmallerThan(coeff(j, i), maxAbsOnDiagonal, prec))
return false;
361 template<>
struct storage_kind_to_shape<DiagonalShape> {
typedef DiagonalShape Shape; };
363 struct Diagonal2Dense {};
365 template<>
struct AssignmentKind<DenseShape,DiagonalShape> {
typedef Diagonal2Dense Kind; };
368 template<
typename DstXprType,
typename SrcXprType,
typename Functor>
369 struct Assignment<DstXprType, SrcXprType, Functor, Diagonal2Dense>
371 static void run(DstXprType &dst,
const SrcXprType &src,
const internal::assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> &)
373 Index dstRows = src.rows();
374 Index dstCols = src.cols();
375 if((dst.rows()!=dstRows) || (dst.cols()!=dstCols))
376 dst.resize(dstRows, dstCols);
379 dst.diagonal() = src.diagonal();
382 static void run(DstXprType &dst,
const SrcXprType &src,
const internal::add_assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> &)
383 { dst.diagonal() += src.diagonal(); }
385 static void run(DstXprType &dst,
const SrcXprType &src,
const internal::sub_assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> &)
386 { dst.diagonal() -= src.diagonal(); }
internal::traits< Homogeneous< MatrixType, Direction_ > >::StorageIndex StorageIndex
The type used to store indices.
Definition: DenseBase.h:58
internal::traits< Homogeneous< MatrixType, Direction_ > >::Scalar Scalar
Definition: DenseBase.h:61
Represents a diagonal matrix with its storage.
Definition: DiagonalMatrix.h:144
DiagonalVectorType & diagonal()
Definition: DiagonalMatrix.h:165
DiagonalMatrix(const Scalar &x, const Scalar &y)
Definition: DiagonalMatrix.h:177
DiagonalMatrix()
Definition: DiagonalMatrix.h:169
DiagonalMatrix(const Scalar &x, const Scalar &y, const Scalar &z)
Definition: DiagonalMatrix.h:181
DiagonalMatrix(DiagonalVectorType &&diag)
Constructs a DiagonalMatrix from an r-value diagonal vector type.
Definition: DiagonalMatrix.h:205
void setIdentity(Index size)
Definition: DiagonalMatrix.h:258
const DiagonalVectorType & diagonal() const
Definition: DiagonalMatrix.h:162
void setZero()
Definition: DiagonalMatrix.h:249
void setIdentity()
Definition: DiagonalMatrix.h:255
DiagonalMatrix(const DiagonalBase< OtherDerived > &other)
Definition: DiagonalMatrix.h:210
DiagonalMatrix(Index dim)
Definition: DiagonalMatrix.h:173
void setZero(Index size)
Definition: DiagonalMatrix.h:252
void resize(Index size)
Definition: DiagonalMatrix.h:246
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:200
DiagonalMatrix(const MatrixBase< OtherDerived > &other)
Definition: DiagonalMatrix.h:220
DiagonalMatrix & operator=(const DiagonalBase< OtherDerived > &other)
Definition: DiagonalMatrix.h:226
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:193
Expression of a diagonal matrix.
Definition: DiagonalMatrix.h:297
const DiagonalVectorType & diagonal() const
Definition: DiagonalMatrix.h:310
DiagonalWrapper(DiagonalVectorType &a_diagonal)
Definition: DiagonalMatrix.h:306
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
const DiagonalWrapper< const Derived > asDiagonal() const
Definition: DiagonalMatrix.h:327
bool isDiagonal(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: DiagonalMatrix.h:341
const unsigned int NoPreferredStorageOrderBit
Definition: Constants.h:180
const unsigned int LvalueBit
Definition: Constants.h:146
Namespace containing all symbols from the Eigen library.
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
const Product< MatrixDerived, PermutationDerived, AliasFreeProduct > operator*(const MatrixBase< MatrixDerived > &matrix, const PermutationBase< PermutationDerived > &permutation)
Definition: PermutationMatrix.h:517
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_inverse_op< typename Derived::Scalar >, const Derived > inverse(const Eigen::ArrayBase< Derived > &x)
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:41