10 #ifndef EIGEN_CONSERVATIVESPARSESPARSEPRODUCT_H
11 #define EIGEN_CONSERVATIVESPARSESPARSEPRODUCT_H
19 template<
typename Lhs,
typename Rhs,
typename ResultType>
35 std::memset(mask,0,
sizeof(
bool)*
rows);
37 evaluator<Lhs> lhsEval(lhs);
38 evaluator<Rhs> rhsEval(rhs);
46 Index estimated_nnz_prod = lhsEval.nonZerosEstimate() + rhsEval.nonZerosEstimate();
49 res.reserve(
Index(estimated_nnz_prod));
56 for (
typename evaluator<Rhs>::InnerIterator rhsIt(rhsEval,
j); rhsIt; ++rhsIt)
58 RhsScalar
y = rhsIt.value();
59 Index k = rhsIt.index();
60 for (
typename evaluator<Lhs>::InnerIterator lhsIt(lhsEval, k); lhsIt; ++lhsIt)
63 LhsScalar
x = lhsIt.value();
78 for(
Index k=0; k<nnz; ++k)
81 res.insertBackByOuterInnerUnordered(
j,
i) = values[
i];
97 if((nnz<200 && nnz<t200) || nnz *
numext::log2(
int(nnz)) < t)
99 if(nnz>1) std::sort(indices,indices+nnz);
100 for(
Index k=0; k<nnz; ++k)
103 res.insertBackByOuterInner(
j,
i) = values[
i];
115 res.insertBackByOuterInner(
j,
i) = values[
i];
131 template<
class Source,
int Order>
134 template<
typename Lhs,
typename Rhs,
typename ResultType,
138 struct conservative_sparse_sparse_product_selector;
140 template<
typename Lhs,
typename Rhs,
typename ResultType>
141 struct conservative_sparse_sparse_product_selector<Lhs,Rhs,ResultType,ColMajor,ColMajor,ColMajor>
144 typedef typename LhsCleaned::Scalar Scalar;
146 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType&
res)
154 if(lhs.rows()>rhs.cols())
157 ColMajorMatrix resCol(lhs.rows(),rhs.cols());
159 internal::conservative_sparse_sparse_product_impl<Lhs,Rhs,ColMajorMatrix>(lhs, rhs, resCol,
true);
160 res = resCol.markAsRValue();
164 ColMajorMatrixAux resCol(lhs.rows(),rhs.cols());
166 internal::conservative_sparse_sparse_product_impl<Lhs,Rhs,ColMajorMatrixAux>(lhs, rhs, resCol,
false);
167 RowMajorMatrix resRow(resCol);
168 res = resRow.markAsRValue();
173 template<
typename Lhs,
typename Rhs,
typename ResultType>
174 struct conservative_sparse_sparse_product_selector<Lhs,Rhs,ResultType,RowMajor,ColMajor,ColMajor>
176 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType&
res)
178 using RowMajorRhs = WithStorageOrder<Rhs, RowMajor>;
179 using RowMajorRes = WithStorageOrder<ResultType, RowMajor>;
180 RowMajorRhs rhsRow = rhs;
181 RowMajorRes resRow(lhs.rows(), rhs.cols());
182 internal::conservative_sparse_sparse_product_impl<RowMajorRhs,Lhs,RowMajorRes>(rhsRow, lhs, resRow);
187 template<
typename Lhs,
typename Rhs,
typename ResultType>
188 struct conservative_sparse_sparse_product_selector<Lhs,Rhs,ResultType,ColMajor,RowMajor,ColMajor>
190 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType&
res)
192 using RowMajorLhs = WithStorageOrder<Lhs, RowMajor>;
193 using RowMajorRes = WithStorageOrder<ResultType, RowMajor>;
194 RowMajorLhs lhsRow = lhs;
195 RowMajorRes resRow(lhs.rows(), rhs.cols());
196 internal::conservative_sparse_sparse_product_impl<Rhs,RowMajorLhs,RowMajorRes>(rhs, lhsRow, resRow);
201 template<
typename Lhs,
typename Rhs,
typename ResultType>
202 struct conservative_sparse_sparse_product_selector<Lhs,Rhs,ResultType,RowMajor,RowMajor,ColMajor>
204 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType&
res)
206 using RowMajorRes = WithStorageOrder<ResultType, RowMajor>;
207 RowMajorRes resRow(lhs.rows(), rhs.cols());
208 internal::conservative_sparse_sparse_product_impl<Rhs,Lhs,RowMajorRes>(rhs, lhs, resRow);
214 template<
typename Lhs,
typename Rhs,
typename ResultType>
215 struct conservative_sparse_sparse_product_selector<Lhs,Rhs,ResultType,ColMajor,ColMajor,RowMajor>
217 typedef typename traits<remove_all_t<Lhs>>::Scalar Scalar;
219 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType&
res)
221 using ColMajorRes = WithStorageOrder<ResultType, ColMajor>;
222 ColMajorRes resCol(lhs.rows(), rhs.cols());
223 internal::conservative_sparse_sparse_product_impl<Lhs,Rhs,ColMajorRes>(lhs, rhs, resCol);
228 template<
typename Lhs,
typename Rhs,
typename ResultType>
231 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType&
res)
233 using ColMajorLhs = WithStorageOrder<Lhs, ColMajor>;
234 using ColMajorRes = WithStorageOrder<ResultType, ColMajor>;
235 ColMajorLhs lhsCol = lhs;
236 ColMajorRes resCol(lhs.rows(), rhs.cols());
237 internal::conservative_sparse_sparse_product_impl<ColMajorLhs,Rhs,ColMajorRes>(lhsCol, rhs, resCol);
242 template<
typename Lhs,
typename Rhs,
typename ResultType>
245 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType&
res)
247 using ColMajorRhs = WithStorageOrder<Rhs, ColMajor>;
248 using ColMajorRes = WithStorageOrder<ResultType, ColMajor>;
249 ColMajorRhs rhsCol = rhs;
250 ColMajorRes resCol(lhs.rows(), rhs.cols());
251 internal::conservative_sparse_sparse_product_impl<Lhs,ColMajorRhs,ColMajorRes>(lhs, rhsCol, resCol);
256 template<
typename Lhs,
typename Rhs,
typename ResultType>
259 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType&
res)
261 using ColMajorRes = WithStorageOrder<ResultType, ColMajor>;
262 using RowMajorRes = WithStorageOrder<ResultType, RowMajor>;
263 RowMajorRes resRow(lhs.rows(),rhs.cols());
264 internal::conservative_sparse_sparse_product_impl<Rhs,Lhs,RowMajorRes>(rhs, lhs, resRow);
266 ColMajorRes resCol(resRow);
276 template<
typename Lhs,
typename Rhs,
typename ResultType>
284 evaluator<Lhs> lhsEval(lhs);
285 evaluator<Rhs> rhsEval(rhs);
289 for (
typename evaluator<Rhs>::InnerIterator rhsIt(rhsEval,
j); rhsIt; ++rhsIt)
291 RhsScalar
y = rhsIt.value();
292 Index k = rhsIt.index();
293 for (
typename evaluator<Lhs>::InnerIterator lhsIt(lhsEval, k); lhsIt; ++lhsIt)
296 LhsScalar
x = lhsIt.value();
308 template<
typename Lhs,
typename Rhs,
typename ResultType,
311 struct sparse_sparse_to_dense_product_selector;
313 template<
typename Lhs,
typename Rhs,
typename ResultType>
314 struct sparse_sparse_to_dense_product_selector<Lhs,Rhs,ResultType,
ColMajor,
ColMajor>
316 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType&
res)
318 internal::sparse_sparse_to_dense_product_impl<Lhs,Rhs,ResultType>(lhs, rhs,
res);
322 template<
typename Lhs,
typename Rhs,
typename ResultType>
323 struct sparse_sparse_to_dense_product_selector<Lhs,Rhs,ResultType,
RowMajor,
ColMajor>
325 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType&
res)
327 using ColMajorLhs = WithStorageOrder<Lhs, ColMajor>;
328 ColMajorLhs lhsCol(lhs);
329 internal::sparse_sparse_to_dense_product_impl<ColMajorLhs,Rhs,ResultType>(lhsCol, rhs,
res);
333 template<
typename Lhs,
typename Rhs,
typename ResultType>
334 struct sparse_sparse_to_dense_product_selector<Lhs,Rhs,ResultType,
ColMajor,
RowMajor>
336 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType&
res)
338 using ColMajorRhs = WithStorageOrder<Rhs, ColMajor>;
339 ColMajorRhs rhsCol(rhs);
340 internal::sparse_sparse_to_dense_product_impl<Lhs,ColMajorRhs,ResultType>(lhs, rhsCol,
res);
344 template<
typename Lhs,
typename Rhs,
typename ResultType>
345 struct sparse_sparse_to_dense_product_selector<Lhs,Rhs,ResultType,
RowMajor,
RowMajor>
347 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType&
res)
349 Transpose<ResultType> trRes(
res);
350 internal::sparse_sparse_to_dense_product_impl<Rhs,Lhs,Transpose<ResultType> >(rhs, lhs, trRes);
#define ei_declare_aligned_stack_constructed_variable(TYPE, NAME, SIZE, BUFFER)
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
A versatible sparse matrix representation.
const unsigned int RowMajorBit
static void conservative_sparse_sparse_product_impl(const Lhs &lhs, const Rhs &rhs, ResultType &res, bool sortedInsertion=false)
typename remove_all< T >::type remove_all_t
static void sparse_sparse_to_dense_product_impl(const Lhs &lhs, const Rhs &rhs, ResultType &res)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.