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 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
28 template<typename Derived> class SparseMatrixBase
29  : public EigenBase<Derived>
30 {
31  public:
32 
33  typedef typename internal::traits<Derived>::Scalar Scalar;
34 
38  typedef Scalar value_type;
39 
40  typedef typename internal::packet_traits<Scalar>::type PacketScalar;
41  typedef typename internal::traits<Derived>::StorageKind StorageKind;
42 
45  typedef typename internal::traits<Derived>::StorageIndex StorageIndex;
46 
47  typedef typename internal::add_const_on_value_type_if_arithmetic<
48  typename internal::packet_traits<Scalar>::type
50 
52 
55 
56  template<typename OtherDerived>
57  Derived& operator=(const EigenBase<OtherDerived> &other);
58 
59  enum {
60 
61  RowsAtCompileTime = internal::traits<Derived>::RowsAtCompileTime,
67  ColsAtCompileTime = internal::traits<Derived>::ColsAtCompileTime,
74  SizeAtCompileTime = (internal::size_of_xpr_at_compile_time<Derived>::ret),
81 
83 
95  Flags = internal::traits<Derived>::Flags,
101 
104 
105  #ifndef EIGEN_PARSED_BY_DOXYGEN
106  HasDirectAccess_ = (int(Flags)&DirectAccessBit) ? 1 : 0 // workaround sunCC
107  #endif
108  };
109 
111  typedef std::conditional_t<NumTraits<Scalar>::IsComplex,
117 
118  // FIXME storage order do not match evaluator storage order
120 
121 #ifndef EIGEN_PARSED_BY_DOXYGEN
128  typedef typename NumTraits<Scalar>::Real RealScalar;
129 
132  typedef std::conditional_t<HasDirectAccess_, const Scalar&, Scalar> CoeffReturnType;
133 
136 
142 
143  inline const Derived& derived() const { return *static_cast<const Derived*>(this); }
144  inline Derived& derived() { return *static_cast<Derived*>(this); }
145  inline Derived& const_cast_derived() const
146  { return *static_cast<Derived*>(const_cast<SparseMatrixBase*>(this)); }
147 
148  typedef EigenBase<Derived> Base;
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,OP)
155 #define EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL
156 #define EIGEN_DOC_BLOCK_ADDONS_INNER_PANEL_IF(COND)
157 #else
158 #define EIGEN_DOC_UNARY_ADDONS(X,Y)
159 #define EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL
160 #define EIGEN_DOC_BLOCK_ADDONS_INNER_PANEL_IF(COND)
161 #endif
162 # include "../plugins/CommonCwiseUnaryOps.h"
163 # include "../plugins/CommonCwiseBinaryOps.h"
164 # include "../plugins/MatrixCwiseUnaryOps.h"
165 # include "../plugins/MatrixCwiseBinaryOps.h"
166 # include "../plugins/BlockMethods.h"
167 # ifdef EIGEN_SPARSEMATRIXBASE_PLUGIN
168 # include EIGEN_SPARSEMATRIXBASE_PLUGIN
169 # endif
170 #undef EIGEN_CURRENT_STORAGE_BASE_CLASS
171 #undef EIGEN_DOC_UNARY_ADDONS
172 #undef EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL
173 #undef EIGEN_DOC_BLOCK_ADDONS_INNER_PANEL_IF
174 
176  inline Index rows() const { return derived().rows(); }
178  inline Index cols() const { return derived().cols(); }
181  inline Index size() const { return rows() * cols(); }
186  inline bool isVector() const { return rows()==1 || cols()==1; }
189  Index outerSize() const { return (int(Flags)&RowMajorBit) ? this->rows() : this->cols(); }
192  Index innerSize() const { return (int(Flags)&RowMajorBit) ? this->cols() : this->rows(); }
193 
194  bool isRValue() const { return m_isRValue; }
195  Derived& markAsRValue() { m_isRValue = true; return derived(); }
196 
197  SparseMatrixBase() : m_isRValue(false) { /* TODO check flags */ }
198 
199 
200  template<typename OtherDerived>
201  Derived& operator=(const ReturnByValue<OtherDerived>& other);
202 
203  template<typename OtherDerived>
204  inline Derived& operator=(const SparseMatrixBase<OtherDerived>& other);
205 
206  inline Derived& operator=(const Derived& other);
207 
208  protected:
209 
210  template<typename OtherDerived>
211  inline Derived& assign(const OtherDerived& other);
212 
213  template<typename OtherDerived>
214  inline void assignGeneric(const OtherDerived& other);
215 
216  public:
217 #ifndef EIGEN_NO_IO
218  friend std::ostream & operator << (std::ostream & s, const SparseMatrixBase& m)
219  {
220  typedef typename Derived::Nested Nested;
221  typedef internal::remove_all_t<Nested> NestedCleaned;
222 
223  if (Flags&RowMajorBit)
224  {
225  Nested nm(m.derived());
226  internal::evaluator<NestedCleaned> thisEval(nm);
227  for (Index row=0; row<nm.outerSize(); ++row)
228  {
229  Index col = 0;
230  for (typename internal::evaluator<NestedCleaned>::InnerIterator it(thisEval, row); it; ++it)
231  {
232  for ( ; col<it.index(); ++col)
233  s << "0 ";
234  s << it.value() << " ";
235  ++col;
236  }
237  for ( ; col<m.cols(); ++col)
238  s << "0 ";
239  s << std::endl;
240  }
241  }
242  else
243  {
244  Nested nm(m.derived());
245  internal::evaluator<NestedCleaned> thisEval(nm);
246  if (m.cols() == 1) {
247  Index row = 0;
248  for (typename internal::evaluator<NestedCleaned>::InnerIterator it(thisEval, 0); it; ++it)
249  {
250  for ( ; row<it.index(); ++row)
251  s << "0" << std::endl;
252  s << it.value() << std::endl;
253  ++row;
254  }
255  for ( ; row<m.rows(); ++row)
256  s << "0" << std::endl;
257  }
258  else
259  {
261  s << static_cast<const SparseMatrixBase<SparseMatrix<Scalar, RowMajorBit, StorageIndex> >&>(trans);
262  }
263  }
264  return s;
265  }
266 #endif
267 
268  template<typename OtherDerived>
269  Derived& operator+=(const SparseMatrixBase<OtherDerived>& other);
270  template<typename OtherDerived>
271  Derived& operator-=(const SparseMatrixBase<OtherDerived>& other);
272 
273  template<typename OtherDerived>
274  Derived& operator+=(const DiagonalBase<OtherDerived>& other);
275  template<typename OtherDerived>
276  Derived& operator-=(const DiagonalBase<OtherDerived>& other);
277 
278  template<typename OtherDerived>
279  Derived& operator+=(const EigenBase<OtherDerived> &other);
280  template<typename OtherDerived>
281  Derived& operator-=(const EigenBase<OtherDerived> &other);
282 
283  Derived& operator*=(const Scalar& other);
284  Derived& operator/=(const Scalar& other);
285 
286  template<typename OtherDerived> struct CwiseProductDenseReturnType {
287  typedef CwiseBinaryOp<internal::scalar_product_op<typename ScalarBinaryOpTraits<
288  typename internal::traits<Derived>::Scalar,
289  typename internal::traits<OtherDerived>::Scalar
290  >::ReturnType>,
291  const Derived,
292  const OtherDerived
293  > Type;
294  };
295 
296  template<typename OtherDerived>
297  EIGEN_STRONG_INLINE const typename CwiseProductDenseReturnType<OtherDerived>::Type
299 
300  // sparse * diagonal
301  template<typename OtherDerived>
304  { return Product<Derived,OtherDerived>(derived(), other.derived()); }
305 
306  // diagonal * sparse
307  template<typename OtherDerived> friend
310  { return Product<OtherDerived,Derived>(lhs.derived(), rhs.derived()); }
311 
312  // sparse * sparse
313  template<typename OtherDerived>
315  operator*(const SparseMatrixBase<OtherDerived> &other) const;
316 
317  // sparse * dense
318  template<typename OtherDerived>
321  { return Product<Derived,OtherDerived>(derived(), other.derived()); }
322 
323  // dense * sparse
324  template<typename OtherDerived> friend
327  { return Product<OtherDerived,Derived>(lhs.derived(), rhs.derived()); }
328 
331  {
333  }
334 
335  template<typename OtherDerived>
337 
338  template<int Mode>
340 
341  template<unsigned int UpLo> struct SelfAdjointViewReturnType { typedef SparseSelfAdjointView<Derived, UpLo> Type; };
342  template<unsigned int UpLo> struct ConstSelfAdjointViewReturnType { typedef const SparseSelfAdjointView<const Derived, UpLo> Type; };
343 
344  template<unsigned int UpLo> inline
346  template<unsigned int UpLo> inline
348 
349  template<typename OtherDerived> Scalar dot(const MatrixBase<OtherDerived>& other) const;
350  template<typename OtherDerived> Scalar dot(const SparseMatrixBase<OtherDerived>& other) const;
351  RealScalar squaredNorm() const;
352  RealScalar norm() const;
353  RealScalar blueNorm() const;
354 
358 
359  DenseMatrixType toDense() const
360  {
361  return DenseMatrixType(derived());
362  }
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 
378  inline const typename internal::eval<Derived>::type eval() const
379  { return typename internal::eval<Derived>::type(derived()); }
380 
381  Scalar sum() const;
382 
383  inline const SparseView<Derived>
384  pruned(const Scalar& reference = Scalar(0), const RealScalar& epsilon = NumTraits<Scalar>::dummy_precision()) const;
385 
386  protected:
387 
389 
390  static inline StorageIndex convert_index(const Index idx) {
391  return internal::convert_index<StorageIndex>(idx);
392  }
393  private:
394  template<typename Dest> void evalTo(Dest &) const;
395 };
396 
397 } // end namespace Eigen
398 
399 #endif // EIGEN_SPARSEMATRIXBASE_H
Matrix3f m
RowXpr row(Index i)
This is the const version of row(). *‍/.
ColXpr col(Index i)
This is the const version of col().
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:86
Generic expression of a matrix where all coefficients are defined by a functor.
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:58
Base class for diagonal matrices and expressions.
const Derived & derived() const
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:77
Base class of any sparse matrices or sparse expressions.
Derived & operator*=(const Scalar &other)
internal::traits< Derived >::StorageIndex StorageIndex
RealScalar blueNorm() const
Definition: SparseDot.h:94
Scalar dot(const SparseMatrixBase< OtherDerived > &other) const
void evalTo(Dest &) const
const ConstTransposeReturnType transpose() const
friend std::ostream & operator<<(std::ostream &s, const SparseMatrixBase &m)
Derived & operator=(const EigenBase< OtherDerived > &other)
Definition: SparseAssign.h:19
Derived & operator/=(const Scalar &other)
internal::traits< Derived >::Scalar Scalar
Scalar dot(const MatrixBase< OtherDerived > &other) const
const Product< Derived, OtherDerived > operator*(const MatrixBase< OtherDerived > &other) const
void assignGeneric(const OtherDerived &other)
Transpose< Derived > TransposeReturnType
SparseSymmetricPermutationProduct< Derived, Upper|Lower > twistedBy(const PermutationMatrix< Dynamic, Dynamic, StorageIndex > &perm) const
std::conditional_t< NumTraits< Scalar >::IsComplex, CwiseUnaryOp< internal::scalar_conjugate_op< Scalar >, Eigen::Transpose< const Derived > >, Transpose< const Derived > > AdjointReturnType
SparseMatrix< Scalar, Flags &RowMajorBit ? RowMajor :ColMajor, StorageIndex > PlainObject
RealScalar squaredNorm() const
Definition: SparseDot.h:79
Derived & operator-=(const SparseMatrixBase< OtherDerived > &other)
internal::packet_traits< Scalar >::type PacketScalar
const TriangularView< const Derived, Mode > triangularView() const
DenseMatrixType toDense() const
bool isApprox(const SparseMatrixBase< OtherDerived > &other, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: SparseFuzzy.h:19
Transpose< const Derived > ConstTransposeReturnType
const internal::eval< Derived >::type eval() const
Derived & operator+=(const SparseMatrixBase< OtherDerived > &other)
Matrix< StorageIndex, Dynamic, 1 > IndexVector
const CwiseProductDenseReturnType< OtherDerived >::Type cwiseProduct(const MatrixBase< OtherDerived > &other) const
internal::traits< Derived >::StorageKind StorageKind
friend const Product< OtherDerived, Derived > operator*(const DiagonalBase< OtherDerived > &lhs, const SparseMatrixBase &rhs)
SelfAdjointViewReturnType< UpLo >::Type selfadjointView()
bool isApprox(const MatrixBase< OtherDerived > &other, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Derived & assign(const OtherDerived &other)
SparseMatrixBase StorageBaseType
RealScalar norm() const
Definition: SparseDot.h:86
Derived & operator*=(const SparseMatrixBase< OtherDerived > &other)
friend const Product< OtherDerived, Derived > operator*(const MatrixBase< OtherDerived > &lhs, const SparseMatrixBase &rhs)
const Product< Derived, OtherDerived > operator*(const DiagonalBase< OtherDerived > &other) const
TransposeReturnType transpose()
ConstSelfAdjointViewReturnType< UpLo >::Type selfadjointView() const
const SparseView< Derived > pruned(const Scalar &reference=Scalar(0), const RealScalar &epsilon=NumTraits< Scalar >::dummy_precision()) const
Definition: SparseView.h:248
const AdjointReturnType adjoint() const
internal::add_const_on_value_type_if_arithmetic< typename internal::packet_traits< Scalar >::type >::type PacketReturnType
Scalar sum() const
Definition: SparseRedux.h:19
Matrix< Scalar, Dynamic, 1 > ScalarVector
static StorageIndex convert_index(const Index idx)
A versatible sparse matrix representation.
Definition: SparseMatrix.h:125
Pseudo expression to manipulate a triangular sparse matrix as a selfadjoint matrix.
Expression of a dense or sparse matrix with zero or too small values removed.
Definition: SparseView.h:48
Expression of the transpose of a matrix.
Definition: Transpose.h:56
Expression of a triangular part in a matrix.
const unsigned int DirectAccessBit
Definition: Constants.h:157
const unsigned int RowMajorBit
Definition: Constants.h:68
constexpr int size_at_compile_time(int rows, int cols)
Definition: XprHelper.h:313
typename remove_all< T >::type remove_all_t
Definition: Meta.h:119
constexpr int max_size_prefer_dynamic(A a, B b)
Definition: Meta.h:566
: InteropHeaders
Definition: Core:139
Derived & derived()
Definition: EigenBase.h:48
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:41
Derived & const_cast_derived() const
Definition: EigenBase.h:54
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:231
Determines whether the given binary operation of two numeric types is allowed and what the scalar ret...
Definition: XprHelper.h:856
const SparseSelfAdjointView< const Derived, UpLo > Type
CwiseBinaryOp< internal::scalar_product_op< typename ScalarBinaryOpTraits< typename internal::traits< Derived >::Scalar, typename internal::traits< OtherDerived >::Scalar >::ReturnType >, const Derived, const OtherDerived > Type
SparseSelfAdjointView< Derived, UpLo > Type