Please, help us to better know about our user community by answering the following short survey: https://forms.gle/wpyrxWi18ox9Z5ae9
Eigen  3.3.9
SparseMatrixBase.h
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 
31  typedef typename internal::traits<Derived>::Scalar Scalar;
32 
36  typedef Scalar value_type;
37 
38  typedef typename internal::packet_traits<Scalar>::type PacketScalar;
39  typedef typename internal::traits<Derived>::StorageKind StorageKind;
40 
43  typedef typename internal::traits<Derived>::StorageIndex StorageIndex;
44 
45  typedef typename internal::add_const_on_value_type_if_arithmetic<
46  typename internal::packet_traits<Scalar>::type
47  >::type PacketReturnType;
48 
50 
53 
54  template<typename OtherDerived>
55  Derived& operator=(const EigenBase<OtherDerived> &other);
56 
57  enum {
58 
59  RowsAtCompileTime = internal::traits<Derived>::RowsAtCompileTime,
65  ColsAtCompileTime = internal::traits<Derived>::ColsAtCompileTime,
72  SizeAtCompileTime = (internal::size_at_compile_time<internal::traits<Derived>::RowsAtCompileTime,
73  internal::traits<Derived>::ColsAtCompileTime>::ret),
78  MaxRowsAtCompileTime = RowsAtCompileTime,
79  MaxColsAtCompileTime = ColsAtCompileTime,
80 
81  MaxSizeAtCompileTime = (internal::size_at_compile_time<MaxRowsAtCompileTime,
82  MaxColsAtCompileTime>::ret),
83 
90  Flags = internal::traits<Derived>::Flags,
95  IsRowMajor = Flags&RowMajorBit ? 1 : 0,
96 
97  InnerSizeAtCompileTime = int(IsVectorAtCompileTime) ? int(SizeAtCompileTime)
98  : int(IsRowMajor) ? int(ColsAtCompileTime) : int(RowsAtCompileTime),
99 
100  #ifndef EIGEN_PARSED_BY_DOXYGEN
101  _HasDirectAccess = (int(Flags)&DirectAccessBit) ? 1 : 0 // workaround sunCC
102  #endif
103  };
104 
106  typedef typename internal::conditional<NumTraits<Scalar>::IsComplex,
109  >::type AdjointReturnType;
111  typedef typename internal::add_const<Transpose<const Derived> >::type ConstTransposeReturnType;
112 
113  // FIXME storage order do not match evaluator storage order
115 
116 #ifndef EIGEN_PARSED_BY_DOXYGEN
117 
123  typedef typename NumTraits<Scalar>::Real RealScalar;
124 
127  typedef typename internal::conditional<_HasDirectAccess, const Scalar&, Scalar>::type CoeffReturnType;
128 
131 
135  typedef Matrix<Scalar,EIGEN_SIZE_MAX(RowsAtCompileTime,ColsAtCompileTime),
136  EIGEN_SIZE_MAX(RowsAtCompileTime,ColsAtCompileTime)> SquareMatrixType;
137 
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 
143  typedef EigenBase<Derived> Base;
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:
204 
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:
212 
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());
221  internal::evaluator<NestedCleaned> thisEval(nm);
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());
240  internal::evaluator<NestedCleaned> thisEval(nm);
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  {
255  SparseMatrix<Scalar, RowMajorBit, StorageIndex> trans = m;
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);
271 
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 {
281  typedef CwiseBinaryOp<internal::scalar_product_op<typename ScalarBinaryOpTraits<
282  typename internal::traits<Derived>::Scalar,
283  typename internal::traits<OtherDerived>::Scalar
284  >::ReturnType>,
285  const Derived,
286  const OtherDerived
287  > Type;
288  };
289 
290  template<typename OtherDerived>
291  EIGEN_STRONG_INLINE const typename CwiseProductDenseReturnType<OtherDerived>::Type
292  cwiseProduct(const MatrixBase<OtherDerived> &other) const;
293 
294  // sparse * diagonal
295  template<typename OtherDerived>
296  const Product<Derived,OtherDerived>
297  operator*(const DiagonalBase<OtherDerived> &other) const
298  { return Product<Derived,OtherDerived>(derived(), other.derived()); }
299 
300  // diagonal * sparse
301  template<typename OtherDerived> friend
302  const Product<OtherDerived,Derived>
303  operator*(const DiagonalBase<OtherDerived> &lhs, const SparseMatrixBase& rhs)
304  { return Product<OtherDerived,Derived>(lhs.derived(), rhs.derived()); }
305 
306  // sparse * sparse
307  template<typename OtherDerived>
308  const Product<Derived,OtherDerived,AliasFreeProduct>
309  operator*(const SparseMatrixBase<OtherDerived> &other) const;
310 
311  // sparse * dense
312  template<typename OtherDerived>
313  const Product<Derived,OtherDerived>
314  operator*(const MatrixBase<OtherDerived> &other) const
315  { return Product<Derived,OtherDerived>(derived(), other.derived()); }
316 
317  // dense * sparse
318  template<typename OtherDerived> friend
319  const Product<OtherDerived,Derived>
320  operator*(const MatrixBase<OtherDerived> &lhs, const SparseMatrixBase& rhs)
321  { return Product<OtherDerived,Derived>(lhs.derived(), rhs.derived()); }
322 
324  SparseSymmetricPermutationProduct<Derived,Upper|Lower> twistedBy(const PermutationMatrix<Dynamic,Dynamic,StorageIndex>& perm) const
325  {
326  return SparseSymmetricPermutationProduct<Derived,Upper|Lower>(derived(), perm);
327  }
328 
329  template<typename OtherDerived>
330  Derived& operator*=(const SparseMatrixBase<OtherDerived>& other);
331 
332  template<int Mode>
333  inline const TriangularView<const Derived, Mode> triangularView() const;
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
339  typename ConstSelfAdjointViewReturnType<UpLo>::Type selfadjointView() const;
340  template<unsigned int UpLo> inline
341  typename SelfAdjointViewReturnType<UpLo>::Type selfadjointView();
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
354  typedef Block<Derived,IsRowMajor?1:Dynamic,IsRowMajor?Dynamic:1,true> InnerVectorReturnType;
355  typedef Block<const Derived,IsRowMajor?1:Dynamic,IsRowMajor?Dynamic:1,true> ConstInnerVectorReturnType;
356  InnerVectorReturnType innerVector(Index outer);
357  const ConstInnerVectorReturnType innerVector(Index outer) const;
358 
359  // set of inner-vectors
360  typedef Block<Derived,Dynamic,Dynamic,true> InnerVectorsReturnType;
361  typedef Block<const Derived,Dynamic,Dynamic,true> ConstInnerVectorsReturnType;
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>
375  bool isApprox(const MatrixBase<OtherDerived>& other,
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 
394  bool m_isRValue;
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
Eigen::SparseMatrixBase::innerSize
Index innerSize() const
Definition: SparseMatrixBase.h:187
Eigen::SparseMatrixBase::cols
Index cols() const
Definition: SparseMatrixBase.h:173
Eigen::SparseMatrixBase::innerVectors
InnerVectorsReturnType innerVectors(Index outerStart, Index outerSize)
Definition: SparseBlock.h:348
Eigen
Namespace containing all symbols from the Eigen library.
Definition: Core:309
Eigen::SparseMatrix
A versatible sparse matrix representation.
Definition: SparseMatrix.h:98
Eigen::EigenBase< CwiseBinaryOp< BinaryOp, Lhs, Rhs > >::Index
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:38
Eigen::SparseMatrixBase::value_type
Scalar value_type
Definition: SparseMatrixBase.h:36
Eigen::SparseMatrixBase::size
Index size() const
Definition: SparseMatrixBase.h:176
Eigen::EigenBase
Definition: EigenBase.h:30
Eigen::SparseMatrixBase::ColsAtCompileTime
@ ColsAtCompileTime
Definition: SparseMatrixBase.h:65
Eigen::RowMajorBit
const unsigned int RowMajorBit
Definition: Constants.h:61
Eigen::SparseMatrixBase::rows
Index rows() const
Definition: SparseMatrixBase.h:171
Eigen::SparseMatrixBase::StorageIndex
internal::traits< Derived >::StorageIndex StorageIndex
Definition: SparseMatrixBase.h:43
Eigen::Transpose
Expression of the transpose of a matrix.
Definition: Transpose.h:54
Eigen::DirectAccessBit
const unsigned int DirectAccessBit
Definition: Constants.h:150
Eigen::CwiseNullaryOp
Generic expression of a matrix where all coefficients are defined by a functor.
Definition: CwiseNullaryOp.h:61
Eigen::SparseMatrixBase::isVector
bool isVector() const
Definition: SparseMatrixBase.h:181
Eigen::SparseMatrixBase::RowsAtCompileTime
@ RowsAtCompileTime
Definition: SparseMatrixBase.h:59
Eigen::SparseMatrixBase::innerVector
InnerVectorReturnType innerVector(Index outer)
Definition: SparseBlock.h:333
Eigen::Diagonal
Expression of a diagonal/subdiagonal/superdiagonal in a matrix.
Definition: Diagonal.h:65
Eigen::SparseMatrixBase::SizeAtCompileTime
@ SizeAtCompileTime
Definition: SparseMatrixBase.h:72
Eigen::SparseMatrixBase::eval
const internal::eval< Derived >::type eval() const
Definition: SparseMatrixBase.h:384
Eigen::SparseMatrixBase::pruned
const SparseView< Derived > pruned(const Scalar &reference=Scalar(0), const RealScalar &epsilon=NumTraits< Scalar >::dummy_precision()) const
Definition: SparseView.h:246
Eigen::SparseMatrixBase::twistedBy
SparseSymmetricPermutationProduct< Derived, Upper|Lower > twistedBy(const PermutationMatrix< Dynamic, Dynamic, StorageIndex > &perm) const
Definition: SparseMatrixBase.h:324
Eigen::EigenBase::derived
Derived & derived()
Definition: EigenBase.h:45
Eigen::SparseMatrixBase::outerSize
Index outerSize() const
Definition: SparseMatrixBase.h:184
Eigen::CwiseUnaryOp
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:56
Eigen::PermutationMatrix< Dynamic, Dynamic, StorageIndex >
Eigen::SparseMatrixBase::IsVectorAtCompileTime
@ IsVectorAtCompileTime
Definition: SparseMatrixBase.h:84
Eigen::SparseView
Expression of a dense or sparse matrix with zero or too small values removed.
Definition: SparseView.h:46
Eigen::SparseMatrixBase
Base class of any sparse matrices or sparse expressions.
Definition: SparseMatrixBase.h:28
Eigen::SparseSelfAdjointView
Pseudo expression to manipulate a triangular sparse matrix as a selfadjoint matrix.
Definition: SparseSelfAdjointView.h:45
Eigen::Matrix< StorageIndex, Dynamic, 1 >
Eigen::TriangularView
Expression of a triangular part in a matrix.
Definition: TriangularMatrix.h:188
Eigen::SparseMatrixBase::Flags
@ Flags
Definition: SparseMatrixBase.h:90
Eigen::NumTraits
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:151
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33