Eigen  5.0.1-dev+master
 
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
38template <typename Derived>
39template <typename OtherDerived>
40Derived& SparseMatrixBase<Derived>::operator*=(const SparseMatrixBase<OtherDerived>& other) {
41 return derived() = derived() * other.derived();
42}
43
44namespace internal {
45
46// sparse * sparse
47template <typename Lhs, typename Rhs, int ProductType>
48struct generic_product_impl<Lhs, Rhs, SparseShape, SparseShape, ProductType> {
49 template <typename Dest>
50 static void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) {
51 evalTo(dst, lhs, rhs, typename evaluator_traits<Dest>::Shape());
52 }
53
54 // dense += sparse * sparse
55 template <typename Dest, typename ActualLhs,
56 std::enable_if_t<std::is_same<typename evaluator_traits<Dest>::Shape, DenseShape>::value, int> = 0>
57 static void addTo(Dest& dst, const ActualLhs& lhs, const Rhs& rhs) {
58 using LhsNested = typename nested_eval<ActualLhs, Dynamic>::type;
59 using RhsNested = typename nested_eval<Rhs, Dynamic>::type;
60 LhsNested lhsNested(lhs);
61 RhsNested rhsNested(rhs);
62 internal::sparse_sparse_to_dense_product_selector<remove_all_t<LhsNested>, remove_all_t<RhsNested>, Dest>::run(
63 lhsNested, rhsNested, dst);
64 }
65
66 // dense -= sparse * sparse
67 template <typename Dest,
68 std::enable_if_t<std::is_same<typename evaluator_traits<Dest>::Shape, DenseShape>::value, int> = 0>
69 static void subTo(Dest& dst, const Lhs& lhs, const Rhs& rhs) {
70 addTo(dst, -lhs, rhs);
71 }
72
73 protected:
74 // sparse = sparse * sparse
75 template <typename Dest>
76 static void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs, SparseShape) {
77 using LhsNested = typename nested_eval<Lhs, Dynamic>::type;
78 using RhsNested = typename nested_eval<Rhs, Dynamic>::type;
79 LhsNested lhsNested(lhs);
80 RhsNested rhsNested(rhs);
81 internal::conservative_sparse_sparse_product_selector<remove_all_t<LhsNested>, remove_all_t<RhsNested>, Dest>::run(
82 lhsNested, rhsNested, dst);
83 }
84
85 // dense = sparse * sparse
86 template <typename Dest>
87 static void evalTo(Dest& dst, const Lhs& lhs, const Rhs& rhs, DenseShape) {
88 dst.setZero();
89 addTo(dst, lhs, rhs);
90 }
91};
92
93// sparse * sparse-triangular
94template <typename Lhs, typename Rhs, int ProductType>
95struct generic_product_impl<Lhs, Rhs, SparseShape, SparseTriangularShape, ProductType>
96 : public generic_product_impl<Lhs, Rhs, SparseShape, SparseShape, ProductType> {};
97
98// sparse-triangular * sparse
99template <typename Lhs, typename Rhs, int ProductType>
100struct generic_product_impl<Lhs, Rhs, SparseTriangularShape, SparseShape, ProductType>
101 : public generic_product_impl<Lhs, Rhs, SparseShape, SparseShape, ProductType> {};
102
103// dense = sparse-product (can be sparse*sparse, sparse*perm, etc.)
104template <typename DstXprType, typename Lhs, typename Rhs>
105struct Assignment<
106 DstXprType, Product<Lhs, Rhs, AliasFreeProduct>,
107 internal::assign_op<typename DstXprType::Scalar, typename Product<Lhs, Rhs, AliasFreeProduct>::Scalar>,
108 Sparse2Dense> {
109 using SrcXprType = Product<Lhs, Rhs, AliasFreeProduct>;
110 static void run(DstXprType& dst, const SrcXprType& src,
111 const internal::assign_op<typename DstXprType::Scalar, typename SrcXprType::Scalar>&) {
112 Index dstRows = src.rows();
113 Index dstCols = src.cols();
114 if ((dst.rows() != dstRows) || (dst.cols() != dstCols)) dst.resize(dstRows, dstCols);
115
116 generic_product_impl<Lhs, Rhs>::evalTo(dst, src.lhs(), src.rhs());
117 }
118};
119
120// dense += sparse-product (can be sparse*sparse, sparse*perm, etc.)
121template <typename DstXprType, typename Lhs, typename Rhs>
122struct Assignment<
123 DstXprType, Product<Lhs, Rhs, AliasFreeProduct>,
124 internal::add_assign_op<typename DstXprType::Scalar, typename Product<Lhs, Rhs, AliasFreeProduct>::Scalar>,
125 Sparse2Dense> {
126 using SrcXprType = Product<Lhs, Rhs, AliasFreeProduct>;
127 static void run(DstXprType& dst, const SrcXprType& src,
128 const internal::add_assign_op<typename DstXprType::Scalar, typename SrcXprType::Scalar>&) {
129 generic_product_impl<Lhs, Rhs>::addTo(dst, src.lhs(), src.rhs());
130 }
131};
132
133// dense -= sparse-product (can be sparse*sparse, sparse*perm, etc.)
134template <typename DstXprType, typename Lhs, typename Rhs>
135struct Assignment<
136 DstXprType, Product<Lhs, Rhs, AliasFreeProduct>,
137 internal::sub_assign_op<typename DstXprType::Scalar, typename Product<Lhs, Rhs, AliasFreeProduct>::Scalar>,
138 Sparse2Dense> {
139 using SrcXprType = Product<Lhs, Rhs, AliasFreeProduct>;
140 static void run(DstXprType& dst, const SrcXprType& src,
141 const internal::sub_assign_op<typename DstXprType::Scalar, typename SrcXprType::Scalar>&) {
142 generic_product_impl<Lhs, Rhs>::subTo(dst, src.lhs(), src.rhs());
143 }
144};
145
146template <typename Lhs, typename Rhs, int Options>
147struct unary_evaluator<SparseView<Product<Lhs, Rhs, Options> >, IteratorBased>
148 : public evaluator<typename Product<Lhs, Rhs, DefaultProduct>::PlainObject> {
149 using XprType = SparseView<Product<Lhs, Rhs, Options>>;
150 using PlainObject = typename XprType::PlainObject;
151 using Base = evaluator<PlainObject>;
152
153 explicit unary_evaluator(const XprType& xpr) : m_result(xpr.rows(), xpr.cols()) {
154 internal::construct_at<Base>(this, m_result);
155 using LhsNested = typename nested_eval<Lhs, Dynamic>::type;
156 using RhsNested = typename nested_eval<Rhs, Dynamic>::type;
157 LhsNested lhsNested(xpr.nestedExpression().lhs());
158 RhsNested rhsNested(xpr.nestedExpression().rhs());
159
160 internal::sparse_sparse_product_with_pruning_selector<remove_all_t<LhsNested>, remove_all_t<RhsNested>,
161 PlainObject>::run(lhsNested, rhsNested, m_result,
162 numext::abs(xpr.reference()) *
163 xpr.epsilon());
164 }
165
166 protected:
167 PlainObject m_result;
168};
169
170} // end namespace internal
171
172// sparse matrix = sparse-product (can be sparse*sparse, sparse*perm, etc.)
173template <typename Scalar, int Options_, typename StorageIndex_>
174template <typename Lhs, typename Rhs>
175SparseMatrix<Scalar, Options_, StorageIndex_>& SparseMatrix<Scalar, Options_, StorageIndex_>::operator=(
177 SparseMatrix dst(src.rows(), src.cols());
178 internal::generic_product_impl<Lhs, Rhs>::evalTo(dst, src.lhs(), src.rhs());
179 this->swap(dst);
180 return *this;
181}
182
183} // end namespace Eigen
184
185#endif // EIGEN_SPARSEPRODUCT_H
Expression of the product of two arbitrary matrices or vectors.
Definition Product.h:211
A versatile sparse matrix representation.
Definition SparseMatrix.h:121
constexpr Derived & derived()
Definition EigenBase.h:50