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 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
29 template <typename Derived>
30 class SparseMatrixBase : public EigenBase<Derived> {
31  public:
33 
37  typedef Scalar value_type;
38 
41 
45 
48 
50 
53 
54  template <typename OtherDerived>
55  Derived& operator=(const EigenBase<OtherDerived>& other);
56 
57  enum {
58 
78 
80 
89  : 2,
100 
104 
105 #ifndef EIGEN_PARSED_BY_DOXYGEN
106  HasDirectAccess_ = (int(Flags) & DirectAccessBit) ? 1 : 0 // workaround sunCC
107 #endif
108  };
109 
117 
118  // FIXME storage order do not match evaluator storage order
120 
121 #ifndef EIGEN_PARSED_BY_DOXYGEN
129 
132  typedef std::conditional_t<HasDirectAccess_, const Scalar&, Scalar> CoeffReturnType;
133 
136 
143 
144  inline const Derived& derived() const { return *static_cast<const Derived*>(this); }
145  inline Derived& derived() { return *static_cast<Derived*>(this); }
146  inline Derived& const_cast_derived() const { return *static_cast<Derived*>(const_cast<SparseMatrixBase*>(this)); }
147 
149 
150 #endif // not EIGEN_PARSED_BY_DOXYGEN
151 
152 #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::SparseMatrixBase
153 #ifdef EIGEN_PARSED_BY_DOXYGEN
154 #define EIGEN_DOC_UNARY_ADDONS(METHOD, \
155  OP)
157 #define EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL
160 #define EIGEN_DOC_BLOCK_ADDONS_INNER_PANEL_IF( \
161  COND)
163 #else
164 #define EIGEN_DOC_UNARY_ADDONS(X, Y)
165 #define EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL
166 #define EIGEN_DOC_BLOCK_ADDONS_INNER_PANEL_IF(COND)
167 #endif
168 #include "../plugins/CommonCwiseUnaryOps.inc"
169 #include "../plugins/CommonCwiseBinaryOps.inc"
170 #include "../plugins/MatrixCwiseUnaryOps.inc"
171 #include "../plugins/MatrixCwiseBinaryOps.inc"
172 #include "../plugins/BlockMethods.inc"
173 #ifdef EIGEN_SPARSEMATRIXBASE_PLUGIN
174 #include EIGEN_SPARSEMATRIXBASE_PLUGIN
175 #endif
176 #undef EIGEN_CURRENT_STORAGE_BASE_CLASS
177 #undef EIGEN_DOC_UNARY_ADDONS
178 #undef EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL
179 #undef EIGEN_DOC_BLOCK_ADDONS_INNER_PANEL_IF
180 
182  inline Index rows() const { return derived().rows(); }
184  inline Index cols() const { return derived().cols(); }
187  inline Index size() const { return rows() * cols(); }
192  inline bool isVector() const { return rows() == 1 || cols() == 1; }
195  Index outerSize() const { return (int(Flags) & RowMajorBit) ? this->rows() : this->cols(); }
198  Index innerSize() const { return (int(Flags) & RowMajorBit) ? this->cols() : this->rows(); }
199 
200  bool isRValue() const { return m_isRValue; }
201  Derived& markAsRValue() {
202  m_isRValue = true;
203  return derived();
204  }
205 
206  SparseMatrixBase() : m_isRValue(false) { /* TODO check flags */
207  }
208 
209  template <typename OtherDerived>
210  Derived& operator=(const ReturnByValue<OtherDerived>& other);
211 
212  template <typename OtherDerived>
213  inline Derived& operator=(const SparseMatrixBase<OtherDerived>& other);
214 
215  inline Derived& operator=(const Derived& other);
216 
217  protected:
218  template <typename OtherDerived>
219  inline Derived& assign(const OtherDerived& other);
220 
221  template <typename OtherDerived>
222  inline void assignGeneric(const OtherDerived& other);
223 
224  public:
225 #ifndef EIGEN_NO_IO
226  friend std::ostream& operator<<(std::ostream& s, const SparseMatrixBase& m) {
227  typedef typename Derived::Nested Nested;
228  typedef internal::remove_all_t<Nested> NestedCleaned;
229 
230  if (Flags & RowMajorBit) {
231  Nested nm(m.derived());
233  for (Index row = 0; row < nm.outerSize(); ++row) {
234  Index col = 0;
235  for (typename internal::evaluator<NestedCleaned>::InnerIterator it(thisEval, row); it; ++it) {
236  for (; col < it.index(); ++col) s << "0 ";
237  s << it.value() << " ";
238  ++col;
239  }
240  for (; col < m.cols(); ++col) s << "0 ";
241  s << std::endl;
242  }
243  } else {
244  Nested nm(m.derived());
246  if (m.cols() == 1) {
247  Index row = 0;
248  for (typename internal::evaluator<NestedCleaned>::InnerIterator it(thisEval, 0); it; ++it) {
249  for (; row < it.index(); ++row) s << "0" << std::endl;
250  s << it.value() << std::endl;
251  ++row;
252  }
253  for (; row < m.rows(); ++row) s << "0" << std::endl;
254  } else {
256  s << static_cast<const SparseMatrixBase<SparseMatrix<Scalar, RowMajorBit, StorageIndex> >&>(trans);
257  }
258  }
259  return s;
260  }
261 #endif
262 
263  template <typename OtherDerived>
265  template <typename OtherDerived>
267 
268  template <typename OtherDerived>
269  Derived& operator+=(const DiagonalBase<OtherDerived>& other);
270  template <typename OtherDerived>
271  Derived& operator-=(const DiagonalBase<OtherDerived>& other);
272 
273  template <typename OtherDerived>
274  Derived& operator+=(const EigenBase<OtherDerived>& other);
275  template <typename OtherDerived>
276  Derived& operator-=(const EigenBase<OtherDerived>& other);
277 
278  Derived& operator*=(const Scalar& other);
279  Derived& operator/=(const Scalar& other);
280 
281  template <typename OtherDerived>
283  typedef CwiseBinaryOp<
286  const Derived, const OtherDerived>
288  };
289 
290  template <typename OtherDerived>
292  const MatrixBase<OtherDerived>& other) const;
293 
294  // sparse * diagonal
295  template <typename OtherDerived>
298  }
299 
300  // diagonal * sparse
301  template <typename OtherDerived>
303  const SparseMatrixBase& rhs) {
304  return Product<OtherDerived, Derived>(lhs.derived(), rhs.derived());
305  }
306 
307  // sparse * sparse
308  template <typename OtherDerived>
310 
311  // sparse * dense
312  template <typename OtherDerived>
314  return Product<Derived, OtherDerived>(derived(), other.derived());
315  }
316 
317  // dense * sparse
318  template <typename OtherDerived>
320  const SparseMatrixBase& rhs) {
321  return Product<OtherDerived, Derived>(lhs.derived(), rhs.derived());
322  }
323 
328  }
329 
330  template <typename OtherDerived>
332 
333  template <int Mode>
335 
336  template <unsigned int UpLo>
339  };
340  template <unsigned int UpLo>
343  };
344 
345  template <unsigned int UpLo>
347  template <unsigned int UpLo>
349 
350  template <typename OtherDerived>
351  Scalar dot(const MatrixBase<OtherDerived>& other) const;
352  template <typename OtherDerived>
354  RealScalar squaredNorm() const;
355  RealScalar norm() const;
356  RealScalar blueNorm() const;
357 
361 
363 
364  template <typename OtherDerived>
365  bool isApprox(const SparseMatrixBase<OtherDerived>& other,
366  const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
367 
368  template <typename OtherDerived>
370  const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const {
371  return toDense().isApprox(other, prec);
372  }
373 
379  inline const typename internal::eval<Derived>::type eval() const {
380  return typename internal::eval<Derived>::type(derived());
381  }
382 
383  Scalar sum() const;
384 
385  inline const SparseView<Derived> pruned(const Scalar& reference = Scalar(0),
387 
388  protected:
390 
391  static inline StorageIndex convert_index(const Index idx) { return internal::convert_index<StorageIndex>(idx); }
392 
393  private:
394  template <typename Dest>
395  void evalTo(Dest&) const;
396 };
397 
398 } // end namespace Eigen
399 
400 #endif // EIGEN_SPARSEMATRIXBASE_H
#define EIGEN_STRONG_INLINE
Definition: Macros.h:834
m col(1)
m row(1)
SCALAR Scalar
Definition: bench_gemm.cpp:45
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:46
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:79
Generic expression of a matrix where all coefficients are defined by a functor.
Definition: CwiseNullaryOp.h:64
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:53
Base class for diagonal matrices and expressions.
Definition: DiagonalMatrix.h:33
EIGEN_DEVICE_FUNC const Derived & derived() const
Definition: DiagonalMatrix.h:57
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:202
Definition: ReturnByValue.h:50
Base class of any sparse matrices or sparse expressions.
Definition: SparseMatrixBase.h:30
Matrix< Scalar, Dynamic, 1 > ScalarVector
Definition: SparseMatrixBase.h:52
internal::traits< Derived >::StorageIndex StorageIndex
Definition: SparseMatrixBase.h:44
const Product< Derived, OtherDerived > operator*(const DiagonalBase< OtherDerived > &other) const
Definition: SparseMatrixBase.h:296
Index size() const
Definition: SparseMatrixBase.h:187
Index innerSize() const
Definition: SparseMatrixBase.h:198
Index rows() const
Definition: SparseMatrixBase.h:182
const Product< Derived, OtherDerived > operator*(const MatrixBase< OtherDerived > &other) const
Definition: SparseMatrixBase.h:313
bool isRValue() const
Definition: SparseMatrixBase.h:200
RealScalar blueNorm() const
Definition: SparseDot.h:94
Scalar dot(const SparseMatrixBase< OtherDerived > &other) const
void evalTo(Dest &) const
const ConstTransposeReturnType transpose() const
Definition: SparseMatrixBase.h:359
friend std::ostream & operator<<(std::ostream &s, const SparseMatrixBase &m)
Definition: SparseMatrixBase.h:226
Derived & const_cast_derived() const
Definition: SparseMatrixBase.h:146
Derived & operator=(const EigenBase< OtherDerived > &other)
Definition: SparseAssign.h:20
SparseMatrixBase()
Definition: SparseMatrixBase.h:206
bool m_isRValue
Definition: SparseMatrixBase.h:389
CwiseNullaryOp< internal::scalar_constant_op< Scalar >, Matrix< Scalar, Dynamic, Dynamic > > ConstantReturnType
Definition: SparseMatrixBase.h:135
internal::traits< Derived >::Scalar Scalar
Definition: SparseMatrixBase.h:32
Derived & operator-=(const SparseMatrixBase< OtherDerived > &other)
Derived & operator+=(const SparseMatrixBase< OtherDerived > &other)
Scalar dot(const MatrixBase< OtherDerived > &other) const
EigenBase< Derived > Base
Definition: SparseMatrixBase.h:148
void assignGeneric(const OtherDerived &other)
Derived & operator*=(const Scalar &other)
Definition: SparseCwiseUnaryOp.h:123
Transpose< Derived > TransposeReturnType
Definition: SparseMatrixBase.h:115
std::conditional_t< NumTraits< Scalar >::IsComplex, CwiseUnaryOp< internal::scalar_conjugate_op< Scalar >, Eigen::Transpose< const Derived > >, Transpose< const Derived > > AdjointReturnType
Definition: SparseMatrixBase.h:114
RealScalar squaredNorm() const
Definition: SparseDot.h:82
internal::packet_traits< Scalar >::type PacketScalar
Definition: SparseMatrixBase.h:39
friend const Product< OtherDerived, Derived > operator*(const MatrixBase< OtherDerived > &lhs, const SparseMatrixBase &rhs)
Definition: SparseMatrixBase.h:319
const TriangularView< const Derived, Mode > triangularView() const
Definition: SparseTriangularView.h:171
DenseMatrixType toDense() const
Definition: SparseMatrixBase.h:362
bool isApprox(const SparseMatrixBase< OtherDerived > &other, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: SparseFuzzy.h:20
Transpose< const Derived > ConstTransposeReturnType
Definition: SparseMatrixBase.h:116
SparseSymmetricPermutationProduct< Derived, Upper|Lower > twistedBy(const PermutationMatrix< Dynamic, Dynamic, StorageIndex > &perm) const
Definition: SparseMatrixBase.h:325
const internal::eval< Derived >::type eval() const
Definition: SparseMatrixBase.h:379
bool isVector() const
Definition: SparseMatrixBase.h:192
Matrix< Scalar, internal::max_size_prefer_dynamic(RowsAtCompileTime, ColsAtCompileTime), internal::max_size_prefer_dynamic(RowsAtCompileTime, ColsAtCompileTime)> SquareMatrixType
Definition: SparseMatrixBase.h:142
internal::traits< Derived >::StorageKind StorageKind
Definition: SparseMatrixBase.h:40
Derived & markAsRValue()
Definition: SparseMatrixBase.h:201
EIGEN_STRONG_INLINE const CwiseProductDenseReturnType< OtherDerived >::Type cwiseProduct(const MatrixBase< OtherDerived > &other) const
SparseMatrix< Scalar, Flags &RowMajorBit ? RowMajor :ColMajor, StorageIndex > PlainObject
Definition: SparseMatrixBase.h:119
SelfAdjointViewReturnType< UpLo >::Type selfadjointView()
const Derived & derived() const
Definition: SparseMatrixBase.h:144
bool isApprox(const MatrixBase< OtherDerived > &other, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: SparseMatrixBase.h:369
Derived & assign(const OtherDerived &other)
std::conditional_t< HasDirectAccess_, const Scalar &, Scalar > CoeffReturnType
Definition: SparseMatrixBase.h:132
SparseMatrixBase StorageBaseType
Definition: SparseMatrixBase.h:49
RealScalar norm() const
Definition: SparseDot.h:88
NumTraits< Scalar >::Real RealScalar
Definition: SparseMatrixBase.h:128
Derived & operator*=(const SparseMatrixBase< OtherDerived > &other)
internal::add_const_on_value_type_if_arithmetic< typename internal::packet_traits< Scalar >::type >::type PacketReturnType
Definition: SparseMatrixBase.h:47
TransposeReturnType transpose()
Definition: SparseMatrixBase.h:358
ConstSelfAdjointViewReturnType< UpLo >::Type selfadjointView() const
Scalar value_type
Definition: SparseMatrixBase.h:37
Index outerSize() const
Definition: SparseMatrixBase.h:195
const SparseView< Derived > pruned(const Scalar &reference=Scalar(0), const RealScalar &epsilon=NumTraits< Scalar >::dummy_precision()) const
Definition: SparseView.h:219
Index cols() const
Definition: SparseMatrixBase.h:184
const AdjointReturnType adjoint() const
Definition: SparseMatrixBase.h:360
Derived & operator/=(const Scalar &other)
Definition: SparseCwiseUnaryOp.h:132
@ InnerSizeAtCompileTime
Definition: SparseMatrixBase.h:101
@ IsVectorAtCompileTime
Definition: SparseMatrixBase.h:81
@ NumDimensions
Definition: SparseMatrixBase.h:87
@ ColsAtCompileTime
Definition: SparseMatrixBase.h:65
@ RowsAtCompileTime
Definition: SparseMatrixBase.h:59
@ MaxColsAtCompileTime
Definition: SparseMatrixBase.h:77
@ MaxRowsAtCompileTime
Definition: SparseMatrixBase.h:76
@ MaxSizeAtCompileTime
Definition: SparseMatrixBase.h:79
@ SizeAtCompileTime
Definition: SparseMatrixBase.h:71
@ HasDirectAccess_
Definition: SparseMatrixBase.h:106
@ IsRowMajor
Definition: SparseMatrixBase.h:99
Derived & derived()
Definition: SparseMatrixBase.h:145
Matrix< Scalar, RowsAtCompileTime, ColsAtCompileTime > DenseMatrixType
Definition: SparseMatrixBase.h:138
Matrix< StorageIndex, Dynamic, 1 > IndexVector
Definition: SparseMatrixBase.h:51
Scalar sum() const
Definition: SparseRedux.h:19
static StorageIndex convert_index(const Index idx)
Definition: SparseMatrixBase.h:391
friend const Product< OtherDerived, Derived > operator*(const DiagonalBase< OtherDerived > &lhs, const SparseMatrixBase &rhs)
Definition: SparseMatrixBase.h:302
A versatible sparse matrix representation.
Definition: SparseMatrix.h:121
Pseudo expression to manipulate a triangular sparse matrix as a selfadjoint matrix.
Definition: SparseSelfAdjointView.h:52
Definition: SparseSelfAdjointView.h:556
Expression of a dense or sparse matrix with zero or too small values removed.
Definition: SparseView.h:45
Expression of the transpose of a matrix.
Definition: Transpose.h:56
Expression of a triangular part in a matrix.
Definition: TriangularMatrix.h:167
@ IsComplex
Definition: common.h:73
const unsigned int DirectAccessBit
Definition: Constants.h:159
const unsigned int RowMajorBit
Definition: Constants.h:70
RealScalar s
Definition: level1_cplx_impl.h:130
return int(ret)+1
int * m
Definition: level2_cplx_impl.h:294
char * trans
Definition: level2_impl.h:240
constexpr int size_at_compile_time(int rows, int cols)
Definition: XprHelper.h:373
typename remove_all< T >::type remove_all_t
Definition: Meta.h:142
constexpr int max_size_prefer_dynamic(A a, B b)
Definition: Meta.h:695
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
Extend namespace for flags.
Definition: fsi_chan_precond_driver.cc:56
type
Definition: compute_granudrum_aor.py:141
double epsilon
Definition: osc_ring_sarah_asymptotics.h:43
Definition: EigenBase.h:33
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:43
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
Determines whether the given binary operation of two numeric types is allowed and what the scalar ret...
Definition: XprHelper.h:1043
const SparseSelfAdjointView< const Derived, UpLo > Type
Definition: SparseMatrixBase.h:342
CwiseBinaryOp< internal::scalar_product_op< typename ScalarBinaryOpTraits< typename internal::traits< Derived >::Scalar, typename internal::traits< OtherDerived >::Scalar >::ReturnType >, const Derived, const OtherDerived > Type
Definition: SparseMatrixBase.h:287
Definition: SparseMatrixBase.h:337
SparseSelfAdjointView< Derived, UpLo > Type
Definition: SparseMatrixBase.h:338
Definition: XprHelper.h:427
Definition: CoreEvaluators.h:104
Template functor to compute the product of two scalars.
Definition: BinaryFunctors.h:73
Definition: ForwardDeclarations.h:21