10 #ifndef EIGEN_CONSERVATIVESPARSESPARSEPRODUCT_H 11 #define EIGEN_CONSERVATIVESPARSESPARSEPRODUCT_H 17 template<
typename Lhs,
typename Rhs,
typename ResultType>
18 static void conservative_sparse_sparse_product_impl(
const Lhs& lhs,
const Rhs& rhs, ResultType& res,
bool sortedInsertion =
false)
20 typedef typename remove_all<Lhs>::type::Scalar Scalar;
23 Index rows = lhs.innerSize();
24 Index cols = rhs.outerSize();
25 eigen_assert(lhs.outerSize() == rhs.innerSize());
27 ei_declare_aligned_stack_constructed_variable(
bool, mask, rows, 0);
28 ei_declare_aligned_stack_constructed_variable(Scalar, values, rows, 0);
29 ei_declare_aligned_stack_constructed_variable(
Index, indices, rows, 0);
31 std::memset(mask,0,
sizeof(
bool)*rows);
33 evaluator<Lhs> lhsEval(lhs);
34 evaluator<Rhs> rhsEval(rhs);
42 Index estimated_nnz_prod = lhsEval.nonZerosEstimate() + rhsEval.nonZerosEstimate();
45 res.reserve(
Index(estimated_nnz_prod));
47 for (
Index j=0; j<cols; ++j)
52 for (
typename evaluator<Rhs>::InnerIterator rhsIt(rhsEval, j); rhsIt; ++rhsIt)
54 Scalar y = rhsIt.value();
55 Index k = rhsIt.index();
56 for (
typename evaluator<Lhs>::InnerIterator lhsIt(lhsEval, k); lhsIt; ++lhsIt)
58 Index i = lhsIt.index();
59 Scalar x = lhsIt.value();
74 for(
Index k=0; k<nnz; ++k)
77 res.insertBackByOuterInnerUnordered(j,i) = values[i];
84 const Index t200 = rows/11;
85 const Index t = (rows*100)/139;
93 if((nnz<200 && nnz<t200) || nnz * numext::log2(
int(nnz)) < t)
95 if(nnz>1) std::sort(indices,indices+nnz);
96 for(
Index k=0; k<nnz; ++k)
99 res.insertBackByOuterInner(j,i) = values[i];
106 for(
Index i=0; i<rows; ++i)
111 res.insertBackByOuterInner(j,i) = values[i];
125 template<
typename Lhs,
typename Rhs,
typename ResultType,
129 struct conservative_sparse_sparse_product_selector;
131 template<
typename Lhs,
typename Rhs,
typename ResultType>
132 struct conservative_sparse_sparse_product_selector<Lhs,Rhs,ResultType,ColMajor,ColMajor,ColMajor>
134 typedef typename remove_all<Lhs>::type LhsCleaned;
135 typedef typename LhsCleaned::Scalar Scalar;
137 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res)
139 typedef SparseMatrix<typename ResultType::Scalar,RowMajor,typename ResultType::StorageIndex> RowMajorMatrix;
140 typedef SparseMatrix<typename ResultType::Scalar,ColMajor,typename ResultType::StorageIndex> ColMajorMatrixAux;
141 typedef typename sparse_eval<ColMajorMatrixAux,ResultType::RowsAtCompileTime,ResultType::ColsAtCompileTime,ColMajorMatrixAux::Flags>::type ColMajorMatrix;
146 if(lhs.rows()>rhs.cols())
148 ColMajorMatrix resCol(lhs.rows(),rhs.cols());
150 internal::conservative_sparse_sparse_product_impl<Lhs,Rhs,ColMajorMatrix>(lhs, rhs, resCol,
true);
151 res = resCol.markAsRValue();
155 ColMajorMatrixAux resCol(lhs.rows(),rhs.cols());
157 internal::conservative_sparse_sparse_product_impl<Lhs,Rhs,ColMajorMatrixAux>(lhs, rhs, resCol,
false);
158 RowMajorMatrix resRow(resCol);
159 res = resRow.markAsRValue();
164 template<
typename Lhs,
typename Rhs,
typename ResultType>
165 struct conservative_sparse_sparse_product_selector<Lhs,Rhs,ResultType,
RowMajor,ColMajor,ColMajor>
167 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res)
169 typedef SparseMatrix<typename ResultType::Scalar,RowMajor,typename ResultType::StorageIndex> RowMajorMatrix;
170 RowMajorMatrix rhsRow = rhs;
171 RowMajorMatrix resRow(lhs.rows(), rhs.cols());
172 internal::conservative_sparse_sparse_product_impl<RowMajorMatrix,Lhs,RowMajorMatrix>(rhsRow, lhs, resRow);
177 template<
typename Lhs,
typename Rhs,
typename ResultType>
178 struct conservative_sparse_sparse_product_selector<Lhs,Rhs,ResultType,ColMajor,
RowMajor,ColMajor>
180 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res)
182 typedef SparseMatrix<typename ResultType::Scalar,RowMajor,typename ResultType::StorageIndex> RowMajorMatrix;
183 RowMajorMatrix lhsRow = lhs;
184 RowMajorMatrix resRow(lhs.rows(), rhs.cols());
185 internal::conservative_sparse_sparse_product_impl<Rhs,RowMajorMatrix,RowMajorMatrix>(rhs, lhsRow, resRow);
190 template<
typename Lhs,
typename Rhs,
typename ResultType>
191 struct conservative_sparse_sparse_product_selector<Lhs,Rhs,ResultType,
RowMajor,
RowMajor,ColMajor>
193 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res)
195 typedef SparseMatrix<typename ResultType::Scalar,RowMajor,typename ResultType::StorageIndex> RowMajorMatrix;
196 RowMajorMatrix resRow(lhs.rows(), rhs.cols());
197 internal::conservative_sparse_sparse_product_impl<Rhs,Lhs,RowMajorMatrix>(rhs, lhs, resRow);
203 template<
typename Lhs,
typename Rhs,
typename ResultType>
204 struct conservative_sparse_sparse_product_selector<Lhs,Rhs,ResultType,ColMajor,ColMajor,
RowMajor>
206 typedef typename traits<typename remove_all<Lhs>::type>::Scalar Scalar;
208 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res)
210 typedef SparseMatrix<typename ResultType::Scalar,ColMajor,typename ResultType::StorageIndex> ColMajorMatrix;
211 ColMajorMatrix resCol(lhs.rows(), rhs.cols());
212 internal::conservative_sparse_sparse_product_impl<Lhs,Rhs,ColMajorMatrix>(lhs, rhs, resCol);
217 template<
typename Lhs,
typename Rhs,
typename ResultType>
218 struct conservative_sparse_sparse_product_selector<Lhs,Rhs,ResultType,
RowMajor,ColMajor,
RowMajor>
220 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res)
222 typedef SparseMatrix<typename ResultType::Scalar,ColMajor,typename ResultType::StorageIndex> ColMajorMatrix;
223 ColMajorMatrix lhsCol = lhs;
224 ColMajorMatrix resCol(lhs.rows(), rhs.cols());
225 internal::conservative_sparse_sparse_product_impl<ColMajorMatrix,Rhs,ColMajorMatrix>(lhsCol, rhs, resCol);
230 template<
typename Lhs,
typename Rhs,
typename ResultType>
231 struct conservative_sparse_sparse_product_selector<Lhs,Rhs,ResultType,ColMajor,
RowMajor,
RowMajor>
233 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res)
235 typedef SparseMatrix<typename ResultType::Scalar,ColMajor,typename ResultType::StorageIndex> ColMajorMatrix;
236 ColMajorMatrix rhsCol = rhs;
237 ColMajorMatrix resCol(lhs.rows(), rhs.cols());
238 internal::conservative_sparse_sparse_product_impl<Lhs,ColMajorMatrix,ColMajorMatrix>(lhs, rhsCol, resCol);
243 template<
typename Lhs,
typename Rhs,
typename ResultType>
246 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res)
248 typedef SparseMatrix<typename ResultType::Scalar,RowMajor,typename ResultType::StorageIndex> RowMajorMatrix;
249 typedef SparseMatrix<typename ResultType::Scalar,ColMajor,typename ResultType::StorageIndex> ColMajorMatrix;
250 RowMajorMatrix resRow(lhs.rows(),rhs.cols());
251 internal::conservative_sparse_sparse_product_impl<Rhs,Lhs,RowMajorMatrix>(rhs, lhs, resRow);
253 ColMajorMatrix resCol(resRow);
263 template<
typename Lhs,
typename Rhs,
typename ResultType>
264 static void sparse_sparse_to_dense_product_impl(
const Lhs& lhs,
const Rhs& rhs, ResultType& res)
266 typedef typename remove_all<Lhs>::type::Scalar Scalar;
267 Index cols = rhs.outerSize();
268 eigen_assert(lhs.outerSize() == rhs.innerSize());
270 evaluator<Lhs> lhsEval(lhs);
271 evaluator<Rhs> rhsEval(rhs);
273 for (
Index j=0; j<cols; ++j)
275 for (
typename evaluator<Rhs>::InnerIterator rhsIt(rhsEval, j); rhsIt; ++rhsIt)
277 Scalar y = rhsIt.value();
278 Index k = rhsIt.index();
279 for (
typename evaluator<Lhs>::InnerIterator lhsIt(lhsEval, k); lhsIt; ++lhsIt)
281 Index i = lhsIt.index();
282 Scalar x = lhsIt.value();
283 res.coeffRef(i,j) += x * y;
294 template<
typename Lhs,
typename Rhs,
typename ResultType,
296 int RhsStorageOrder = (traits<Rhs>::Flags&RowMajorBit) ?
RowMajor : ColMajor>
297 struct sparse_sparse_to_dense_product_selector;
299 template<
typename Lhs,
typename Rhs,
typename ResultType>
300 struct sparse_sparse_to_dense_product_selector<Lhs,Rhs,ResultType,ColMajor,ColMajor>
302 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res)
304 internal::sparse_sparse_to_dense_product_impl<Lhs,Rhs,ResultType>(lhs, rhs, res);
308 template<
typename Lhs,
typename Rhs,
typename ResultType>
309 struct sparse_sparse_to_dense_product_selector<Lhs,Rhs,ResultType,
RowMajor,ColMajor>
311 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res)
313 typedef SparseMatrix<typename ResultType::Scalar,ColMajor,typename ResultType::StorageIndex> ColMajorMatrix;
314 ColMajorMatrix lhsCol(lhs);
315 internal::sparse_sparse_to_dense_product_impl<ColMajorMatrix,Rhs,ResultType>(lhsCol, rhs, res);
319 template<
typename Lhs,
typename Rhs,
typename ResultType>
320 struct sparse_sparse_to_dense_product_selector<Lhs,Rhs,ResultType,ColMajor,
RowMajor>
322 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res)
324 typedef SparseMatrix<typename ResultType::Scalar,ColMajor,typename ResultType::StorageIndex> ColMajorMatrix;
325 ColMajorMatrix rhsCol(rhs);
326 internal::sparse_sparse_to_dense_product_impl<Lhs,ColMajorMatrix,ResultType>(lhs, rhsCol, res);
330 template<
typename Lhs,
typename Rhs,
typename ResultType>
331 struct sparse_sparse_to_dense_product_selector<Lhs,Rhs,ResultType,
RowMajor,
RowMajor>
333 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res)
335 Transpose<ResultType> trRes(res);
336 internal::sparse_sparse_to_dense_product_impl<Rhs,Lhs,Transpose<ResultType> >(rhs, lhs, trRes);
345 #endif // EIGEN_CONSERVATIVESPARSESPARSEPRODUCT_H Definition: Constants.h:320
Namespace containing all symbols from the Eigen library.
Definition: Core:287
const unsigned int RowMajorBit
Definition: Constants.h:61
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
Definition: Eigen_Colamd.h:50
Definition: Constants.h:322