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,
33  Flags = 0
34  };
35 
37  typedef DenseMatrixType DenseType;
39 
40  inline const Derived& derived() const { return *static_cast<const Derived*>(this); }
41  inline Derived& derived() { return *static_cast<Derived*>(this); }
42 
43  DenseMatrixType toDenseMatrix() const { return derived(); }
44  template<typename DenseDerived>
45  void evalTo(MatrixBase<DenseDerived> &other) const;
46  template<typename DenseDerived>
47  void addTo(MatrixBase<DenseDerived> &other) const
48  { other.diagonal() += diagonal(); }
49  template<typename DenseDerived>
50  void subTo(MatrixBase<DenseDerived> &other) const
51  { other.diagonal() -= diagonal(); }
52 
53  inline const DiagonalVectorType& diagonal() const { return derived().diagonal(); }
54  inline DiagonalVectorType& diagonal() { return derived().diagonal(); }
55 
56  inline Index rows() const { return diagonal().size(); }
57  inline Index cols() const { return diagonal().size(); }
58 
61  template<typename MatrixDerived>
63  operator*(const MatrixBase<MatrixDerived> &matrix) const
64  {
66  }
67 
68  inline const DiagonalWrapper<const CwiseUnaryOp<internal::scalar_inverse_op<Scalar>, const DiagonalVectorType> >
69  inverse() const
70  {
71  return diagonal().cwiseInverse();
72  }
73 
74  inline const DiagonalWrapper<const CwiseUnaryOp<internal::scalar_multiple_op<Scalar>, const DiagonalVectorType> >
75  operator*(const Scalar& scalar) const
76  {
77  return diagonal() * scalar;
78  }
79  friend inline const DiagonalWrapper<const CwiseUnaryOp<internal::scalar_multiple_op<Scalar>, const DiagonalVectorType> >
80  operator*(const Scalar& scalar, const DiagonalBase& other)
81  {
82  return other.diagonal() * scalar;
83  }
84 
85  #ifdef EIGEN2_SUPPORT
86  template<typename OtherDerived>
88  {
89  return diagonal().isApprox(other.diagonal(), precision);
90  }
91  template<typename OtherDerived>
93  {
94  return toDenseMatrix().isApprox(other, precision);
95  }
96  #endif
97 };
98 
99 template<typename Derived>
100 template<typename DenseDerived>
102 {
103  other.setZero();
104  other.diagonal() = diagonal();
105 }
106 #endif
107 
121 namespace internal {
122 template<typename _Scalar, int SizeAtCompileTime, int MaxSizeAtCompileTime>
123 struct traits<DiagonalMatrix<_Scalar,SizeAtCompileTime,MaxSizeAtCompileTime> >
124  : traits<Matrix<_Scalar,SizeAtCompileTime,SizeAtCompileTime,0,MaxSizeAtCompileTime,MaxSizeAtCompileTime> >
125 {
128  typedef DenseIndex Index;
129  enum {
131  };
132 };
133 }
134 template<typename _Scalar, int SizeAtCompileTime, int MaxSizeAtCompileTime>
136  : public DiagonalBase<DiagonalMatrix<_Scalar,SizeAtCompileTime,MaxSizeAtCompileTime> >
137 {
138  public:
139  #ifndef EIGEN_PARSED_BY_DOXYGEN
141  typedef const DiagonalMatrix& Nested;
142  typedef _Scalar Scalar;
145  #endif
146 
147  protected:
148 
149  DiagonalVectorType m_diagonal;
150 
151  public:
152 
154  inline const DiagonalVectorType& diagonal() const { return m_diagonal; }
156  inline DiagonalVectorType& diagonal() { return m_diagonal; }
157 
159  inline DiagonalMatrix() {}
160 
162  inline DiagonalMatrix(Index dim) : m_diagonal(dim) {}
163 
165  inline DiagonalMatrix(const Scalar& x, const Scalar& y) : m_diagonal(x,y) {}
166 
168  inline DiagonalMatrix(const Scalar& x, const Scalar& y, const Scalar& z) : m_diagonal(x,y,z) {}
169 
171  template<typename OtherDerived>
172  inline DiagonalMatrix(const DiagonalBase<OtherDerived>& other) : m_diagonal(other.diagonal()) {}
173 
174  #ifndef EIGEN_PARSED_BY_DOXYGEN
175 
176  inline DiagonalMatrix(const DiagonalMatrix& other) : m_diagonal(other.diagonal()) {}
177  #endif
178 
180  template<typename OtherDerived>
181  explicit inline DiagonalMatrix(const MatrixBase<OtherDerived>& other) : m_diagonal(other)
182  {}
183 
185  template<typename OtherDerived>
187  {
188  m_diagonal = other.diagonal();
189  return *this;
190  }
191 
192  #ifndef EIGEN_PARSED_BY_DOXYGEN
193 
197  {
198  m_diagonal = other.diagonal();
199  return *this;
200  }
201  #endif
202 
204  inline void resize(Index size) { m_diagonal.resize(size); }
206  inline void setZero() { m_diagonal.setZero(); }
208  inline void setZero(Index size) { m_diagonal.setZero(size); }
210  inline void setIdentity() { m_diagonal.setOnes(); }
212  inline void setIdentity(Index size) { m_diagonal.setOnes(size); }
213 };
214 
229 namespace internal {
230 template<typename _DiagonalVectorType>
231 struct traits<DiagonalWrapper<_DiagonalVectorType> >
232 {
233  typedef _DiagonalVectorType DiagonalVectorType;
234  typedef typename DiagonalVectorType::Scalar Scalar;
235  typedef typename DiagonalVectorType::Index Index;
236  typedef typename DiagonalVectorType::StorageKind StorageKind;
237  enum {
238  RowsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
239  ColsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
240  MaxRowsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
241  MaxColsAtCompileTime = DiagonalVectorType::SizeAtCompileTime,
243  };
244 };
245 }
246 
247 template<typename _DiagonalVectorType>
249  : public DiagonalBase<DiagonalWrapper<_DiagonalVectorType> >, internal::no_assignment_operator
250 {
251  public:
252  #ifndef EIGEN_PARSED_BY_DOXYGEN
253  typedef _DiagonalVectorType DiagonalVectorType;
255  #endif
256 
258  inline DiagonalWrapper(DiagonalVectorType& a_diagonal) : m_diagonal(a_diagonal) {}
259 
261  const DiagonalVectorType& diagonal() const { return m_diagonal; }
262 
263  protected:
264  typename DiagonalVectorType::Nested m_diagonal;
265 };
266 
276 template<typename Derived>
279 {
280  return derived();
281 }
282 
291 template<typename Derived>
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));
300  if(absOnDiagonal > maxAbsOnDiagonal) maxAbsOnDiagonal = absOnDiagonal;
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 
311 } // end namespace Eigen
312 
313 #endif // EIGEN_DIAGONALMATRIX_H
DiagonalMatrix< Scalar, DiagonalVectorType::SizeAtCompileTime, DiagonalVectorType::MaxSizeAtCompileTime > PlainObject
DiagonalMatrix & operator=(const DiagonalBase< OtherDerived > &other)
void evalTo(MatrixBase< DenseDerived > &other) const
DenseMatrixType DenseType
DiagonalVectorType::Scalar Scalar
DiagonalMatrix(const DiagonalBase< OtherDerived > &other)
internal::traits< Derived >::Index Index
The type of indices.
Definition: DenseBase.h:61
DiagonalReturnType diagonal()
Definition: Diagonal.h:168
const unsigned int LvalueBit
DiagonalMatrix(const Scalar &x, const Scalar &y)
const DiagonalVectorType & diagonal() const
Represents a diagonal matrix with its storage.
Derived & setZero()
iterative scaling algorithm to equilibrate rows and column norms in matrices
Definition: matrix.hpp:471
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:88
bool isMuchSmallerThan(const Scalar &x, const OtherScalar &y, typename NumTraits< Scalar >::Real precision=NumTraits< Scalar >::dummy_precision())
const DiagonalWrapper< const CwiseUnaryOp< internal::scalar_multiple_op< Scalar >, const DiagonalVectorType > > operator*(const Scalar &scalar) const
void addTo(MatrixBase< DenseDerived > &other) const
void resize(Index size)
DiagonalWrapper(DiagonalVectorType &a_diagonal)
internal::traits< Derived >::Index Index
Index rows() const
internal::traits< Derived >::DiagonalVectorType DiagonalVectorType
const DiagonalVectorType & diagonal() const
EIGEN_STRONG_INLINE const CwiseUnaryOp< internal::scalar_abs_op< Scalar >, const Derived > abs() const
DenseMatrixType toDenseMatrix() const
DiagonalVectorType m_diagonal
internal::traits< DiagonalMatrix >::Index Index
bool isApprox(const Scalar &x, const Scalar &y, typename NumTraits< Scalar >::Real precision=NumTraits< Scalar >::dummy_precision())
DiagonalMatrix(const Scalar &x, const Scalar &y, const Scalar &z)
internal::traits< DiagonalMatrix >::StorageKind StorageKind
const DiagonalMatrix & Nested
const DiagonalVectorType & diagonal() const
DiagonalVectorType::RealScalar RealScalar
DiagonalWrapper Nested
Matrix< Scalar, RowsAtCompileTime, ColsAtCompileTime, 0, MaxRowsAtCompileTime, MaxColsAtCompileTime > DenseMatrixType
DiagonalMatrix & operator=(const DiagonalMatrix &other)
EIGEN_DEFAULT_DENSE_INDEX_TYPE DenseIndex
Definition: XprHelper.h:27
internal::traits< Derived >::StorageKind StorageKind
Index cols() const
NumTraits< Scalar >::Real RealScalar
Definition: DenseBase.h:65
void subTo(MatrixBase< DenseDerived > &other) const
DiagonalVectorType & diagonal()
_DiagonalVectorType DiagonalVectorType
DiagonalVectorType::Nested m_diagonal
const DiagonalWrapper< const CwiseUnaryOp< internal::scalar_inverse_op< Scalar >, const DiagonalVectorType > > inverse() const
Expression of a diagonal matrix.
void setIdentity(Index size)
internal::traits< DiagonalMatrix >::DiagonalVectorType DiagonalVectorType
const Derived & derived() const
DiagonalVectorType & diagonal()
DiagonalMatrix(const MatrixBase< OtherDerived > &other)
bool isDiagonal(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
const DiagonalWrapper< const Derived > asDiagonal() const
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
void setZero(Index size)
DiagonalMatrix(const DiagonalMatrix &other)
friend const DiagonalWrapper< const CwiseUnaryOp< internal::scalar_multiple_op< Scalar >, const DiagonalVectorType > > operator*(const Scalar &scalar, const DiagonalBase &other)
const T & y
Matrix< _Scalar, SizeAtCompileTime, 1, 0, MaxSizeAtCompileTime, 1 > DiagonalVectorType
Index size() const
Definition: EigenBase.h:49
const DiagonalProduct< MatrixDerived, Derived, OnTheLeft > operator*(const MatrixBase< MatrixDerived > &matrix) const


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:34:32