Eigen  3.2.92
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 #ifndef EIGEN_PARSED_BY_DOXYGEN
28  : public internal::special_scalar_op_base<Derived,typename internal::traits<Derived>::Scalar,
29  typename NumTraits<typename internal::traits<Derived>::Scalar>::Real,
30  EigenBase<Derived> >
31 #else
32  : public EigenBase<Derived>
33 #endif // not EIGEN_PARSED_BY_DOXYGEN
34 {
35  public:
36 
37  typedef typename internal::traits<Derived>::Scalar Scalar;
38 
42  typedef Scalar value_type;
43 
44  typedef typename internal::packet_traits<Scalar>::type PacketScalar;
45  typedef typename internal::traits<Derived>::StorageKind StorageKind;
46  typedef typename internal::traits<Derived>::StorageIndex StorageIndex;
47  typedef typename internal::add_const_on_value_type_if_arithmetic<
48  typename internal::packet_traits<Scalar>::type
49  >::type PacketReturnType;
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_at_compile_time<internal::traits<Derived>::RowsAtCompileTime,
75  internal::traits<Derived>::ColsAtCompileTime>::ret),
80  MaxRowsAtCompileTime = RowsAtCompileTime,
81  MaxColsAtCompileTime = ColsAtCompileTime,
82 
83  MaxSizeAtCompileTime = (internal::size_at_compile_time<MaxRowsAtCompileTime,
84  MaxColsAtCompileTime>::ret),
85 
92  Flags = internal::traits<Derived>::Flags,
97  IsRowMajor = Flags&RowMajorBit ? 1 : 0,
98 
99  InnerSizeAtCompileTime = int(IsVectorAtCompileTime) ? int(SizeAtCompileTime)
100  : int(IsRowMajor) ? int(ColsAtCompileTime) : int(RowsAtCompileTime),
101 
102  #ifndef EIGEN_PARSED_BY_DOXYGEN
103  _HasDirectAccess = (int(Flags)&DirectAccessBit) ? 1 : 0 // workaround sunCC
104  #endif
105  };
106 
108  typedef typename internal::conditional<NumTraits<Scalar>::IsComplex,
111  >::type AdjointReturnType;
113  typedef typename internal::add_const<Transpose<const Derived> >::type ConstTransposeReturnType;
114 
115  // FIXME storage order do not match evaluator storage order
117 
118 #ifndef EIGEN_PARSED_BY_DOXYGEN
119 
125  typedef typename NumTraits<Scalar>::Real RealScalar;
126 
129  typedef typename internal::conditional<_HasDirectAccess, const Scalar&, Scalar>::type CoeffReturnType;
130 
133 
137  typedef Matrix<Scalar,EIGEN_SIZE_MAX(RowsAtCompileTime,ColsAtCompileTime),
138  EIGEN_SIZE_MAX(RowsAtCompileTime,ColsAtCompileTime)> SquareMatrixType;
140  inline const Derived& derived() const { return *static_cast<const Derived*>(this); }
141  inline Derived& derived() { return *static_cast<Derived*>(this); }
142  inline Derived& const_cast_derived() const
143  { return *static_cast<Derived*>(const_cast<SparseMatrixBase*>(this)); }
144 
145  typedef internal::special_scalar_op_base<Derived, Scalar, RealScalar, EigenBase<Derived> > Base;
146  using Base::operator*;
147  using Base::operator/;
148 #endif // not EIGEN_PARSED_BY_DOXYGEN
149 
150 #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::SparseMatrixBase
151 # include "../plugins/CommonCwiseUnaryOps.h"
152 # include "../plugins/CommonCwiseBinaryOps.h"
153 # include "../plugins/MatrixCwiseUnaryOps.h"
154 # include "../plugins/MatrixCwiseBinaryOps.h"
155 # include "../plugins/BlockMethods.h"
156 # ifdef EIGEN_SPARSEMATRIXBASE_PLUGIN
157 # include EIGEN_SPARSEMATRIXBASE_PLUGIN
158 # endif
159 # undef EIGEN_CURRENT_STORAGE_BASE_CLASS
160 #undef EIGEN_CURRENT_STORAGE_BASE_CLASS
161 
163  inline Index rows() const { return derived().rows(); }
165  inline Index cols() const { return derived().cols(); }
168  inline Index size() const { return rows() * cols(); }
173  inline bool isVector() const { return rows()==1 || cols()==1; }
176  Index outerSize() const { return (int(Flags)&RowMajorBit) ? this->rows() : this->cols(); }
179  Index innerSize() const { return (int(Flags)&RowMajorBit) ? this->cols() : this->rows(); }
180 
181  bool isRValue() const { return m_isRValue; }
182  Derived& markAsRValue() { m_isRValue = true; return derived(); }
183 
184  SparseMatrixBase() : m_isRValue(false) { /* TODO check flags */ }
186 
187  template<typename OtherDerived>
188  Derived& operator=(const ReturnByValue<OtherDerived>& other);
189 
190  template<typename OtherDerived>
191  inline Derived& operator=(const SparseMatrixBase<OtherDerived>& other);
193  inline Derived& operator=(const Derived& other);
194 
195  protected:
196 
197  template<typename OtherDerived>
198  inline Derived& assign(const OtherDerived& other);
199 
200  template<typename OtherDerived>
201  inline void assignGeneric(const OtherDerived& other);
202 
203  public:
204 
205  friend std::ostream & operator << (std::ostream & s, const SparseMatrixBase& m)
206  {
207  typedef typename Derived::Nested Nested;
208  typedef typename internal::remove_all<Nested>::type NestedCleaned;
209 
210  if (Flags&RowMajorBit)
211  {
212  const Nested nm(m.derived());
213  for (Index row=0; row<nm.outerSize(); ++row)
214  {
215  Index col = 0;
216  for (typename NestedCleaned::InnerIterator it(nm.derived(), row); it; ++it)
217  {
218  for ( ; col<it.index(); ++col)
219  s << "0 ";
220  s << it.value() << " ";
221  ++col;
222  }
223  for ( ; col<m.cols(); ++col)
224  s << "0 ";
225  s << std::endl;
226  }
227  }
228  else
229  {
230  const Nested nm(m.derived());
231  if (m.cols() == 1) {
232  Index row = 0;
233  for (typename NestedCleaned::InnerIterator it(nm.derived(), 0); it; ++it)
234  {
235  for ( ; row<it.index(); ++row)
236  s << "0" << std::endl;
237  s << it.value() << std::endl;
238  ++row;
239  }
240  for ( ; row<m.rows(); ++row)
241  s << "0" << std::endl;
242  }
243  else
244  {
246  s << static_cast<const SparseMatrixBase<SparseMatrix<Scalar, RowMajorBit, StorageIndex> >&>(trans);
247  }
248  }
249  return s;
250  }
251 
252  template<typename OtherDerived>
253  Derived& operator+=(const SparseMatrixBase<OtherDerived>& other);
254  template<typename OtherDerived>
255  Derived& operator-=(const SparseMatrixBase<OtherDerived>& other);
256 
257  template<typename OtherDerived>
258  Derived& operator+=(const DiagonalBase<OtherDerived>& other);
259  template<typename OtherDerived>
260  Derived& operator-=(const DiagonalBase<OtherDerived>& other);
261 
262  Derived& operator*=(const Scalar& other);
263  Derived& operator/=(const Scalar& other);
264 
265  template<typename OtherDerived> struct CwiseProductDenseReturnType {
266  typedef CwiseBinaryOp<internal::scalar_product_op<typename internal::scalar_product_traits<
267  typename internal::traits<Derived>::Scalar,
268  typename internal::traits<OtherDerived>::Scalar
269  >::ReturnType>,
270  const Derived,
271  const OtherDerived
272  > Type;
273  };
275  template<typename OtherDerived>
276  EIGEN_STRONG_INLINE const typename CwiseProductDenseReturnType<OtherDerived>::Type
277  cwiseProduct(const MatrixBase<OtherDerived> &other) const;
278 
279  // sparse * diagonal
280  template<typename OtherDerived>
282  operator*(const DiagonalBase<OtherDerived> &other) const
283  { return Product<Derived,OtherDerived>(derived(), other.derived()); }
284 
285  // diagonal * sparse
286  template<typename OtherDerived> friend
288  operator*(const DiagonalBase<OtherDerived> &lhs, const SparseMatrixBase& rhs)
289  { return Product<OtherDerived,Derived>(lhs.derived(), rhs.derived()); }
290 
291  // sparse * sparse
292  template<typename OtherDerived>
293  const Product<Derived,OtherDerived,AliasFreeProduct>
294  operator*(const SparseMatrixBase<OtherDerived> &other) const;
295 
296  // sparse * dense
297  template<typename OtherDerived>
299  operator*(const MatrixBase<OtherDerived> &other) const
300  { return Product<Derived,OtherDerived>(derived(), other.derived()); }
301 
302  // dense * sparse
303  template<typename OtherDerived> friend
305  operator*(const MatrixBase<OtherDerived> &lhs, const SparseMatrixBase& rhs)
306  { return Product<OtherDerived,Derived>(lhs.derived(), rhs.derived()); }
307 
309  SparseSymmetricPermutationProduct<Derived,Upper|Lower> twistedBy(const PermutationMatrix<Dynamic,Dynamic,StorageIndex>& perm) const
310  {
311  return SparseSymmetricPermutationProduct<Derived,Upper|Lower>(derived(), perm);
312  }
313 
314  template<typename OtherDerived>
315  Derived& operator*=(const SparseMatrixBase<OtherDerived>& other);
316 
317  template<int Mode>
318  inline const TriangularView<const Derived, Mode> triangularView() const;
319 
320  template<unsigned int UpLo> struct SelfAdjointViewReturnType { typedef SparseSelfAdjointView<Derived, UpLo> Type; };
321  template<unsigned int UpLo> struct ConstSelfAdjointViewReturnType { typedef const SparseSelfAdjointView<const Derived, UpLo> Type; };
323  template<unsigned int UpLo> inline
324  typename ConstSelfAdjointViewReturnType<UpLo>::Type selfadjointView() const;
325  template<unsigned int UpLo> inline
326  typename SelfAdjointViewReturnType<UpLo>::Type selfadjointView();
327 
328  template<typename OtherDerived> Scalar dot(const MatrixBase<OtherDerived>& other) const;
329  template<typename OtherDerived> Scalar dot(const SparseMatrixBase<OtherDerived>& other) const;
330  RealScalar squaredNorm() const;
331  RealScalar norm() const;
332  RealScalar blueNorm() const;
333 
334  TransposeReturnType transpose() { return TransposeReturnType(derived()); }
335  const ConstTransposeReturnType transpose() const { return ConstTransposeReturnType(derived()); }
336  const AdjointReturnType adjoint() const { return AdjointReturnType(transpose()); }
337 
338  // inner-vector
339  typedef Block<Derived,IsRowMajor?1:Dynamic,IsRowMajor?Dynamic:1,true> InnerVectorReturnType;
340  typedef Block<const Derived,IsRowMajor?1:Dynamic,IsRowMajor?Dynamic:1,true> ConstInnerVectorReturnType;
341  InnerVectorReturnType innerVector(Index outer);
342  const ConstInnerVectorReturnType innerVector(Index outer) const;
343 
344  // set of inner-vectors
347  InnerVectorsReturnType innerVectors(Index outerStart, Index outerSize);
348  const ConstInnerVectorsReturnType innerVectors(Index outerStart, Index outerSize) const;
349 
350  DenseMatrixType toDense() const
351  {
352  return DenseMatrixType(derived());
353  }
354 
355  template<typename OtherDerived>
356  bool isApprox(const SparseMatrixBase<OtherDerived>& other,
357  const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
358 
359  template<typename OtherDerived>
360  bool isApprox(const MatrixBase<OtherDerived>& other,
361  const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const
362  { return toDense().isApprox(other,prec); }
363 
369  inline const typename internal::eval<Derived>::type eval() const
370  { return typename internal::eval<Derived>::type(derived()); }
371 
372  Scalar sum() const;
373 
374  inline const SparseView<Derived>
375  pruned(const Scalar& reference = Scalar(0), const RealScalar& epsilon = NumTraits<Scalar>::dummy_precision()) const;
377  protected:
378 
379  bool m_isRValue;
380 
381  static inline StorageIndex convert_index(const Index idx) {
382  return internal::convert_index<StorageIndex>(idx);
383  }
384  private:
385  template<typename Dest> void evalTo(Dest &) const;
386 };
387 
388 } // end namespace Eigen
389 
390 #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:168
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:107
Definition: SparseMatrixBase.h:86
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:149
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: SparseMatrixBase.h:74
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:214
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
Definition: EigenBase.h:28
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:78
Base class of any sparse matrices or sparse expressions.
Definition: ForwardDeclarations.h:278
Definition: SparseMatrixBase.h:92
Index outerSize() const
Definition: SparseMatrixBase.h:176
const internal::eval< Derived >::type eval() const
Definition: SparseMatrixBase.h:369
InnerVectorReturnType innerVector(Index outer)
Definition: SparseBlock.h:322
Definition: SparseMatrixBase.h:67
const ScalarMultipleReturnType operator*(const Scalar &scalar) const
Definition: SparseMatrixBase.h:57
Index cols() const
Definition: SparseMatrixBase.h:165
Scalar value_type
Definition: SparseMatrixBase.h:42
SparseSymmetricPermutationProduct< Derived, Upper|Lower > twistedBy(const PermutationMatrix< Dynamic, Dynamic, StorageIndex > &perm) const
Definition: SparseMatrixBase.h:309
InnerVectorsReturnType innerVectors(Index outerStart, Index outerSize)
Definition: SparseBlock.h:337
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:163
ColXpr col(Index i)
Definition: SparseMatrixBase.h:778
Index innerSize() const
Definition: SparseMatrixBase.h:179
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:56
bool isVector() const
Definition: SparseMatrixBase.h:173
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
Definition: SparseMatrixBase.h:61