Eigen  3.2.93
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  typedef typename internal::traits<Derived>::StorageIndex StorageIndex;
41  typedef typename internal::add_const_on_value_type_if_arithmetic<
42  typename internal::packet_traits<Scalar>::type
43  >::type PacketReturnType;
44 
46 
49 
50  template<typename OtherDerived>
51  Derived& operator=(const EigenBase<OtherDerived> &other);
52 
53  enum {
54 
55  RowsAtCompileTime = internal::traits<Derived>::RowsAtCompileTime,
61  ColsAtCompileTime = internal::traits<Derived>::ColsAtCompileTime,
68  SizeAtCompileTime = (internal::size_at_compile_time<internal::traits<Derived>::RowsAtCompileTime,
69  internal::traits<Derived>::ColsAtCompileTime>::ret),
74  MaxRowsAtCompileTime = RowsAtCompileTime,
75  MaxColsAtCompileTime = ColsAtCompileTime,
76 
77  MaxSizeAtCompileTime = (internal::size_at_compile_time<MaxRowsAtCompileTime,
78  MaxColsAtCompileTime>::ret),
79 
86  Flags = internal::traits<Derived>::Flags,
91  IsRowMajor = Flags&RowMajorBit ? 1 : 0,
92 
93  InnerSizeAtCompileTime = int(IsVectorAtCompileTime) ? int(SizeAtCompileTime)
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  };
100 
102  typedef typename internal::conditional<NumTraits<Scalar>::IsComplex,
105  >::type AdjointReturnType;
107  typedef typename internal::add_const<Transpose<const Derived> >::type ConstTransposeReturnType;
108 
109  // FIXME storage order do not match evaluator storage order
111 
112 #ifndef EIGEN_PARSED_BY_DOXYGEN
119  typedef typename NumTraits<Scalar>::Real RealScalar;
120 
123  typedef typename internal::conditional<_HasDirectAccess, const Scalar&, Scalar>::type CoeffReturnType;
124 
127 
131  typedef Matrix<Scalar,EIGEN_SIZE_MAX(RowsAtCompileTime,ColsAtCompileTime),
132  EIGEN_SIZE_MAX(RowsAtCompileTime,ColsAtCompileTime)> SquareMatrixType;
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 
139  typedef EigenBase<Derived> Base;
140 
141 #endif // not EIGEN_PARSED_BY_DOXYGEN
142 
143 #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::SparseMatrixBase
144 # include "../plugins/CommonCwiseUnaryOps.h"
145 # include "../plugins/CommonCwiseBinaryOps.h"
146 # include "../plugins/MatrixCwiseUnaryOps.h"
147 # include "../plugins/MatrixCwiseBinaryOps.h"
148 # include "../plugins/BlockMethods.h"
149 # ifdef EIGEN_SPARSEMATRIXBASE_PLUGIN
150 # include EIGEN_SPARSEMATRIXBASE_PLUGIN
151 # endif
152 # undef EIGEN_CURRENT_STORAGE_BASE_CLASS
153 #undef EIGEN_CURRENT_STORAGE_BASE_CLASS
154 
156  inline Index rows() const { return derived().rows(); }
158  inline Index cols() const { return derived().cols(); }
161  inline Index size() const { return rows() * cols(); }
166  inline bool isVector() const { return rows()==1 || cols()==1; }
169  Index outerSize() const { return (int(Flags)&RowMajorBit) ? this->rows() : this->cols(); }
172  Index innerSize() const { return (int(Flags)&RowMajorBit) ? this->cols() : this->rows(); }
173 
174  bool isRValue() const { return m_isRValue; }
175  Derived& markAsRValue() { m_isRValue = true; return derived(); }
177  SparseMatrixBase() : m_isRValue(false) { /* TODO check flags */ }
178 
179 
180  template<typename OtherDerived>
181  Derived& operator=(const ReturnByValue<OtherDerived>& other);
182 
183  template<typename OtherDerived>
184  inline Derived& operator=(const SparseMatrixBase<OtherDerived>& other);
185 
186  inline Derived& operator=(const Derived& other);
187 
188  protected:
189 
190  template<typename OtherDerived>
191  inline Derived& assign(const OtherDerived& other);
193  template<typename OtherDerived>
194  inline void assignGeneric(const OtherDerived& other);
195 
196  public:
197 
198  friend std::ostream & operator << (std::ostream & s, const SparseMatrixBase& m)
199  {
200  typedef typename Derived::Nested Nested;
201  typedef typename internal::remove_all<Nested>::type NestedCleaned;
202 
203  if (Flags&RowMajorBit)
204  {
205  const Nested nm(m.derived());
206  for (Index row=0; row<nm.outerSize(); ++row)
207  {
208  Index col = 0;
209  for (typename NestedCleaned::InnerIterator it(nm.derived(), row); it; ++it)
210  {
211  for ( ; col<it.index(); ++col)
212  s << "0 ";
213  s << it.value() << " ";
214  ++col;
215  }
216  for ( ; col<m.cols(); ++col)
217  s << "0 ";
218  s << std::endl;
219  }
220  }
221  else
222  {
223  const Nested nm(m.derived());
224  if (m.cols() == 1) {
225  Index row = 0;
226  for (typename NestedCleaned::InnerIterator it(nm.derived(), 0); it; ++it)
227  {
228  for ( ; row<it.index(); ++row)
229  s << "0" << std::endl;
230  s << it.value() << std::endl;
231  ++row;
232  }
233  for ( ; row<m.rows(); ++row)
234  s << "0" << std::endl;
235  }
236  else
237  {
239  s << static_cast<const SparseMatrixBase<SparseMatrix<Scalar, RowMajorBit, StorageIndex> >&>(trans);
240  }
241  }
242  return s;
243  }
244 
245  template<typename OtherDerived>
246  Derived& operator+=(const SparseMatrixBase<OtherDerived>& other);
247  template<typename OtherDerived>
248  Derived& operator-=(const SparseMatrixBase<OtherDerived>& other);
249 
250  template<typename OtherDerived>
251  Derived& operator+=(const DiagonalBase<OtherDerived>& other);
252  template<typename OtherDerived>
253  Derived& operator-=(const DiagonalBase<OtherDerived>& other);
254 
255  Derived& operator*=(const Scalar& other);
256  Derived& operator/=(const Scalar& other);
257 
258  template<typename OtherDerived> struct CwiseProductDenseReturnType {
259  typedef CwiseBinaryOp<internal::scalar_product_op<typename ScalarBinaryOpTraits<
260  typename internal::traits<Derived>::Scalar,
261  typename internal::traits<OtherDerived>::Scalar
262  >::ReturnType>,
263  const Derived,
264  const OtherDerived
265  > Type;
266  };
267 
268  template<typename OtherDerived>
269  EIGEN_STRONG_INLINE const typename CwiseProductDenseReturnType<OtherDerived>::Type
270  cwiseProduct(const MatrixBase<OtherDerived> &other) const;
272  // sparse * diagonal
273  template<typename OtherDerived>
275  operator*(const DiagonalBase<OtherDerived> &other) const
276  { return Product<Derived,OtherDerived>(derived(), other.derived()); }
277 
278  // diagonal * sparse
279  template<typename OtherDerived> friend
281  operator*(const DiagonalBase<OtherDerived> &lhs, const SparseMatrixBase& rhs)
282  { return Product<OtherDerived,Derived>(lhs.derived(), rhs.derived()); }
283 
284  // sparse * sparse
285  template<typename OtherDerived>
287  operator*(const SparseMatrixBase<OtherDerived> &other) const;
288 
289  // sparse * dense
290  template<typename OtherDerived>
292  operator*(const MatrixBase<OtherDerived> &other) const
293  { return Product<Derived,OtherDerived>(derived(), other.derived()); }
294 
295  // dense * sparse
296  template<typename OtherDerived> friend
298  operator*(const MatrixBase<OtherDerived> &lhs, const SparseMatrixBase& rhs)
299  { return Product<OtherDerived,Derived>(lhs.derived(), rhs.derived()); }
300 
302  SparseSymmetricPermutationProduct<Derived,Upper|Lower> twistedBy(const PermutationMatrix<Dynamic,Dynamic,StorageIndex>& perm) const
303  {
304  return SparseSymmetricPermutationProduct<Derived,Upper|Lower>(derived(), perm);
305  }
306 
307  template<typename OtherDerived>
308  Derived& operator*=(const SparseMatrixBase<OtherDerived>& other);
310  template<int Mode>
311  inline const TriangularView<const Derived, Mode> triangularView() const;
312 
313  template<unsigned int UpLo> struct SelfAdjointViewReturnType { typedef SparseSelfAdjointView<Derived, UpLo> Type; };
314  template<unsigned int UpLo> struct ConstSelfAdjointViewReturnType { typedef const SparseSelfAdjointView<const Derived, UpLo> Type; };
315 
316  template<unsigned int UpLo> inline
317  typename ConstSelfAdjointViewReturnType<UpLo>::Type selfadjointView() const;
318  template<unsigned int UpLo> inline
319  typename SelfAdjointViewReturnType<UpLo>::Type selfadjointView();
320 
321  template<typename OtherDerived> Scalar dot(const MatrixBase<OtherDerived>& other) const;
322  template<typename OtherDerived> Scalar dot(const SparseMatrixBase<OtherDerived>& other) const;
323  RealScalar squaredNorm() const;
324  RealScalar norm() const;
325  RealScalar blueNorm() const;
326 
327  TransposeReturnType transpose() { return TransposeReturnType(derived()); }
328  const ConstTransposeReturnType transpose() const { return ConstTransposeReturnType(derived()); }
329  const AdjointReturnType adjoint() const { return AdjointReturnType(transpose()); }
330 
331  // inner-vector
334  InnerVectorReturnType innerVector(Index outer);
335  const ConstInnerVectorReturnType innerVector(Index outer) const;
336 
337  // set of inner-vectors
340  InnerVectorsReturnType innerVectors(Index outerStart, Index outerSize);
341  const ConstInnerVectorsReturnType innerVectors(Index outerStart, Index outerSize) const;
342 
343  DenseMatrixType toDense() const
344  {
345  return DenseMatrixType(derived());
346  }
347 
348  template<typename OtherDerived>
349  bool isApprox(const SparseMatrixBase<OtherDerived>& other,
350  const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
351 
352  template<typename OtherDerived>
353  bool isApprox(const MatrixBase<OtherDerived>& other,
354  const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const
355  { return toDense().isApprox(other,prec); }
356 
362  inline const typename internal::eval<Derived>::type eval() const
363  { return typename internal::eval<Derived>::type(derived()); }
364 
365  Scalar sum() const;
366 
367  inline const SparseView<Derived>
368  pruned(const Scalar& reference = Scalar(0), const RealScalar& epsilon = NumTraits<Scalar>::dummy_precision()) const;
369 
370  protected:
371 
372  bool m_isRValue;
373 
374  static inline StorageIndex convert_index(const Index idx) {
375  return internal::convert_index<StorageIndex>(idx);
376  }
377  private:
378  template<typename Dest> void evalTo(Dest &) const;
379 };
380 
381 } // end namespace Eigen
382 
383 #endif // EIGEN_SPARSEMATRIXBASE_H
Generic expression of a matrix where all coefficients are defined by a functor.
Definition: CwiseNullaryOp.h:43
Index innerSize() const
Definition: SparseMatrixBase.h:172
Index cols() const
Definition: SparseMatrixBase.h:158
Definition: SparseMatrixBase.h:80
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:71
A versatible sparse matrix representation.
Definition: SparseMatrix.h:92
Definition: SparseMatrixBase.h:55
Expression of the transpose of a matrix.
Definition: Transpose.h:52
const unsigned int DirectAccessBit
Definition: Constants.h:150
RowXpr row(Index i)
Definition: SparseMatrixBase.h:802
Namespace containing all symbols from the Eigen library.
Definition: Core:271
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:167
Index outerSize() const
Definition: SparseMatrixBase.h:169
Definition: SparseMatrixBase.h:86
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:61
bool isVector() const
Definition: SparseMatrixBase.h:166
Definition: EigenBase.h:28
Index size() const
Definition: SparseMatrixBase.h:161
const SparseView< Derived > pruned(const Scalar &reference=Scalar(0), const RealScalar &epsilon=NumTraits< Scalar >::dummy_precision()) const
Definition: SparseView.h:214
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:77
Index rows() const
Definition: SparseMatrixBase.h:156
Base class of any sparse matrices or sparse expressions.
Definition: ForwardDeclarations.h:281
const CwiseBinaryOp< internal::scalar_product_op< Scalar, T >, Derived, Constant< T > > operator*(const T &scalar) const
InnerVectorReturnType innerVector(Index outer)
Definition: SparseBlock.h:331
Scalar value_type
Definition: SparseMatrixBase.h:36
Definition: SparseMatrixBase.h:61
InnerVectorsReturnType innerVectors(Index outerStart, Index outerSize)
Definition: SparseBlock.h:346
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:103
Expression of a triangular part in a matrix.
Definition: TriangularMatrix.h:186
Definition: SparseMatrixBase.h:68
SparseSymmetricPermutationProduct< Derived, Upper|Lower > twistedBy(const PermutationMatrix< Dynamic, Dynamic, StorageIndex > &perm) const
Definition: SparseMatrixBase.h:302
Determines whether the given binary operation of two numeric types is allowed and what the scalar ret...
Definition: XprHelper.h:741
const internal::eval< Derived >::type eval() const
Definition: SparseMatrixBase.h:362
ColXpr col(Index i)
Definition: SparseMatrixBase.h:783
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:55
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48