11#ifndef EIGEN_SPARSESPARSEPRODUCTWITHPRUNING_H
12#define EIGEN_SPARSESPARSEPRODUCTWITHPRUNING_H
15#include "./InternalHeaderCheck.h"
22template <
typename Lhs,
typename Rhs,
typename ResultType,
bool ReverseProduct = false>
23static void sparse_sparse_product_with_pruning_impl(
const Lhs& lhs,
const Rhs& rhs, ResultType& res,
24 const typename ResultType::RealScalar& tolerance) {
25 using LhsScalar =
typename remove_all_t<Lhs>::Scalar;
26 using RhsScalar =
typename remove_all_t<Rhs>::Scalar;
27 using ResScalar =
typename remove_all_t<ResultType>::Scalar;
28 const sparse_scalar_product_op<LhsScalar, RhsScalar, ReverseProduct> product;
29 using StorageIndex =
typename remove_all_t<Lhs>::StorageIndex;
32 Index rows = lhs.innerSize();
33 Index cols = rhs.outerSize();
34 eigen_assert(lhs.outerSize() == rhs.innerSize());
37 AmbiVector<ResScalar, StorageIndex> tempVector(rows);
40 EIGEN_IF_CONSTEXPR (ResultType::IsRowMajor) {
41 res.resize(cols, rows);
43 res.resize(rows, cols);
46 evaluator<Lhs> lhsEval(lhs);
47 evaluator<Rhs> rhsEval(rhs);
55 Index estimated_nnz_prod = lhsEval.nonZerosEstimate() + rhsEval.nonZerosEstimate();
57 res.reserve(estimated_nnz_prod);
58 double ratioColRes = double(estimated_nnz_prod) / (double(lhs.rows()) * double(rhs.cols()));
59 for (Index j = 0; j < cols; ++j) {
61 tempVector.init(ratioColRes);
63 for (
typename evaluator<Rhs>::InnerIterator rhsIt(rhsEval, j); rhsIt; ++rhsIt) {
66 RhsScalar x = rhsIt.value();
67 for (
typename evaluator<Lhs>::InnerIterator lhsIt(lhsEval, rhsIt.index()); lhsIt; ++lhsIt) {
68 product.accumulate(tempVector.coeffRef(lhsIt.index()), lhsIt.value(), x);
73 res.insertBackByOuterInner(j, it.index()) = it.value();
78template <typename Lhs, typename Rhs, typename ResultType, int LhsStorageOrder = traits<Lhs>::Flags &
RowMajorBit,
79 int RhsStorageOrder = traits<Rhs>::Flags &
RowMajorBit,
80 int ResStorageOrder = traits<ResultType>::Flags &
RowMajorBit>
81struct sparse_sparse_product_with_pruning_selector;
83template <
typename Lhs,
typename Rhs,
typename ResultType>
85 using RealScalar =
typename ResultType::RealScalar;
87 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res,
const RealScalar& tolerance) {
88 remove_all_t<ResultType> res_{res.rows(), res.cols()};
89 internal::sparse_sparse_product_with_pruning_impl<Lhs, Rhs, ResultType>(lhs, rhs, res_, tolerance);
94template <
typename Lhs,
typename Rhs,
typename ResultType>
96 using RealScalar =
typename ResultType::RealScalar;
97 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res,
const RealScalar& tolerance) {
99 using SparseTemporaryType = SparseMatrix<typename ResultType::Scalar, ColMajor, typename ResultType::StorageIndex>;
100 SparseTemporaryType res_{res.rows(), res.cols()};
101 internal::sparse_sparse_product_with_pruning_impl<Lhs, Rhs, SparseTemporaryType>(lhs, rhs, res_, tolerance);
106template <
typename Lhs,
typename Rhs,
typename ResultType>
108 using RealScalar =
typename ResultType::RealScalar;
109 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res,
const RealScalar& tolerance) {
111 remove_all_t<ResultType> res_{res.rows(), res.cols()};
112 internal::sparse_sparse_product_with_pruning_impl<Rhs, Lhs, ResultType, true>(rhs, lhs, res_, tolerance);
117template <
typename Lhs,
typename Rhs,
typename ResultType>
119 using RealScalar =
typename ResultType::RealScalar;
120 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res,
const RealScalar& tolerance) {
121 using ColMajorMatrixLhs = SparseMatrix<typename Lhs::Scalar, ColMajor, typename Lhs::StorageIndex>;
122 using ColMajorMatrixRhs = SparseMatrix<typename Rhs::Scalar, ColMajor, typename Lhs::StorageIndex>;
123 ColMajorMatrixLhs colLhs(lhs);
124 ColMajorMatrixRhs colRhs(rhs);
125 internal::sparse_sparse_product_with_pruning_impl<ColMajorMatrixLhs, ColMajorMatrixRhs, ResultType>(colLhs, colRhs,
130template <
typename Lhs,
typename Rhs,
typename ResultType>
132 using RealScalar =
typename ResultType::RealScalar;
133 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res,
const RealScalar& tolerance) {
134 using RowMajorMatrixLhs = SparseMatrix<typename Lhs::Scalar, RowMajor, typename Lhs::StorageIndex>;
135 RowMajorMatrixLhs rowLhs(lhs);
136 sparse_sparse_product_with_pruning_selector<RowMajorMatrixLhs, Rhs, ResultType, RowMajor, RowMajor>::run(
137 rowLhs, rhs, res, tolerance);
141template <
typename Lhs,
typename Rhs,
typename ResultType>
143 using RealScalar =
typename ResultType::RealScalar;
144 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res,
const RealScalar& tolerance) {
145 using RowMajorMatrixRhs = SparseMatrix<typename Rhs::Scalar, RowMajor, typename Lhs::StorageIndex>;
146 RowMajorMatrixRhs rowRhs(rhs);
147 sparse_sparse_product_with_pruning_selector<Lhs, RowMajorMatrixRhs, ResultType, RowMajor, RowMajor, RowMajor>::run(
148 lhs, rowRhs, res, tolerance);
152template <
typename Lhs,
typename Rhs,
typename ResultType>
154 using RealScalar =
typename ResultType::RealScalar;
155 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res,
const RealScalar& tolerance) {
156 using ColMajorMatrixRhs = SparseMatrix<typename Rhs::Scalar, ColMajor, typename Lhs::StorageIndex>;
157 ColMajorMatrixRhs colRhs(rhs);
158 internal::sparse_sparse_product_with_pruning_impl<Lhs, ColMajorMatrixRhs, ResultType>(lhs, colRhs, res, tolerance);
162template <
typename Lhs,
typename Rhs,
typename ResultType>
164 using RealScalar =
typename ResultType::RealScalar;
165 static void run(
const Lhs& lhs,
const Rhs& rhs, ResultType& res,
const RealScalar& tolerance) {
166 using ColMajorMatrixLhs = SparseMatrix<typename Lhs::Scalar, ColMajor, typename Lhs::StorageIndex>;
167 ColMajorMatrixLhs colLhs(lhs);
168 internal::sparse_sparse_product_with_pruning_impl<ColMajorMatrixLhs, Rhs, ResultType>(colLhs, rhs, res, tolerance);
Definition AmbiVector.h:312
@ ColMajor
Definition Constants.h:319
@ RowMajor
Definition Constants.h:321
constexpr unsigned int RowMajorBit
Definition Constants.h:71