SparseProduct.h
Go to the documentation of this file.
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 // Eigen is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // Alternatively, you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 2 of
14 // the License, or (at your option) any later version.
15 //
16 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License and a copy of the GNU General Public License along with
23 // Eigen. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef EIGEN_SPARSEPRODUCT_H
26 #define EIGEN_SPARSEPRODUCT_H
27 
28 namespace Eigen {
29 
30 template<typename Lhs, typename Rhs>
32 {
33  typedef typename internal::traits<Lhs>::Scalar Scalar;
34  enum {
35  LhsRowMajor = internal::traits<Lhs>::Flags & RowMajorBit,
36  RhsRowMajor = internal::traits<Rhs>::Flags & RowMajorBit,
39  };
40 
41  typedef typename internal::conditional<TransposeLhs,
43  typename internal::nested<Lhs,Rhs::RowsAtCompileTime>::type>::type LhsNested;
44 
45  typedef typename internal::conditional<TransposeRhs,
47  typename internal::nested<Rhs,Lhs::RowsAtCompileTime>::type>::type RhsNested;
48 
50 };
51 
52 namespace internal {
53 template<typename LhsNested, typename RhsNested>
54 struct traits<SparseSparseProduct<LhsNested, RhsNested> >
55 {
56  typedef MatrixXpr XprKind;
57  // clean the nested types:
58  typedef typename remove_all<LhsNested>::type _LhsNested;
59  typedef typename remove_all<RhsNested>::type _RhsNested;
60  typedef typename _LhsNested::Scalar Scalar;
61  typedef typename promote_index_type<typename traits<_LhsNested>::Index,
62  typename traits<_RhsNested>::Index>::type Index;
63 
64  enum {
65  LhsCoeffReadCost = _LhsNested::CoeffReadCost,
66  RhsCoeffReadCost = _RhsNested::CoeffReadCost,
67  LhsFlags = _LhsNested::Flags,
68  RhsFlags = _RhsNested::Flags,
69 
70  RowsAtCompileTime = _LhsNested::RowsAtCompileTime,
71  ColsAtCompileTime = _RhsNested::ColsAtCompileTime,
72  MaxRowsAtCompileTime = _LhsNested::MaxRowsAtCompileTime,
73  MaxColsAtCompileTime = _RhsNested::MaxColsAtCompileTime,
74 
75  InnerSize = EIGEN_SIZE_MIN_PREFER_FIXED(_LhsNested::ColsAtCompileTime, _RhsNested::RowsAtCompileTime),
76 
77  EvalToRowMajor = (RhsFlags & LhsFlags & RowMajorBit),
78 
79  RemovedBits = ~(EvalToRowMajor ? 0 : RowMajorBit),
80 
81  Flags = (int(LhsFlags | RhsFlags) & HereditaryBits & RemovedBits)
84 
85  CoeffReadCost = Dynamic
86  };
87 
88  typedef Sparse StorageKind;
89 };
90 
91 } // end namespace internal
92 
93 template<typename LhsNested, typename RhsNested>
94 class SparseSparseProduct : internal::no_assignment_operator,
95  public SparseMatrixBase<SparseSparseProduct<LhsNested, RhsNested> >
96 {
97  public:
98 
100  EIGEN_DENSE_PUBLIC_INTERFACE(SparseSparseProduct)
101 
102  private:
103 
104  typedef typename internal::traits<SparseSparseProduct>::_LhsNested _LhsNested;
105  typedef typename internal::traits<SparseSparseProduct>::_RhsNested _RhsNested;
106 
107  public:
108 
109  template<typename Lhs, typename Rhs>
110  EIGEN_STRONG_INLINE SparseSparseProduct(const Lhs& lhs, const Rhs& rhs)
111  : m_lhs(lhs), m_rhs(rhs), m_tolerance(0), m_conservative(true)
112  {
113  init();
114  }
115 
116  template<typename Lhs, typename Rhs>
117  EIGEN_STRONG_INLINE SparseSparseProduct(const Lhs& lhs, const Rhs& rhs, RealScalar tolerance)
118  : m_lhs(lhs), m_rhs(rhs), m_tolerance(tolerance), m_conservative(false)
119  {
120  init();
121  }
122 
123  SparseSparseProduct pruned(Scalar reference = 0, RealScalar epsilon = NumTraits<RealScalar>::dummy_precision()) const
124  {
125  return SparseSparseProduct(m_lhs,m_rhs,internal::abs(reference)*epsilon);
126  }
127 
128  template<typename Dest>
129  void evalTo(Dest& result) const
130  {
131  if(m_conservative)
132  internal::conservative_sparse_sparse_product_selector<_LhsNested, _RhsNested, Dest>::run(lhs(),rhs(),result);
133  else
134  internal::sparse_sparse_product_with_pruning_selector<_LhsNested, _RhsNested, Dest>::run(lhs(),rhs(),result,m_tolerance);
135  }
136 
137  EIGEN_STRONG_INLINE Index rows() const { return m_lhs.rows(); }
138  EIGEN_STRONG_INLINE Index cols() const { return m_rhs.cols(); }
139 
140  EIGEN_STRONG_INLINE const _LhsNested& lhs() const { return m_lhs; }
141  EIGEN_STRONG_INLINE const _RhsNested& rhs() const { return m_rhs; }
142 
143  protected:
144  void init()
145  {
146  eigen_assert(m_lhs.cols() == m_rhs.rows());
147 
148  enum {
149  ProductIsValid = _LhsNested::ColsAtCompileTime==Dynamic
150  || _RhsNested::RowsAtCompileTime==Dynamic
151  || int(_LhsNested::ColsAtCompileTime)==int(_RhsNested::RowsAtCompileTime),
152  AreVectors = _LhsNested::IsVectorAtCompileTime && _RhsNested::IsVectorAtCompileTime,
153  SameSizes = EIGEN_PREDICATE_SAME_MATRIX_SIZE(_LhsNested,_RhsNested)
154  };
155  // note to the lost user:
156  // * for a dot product use: v1.dot(v2)
157  // * for a coeff-wise product use: v1.cwise()*v2
158  EIGEN_STATIC_ASSERT(ProductIsValid || !(AreVectors && SameSizes),
159  INVALID_VECTOR_VECTOR_PRODUCT__IF_YOU_WANTED_A_DOT_OR_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTIONS)
160  EIGEN_STATIC_ASSERT(ProductIsValid || !(SameSizes && !AreVectors),
161  INVALID_MATRIX_PRODUCT__IF_YOU_WANTED_A_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTION)
162  EIGEN_STATIC_ASSERT(ProductIsValid || SameSizes, INVALID_MATRIX_PRODUCT)
163  }
164 
165  LhsNested m_lhs;
166  RhsNested m_rhs;
167  RealScalar m_tolerance;
169 };
170 
171 // sparse = sparse * sparse
172 template<typename Derived>
173 template<typename Lhs, typename Rhs>
175 {
176  product.evalTo(derived());
177  return derived();
178 }
179 
191 template<typename Derived>
192 template<typename OtherDerived>
195 {
196  return typename SparseSparseProductReturnType<Derived,OtherDerived>::Type(derived(), other.derived());
197 }
198 
199 } // end namespace Eigen
200 
201 #endif // EIGEN_SPARSEPRODUCT_H