Eigen  3.2.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
SparseProduct.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2010 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_SPARSEPRODUCT_H
11 #define EIGEN_SPARSEPRODUCT_H
12 
13 namespace Eigen {
14 
15 template<typename Lhs, typename Rhs>
16 struct SparseSparseProductReturnType
17 {
18  typedef typename internal::traits<Lhs>::Scalar Scalar;
19  enum {
20  LhsRowMajor = internal::traits<Lhs>::Flags & RowMajorBit,
21  RhsRowMajor = internal::traits<Rhs>::Flags & RowMajorBit,
22  TransposeRhs = (!LhsRowMajor) && RhsRowMajor,
23  TransposeLhs = LhsRowMajor && (!RhsRowMajor)
24  };
25 
26  typedef typename internal::conditional<TransposeLhs,
27  SparseMatrix<Scalar,0>,
28  typename internal::nested<Lhs,Rhs::RowsAtCompileTime>::type>::type LhsNested;
29 
30  typedef typename internal::conditional<TransposeRhs,
31  SparseMatrix<Scalar,0>,
32  typename internal::nested<Rhs,Lhs::RowsAtCompileTime>::type>::type RhsNested;
33 
34  typedef SparseSparseProduct<LhsNested, RhsNested> Type;
35 };
36 
37 namespace internal {
38 template<typename LhsNested, typename RhsNested>
39 struct traits<SparseSparseProduct<LhsNested, RhsNested> >
40 {
41  typedef MatrixXpr XprKind;
42  // clean the nested types:
43  typedef typename remove_all<LhsNested>::type _LhsNested;
44  typedef typename remove_all<RhsNested>::type _RhsNested;
45  typedef typename _LhsNested::Scalar Scalar;
46  typedef typename promote_index_type<typename traits<_LhsNested>::Index,
47  typename traits<_RhsNested>::Index>::type Index;
48 
49  enum {
50  LhsCoeffReadCost = _LhsNested::CoeffReadCost,
51  RhsCoeffReadCost = _RhsNested::CoeffReadCost,
52  LhsFlags = _LhsNested::Flags,
53  RhsFlags = _RhsNested::Flags,
54 
55  RowsAtCompileTime = _LhsNested::RowsAtCompileTime,
56  ColsAtCompileTime = _RhsNested::ColsAtCompileTime,
57  MaxRowsAtCompileTime = _LhsNested::MaxRowsAtCompileTime,
58  MaxColsAtCompileTime = _RhsNested::MaxColsAtCompileTime,
59 
60  InnerSize = EIGEN_SIZE_MIN_PREFER_FIXED(_LhsNested::ColsAtCompileTime, _RhsNested::RowsAtCompileTime),
61 
62  EvalToRowMajor = (RhsFlags & LhsFlags & RowMajorBit),
63 
64  RemovedBits = ~(EvalToRowMajor ? 0 : RowMajorBit),
65 
66  Flags = (int(LhsFlags | RhsFlags) & HereditaryBits & RemovedBits)
69 
70  CoeffReadCost = Dynamic
71  };
72 
73  typedef Sparse StorageKind;
74 };
75 
76 } // end namespace internal
77 
78 template<typename LhsNested, typename RhsNested>
79 class SparseSparseProduct : internal::no_assignment_operator,
80  public SparseMatrixBase<SparseSparseProduct<LhsNested, RhsNested> >
81 {
82  public:
83 
84  typedef SparseMatrixBase<SparseSparseProduct> Base;
85  EIGEN_DENSE_PUBLIC_INTERFACE(SparseSparseProduct)
86 
87  private:
88 
89  typedef typename internal::traits<SparseSparseProduct>::_LhsNested _LhsNested;
90  typedef typename internal::traits<SparseSparseProduct>::_RhsNested _RhsNested;
91 
92  public:
93 
94  template<typename Lhs, typename Rhs>
95  EIGEN_STRONG_INLINE SparseSparseProduct(const Lhs& lhs, const Rhs& rhs)
96  : m_lhs(lhs), m_rhs(rhs), m_tolerance(0), m_conservative(true)
97  {
98  init();
99  }
100 
101  template<typename Lhs, typename Rhs>
102  EIGEN_STRONG_INLINE SparseSparseProduct(const Lhs& lhs, const Rhs& rhs, const RealScalar& tolerance)
103  : m_lhs(lhs), m_rhs(rhs), m_tolerance(tolerance), m_conservative(false)
104  {
105  init();
106  }
107 
108  SparseSparseProduct pruned(const Scalar& reference = 0, const RealScalar& epsilon = NumTraits<RealScalar>::dummy_precision()) const
109  {
110  using std::abs;
111  return SparseSparseProduct(m_lhs,m_rhs,abs(reference)*epsilon);
112  }
113 
114  template<typename Dest>
115  void evalTo(Dest& result) const
116  {
117  if(m_conservative)
118  internal::conservative_sparse_sparse_product_selector<_LhsNested, _RhsNested, Dest>::run(lhs(),rhs(),result);
119  else
120  internal::sparse_sparse_product_with_pruning_selector<_LhsNested, _RhsNested, Dest>::run(lhs(),rhs(),result,m_tolerance);
121  }
122 
123  EIGEN_STRONG_INLINE Index rows() const { return m_lhs.rows(); }
124  EIGEN_STRONG_INLINE Index cols() const { return m_rhs.cols(); }
125 
126  EIGEN_STRONG_INLINE const _LhsNested& lhs() const { return m_lhs; }
127  EIGEN_STRONG_INLINE const _RhsNested& rhs() const { return m_rhs; }
128 
129  protected:
130  void init()
131  {
132  eigen_assert(m_lhs.cols() == m_rhs.rows());
133 
134  enum {
135  ProductIsValid = _LhsNested::ColsAtCompileTime==Dynamic
136  || _RhsNested::RowsAtCompileTime==Dynamic
137  || int(_LhsNested::ColsAtCompileTime)==int(_RhsNested::RowsAtCompileTime),
138  AreVectors = _LhsNested::IsVectorAtCompileTime && _RhsNested::IsVectorAtCompileTime,
139  SameSizes = EIGEN_PREDICATE_SAME_MATRIX_SIZE(_LhsNested,_RhsNested)
140  };
141  // note to the lost user:
142  // * for a dot product use: v1.dot(v2)
143  // * for a coeff-wise product use: v1.cwise()*v2
144  EIGEN_STATIC_ASSERT(ProductIsValid || !(AreVectors && SameSizes),
145  INVALID_VECTOR_VECTOR_PRODUCT__IF_YOU_WANTED_A_DOT_OR_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTIONS)
146  EIGEN_STATIC_ASSERT(ProductIsValid || !(SameSizes && !AreVectors),
147  INVALID_MATRIX_PRODUCT__IF_YOU_WANTED_A_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTION)
148  EIGEN_STATIC_ASSERT(ProductIsValid || SameSizes, INVALID_MATRIX_PRODUCT)
149  }
150 
151  LhsNested m_lhs;
152  RhsNested m_rhs;
153  RealScalar m_tolerance;
154  bool m_conservative;
155 };
156 
157 // sparse = sparse * sparse
158 template<typename Derived>
159 template<typename Lhs, typename Rhs>
160 inline Derived& SparseMatrixBase<Derived>::operator=(const SparseSparseProduct<Lhs,Rhs>& product)
161 {
162  product.evalTo(derived());
163  return derived();
164 }
165 
177 template<typename Derived>
178 template<typename OtherDerived>
179 inline const typename SparseSparseProductReturnType<Derived,OtherDerived>::Type
181 {
182  return typename SparseSparseProductReturnType<Derived,OtherDerived>::Type(derived(), other.derived());
183 }
184 
185 } // end namespace Eigen
186 
187 #endif // EIGEN_SPARSEPRODUCT_H