SparseMatrixBase.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) 2008-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_SPARSEMATRIXBASE_H
11 #define EIGEN_SPARSEMATRIXBASE_H
12 
13 namespace Eigen {
14 
26 template<typename Derived> class SparseMatrixBase
27  : public EigenBase<Derived>
28 {
29  public:
30 
32 
36  typedef Scalar value_type;
37 
40 
44 
48 
50 
53 
54  template<typename OtherDerived>
55  Derived& operator=(const EigenBase<OtherDerived> &other);
56 
57  enum {
58 
80 
83 
96 
99 
100  #ifndef EIGEN_PARSED_BY_DOXYGEN
101  _HasDirectAccess = (int(Flags)&DirectAccessBit) ? 1 : 0 // workaround sunCC
102  #endif
103  };
104 
112 
113  // FIXME storage order do not match evaluator storage order
115 
116 #ifndef EIGEN_PARSED_BY_DOXYGEN
117 
124 
128 
131 
138  inline const Derived& derived() const { return *static_cast<const Derived*>(this); }
139  inline Derived& derived() { return *static_cast<Derived*>(this); }
140  inline Derived& const_cast_derived() const
141  { return *static_cast<Derived*>(const_cast<SparseMatrixBase*>(this)); }
142 
144 
145 #endif // not EIGEN_PARSED_BY_DOXYGEN
146 
147 #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::SparseMatrixBase
148 #ifdef EIGEN_PARSED_BY_DOXYGEN
149 #define EIGEN_DOC_UNARY_ADDONS(METHOD,OP)
150 #define EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL
151 #define EIGEN_DOC_BLOCK_ADDONS_INNER_PANEL_IF(COND)
152 #else
153 #define EIGEN_DOC_UNARY_ADDONS(X,Y)
154 #define EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL
155 #define EIGEN_DOC_BLOCK_ADDONS_INNER_PANEL_IF(COND)
156 #endif
157 # include "../plugins/CommonCwiseUnaryOps.h"
158 # include "../plugins/CommonCwiseBinaryOps.h"
159 # include "../plugins/MatrixCwiseUnaryOps.h"
160 # include "../plugins/MatrixCwiseBinaryOps.h"
161 # include "../plugins/BlockMethods.h"
162 # ifdef EIGEN_SPARSEMATRIXBASE_PLUGIN
163 # include EIGEN_SPARSEMATRIXBASE_PLUGIN
164 # endif
165 #undef EIGEN_CURRENT_STORAGE_BASE_CLASS
166 #undef EIGEN_DOC_UNARY_ADDONS
167 #undef EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL
168 #undef EIGEN_DOC_BLOCK_ADDONS_INNER_PANEL_IF
169 
171  inline Index rows() const { return derived().rows(); }
173  inline Index cols() const { return derived().cols(); }
176  inline Index size() const { return rows() * cols(); }
181  inline bool isVector() const { return rows()==1 || cols()==1; }
184  Index outerSize() const { return (int(Flags)&RowMajorBit) ? this->rows() : this->cols(); }
187  Index innerSize() const { return (int(Flags)&RowMajorBit) ? this->cols() : this->rows(); }
188 
189  bool isRValue() const { return m_isRValue; }
190  Derived& markAsRValue() { m_isRValue = true; return derived(); }
191 
192  SparseMatrixBase() : m_isRValue(false) { /* TODO check flags */ }
193 
194 
195  template<typename OtherDerived>
196  Derived& operator=(const ReturnByValue<OtherDerived>& other);
197 
198  template<typename OtherDerived>
199  inline Derived& operator=(const SparseMatrixBase<OtherDerived>& other);
200 
201  inline Derived& operator=(const Derived& other);
202 
203  protected:
205  template<typename OtherDerived>
206  inline Derived& assign(const OtherDerived& other);
207 
208  template<typename OtherDerived>
209  inline void assignGeneric(const OtherDerived& other);
210 
211  public:
213  friend std::ostream & operator << (std::ostream & s, const SparseMatrixBase& m)
214  {
215  typedef typename Derived::Nested Nested;
216  typedef typename internal::remove_all<Nested>::type NestedCleaned;
217 
218  if (Flags&RowMajorBit)
219  {
220  Nested nm(m.derived());
222  for (Index row=0; row<nm.outerSize(); ++row)
223  {
224  Index col = 0;
225  for (typename internal::evaluator<NestedCleaned>::InnerIterator it(thisEval, row); it; ++it)
226  {
227  for ( ; col<it.index(); ++col)
228  s << "0 ";
229  s << it.value() << " ";
230  ++col;
231  }
232  for ( ; col<m.cols(); ++col)
233  s << "0 ";
234  s << std::endl;
235  }
236  }
237  else
238  {
239  Nested nm(m.derived());
241  if (m.cols() == 1) {
242  Index row = 0;
243  for (typename internal::evaluator<NestedCleaned>::InnerIterator it(thisEval, 0); it; ++it)
244  {
245  for ( ; row<it.index(); ++row)
246  s << "0" << std::endl;
247  s << it.value() << std::endl;
248  ++row;
249  }
250  for ( ; row<m.rows(); ++row)
251  s << "0" << std::endl;
252  }
253  else
254  {
256  s << static_cast<const SparseMatrixBase<SparseMatrix<Scalar, RowMajorBit, StorageIndex> >&>(trans);
257  }
258  }
259  return s;
260  }
261 
262  template<typename OtherDerived>
263  Derived& operator+=(const SparseMatrixBase<OtherDerived>& other);
264  template<typename OtherDerived>
265  Derived& operator-=(const SparseMatrixBase<OtherDerived>& other);
266 
267  template<typename OtherDerived>
268  Derived& operator+=(const DiagonalBase<OtherDerived>& other);
269  template<typename OtherDerived>
270  Derived& operator-=(const DiagonalBase<OtherDerived>& other);
272  template<typename OtherDerived>
273  Derived& operator+=(const EigenBase<OtherDerived> &other);
274  template<typename OtherDerived>
275  Derived& operator-=(const EigenBase<OtherDerived> &other);
276 
277  Derived& operator*=(const Scalar& other);
278  Derived& operator/=(const Scalar& other);
279 
280  template<typename OtherDerived> struct CwiseProductDenseReturnType {
284  >::ReturnType>,
285  const Derived,
286  const OtherDerived
287  > Type;
288  };
290  template<typename OtherDerived>
292  cwiseProduct(const MatrixBase<OtherDerived> &other) const;
293 
294  // sparse * diagonal
295  template<typename OtherDerived>
298  { return Product<Derived,OtherDerived>(derived(), other.derived()); }
299 
300  // diagonal * sparse
301  template<typename OtherDerived> friend
304  { return Product<OtherDerived,Derived>(lhs.derived(), rhs.derived()); }
305 
306  // sparse * sparse
307  template<typename OtherDerived>
309  operator*(const SparseMatrixBase<OtherDerived> &other) const;
310 
311  // sparse * dense
312  template<typename OtherDerived>
315  { return Product<Derived,OtherDerived>(derived(), other.derived()); }
316 
317  // dense * sparse
318  template<typename OtherDerived> friend
321  { return Product<OtherDerived,Derived>(lhs.derived(), rhs.derived()); }
322 
325  {
327  }
328 
329  template<typename OtherDerived>
330  Derived& operator*=(const SparseMatrixBase<OtherDerived>& other);
331 
332  template<int Mode>
334 
335  template<unsigned int UpLo> struct SelfAdjointViewReturnType { typedef SparseSelfAdjointView<Derived, UpLo> Type; };
336  template<unsigned int UpLo> struct ConstSelfAdjointViewReturnType { typedef const SparseSelfAdjointView<const Derived, UpLo> Type; };
337 
338  template<unsigned int UpLo> inline
340  template<unsigned int UpLo> inline
342 
343  template<typename OtherDerived> Scalar dot(const MatrixBase<OtherDerived>& other) const;
344  template<typename OtherDerived> Scalar dot(const SparseMatrixBase<OtherDerived>& other) const;
345  RealScalar squaredNorm() const;
346  RealScalar norm() const;
347  RealScalar blueNorm() const;
348 
349  TransposeReturnType transpose() { return TransposeReturnType(derived()); }
350  const ConstTransposeReturnType transpose() const { return ConstTransposeReturnType(derived()); }
351  const AdjointReturnType adjoint() const { return AdjointReturnType(transpose()); }
352 
353  // inner-vector
356  InnerVectorReturnType innerVector(Index outer);
357  const ConstInnerVectorReturnType innerVector(Index outer) const;
358 
359  // set of inner-vectors
362  InnerVectorsReturnType innerVectors(Index outerStart, Index outerSize);
363  const ConstInnerVectorsReturnType innerVectors(Index outerStart, Index outerSize) const;
364 
365  DenseMatrixType toDense() const
366  {
367  return DenseMatrixType(derived());
368  }
369 
370  template<typename OtherDerived>
371  bool isApprox(const SparseMatrixBase<OtherDerived>& other,
372  const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
373 
374  template<typename OtherDerived>
376  const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const
377  { return toDense().isApprox(other,prec); }
378 
384  inline const typename internal::eval<Derived>::type eval() const
385  { return typename internal::eval<Derived>::type(derived()); }
386 
387  Scalar sum() const;
388 
389  inline const SparseView<Derived>
390  pruned(const Scalar& reference = Scalar(0), const RealScalar& epsilon = NumTraits<Scalar>::dummy_precision()) const;
391 
392  protected:
393 
395 
396  static inline StorageIndex convert_index(const Index idx) {
397  return internal::convert_index<StorageIndex>(idx);
398  }
399  private:
400  template<typename Dest> void evalTo(Dest &) const;
401 };
402 
403 } // end namespace Eigen
404 
405 #endif // EIGEN_SPARSEMATRIXBASE_H
ConstSelfAdjointViewReturnType< UpLo >::Type selfadjointView() const
Matrix3f m
Generic expression of a matrix where all coefficients are defined by a functor.
RealScalar squaredNorm() const
Definition: SparseDot.h:77
EIGEN_DEVICE_FUNC const Derived & derived() const
#define EIGEN_STRONG_INLINE
Definition: Macros.h:494
const AdjointReturnType adjoint() const
Block< const Derived, IsRowMajor?1:Dynamic, IsRowMajor?Dynamic:1, true > ConstInnerVectorReturnType
RealScalar norm() const
Definition: SparseDot.h:84
friend std::ostream & operator<<(std::ostream &s, const SparseMatrixBase &m)
Block< const Derived, Dynamic, Dynamic, true > ConstInnerVectorsReturnType
internal::traits< Derived >::StorageKind StorageKind
SparseMatrixBase StorageBaseType
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:71
EIGEN_STRONG_INLINE const CwiseProductDenseReturnType< OtherDerived >::Type cwiseProduct(const MatrixBase< OtherDerived > &other) const
return int(ret)+1
RealScalar blueNorm() const
Definition: SparseDot.h:92
A versatible sparse matrix representation.
Definition: SparseMatrix.h:96
internal::packet_traits< Scalar >::type PacketScalar
CwiseBinaryOp< internal::scalar_product_op< typename ScalarBinaryOpTraits< typename internal::traits< Derived >::Scalar, typename internal::traits< OtherDerived >::Scalar >::ReturnType >, const Derived, const OtherDerived > Type
Expression of the transpose of a matrix.
Definition: Transpose.h:52
const unsigned int DirectAccessBit
Definition: Constants.h:150
CwiseNullaryOp< internal::scalar_constant_op< Scalar >, Matrix< Scalar, Dynamic, Dynamic > > ConstantReturnType
Derived & operator+=(const SparseMatrixBase< OtherDerived > &other)
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Pseudo expression to manipulate a triangular sparse matrix as a selfadjoint matrix.
const TriangularView< const Derived, Mode > triangularView() const
static char trans
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:150
Derived & operator=(const EigenBase< OtherDerived > &other)
Definition: SparseAssign.h:17
Scalar dot(const MatrixBase< OtherDerived > &other) const
const SparseView< Derived > pruned(const Scalar &reference=Scalar(0), const RealScalar &epsilon=NumTraits< Scalar >::dummy_precision()) const
Definition: SparseView.h:245
EIGEN_DEVICE_FUNC RowXpr row(Index i)
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:38
Block< Derived, IsRowMajor?1:Dynamic, IsRowMajor?Dynamic:1, true > InnerVectorReturnType
const unsigned int RowMajorBit
Definition: Constants.h:61
Transpose< Derived > TransposeReturnType
internal::conditional< NumTraits< Scalar >::IsComplex, CwiseUnaryOp< internal::scalar_conjugate_op< Scalar >, Eigen::Transpose< const Derived > >, Transpose< const Derived > >::type AdjointReturnType
static double epsilon
Definition: testRot3.cpp:39
const Product< Derived, OtherDerived > operator*(const MatrixBase< OtherDerived > &other) const
static StorageIndex convert_index(const Index idx)
internal::traits< Derived >::StorageIndex StorageIndex
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:77
Base class of any sparse matrices or sparse expressions.
TransposeReturnType transpose()
internal::traits< Derived >::Scalar Scalar
const internal::eval< Derived >::type eval() const
SparseSelfAdjointView< Derived, UpLo > Type
Matrix< Scalar, RowsAtCompileTime, ColsAtCompileTime > DenseMatrixType
InnerVectorReturnType innerVector(Index outer)
Definition: SparseBlock.h:333
idx_t idx_t idx_t idx_t idx_t * perm
RealScalar s
Expression of a dense or sparse matrix with zero or too small values removed.
Derived & assign(const OtherDerived &other)
bool isApprox(const SparseMatrixBase< OtherDerived > &other, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: SparseFuzzy.h:17
Scalar sum() const
Definition: SparseRedux.h:17
EIGEN_DEVICE_FUNC ColXpr col(Index i)
void evalTo(Dest &) const
const Product< Derived, OtherDerived > operator*(const DiagonalBase< OtherDerived > &other) const
SparseSymmetricPermutationProduct< Derived, Upper|Lower > twistedBy(const PermutationMatrix< Dynamic, Dynamic, StorageIndex > &perm) const
internal::add_const< Transpose< const Derived > >::type ConstTransposeReturnType
InnerVectorsReturnType innerVectors(Index outerStart, Index outerSize)
Definition: SparseBlock.h:348
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:103
EigenBase< Derived > Base
Derived & operator*=(const Scalar &other)
DenseIndex ret
Definition: level1_impl.h:59
const Derived & derived() const
Derived & operator/=(const Scalar &other)
Expression of a triangular part in a matrix.
Derived & operator-=(const SparseMatrixBase< OtherDerived > &other)
internal::conditional< _HasDirectAccess, const Scalar &, Scalar >::type CoeffReturnType
void assignGeneric(const OtherDerived &other)
Matrix< Scalar, EIGEN_SIZE_MAX(RowsAtCompileTime, ColsAtCompileTime), EIGEN_SIZE_MAX(RowsAtCompileTime, ColsAtCompileTime)> SquareMatrixType
Matrix< StorageIndex, Dynamic, 1 > IndexVector
friend const Product< OtherDerived, Derived > operator*(const MatrixBase< OtherDerived > &lhs, const SparseMatrixBase &rhs)
Determines whether the given binary operation of two numeric types is allowed and what the scalar ret...
Definition: XprHelper.h:766
DenseMatrixType toDense() const
Block< Derived, Dynamic, Dynamic, true > InnerVectorsReturnType
const SparseSelfAdjointView< const Derived, UpLo > Type
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:55
bool isApprox(const MatrixBase< OtherDerived > &other, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
NumTraits< Scalar >::Real RealScalar
Matrix< Scalar, Dynamic, 1 > ScalarVector
internal::add_const_on_value_type_if_arithmetic< typename internal::packet_traits< Scalar >::type >::type PacketReturnType
#define EIGEN_SIZE_MAX(a, b)
Definition: Macros.h:897
SparseMatrix< Scalar, Flags &RowMajorBit?RowMajor:ColMajor, StorageIndex > PlainObject
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
Definition: pytypes.h:897
friend const Product< OtherDerived, Derived > operator*(const DiagonalBase< OtherDerived > &lhs, const SparseMatrixBase &rhs)
Derived & const_cast_derived() const
const ConstTransposeReturnType transpose() const


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:44:35