Eigen  5.0.1
 
Loading...
Searching...
No Matches
SparseProduct.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008-2015 Gael Guennebaud <gael.guennebaud@inria.fr>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_SPARSEPRODUCT_H
12#define EIGEN_SPARSEPRODUCT_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
30template <typename Derived>
31template <typename OtherDerived>
32inline const Product<Derived, OtherDerived, AliasFreeProduct> SparseMatrixBase<Derived>::operator*(
33 const SparseMatrixBase<OtherDerived>& other) const {
35}
36
37namespace internal {
38
39// sparse * sparse
40template <typename Lhs, typename Rhs, int ProductType>
41struct generic_product_impl<Lhs, Rhs, SparseShape, SparseShape, ProductType> {
42 template <typename Dest>
43 static void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) {
44 evalTo(dst, lhs, rhs, typename evaluator_traits<Dest>::Shape());
45 }
46
47 // dense += sparse * sparse
48 template <typename Dest, typename ActualLhs,
49 std::enable_if_t<std::is_same<typename evaluator_traits<Dest>::Shape, DenseShape>::value, int> = 0>
50 static void addTo(Dest& dst, const ActualLhs& lhs, const Rhs& rhs) {
51 using LhsNested = typename nested_eval<ActualLhs, Dynamic>::type;
52 using RhsNested = typename nested_eval<Rhs, Dynamic>::type;
53 LhsNested lhsNested(lhs);
54 RhsNested rhsNested(rhs);
55 internal::sparse_sparse_to_dense_product_selector<remove_all_t<LhsNested>, remove_all_t<RhsNested>, Dest>::run(
56 lhsNested, rhsNested, dst);
57 }
58
59 // dense -= sparse * sparse
60 template <typename Dest,
61 std::enable_if_t<std::is_same<typename evaluator_traits<Dest>::Shape, DenseShape>::value, int> = 0>
62 static void subTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) {
63 addTo(dst, -lhs, rhs);
64 }
65
66 protected:
67 // sparse = sparse * sparse
68 template <typename Dest>
69 static void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs, SparseShape) {
70 using LhsNested = typename nested_eval<Lhs, Dynamic>::type;
71 using RhsNested = typename nested_eval<Rhs, Dynamic>::type;
72 LhsNested lhsNested(lhs);
73 RhsNested rhsNested(rhs);
74 internal::conservative_sparse_sparse_product_selector<remove_all_t<LhsNested>, remove_all_t<RhsNested>, Dest>::run(
75 lhsNested, rhsNested, dst);
76 }
77
78 // dense = sparse * sparse
79 template <typename Dest>
80 static void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs, DenseShape) {
81 dst.setZero();
82 addTo(dst, lhs, rhs);
83 }
84};
85
86// sparse * sparse-triangular
87template <typename Lhs, typename Rhs, int ProductType>
88struct generic_product_impl<Lhs, Rhs, SparseShape, SparseTriangularShape, ProductType>
89 : public generic_product_impl<Lhs, Rhs, SparseShape, SparseShape, ProductType> {};
90
91// sparse-triangular * sparse
92template <typename Lhs, typename Rhs, int ProductType>
93struct generic_product_impl<Lhs, Rhs, SparseTriangularShape, SparseShape, ProductType>
94 : public generic_product_impl<Lhs, Rhs, SparseShape, SparseShape, ProductType> {};
95
96// dense = sparse-product (can be sparse*sparse, sparse*perm, etc.)
97template <typename DstXprType, typename Lhs, typename Rhs>
98struct Assignment<
99 DstXprType, Product<Lhs, Rhs, AliasFreeProduct>,
100 internal::assign_op<typename DstXprType::Scalar, typename Product<Lhs, Rhs, AliasFreeProduct>::Scalar>,
101 Sparse2Dense> {
102 using SrcXprType = Product<Lhs, Rhs, AliasFreeProduct>;
103 static void run(DstXprType& dst, const SrcXprType& src,
104 const internal::assign_op<typename DstXprType::Scalar, typename SrcXprType::Scalar>&) {
105 Index dstRows = src.rows();
106 Index dstCols = src.cols();
107 if ((dst.rows() != dstRows) || (dst.cols() != dstCols)) dst.resize(dstRows, dstCols);
108
109 generic_product_impl<Lhs, Rhs>::evalTo(dst, src.lhs(), src.rhs());
110 }
111};
112
113// dense += sparse-product (can be sparse*sparse, sparse*perm, etc.)
114template <typename DstXprType, typename Lhs, typename Rhs>
115struct Assignment<
116 DstXprType, Product<Lhs, Rhs, AliasFreeProduct>,
117 internal::add_assign_op<typename DstXprType::Scalar, typename Product<Lhs, Rhs, AliasFreeProduct>::Scalar>,
118 Sparse2Dense> {
119 using SrcXprType = Product<Lhs, Rhs, AliasFreeProduct>;
120 static void run(DstXprType& dst, const SrcXprType& src,
121 const internal::add_assign_op<typename DstXprType::Scalar, typename SrcXprType::Scalar>&) {
122 generic_product_impl<Lhs, Rhs>::addTo(dst, src.lhs(), src.rhs());
123 }
124};
125
126// dense -= sparse-product (can be sparse*sparse, sparse*perm, etc.)
127template <typename DstXprType, typename Lhs, typename Rhs>
128struct Assignment<
129 DstXprType, Product<Lhs, Rhs, AliasFreeProduct>,
130 internal::sub_assign_op<typename DstXprType::Scalar, typename Product<Lhs, Rhs, AliasFreeProduct>::Scalar>,
131 Sparse2Dense> {
132 using SrcXprType = Product<Lhs, Rhs, AliasFreeProduct>;
133 static void run(DstXprType& dst, const SrcXprType& src,
134 const internal::sub_assign_op<typename DstXprType::Scalar, typename SrcXprType::Scalar>&) {
135 generic_product_impl<Lhs, Rhs>::subTo(dst, src.lhs(), src.rhs());
136 }
137};
138
139template <typename Lhs, typename Rhs, int Options>
140struct unary_evaluator<SparseView<Product<Lhs, Rhs, Options> >, IteratorBased>
141 : public evaluator<typename Product<Lhs, Rhs, DefaultProduct>::PlainObject> {
142 using XprType = SparseView<Product<Lhs, Rhs, Options>>;
143 using PlainObject = typename XprType::PlainObject;
144 using Base = evaluator<PlainObject>;
145
146 explicit unary_evaluator(const XprType& xpr) : m_result(xpr.rows(), xpr.cols()) {
147 using std::abs;
148 internal::construct_at<Base>(this, m_result);
149 using LhsNested = typename nested_eval<Lhs, Dynamic>::type;
150 using RhsNested = typename nested_eval<Rhs, Dynamic>::type;
151 LhsNested lhsNested(xpr.nestedExpression().lhs());
152 RhsNested rhsNested(xpr.nestedExpression().rhs());
153
154 internal::sparse_sparse_product_with_pruning_selector<remove_all_t<LhsNested>, remove_all_t<RhsNested>,
155 PlainObject>::run(lhsNested, rhsNested, m_result,
156 abs(xpr.reference()) * xpr.epsilon());
157 }
158
159 protected:
160 PlainObject m_result;
161};
162
163} // end namespace internal
164
165// sparse matrix = sparse-product (can be sparse*sparse, sparse*perm, etc.)
166template <typename Scalar, int Options_, typename StorageIndex_>
167template <typename Lhs, typename Rhs>
168SparseMatrix<Scalar, Options_, StorageIndex_>& SparseMatrix<Scalar, Options_, StorageIndex_>::operator=(
170 SparseMatrix dst(src.rows(), src.cols());
171 internal::generic_product_impl<Lhs, Rhs>::evalTo(dst, src.lhs(), src.rhs());
172 this->swap(dst);
173 return *this;
174}
175
176} // end namespace Eigen
177
178#endif // EIGEN_SPARSEPRODUCT_H
Expression of the product of two arbitrary matrices or vectors.
Definition Product.h:203
A versatile sparse matrix representation.
Definition SparseMatrix.h:122
constexpr Derived & derived()
Definition EigenBase.h:50