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 
44 
46 
49 
50  template<typename OtherDerived>
51  Derived& operator=(const EigenBase<OtherDerived> &other);
52 
53  enum {
54 
76 
78  MaxColsAtCompileTime>::ret),
79 
92 
94  : int(IsRowMajor) ? int(ColsAtCompileTime) : int(RowsAtCompileTime),
95 
96  #ifndef EIGEN_PARSED_BY_DOXYGEN
97  _HasDirectAccess = (int(Flags)&DirectAccessBit) ? 1 : 0 // workaround sunCC
98  #endif
99  };
108 
109  // FIXME storage order do not match evaluator storage order
112 #ifndef EIGEN_PARSED_BY_DOXYGEN
113 
120 
124 
133 
134  inline const Derived& derived() const { return *static_cast<const Derived*>(this); }
135  inline Derived& derived() { return *static_cast<Derived*>(this); }
136  inline Derived& const_cast_derived() const
137  { return *static_cast<Derived*>(const_cast<SparseMatrixBase*>(this)); }
138 
140 
141 #endif // not EIGEN_PARSED_BY_DOXYGEN
142 
143 #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::SparseMatrixBase
144 #ifdef EIGEN_PARSED_BY_DOXYGEN
145 #define EIGEN_DOC_UNARY_ADDONS(METHOD,OP)
146 #define EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL
147 #define EIGEN_DOC_BLOCK_ADDONS_INNER_PANEL_IF(COND)
148 #else
149 #define EIGEN_DOC_UNARY_ADDONS(X,Y)
150 #define EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL
151 #define EIGEN_DOC_BLOCK_ADDONS_INNER_PANEL_IF(COND)
152 #endif
153 # include "../plugins/CommonCwiseUnaryOps.h"
154 # include "../plugins/CommonCwiseBinaryOps.h"
155 # include "../plugins/MatrixCwiseUnaryOps.h"
156 # include "../plugins/MatrixCwiseBinaryOps.h"
157 # include "../plugins/BlockMethods.h"
158 # ifdef EIGEN_SPARSEMATRIXBASE_PLUGIN
159 # include EIGEN_SPARSEMATRIXBASE_PLUGIN
160 # endif
161 #undef EIGEN_CURRENT_STORAGE_BASE_CLASS
162 #undef EIGEN_DOC_UNARY_ADDONS
163 #undef EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL
164 #undef EIGEN_DOC_BLOCK_ADDONS_INNER_PANEL_IF
165 
167  inline Index rows() const { return derived().rows(); }
169  inline Index cols() const { return derived().cols(); }
172  inline Index size() const { return rows() * cols(); }
177  inline bool isVector() const { return rows()==1 || cols()==1; }
180  Index outerSize() const { return (int(Flags)&RowMajorBit) ? this->rows() : this->cols(); }
183  Index innerSize() const { return (int(Flags)&RowMajorBit) ? this->cols() : this->rows(); }
184 
185  bool isRValue() const { return m_isRValue; }
186  Derived& markAsRValue() { m_isRValue = true; return derived(); }
187 
188  SparseMatrixBase() : m_isRValue(false) { /* TODO check flags */ }
189 
190 
191  template<typename OtherDerived>
192  Derived& operator=(const ReturnByValue<OtherDerived>& other);
193 
194  template<typename OtherDerived>
195  inline Derived& operator=(const SparseMatrixBase<OtherDerived>& other);
196 
197  inline Derived& operator=(const Derived& other);
198 
199  protected:
200 
201  template<typename OtherDerived>
202  inline Derived& assign(const OtherDerived& other);
203 
204  template<typename OtherDerived>
205  inline void assignGeneric(const OtherDerived& other);
206 
207  public:
208 
209  friend std::ostream & operator << (std::ostream & s, const SparseMatrixBase& m)
210  {
211  typedef typename Derived::Nested Nested;
212  typedef typename internal::remove_all<Nested>::type NestedCleaned;
213 
214  if (Flags&RowMajorBit)
215  {
216  const Nested nm(m.derived());
218  for (Index row=0; row<nm.outerSize(); ++row)
219  {
220  Index col = 0;
221  for (typename internal::evaluator<NestedCleaned>::InnerIterator it(thisEval, row); it; ++it)
222  {
223  for ( ; col<it.index(); ++col)
224  s << "0 ";
225  s << it.value() << " ";
226  ++col;
227  }
228  for ( ; col<m.cols(); ++col)
229  s << "0 ";
230  s << std::endl;
231  }
232  }
233  else
234  {
235  const Nested nm(m.derived());
237  if (m.cols() == 1) {
238  Index row = 0;
239  for (typename internal::evaluator<NestedCleaned>::InnerIterator it(thisEval, 0); it; ++it)
240  {
241  for ( ; row<it.index(); ++row)
242  s << "0" << std::endl;
243  s << it.value() << std::endl;
244  ++row;
245  }
246  for ( ; row<m.rows(); ++row)
247  s << "0" << std::endl;
248  }
249  else
250  {
252  s << static_cast<const SparseMatrixBase<SparseMatrix<Scalar, RowMajorBit, StorageIndex> >&>(trans);
253  }
254  }
255  return s;
256  }
257 
258  template<typename OtherDerived>
259  Derived& operator+=(const SparseMatrixBase<OtherDerived>& other);
260  template<typename OtherDerived>
261  Derived& operator-=(const SparseMatrixBase<OtherDerived>& other);
262 
263  template<typename OtherDerived>
264  Derived& operator+=(const DiagonalBase<OtherDerived>& other);
265  template<typename OtherDerived>
266  Derived& operator-=(const DiagonalBase<OtherDerived>& other);
267 
268  Derived& operator*=(const Scalar& other);
269  Derived& operator/=(const Scalar& other);
270 
271  template<typename OtherDerived> struct CwiseProductDenseReturnType {
275  >::ReturnType>,
276  const Derived,
277  const OtherDerived
278  > Type;
279  };
280 
281  template<typename OtherDerived>
283  cwiseProduct(const MatrixBase<OtherDerived> &other) const;
284 
285  // sparse * diagonal
286  template<typename OtherDerived>
290 
291  // diagonal * sparse
292  template<typename OtherDerived> friend
295  { return Product<OtherDerived,Derived>(lhs.derived(), rhs.derived()); }
296 
297  // sparse * sparse
298  template<typename OtherDerived>
300  operator*(const SparseMatrixBase<OtherDerived> &other) const;
301 
302  // sparse * dense
303  template<typename OtherDerived>
306  { return Product<Derived,OtherDerived>(derived(), other.derived()); }
307 
308  // dense * sparse
309  template<typename OtherDerived> friend
312  { return Product<OtherDerived,Derived>(lhs.derived(), rhs.derived()); }
313 
316  {
318  }
319 
320  template<typename OtherDerived>
321  Derived& operator*=(const SparseMatrixBase<OtherDerived>& other);
323  template<int Mode>
325 
326  template<unsigned int UpLo> struct SelfAdjointViewReturnType { typedef SparseSelfAdjointView<Derived, UpLo> Type; };
327  template<unsigned int UpLo> struct ConstSelfAdjointViewReturnType { typedef const SparseSelfAdjointView<const Derived, UpLo> Type; };
328 
329  template<unsigned int UpLo> inline
331  template<unsigned int UpLo> inline
333 
334  template<typename OtherDerived> Scalar dot(const MatrixBase<OtherDerived>& other) const;
335  template<typename OtherDerived> Scalar dot(const SparseMatrixBase<OtherDerived>& other) const;
336  RealScalar squaredNorm() const;
337  RealScalar norm() const;
338  RealScalar blueNorm() const;
339 
340  TransposeReturnType transpose() { return TransposeReturnType(derived()); }
341  const ConstTransposeReturnType transpose() const { return ConstTransposeReturnType(derived()); }
342  const AdjointReturnType adjoint() const { return AdjointReturnType(transpose()); }
343 
344  // inner-vector
347  InnerVectorReturnType innerVector(Index outer);
348  const ConstInnerVectorReturnType innerVector(Index outer) const;
350  // set of inner-vectors
353  InnerVectorsReturnType innerVectors(Index outerStart, Index outerSize);
354  const ConstInnerVectorsReturnType innerVectors(Index outerStart, Index outerSize) const;
355 
356  DenseMatrixType toDense() const
357  {
358  return DenseMatrixType(derived());
359  }
360 
361  template<typename OtherDerived>
362  bool isApprox(const SparseMatrixBase<OtherDerived>& other,
363  const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
364 
365  template<typename OtherDerived>
367  const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const
368  { return toDense().isApprox(other,prec); }
369 
375  inline const typename internal::eval<Derived>::type eval() const
376  { return typename internal::eval<Derived>::type(derived()); }
377 
378  Scalar sum() const;
379 
380  inline const SparseView<Derived>
381  pruned(const Scalar& reference = Scalar(0), const RealScalar& epsilon = NumTraits<Scalar>::dummy_precision()) const;
383  protected:
384 
386 
387  static inline StorageIndex convert_index(const Index idx) {
388  return internal::convert_index<StorageIndex>(idx);
389  }
390  private:
391  template<typename Dest> void evalTo(Dest &) const;
392 };
393 
394 } // end namespace Eigen
395 
396 #endif // EIGEN_SPARSEMATRIXBASE_H
Generic expression of a matrix where all coefficients are defined by a functor.
#define EIGEN_STRONG_INLINE
Definition: Macros.h:493
const ConstTransposeReturnType transpose() const
double epsilon
Block< const Derived, IsRowMajor?1:Dynamic, IsRowMajor?Dynamic:1, true > ConstInnerVectorReturnType
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
A versatible sparse matrix representation.
Definition: SparseMatrix.h:92
RealScalar squaredNorm() const
Definition: SparseDot.h:77
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)
Definition: LDLT.h:16
Pseudo expression to manipulate a triangular sparse matrix as a selfadjoint matrix.
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
void evalTo(Dest &) const
EIGEN_DEVICE_FUNC RowXpr row(Index i)
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:37
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 StorageIndex convert_index(const Index idx)
const SparseView< Derived > pruned(const Scalar &reference=Scalar(0), const RealScalar &epsilon=NumTraits< Scalar >::dummy_precision()) const
Definition: SparseView.h:214
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.
EIGEN_STRONG_INLINE const CwiseProductDenseReturnType< OtherDerived >::Type cwiseProduct(const MatrixBase< OtherDerived > &other) const
TransposeReturnType transpose()
internal::traits< Derived >::Scalar Scalar
SparseSelfAdjointView< Derived, UpLo > Type
RealScalar blueNorm() const
Definition: SparseDot.h:92
Matrix< Scalar, RowsAtCompileTime, ColsAtCompileTime > DenseMatrixType
Scalar dot(const MatrixBase< OtherDerived > &other) const
InnerVectorReturnType innerVector(Index outer)
Definition: SparseBlock.h:333
const TriangularView< const Derived, Mode > triangularView() const
Derived & assign(const OtherDerived &other)
EIGEN_DEVICE_FUNC ColXpr col(Index i)
const AdjointReturnType adjoint() const
bool isApprox(const MatrixBase< OtherDerived > &other, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
internal::add_const< Transpose< const Derived > >::type ConstTransposeReturnType
const Product< Derived, OtherDerived > operator*(const DiagonalBase< OtherDerived > &other) const
ConstSelfAdjointViewReturnType< UpLo >::Type selfadjointView() const
Scalar sum() const
Definition: SparseRedux.h:17
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)
Derived & const_cast_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)
SparseSymmetricPermutationProduct< Derived, Upper|Lower > twistedBy(const PermutationMatrix< Dynamic, Dynamic, StorageIndex > &perm) const
Matrix< Scalar, EIGEN_SIZE_MAX(RowsAtCompileTime, ColsAtCompileTime), EIGEN_SIZE_MAX(RowsAtCompileTime, ColsAtCompileTime)> SquareMatrixType
const Derived & derived() const
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:757
const internal::eval< Derived >::type eval() const
DenseMatrixType toDense() const
Block< Derived, Dynamic, Dynamic, true > InnerVectorsReturnType
RealScalar norm() const
Definition: SparseDot.h:84
const SparseSelfAdjointView< const Derived, UpLo > Type
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:55
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:893
SparseMatrix< Scalar, Flags &RowMajorBit ? RowMajor :ColMajor, StorageIndex > PlainObject
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
const Product< Derived, OtherDerived > operator*(const MatrixBase< OtherDerived > &other) const
friend const Product< OtherDerived, Derived > operator*(const DiagonalBase< OtherDerived > &lhs, const SparseMatrixBase &rhs)
EIGEN_DEVICE_FUNC const Derived & derived() const
bool isApprox(const SparseMatrixBase< OtherDerived > &other, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: SparseFuzzy.h:17


co_scan
Author(s):
autogenerated on Mon Feb 28 2022 23:00:51