OSVR-Core
SparseMatrixBase.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2011 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;
39  typedef typename internal::traits<Derived>::StorageKind StorageKind;
40  typedef typename internal::traits<Derived>::Index Index;
44 
46 
47  template<typename OtherDerived>
48  Derived& operator=(const EigenBase<OtherDerived> &other)
49  {
50  other.derived().evalTo(derived());
51  return derived();
52  }
53 
54  enum {
55 
75  MaxRowsAtCompileTime = RowsAtCompileTime,
76  MaxColsAtCompileTime = ColsAtCompileTime,
77 
78  MaxSizeAtCompileTime = (internal::size_at_compile_time<MaxRowsAtCompileTime,
79  MaxColsAtCompileTime>::ret),
80 
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 
112 
113 
115 
116 
117 #ifndef EIGEN_PARSED_BY_DOXYGEN
118 
125 
129 
132 
134  typedef Matrix<Scalar,EIGEN_SIZE_MAX(RowsAtCompileTime,ColsAtCompileTime),
136 
137  inline const Derived& derived() const { return *static_cast<const Derived*>(this); }
138  inline Derived& derived() { return *static_cast<Derived*>(this); }
139  inline Derived& const_cast_derived() const
140  { return *static_cast<Derived*>(const_cast<SparseMatrixBase*>(this)); }
143  using Base::operator*;
144 #endif // not EIGEN_PARSED_BY_DOXYGEN
145 
146 #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::SparseMatrixBase
147 # include "../plugins/CommonCwiseUnaryOps.h"
148 # include "../plugins/CommonCwiseBinaryOps.h"
149 # include "../plugins/MatrixCwiseUnaryOps.h"
150 # include "../plugins/MatrixCwiseBinaryOps.h"
151 # include "../plugins/BlockMethods.h"
152 # ifdef EIGEN_SPARSEMATRIXBASE_PLUGIN
153 # include EIGEN_SPARSEMATRIXBASE_PLUGIN
154 # endif
155 # undef EIGEN_CURRENT_STORAGE_BASE_CLASS
156 #undef EIGEN_CURRENT_STORAGE_BASE_CLASS
159  inline Index rows() const { return derived().rows(); }
161  inline Index cols() const { return derived().cols(); }
164  inline Index size() const { return rows() * cols(); }
167  inline Index nonZeros() const { return derived().nonZeros(); }
172  inline bool isVector() const { return rows()==1 || cols()==1; }
175  Index outerSize() const { return (int(Flags)&RowMajorBit) ? this->rows() : this->cols(); }
178  Index innerSize() const { return (int(Flags)&RowMajorBit) ? this->cols() : this->rows(); }
179 
180  bool isRValue() const { return m_isRValue; }
181  Derived& markAsRValue() { m_isRValue = true; return derived(); }
182 
183  SparseMatrixBase() : m_isRValue(false) { /* TODO check flags */ }
184 
185 
186  template<typename OtherDerived>
187  Derived& operator=(const ReturnByValue<OtherDerived>& other)
188  {
189  other.evalTo(derived());
190  return derived();
191  }
192 
193 
194  template<typename OtherDerived>
195  inline Derived& operator=(const SparseMatrixBase<OtherDerived>& other)
196  {
197  return assign(other.derived());
198  }
199 
200  inline Derived& operator=(const Derived& other)
201  {
202 // if (other.isRValue())
203 // derived().swap(other.const_cast_derived());
204 // else
205  return assign(other.derived());
206  }
207 
208  protected:
209 
210  template<typename OtherDerived>
211  inline Derived& assign(const OtherDerived& other)
212  {
213  const bool transpose = (Flags & RowMajorBit) != (OtherDerived::Flags & RowMajorBit);
214  const Index outerSize = (int(OtherDerived::Flags) & RowMajorBit) ? other.rows() : other.cols();
215  if ((!transpose) && other.isRValue())
216  {
217  // eval without temporary
218  derived().resize(other.rows(), other.cols());
219  derived().setZero();
220  derived().reserve((std::max)(this->rows(),this->cols())*2);
221  for (Index j=0; j<outerSize; ++j)
222  {
223  derived().startVec(j);
224  for (typename OtherDerived::InnerIterator it(other, j); it; ++it)
225  {
226  Scalar v = it.value();
227  derived().insertBackByOuterInner(j,it.index()) = v;
228  }
229  }
230  derived().finalize();
231  }
232  else
233  {
234  assignGeneric(other);
235  }
236  return derived();
237  }
239  template<typename OtherDerived>
240  inline void assignGeneric(const OtherDerived& other)
241  {
242  //const bool transpose = (Flags & RowMajorBit) != (OtherDerived::Flags & RowMajorBit);
243  eigen_assert(( ((internal::traits<Derived>::SupportedAccessPatterns&OuterRandomAccessPattern)==OuterRandomAccessPattern) ||
244  (!((Flags & RowMajorBit) != (OtherDerived::Flags & RowMajorBit)))) &&
245  "the transpose operation is supposed to be handled in SparseMatrix::operator=");
246 
247  enum { Flip = (Flags & RowMajorBit) != (OtherDerived::Flags & RowMajorBit) };
248 
249  const Index outerSize = other.outerSize();
250  //typedef typename internal::conditional<transpose, LinkedVectorMatrix<Scalar,Flags&RowMajorBit>, Derived>::type TempType;
251  // thanks to shallow copies, we always eval to a tempary
252  Derived temp(other.rows(), other.cols());
254  temp.reserve((std::max)(this->rows(),this->cols())*2);
255  for (Index j=0; j<outerSize; ++j)
256  {
257  temp.startVec(j);
258  for (typename OtherDerived::InnerIterator it(other.derived(), j); it; ++it)
259  {
260  Scalar v = it.value();
261  temp.insertBackByOuterInner(Flip?it.index():j,Flip?j:it.index()) = v;
262  }
263  }
264  temp.finalize();
265 
266  derived() = temp.markAsRValue();
267  }
268 
269  public:
270 
271  template<typename Lhs, typename Rhs>
272  inline Derived& operator=(const SparseSparseProduct<Lhs,Rhs>& product);
273 
274  friend std::ostream & operator << (std::ostream & s, const SparseMatrixBase& m)
275  {
276  typedef typename Derived::Nested Nested;
277  typedef typename internal::remove_all<Nested>::type NestedCleaned;
278 
279  if (Flags&RowMajorBit)
280  {
281  const Nested nm(m.derived());
282  for (Index row=0; row<nm.outerSize(); ++row)
283  {
284  Index col = 0;
285  for (typename NestedCleaned::InnerIterator it(nm.derived(), row); it; ++it)
286  {
287  for ( ; col<it.index(); ++col)
288  s << "0 ";
289  s << it.value() << " ";
290  ++col;
291  }
292  for ( ; col<m.cols(); ++col)
293  s << "0 ";
294  s << std::endl;
295  }
296  }
297  else
298  {
299  const Nested nm(m.derived());
300  if (m.cols() == 1) {
301  Index row = 0;
302  for (typename NestedCleaned::InnerIterator it(nm.derived(), 0); it; ++it)
303  {
304  for ( ; row<it.index(); ++row)
305  s << "0" << std::endl;
306  s << it.value() << std::endl;
307  ++row;
308  }
309  for ( ; row<m.rows(); ++row)
310  s << "0" << std::endl;
311  }
312  else
313  {
315  s << static_cast<const SparseMatrixBase<SparseMatrix<Scalar, RowMajorBit, Index> >&>(trans);
316  }
317  }
318  return s;
319  }
320 
321  template<typename OtherDerived>
322  Derived& operator+=(const SparseMatrixBase<OtherDerived>& other);
323  template<typename OtherDerived>
324  Derived& operator-=(const SparseMatrixBase<OtherDerived>& other);
325 
326  Derived& operator*=(const Scalar& other);
327  Derived& operator/=(const Scalar& other);
329  template<typename OtherDerived> struct CwiseProductDenseReturnType {
333  >::ReturnType>,
334  const Derived,
335  const OtherDerived
336  > Type;
337  };
338 
339  template<typename OtherDerived>
340  EIGEN_STRONG_INLINE const typename CwiseProductDenseReturnType<OtherDerived>::Type
341  cwiseProduct(const MatrixBase<OtherDerived> &other) const;
342 
343  // sparse * sparse
344  template<typename OtherDerived>
346  operator*(const SparseMatrixBase<OtherDerived> &other) const;
347 
348  // sparse * diagonal
349  template<typename OtherDerived>
351  operator*(const DiagonalBase<OtherDerived> &other) const;
352 
353  // diagonal * sparse
354  template<typename OtherDerived> friend
357  { return SparseDiagonalProduct<OtherDerived,Derived>(lhs.derived(), rhs.derived()); }
360  template<typename OtherDerived> friend
362  operator*(const MatrixBase<OtherDerived>& lhs, const Derived& rhs)
363  { return typename DenseSparseProductReturnType<OtherDerived,Derived>::Type(lhs.derived(),rhs); }
364 
366  template<typename OtherDerived>
369  { return typename SparseDenseProductReturnType<Derived,OtherDerived>::Type(derived(), other.derived()); }
370 
373  {
375  }
376 
377  template<typename OtherDerived>
378  Derived& operator*=(const SparseMatrixBase<OtherDerived>& other);
379 
380  #ifdef EIGEN2_SUPPORT
381  // deprecated
382  template<typename OtherDerived>
384  solveTriangular(const MatrixBase<OtherDerived>& other) const;
385 
386  // deprecated
387  template<typename OtherDerived>
388  void solveTriangularInPlace(MatrixBase<OtherDerived>& other) const;
389  #endif // EIGEN2_SUPPORT
390 
391  template<int Mode>
392  inline const SparseTriangularView<Derived, Mode> triangularView() const;
393 
394  template<unsigned int UpLo> inline const SparseSelfAdjointView<Derived, UpLo> selfadjointView() const;
395  template<unsigned int UpLo> inline SparseSelfAdjointView<Derived, UpLo> selfadjointView();
396 
397  template<typename OtherDerived> Scalar dot(const MatrixBase<OtherDerived>& other) const;
398  template<typename OtherDerived> Scalar dot(const SparseMatrixBase<OtherDerived>& other) const;
399  RealScalar squaredNorm() const;
400  RealScalar norm() const;
401  RealScalar blueNorm() const;
402 
403  Transpose<Derived> transpose() { return derived(); }
404  const Transpose<const Derived> transpose() const { return derived(); }
405  const AdjointReturnType adjoint() const { return transpose(); }
407  // inner-vector
410  InnerVectorReturnType innerVector(Index outer);
411  const ConstInnerVectorReturnType innerVector(Index outer) const;
412 
413  // set of inner-vectors
416  InnerVectorsReturnType innerVectors(Index outerStart, Index outerSize);
417  const ConstInnerVectorsReturnType innerVectors(Index outerStart, Index outerSize) const;
418 
420  template<typename DenseDerived>
421  void evalTo(MatrixBase<DenseDerived>& dst) const
422  {
423  dst.setZero();
424  for (Index j=0; j<outerSize(); ++j)
425  for (typename Derived::InnerIterator i(derived(),j); i; ++i)
426  dst.coeffRef(i.row(),i.col()) = i.value();
427  }
428 
430  {
431  return derived();
432  }
433 
434  template<typename OtherDerived>
435  bool isApprox(const SparseMatrixBase<OtherDerived>& other,
436  const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const
437  { return toDense().isApprox(other.toDense(),prec); }
438 
439  template<typename OtherDerived>
440  bool isApprox(const MatrixBase<OtherDerived>& other,
441  const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const
442  { return toDense().isApprox(other,prec); }
443 
449  inline const typename internal::eval<Derived>::type eval() const
450  { return typename internal::eval<Derived>::type(derived()); }
451 
452  Scalar sum() const;
453 
454  protected:
455 
456  bool m_isRValue;
457 };
458 
459 } // end namespace Eigen
460 
461 #endif // EIGEN_SPARSEMATRIXBASE_H
Generic expression of a matrix where all coefficients are defined by a functor.
Definition: CwiseNullaryOp.h:49
Index nonZeros() const
Definition: SparseMatrixBase.h:167
Index innerSize() const
Definition: SparseMatrixBase.h:178
Index cols() const
Definition: SparseMatrixBase.h:161
friend const DenseSparseProductReturnType< OtherDerived, Derived >::Type operator*(const MatrixBase< OtherDerived > &lhs, const Derived &rhs)
dense * sparse (return a dense object unless it is an outer product)
Definition: SparseMatrixBase.h:362
Definition: SparseProduct.h:80
This is a rough measure of how expensive it is to read one coefficient from this expression.
Definition: SparseMatrixBase.h:92
A versatible sparse matrix representation.
Definition: SparseMatrix.h:85
This stores expression Flags flags which may or may not be inherited by new expressions constructed f...
Definition: SparseMatrixBase.h:87
Definition: SparseSelfAdjointView.h:465
Expression of the transpose of a matrix.
Definition: Transpose.h:57
const unsigned int DirectAccessBit
Means that the underlying array of coefficients can be directly accessed as a plain strided array...
Definition: Constants.h:142
RowXpr row(Index i)
Definition: SparseMatrixBase.h:750
Derived & setZero()
Sets all coefficients in this expression to zero.
Definition: CwiseNullaryOp.h:499
iterative scaling algorithm to equilibrate rows and column norms in matrices
Definition: TestIMU_Common.h:87
Pseudo expression to manipulate a triangular sparse matrix as a selfadjoint matrix.
Definition: SparseSelfAdjointView.h:49
Holds information about the various numeric (i.e.
Definition: NumTraits.h:88
const internal::permut_matrix_product_retval< PermutationDerived, Derived, OnTheRight > operator*(const MatrixBase< Derived > &matrix, const PermutationBase< PermutationDerived > &permutation)
Definition: PermutationMatrix.h:539
Definition: Meta.h:29
Index outerSize() const
Definition: SparseMatrixBase.h:175
Derived & derived()
Definition: EigenBase.h:34
const unsigned int RowMajorBit
for a matrix, this means that the storage order is row-major.
Definition: Constants.h:53
This is equal to the number of coefficients, i.e.
Definition: SparseMatrixBase.h:69
bool isVector() const
Definition: SparseMatrixBase.h:172
Definition: SparseTriangularView.h:25
Common base class for all classes T such that MatrixBase has an operator=(T) and a constructor Matrix...
Definition: EigenBase.h:26
The number of rows at compile-time.
Definition: SparseMatrixBase.h:56
Definition: ReturnByValue.h:50
Index size() const
Definition: SparseMatrixBase.h:164
Definition: Functors.h:47
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:107
Index rows() const
Definition: SparseMatrixBase.h:159
Base class of any sparse matrices or sparse expressions.
Definition: ForwardDeclarations.h:239
Definition: XprHelper.h:371
InnerVectorReturnType innerVector(Index outer)
Definition: SparseBlock.h:380
The number of columns at compile-time.
Definition: SparseMatrixBase.h:62
Definition: SparseDenseProduct.h:258
Definition: SparseDenseProduct.h:288
InnerVectorsReturnType innerVectors(Index outerStart, Index outerSize)
Definition: SparseBlock.h:395
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:103
This is set to true if either the number of rows or the number of columns is known at compile-time to...
Definition: SparseMatrixBase.h:81
Definition: DiagonalMatrix.h:18
Definition: SparseDiagonalProduct.h:59
Matrix< Scalar, EIGEN_SIZE_MAX(RowsAtCompileTime, ColsAtCompileTime), EIGEN_SIZE_MAX(RowsAtCompileTime, ColsAtCompileTime)> SquareMatrixType
type of the equivalent square matrix
Definition: SparseMatrixBase.h:135
const internal::eval< Derived >::type eval() const
Definition: SparseMatrixBase.h:449
ColXpr col(Index i)
Definition: SparseMatrixBase.h:733
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:59
const SparseDenseProductReturnType< Derived, OtherDerived >::Type operator*(const MatrixBase< OtherDerived > &other) const
sparse * dense (returns a dense object unless it is an outer product)
Definition: SparseMatrixBase.h:368
NumTraits< Scalar >::Real RealScalar
This is the "real scalar" type; if the Scalar type is already real numbers (e.g.
Definition: SparseMatrixBase.h:124
Definition: Meta.h:25
SparseSymmetricPermutationProduct< Derived, Upper|Lower > twistedBy(const PermutationMatrix< Dynamic, Dynamic, Index > &perm) const
Definition: SparseMatrixBase.h:372
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
Definition: ForwardDeclarations.h:17
Definition: XprHelper.h:203
Definition: XprHelper.h:161