Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
SparseUtil.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008-2014 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_SPARSEUTIL_H
12#define EIGEN_SPARSEUTIL_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19#ifdef NDEBUG
20#define EIGEN_DBG_SPARSE(X)
21#else
22#define EIGEN_DBG_SPARSE(X) X
23#endif
24
25#define EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, Op) \
26 template <typename OtherDerived> \
27 EIGEN_STRONG_INLINE Derived& operator Op(const Eigen::SparseMatrixBase<OtherDerived>& other) { \
28 return Base::operator Op(other.derived()); \
29 } \
30 EIGEN_STRONG_INLINE Derived& operator Op(const Derived & other) { return Base::operator Op(other); }
31
32#define EIGEN_SPARSE_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Derived, Op) \
33 template <typename Other> \
34 EIGEN_STRONG_INLINE Derived& operator Op(const Other & scalar) { \
35 return Base::operator Op(scalar); \
36 }
37
38#define EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATORS(Derived) EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, =)
39
40#define EIGEN_SPARSE_PUBLIC_INTERFACE(Derived) EIGEN_GENERIC_PUBLIC_INTERFACE(Derived)
41
42const int CoherentAccessPattern = 0x1;
43const int InnerRandomAccessPattern = 0x2 | CoherentAccessPattern;
44const int OuterRandomAccessPattern = 0x4 | CoherentAccessPattern;
45const int RandomAccessPattern = 0x8 | OuterRandomAccessPattern | InnerRandomAccessPattern;
46
47template <typename Scalar_, int Flags_ = 0, typename StorageIndex_ = int>
48class SparseMatrix;
49template <typename Scalar_, int Flags_ = 0, typename StorageIndex_ = int>
50class SparseVector;
51
52template <typename MatrixType, unsigned int UpLo>
54template <typename MatrixType>
55class SparseView;
56
57template <typename MatrixType, int UpLo>
58class SparseSymmetricPermutationProduct;
59
60namespace internal {
61
62// Retain the existing evaluation order for products declared commutative.
63template <typename LhsScalar, typename RhsScalar, bool Reverse,
64 bool Commutative = functor_is_commutative<scalar_product_op<LhsScalar, RhsScalar>>::value>
65struct sparse_scalar_product_op {
66 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE auto operator()(const LhsScalar& lhs, const RhsScalar& rhs) const {
67 return lhs * rhs;
68 }
69 // Keep multiply and addition in one expression for -ffp-contract=on.
70 template <typename Acc>
71 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void accumulate(Acc& acc, const LhsScalar& lhs, const RhsScalar& rhs) const {
72 acc += lhs * rhs;
73 }
74};
75
76template <typename LhsScalar, typename RhsScalar>
77struct sparse_scalar_product_op<LhsScalar, RhsScalar, true, false> {
78 EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE auto operator()(const LhsScalar& lhs, const RhsScalar& rhs) const {
79 return rhs * lhs;
80 }
81 template <typename Acc>
82 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void accumulate(Acc& acc, const LhsScalar& lhs, const RhsScalar& rhs) const {
83 acc += rhs * lhs;
84 }
85};
86
94template <typename Derived>
95struct has_compressed_access : bool_constant<(int(traits<Derived>::Flags) & CompressedAccessBit) != 0> {};
96
97// Defined in SparseApproxComparisons.h; BlockSparseMatrix.h uses it before that.
98template <typename Scalar, bool = use_scaled_comparison<Scalar>::value>
99struct sparse_approx_comparison;
100
101template <typename T, int Rows, int Cols, int Flags>
102struct sparse_eval;
103
104template <typename T>
105struct eval<T, Sparse> : sparse_eval<T, traits<T>::RowsAtCompileTime, traits<T>::ColsAtCompileTime, traits<T>::Flags> {
106};
107
108template <typename T, int Cols, int Flags>
109struct sparse_eval<T, 1, Cols, Flags> {
110 using Scalar_ = typename traits<T>::Scalar;
111 using StorageIndex_ = typename traits<T>::StorageIndex;
112
113 public:
114 using type = SparseVector<Scalar_, RowMajor, StorageIndex_>;
115};
116
117template <typename T, int Rows, int Flags>
118struct sparse_eval<T, Rows, 1, Flags> {
119 using Scalar_ = typename traits<T>::Scalar;
120 using StorageIndex_ = typename traits<T>::StorageIndex;
121
122 public:
123 using type = SparseVector<Scalar_, ColMajor, StorageIndex_>;
124};
125
126// TODO: consider unifying with plain_matrix_type<T, Sparse>.
127template <typename T, int Rows, int Cols, int Flags>
128struct sparse_eval {
129 using Scalar_ = typename traits<T>::Scalar;
130 using StorageIndex_ = typename traits<T>::StorageIndex;
131 enum { Options_ = ((Flags & RowMajorBit) == RowMajorBit) ? RowMajor : ColMajor };
132
133 public:
134 using type = SparseMatrix<Scalar_, Options_, StorageIndex_>;
135};
136
137template <typename T, int Flags>
138struct sparse_eval<T, 1, 1, Flags> {
139 using Scalar_ = typename traits<T>::Scalar;
140
141 public:
142 using type = Matrix<Scalar_, 1, 1>;
143};
144
145template <typename T>
146struct plain_matrix_type<T, Sparse> {
147 using Scalar_ = typename traits<T>::Scalar;
148 using StorageIndex_ = typename traits<T>::StorageIndex;
149 enum { Options_ = ((evaluator<T>::Flags & RowMajorBit) == RowMajorBit) ? RowMajor : ColMajor };
150
151 public:
152 using type = SparseMatrix<Scalar_, Options_, StorageIndex_>;
153};
154
155template <typename T>
156struct plain_object_eval<T, Sparse>
157 : sparse_eval<T, traits<T>::RowsAtCompileTime, traits<T>::ColsAtCompileTime, evaluator<T>::Flags> {};
158
159template <typename Decomposition, typename RhsType>
160struct solve_traits<Decomposition, RhsType, Sparse> {
161 using PlainObject = typename sparse_eval<RhsType, RhsType::RowsAtCompileTime, RhsType::ColsAtCompileTime,
162 traits<RhsType>::Flags>::type;
163};
164
165template <typename Derived>
166struct generic_xpr_base<Derived, MatrixXpr, Sparse> {
167 using type = SparseMatrixBase<Derived>;
168};
169
170struct SparseTriangularShape {
171 static std::string debugName() { return "SparseTriangularShape"; }
172};
173struct SparseSelfAdjointShape {
174 static std::string debugName() { return "SparseSelfAdjointShape"; }
175};
176
177template <>
178struct glue_shapes<SparseShape, SelfAdjointShape> {
179 using type = SparseSelfAdjointShape;
180};
181template <>
182struct glue_shapes<SparseShape, TriangularShape> {
183 using type = SparseTriangularShape;
184};
185
186// return type of SparseCompressedBase::lower_bound;
187struct LowerBoundIndex {
188 LowerBoundIndex() = default;
189 LowerBoundIndex(Index val, bool ok) : value(val), found(ok) {}
190 Index value = -1;
191 bool found = false;
192};
193
194} // end namespace internal
195
204template <typename Scalar, typename StorageIndex = typename SparseMatrix<Scalar>::StorageIndex>
205class Triplet {
206 public:
207 Triplet() = default;
208
209 Triplet(const StorageIndex& i, const StorageIndex& j, const Scalar& v = Scalar(0)) : m_row(i), m_col(j), m_value(v) {}
210
212 const StorageIndex& row() const { return m_row; }
213
215 const StorageIndex& col() const { return m_col; }
216
218 const Scalar& value() const { return m_value; }
219
220 protected:
221 StorageIndex m_row = 0;
222 StorageIndex m_col = 0;
223 Scalar m_value = Scalar(0);
224};
225
226} // end namespace Eigen
227
228#endif // EIGEN_SPARSEUTIL_H
A versatile sparse matrix representation.
Definition SparseMatrix.h:121
Pseudo expression to manipulate a triangular sparse matrix as a selfadjoint matrix.
Definition SparseSelfAdjointView.h:53
a sparse vector class
Definition SparseVector.h:63
Expression of a dense or sparse matrix with zero or too small values removed.
Definition SparseView.h:46
const StorageIndex & col() const
Definition SparseUtil.h:215
const Scalar & value() const
Definition SparseUtil.h:218
const StorageIndex & row() const
Definition SparseUtil.h:212
@ ColMajor
Definition Constants.h:319
@ RowMajor
Definition Constants.h:321
constexpr unsigned int RowMajorBit
Definition Constants.h:71