25 #ifndef EIGEN_CONSERVATIVESPARSESPARSEPRODUCT_H
26 #define EIGEN_CONSERVATIVESPARSESPARSEPRODUCT_H
32 template<
typename Lhs,
typename Rhs,
typename ResultType>
33 static void conservative_sparse_sparse_product_impl(
const Lhs& lhs,
const Rhs& rhs, ResultType& res)
35 typedef typename remove_all<Lhs>::type::Scalar Scalar;
36 typedef typename remove_all<Lhs>::type::Index Index;
39 Index rows = lhs.innerSize();
40 Index cols = rhs.outerSize();
43 std::vector<bool> mask(rows,
false);
44 Matrix<Scalar,Dynamic,1> values(rows);
45 Matrix<Index,Dynamic,1> indices(rows);
53 Index estimated_nnz_prod = lhs.nonZeros() + rhs.nonZeros();
56 res.reserve(Index(estimated_nnz_prod));
58 for (Index j=0; j<cols; ++j)
63 for (
typename Rhs::InnerIterator rhsIt(rhs, j); rhsIt; ++rhsIt)
65 Scalar
y = rhsIt.value();
66 Index k = rhsIt.index();
67 for (
typename Lhs::InnerIterator lhsIt(lhs, k); lhsIt; ++lhsIt)
69 Index i = lhsIt.index();
70 Scalar x = lhsIt.value();
84 for(
int k=0; k<nnz; ++k)
87 res.insertBackByOuterInnerUnordered(j,i) = values[i];
94 int t200 = rows/(log2(200)*1.39);
95 int t = (rows*100)/139;
107 if(nnz>1) std::sort(indices.data(),indices.data()+nnz);
108 for(
int k=0; k<nnz; ++k)
111 res.insertBackByOuterInner(j,i) = values[i];
118 for(
int i=0; i<rows; ++i)
123 res.insertBackByOuterInner(j,i) = values[i];
138 template<
typename Lhs,
typename Rhs,
typename ResultType,
142 struct conservative_sparse_sparse_product_selector;
144 template<
typename Lhs,
typename Rhs,
typename ResultType>
147 typedef typename remove_all<Lhs>::type LhsCleaned;
148 typedef typename LhsCleaned::Scalar Scalar;
150 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res)
152 typedef SparseMatrix<typename ResultType::Scalar,RowMajor> RowMajorMatrix;
153 typedef SparseMatrix<typename ResultType::Scalar,ColMajor> ColMajorMatrix;
154 ColMajorMatrix resCol(lhs.rows(),rhs.cols());
155 internal::conservative_sparse_sparse_product_impl<Lhs,Rhs,ColMajorMatrix>(lhs, rhs, resCol);
157 RowMajorMatrix resRow(resCol);
162 template<
typename Lhs,
typename Rhs,
typename ResultType>
165 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res)
167 typedef SparseMatrix<typename ResultType::Scalar,RowMajor> RowMajorMatrix;
168 RowMajorMatrix rhsRow = rhs;
169 RowMajorMatrix resRow(lhs.rows(), rhs.cols());
170 internal::conservative_sparse_sparse_product_impl<RowMajorMatrix,Lhs,RowMajorMatrix>(rhsRow, lhs, resRow);
175 template<
typename Lhs,
typename Rhs,
typename ResultType>
178 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res)
180 typedef SparseMatrix<typename ResultType::Scalar,RowMajor> RowMajorMatrix;
181 RowMajorMatrix lhsRow = lhs;
182 RowMajorMatrix resRow(lhs.rows(), rhs.cols());
183 internal::conservative_sparse_sparse_product_impl<Rhs,RowMajorMatrix,RowMajorMatrix>(rhs, lhsRow, resRow);
188 template<
typename Lhs,
typename Rhs,
typename ResultType>
191 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res)
193 typedef SparseMatrix<typename ResultType::Scalar,RowMajor> RowMajorMatrix;
194 RowMajorMatrix resRow(lhs.rows(), rhs.cols());
195 internal::conservative_sparse_sparse_product_impl<Rhs,Lhs,RowMajorMatrix>(rhs, lhs, resRow);
201 template<
typename Lhs,
typename Rhs,
typename ResultType>
206 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res)
208 typedef SparseMatrix<typename ResultType::Scalar,ColMajor> ColMajorMatrix;
209 ColMajorMatrix resCol(lhs.rows(), rhs.cols());
210 internal::conservative_sparse_sparse_product_impl<Lhs,Rhs,ColMajorMatrix>(lhs, rhs, resCol);
215 template<
typename Lhs,
typename Rhs,
typename ResultType>
218 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res)
220 typedef SparseMatrix<typename ResultType::Scalar,ColMajor> ColMajorMatrix;
221 ColMajorMatrix lhsCol = lhs;
222 ColMajorMatrix resCol(lhs.rows(), rhs.cols());
223 internal::conservative_sparse_sparse_product_impl<ColMajorMatrix,Rhs,ColMajorMatrix>(lhsCol, rhs, resCol);
228 template<
typename Lhs,
typename Rhs,
typename ResultType>
231 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res)
233 typedef SparseMatrix<typename ResultType::Scalar,ColMajor> ColMajorMatrix;
234 ColMajorMatrix rhsCol = rhs;
235 ColMajorMatrix resCol(lhs.rows(), rhs.cols());
236 internal::conservative_sparse_sparse_product_impl<Lhs,ColMajorMatrix,ColMajorMatrix>(lhs, rhsCol, resCol);
241 template<
typename Lhs,
typename Rhs,
typename ResultType>
244 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res)
246 typedef SparseMatrix<typename ResultType::Scalar,RowMajor> RowMajorMatrix;
247 typedef SparseMatrix<typename ResultType::Scalar,ColMajor> ColMajorMatrix;
248 RowMajorMatrix resRow(lhs.rows(),rhs.cols());
249 internal::conservative_sparse_sparse_product_impl<Rhs,Lhs,RowMajorMatrix>(rhs, lhs, resRow);
251 ColMajorMatrix resCol(resRow);
260 #endif // EIGEN_CONSERVATIVESPARSESPARSEPRODUCT_H