Eigen  3.2.91
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 : public EigenBase<Derived>
27 {
28  public:
29 
30  typedef typename internal::traits<Derived>::Scalar Scalar;
31 
35  typedef Scalar value_type;
36 
37  typedef typename internal::packet_traits<Scalar>::type PacketScalar;
38  typedef typename internal::traits<Derived>::StorageKind StorageKind;
39  typedef typename internal::traits<Derived>::StorageIndex StorageIndex;
40  typedef typename internal::add_const_on_value_type_if_arithmetic<
41  typename internal::packet_traits<Scalar>::type
42  >::type PacketReturnType;
43 
45  typedef EigenBase<Derived> Base;
48 
49  template<typename OtherDerived>
50  Derived& operator=(const EigenBase<OtherDerived> &other);
51 
52  enum {
53 
54  RowsAtCompileTime = internal::traits<Derived>::RowsAtCompileTime,
60  ColsAtCompileTime = internal::traits<Derived>::ColsAtCompileTime,
67  SizeAtCompileTime = (internal::size_at_compile_time<internal::traits<Derived>::RowsAtCompileTime,
68  internal::traits<Derived>::ColsAtCompileTime>::ret),
73  MaxRowsAtCompileTime = RowsAtCompileTime,
74  MaxColsAtCompileTime = ColsAtCompileTime,
75 
76  MaxSizeAtCompileTime = (internal::size_at_compile_time<MaxRowsAtCompileTime,
77  MaxColsAtCompileTime>::ret),
78 
85  Flags = internal::traits<Derived>::Flags,
90  IsRowMajor = Flags&RowMajorBit ? 1 : 0,
91 
92  InnerSizeAtCompileTime = int(IsVectorAtCompileTime) ? int(SizeAtCompileTime)
93  : int(IsRowMajor) ? int(ColsAtCompileTime) : int(RowsAtCompileTime),
94 
95  #ifndef EIGEN_PARSED_BY_DOXYGEN
96  _HasDirectAccess = (int(Flags)&DirectAccessBit) ? 1 : 0 // workaround sunCC
97  #endif
98  };
99 
101  typedef typename internal::conditional<NumTraits<Scalar>::IsComplex,
104  >::type AdjointReturnType;
106  typedef typename internal::add_const<Transpose<const Derived> >::type ConstTransposeReturnType;
107 
108  // FIXME storage order do not match evaluator storage order
110 
111 #ifndef EIGEN_PARSED_BY_DOXYGEN
112 
118  typedef typename NumTraits<Scalar>::Real RealScalar;
119 
122  typedef typename internal::conditional<_HasDirectAccess, const Scalar&, Scalar>::type CoeffReturnType;
123 
126 
130  typedef Matrix<Scalar,EIGEN_SIZE_MAX(RowsAtCompileTime,ColsAtCompileTime),
131  EIGEN_SIZE_MAX(RowsAtCompileTime,ColsAtCompileTime)> SquareMatrixType;
133  inline const Derived& derived() const { return *static_cast<const Derived*>(this); }
134  inline Derived& derived() { return *static_cast<Derived*>(this); }
135  inline Derived& const_cast_derived() const
136  { return *static_cast<Derived*>(const_cast<SparseMatrixBase*>(this)); }
137 #endif // not EIGEN_PARSED_BY_DOXYGEN
138 
139 #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::SparseMatrixBase
140 # include "../plugins/CommonCwiseUnaryOps.h"
141 # include "../plugins/CommonCwiseBinaryOps.h"
142 # include "../plugins/MatrixCwiseUnaryOps.h"
143 # include "../plugins/MatrixCwiseBinaryOps.h"
144 # include "../plugins/BlockMethods.h"
145 # ifdef EIGEN_SPARSEMATRIXBASE_PLUGIN
146 # include EIGEN_SPARSEMATRIXBASE_PLUGIN
147 # endif
148 # undef EIGEN_CURRENT_STORAGE_BASE_CLASS
149 #undef EIGEN_CURRENT_STORAGE_BASE_CLASS
152  inline Index rows() const { return derived().rows(); }
154  inline Index cols() const { return derived().cols(); }
157  inline Index size() const { return rows() * cols(); }
162  inline bool isVector() const { return rows()==1 || cols()==1; }
165  Index outerSize() const { return (int(Flags)&RowMajorBit) ? this->rows() : this->cols(); }
168  Index innerSize() const { return (int(Flags)&RowMajorBit) ? this->cols() : this->rows(); }
169 
170  bool isRValue() const { return m_isRValue; }
171  Derived& markAsRValue() { m_isRValue = true; return derived(); }
172 
173  SparseMatrixBase() : m_isRValue(false) { /* TODO check flags */ }
174 
175 
176  template<typename OtherDerived>
177  Derived& operator=(const ReturnByValue<OtherDerived>& other);
178 
179  template<typename OtherDerived>
180  inline Derived& operator=(const SparseMatrixBase<OtherDerived>& other);
181 
182  inline Derived& operator=(const Derived& other);
183 
184  protected:
186  template<typename OtherDerived>
187  inline Derived& assign(const OtherDerived& other);
188 
189  template<typename OtherDerived>
190  inline void assignGeneric(const OtherDerived& other);
191 
192  public:
193 
194  friend std::ostream & operator << (std::ostream & s, const SparseMatrixBase& m)
195  {
196  typedef typename Derived::Nested Nested;
197  typedef typename internal::remove_all<Nested>::type NestedCleaned;
198 
199  if (Flags&RowMajorBit)
200  {
201  const Nested nm(m.derived());
202  for (Index row=0; row<nm.outerSize(); ++row)
203  {
204  Index col = 0;
205  for (typename NestedCleaned::InnerIterator it(nm.derived(), row); it; ++it)
206  {
207  for ( ; col<it.index(); ++col)
208  s << "0 ";
209  s << it.value() << " ";
210  ++col;
211  }
212  for ( ; col<m.cols(); ++col)
213  s << "0 ";
214  s << std::endl;
215  }
216  }
217  else
218  {
219  const Nested nm(m.derived());
220  if (m.cols() == 1) {
221  Index row = 0;
222  for (typename NestedCleaned::InnerIterator it(nm.derived(), 0); it; ++it)
223  {
224  for ( ; row<it.index(); ++row)
225  s << "0" << std::endl;
226  s << it.value() << std::endl;
227  ++row;
228  }
229  for ( ; row<m.rows(); ++row)
230  s << "0" << std::endl;
231  }
232  else
233  {
234  SparseMatrix<Scalar, RowMajorBit, StorageIndex> trans = m;
235  s << static_cast<const SparseMatrixBase<SparseMatrix<Scalar, RowMajorBit, StorageIndex> >&>(trans);
236  }
237  }
238  return s;
239  }
240 
241  template<typename OtherDerived>
242  Derived& operator+=(const SparseMatrixBase<OtherDerived>& other);
243  template<typename OtherDerived>
244  Derived& operator-=(const SparseMatrixBase<OtherDerived>& other);
245 
246  template<typename OtherDerived>
247  Derived& operator+=(const DiagonalBase<OtherDerived>& other);
248  template<typename OtherDerived>
249  Derived& operator-=(const DiagonalBase<OtherDerived>& other);
251  Derived& operator*=(const Scalar& other);
252  Derived& operator/=(const Scalar& other);
253 
254  #define EIGEN_SPARSE_CWISE_PRODUCT_RETURN_TYPE \
255  CwiseBinaryOp< \
256  internal::scalar_product_op< \
257  typename internal::scalar_product_traits< \
258  typename internal::traits<Derived>::Scalar, \
259  typename internal::traits<OtherDerived>::Scalar \
260  >::ReturnType \
261  >, \
262  const Derived, \
263  const OtherDerived \
264  >
265 
266  template<typename OtherDerived>
267  EIGEN_STRONG_INLINE const EIGEN_SPARSE_CWISE_PRODUCT_RETURN_TYPE
268  cwiseProduct(const MatrixBase<OtherDerived> &other) const;
269 
270  // sparse * diagonal
271  template<typename OtherDerived>
273  operator*(const DiagonalBase<OtherDerived> &other) const
274  { return Product<Derived,OtherDerived>(derived(), other.derived()); }
275 
276  // diagonal * sparse
277  template<typename OtherDerived> friend
279  operator*(const DiagonalBase<OtherDerived> &lhs, const SparseMatrixBase& rhs)
280  { return Product<OtherDerived,Derived>(lhs.derived(), rhs.derived()); }
281 
282  // sparse * sparse
283  template<typename OtherDerived>
284  const Product<Derived,OtherDerived>
285  operator*(const SparseMatrixBase<OtherDerived> &other) const;
286 
287  // sparse * dense
288  template<typename OtherDerived>
289  const Product<Derived,OtherDerived>
290  operator*(const MatrixBase<OtherDerived> &other) const
291  { return Product<Derived,OtherDerived>(derived(), other.derived()); }
292 
293  // dense * sparse
294  template<typename OtherDerived> friend
295  const Product<OtherDerived,Derived>
296  operator*(const MatrixBase<OtherDerived> &lhs, const SparseMatrixBase& rhs)
297  { return Product<OtherDerived,Derived>(lhs.derived(), rhs.derived()); }
298 
300  SparseSymmetricPermutationProduct<Derived,Upper|Lower> twistedBy(const PermutationMatrix<Dynamic,Dynamic,StorageIndex>& perm) const
301  {
302  return SparseSymmetricPermutationProduct<Derived,Upper|Lower>(derived(), perm);
303  }
305  template<typename OtherDerived>
306  Derived& operator*=(const SparseMatrixBase<OtherDerived>& other);
307 
308  template<int Mode>
309  inline const TriangularView<const Derived, Mode> triangularView() const;
310 
311  template<unsigned int UpLo> struct SelfAdjointViewReturnType { typedef SparseSelfAdjointView<Derived, UpLo> Type; };
312  template<unsigned int UpLo> struct ConstSelfAdjointViewReturnType { typedef const SparseSelfAdjointView<const Derived, UpLo> Type; };
313 
314  template<unsigned int UpLo> inline
315  typename ConstSelfAdjointViewReturnType<UpLo>::Type selfadjointView() const;
316  template<unsigned int UpLo> inline
317  typename SelfAdjointViewReturnType<UpLo>::Type selfadjointView();
318 
319  template<typename OtherDerived> Scalar dot(const MatrixBase<OtherDerived>& other) const;
320  template<typename OtherDerived> Scalar dot(const SparseMatrixBase<OtherDerived>& other) const;
321  RealScalar squaredNorm() const;
322  RealScalar norm() const;
323  RealScalar blueNorm() const;
324 
325  TransposeReturnType transpose() { return TransposeReturnType(derived()); }
326  const ConstTransposeReturnType transpose() const { return ConstTransposeReturnType(derived()); }
327  const AdjointReturnType adjoint() const { return AdjointReturnType(transpose()); }
328 
329  // inner-vector
332  InnerVectorReturnType innerVector(Index outer);
333  const ConstInnerVectorReturnType innerVector(Index outer) const;
334 
335  // set of inner-vectors
338  InnerVectorsReturnType innerVectors(Index outerStart, Index outerSize);
339  const ConstInnerVectorsReturnType innerVectors(Index outerStart, Index outerSize) const;
340 
341  DenseMatrixType toDense() const
342  {
343  return DenseMatrixType(derived());
344  }
346  template<typename OtherDerived>
347  bool isApprox(const SparseMatrixBase<OtherDerived>& other,
348  const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
349 
350  template<typename OtherDerived>
351  bool isApprox(const MatrixBase<OtherDerived>& other,
352  const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const
353  { return toDense().isApprox(other,prec); }
354 
360  inline const typename internal::eval<Derived>::type eval() const
361  { return typename internal::eval<Derived>::type(derived()); }
362 
363  Scalar sum() const;
364 
365  inline const SparseView<Derived>
366  pruned(const Scalar& reference = Scalar(0), const RealScalar& epsilon = NumTraits<Scalar>::dummy_precision()) const;
367 
368  protected:
369 
370  bool m_isRValue;
371 
372  static inline StorageIndex convert_index(const Index idx) {
373  return internal::convert_index<StorageIndex>(idx);
374  }
375  private:
376  template<typename Dest> void evalTo(Dest &) const;
377 };
378 
379 } // end namespace Eigen
380 
381 #endif // EIGEN_SPARSEMATRIXBASE_H
Generic expression of a matrix where all coefficients are defined by a functor.
Definition: CwiseNullaryOp.h:44
Index size() const
Definition: SparseMatrixBase.h:157
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:107
A versatible sparse matrix representation.
Definition: SparseMatrix.h:92
Expression of the transpose of a matrix.
Definition: Transpose.h:53
const unsigned int DirectAccessBit
Definition: Constants.h:141
RowXpr row(Index i)
Definition: SparseMatrixBase.h:797
const CwiseBinaryOp< internal::scalar_product_op< typename Derived::Scalar, typename OtherDerived::Scalar >, const Derived, const OtherDerived > cwiseProduct(const Eigen::SparseMatrixBase< OtherDerived > &other) const
Definition: SparseMatrixBase.h:24
Definition: LDLT.h:16
Pseudo expression to manipulate a triangular sparse matrix as a selfadjoint matrix.
Definition: SparseSelfAdjointView.h:43
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:107
const SparseView< Derived > pruned(const Scalar &reference=Scalar(0), const RealScalar &epsilon=NumTraits< Scalar >::dummy_precision()) const
Definition: SparseView.h:213
Definition: SparseMatrixBase.h:67
Derived & derived()
Definition: EigenBase.h:44
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:37
const unsigned int RowMajorBit
Definition: Constants.h:53
Definition: SparseMatrixBase.h:54
Definition: EigenBase.h:28
Base class of any sparse matrices or sparse expressions.
Definition: SparseMatrixBase.h:26
Index outerSize() const
Definition: SparseMatrixBase.h:165
const internal::eval< Derived >::type eval() const
Definition: SparseMatrixBase.h:360
InnerVectorReturnType innerVector(Index outer)
Definition: SparseBlock.h:299
const ScalarMultipleReturnType operator*(const Scalar &scalar) const
Definition: SparseMatrixBase.h:57
Index cols() const
Definition: SparseMatrixBase.h:154
Scalar value_type
Definition: SparseMatrixBase.h:35
SparseSymmetricPermutationProduct< Derived, Upper|Lower > twistedBy(const PermutationMatrix< Dynamic, Dynamic, StorageIndex > &perm) const
Definition: SparseMatrixBase.h:300
Definition: SparseMatrixBase.h:79
InnerVectorsReturnType innerVectors(Index outerStart, Index outerSize)
Definition: SparseBlock.h:314
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:104
Expression of a triangular part in a matrix.
Definition: TriangularMatrix.h:186
Index rows() const
Definition: SparseMatrixBase.h:152
ColXpr col(Index i)
Definition: SparseMatrixBase.h:778
Definition: SparseMatrixBase.h:85
Index innerSize() const
Definition: SparseMatrixBase.h:168
Definition: SparseMatrixBase.h:60
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:56
bool isVector() const
Definition: SparseMatrixBase.h:162
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48