Eigen  5.0.1
 
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
69template <typename Derived>
70struct has_compressed_access : bool_constant<(int(traits<Derived>::Flags) & CompressedAccessBit) != 0> {};
71
72template <typename T, int Rows, int Cols, int Flags>
73struct sparse_eval;
74
75template <typename T>
76struct eval<T, Sparse> : sparse_eval<T, traits<T>::RowsAtCompileTime, traits<T>::ColsAtCompileTime, traits<T>::Flags> {
77};
78
79template <typename T, int Cols, int Flags>
80struct sparse_eval<T, 1, Cols, Flags> {
81 using Scalar_ = typename traits<T>::Scalar;
82 using StorageIndex_ = typename traits<T>::StorageIndex;
83
84 public:
85 using type = SparseVector<Scalar_, RowMajor, StorageIndex_>;
86};
87
88template <typename T, int Rows, int Flags>
89struct sparse_eval<T, Rows, 1, Flags> {
90 using Scalar_ = typename traits<T>::Scalar;
91 using StorageIndex_ = typename traits<T>::StorageIndex;
92
93 public:
94 using type = SparseVector<Scalar_, ColMajor, StorageIndex_>;
95};
96
97// TODO: consider unifying with plain_matrix_type<T, Sparse>.
98template <typename T, int Rows, int Cols, int Flags>
99struct sparse_eval {
100 using Scalar_ = typename traits<T>::Scalar;
101 using StorageIndex_ = typename traits<T>::StorageIndex;
102 enum { Options_ = ((Flags & RowMajorBit) == RowMajorBit) ? RowMajor : ColMajor };
103
104 public:
105 using type = SparseMatrix<Scalar_, Options_, StorageIndex_>;
106};
107
108template <typename T, int Flags>
109struct sparse_eval<T, 1, 1, Flags> {
110 using Scalar_ = typename traits<T>::Scalar;
111
112 public:
113 using type = Matrix<Scalar_, 1, 1>;
114};
115
116template <typename T>
117struct plain_matrix_type<T, Sparse> {
118 using Scalar_ = typename traits<T>::Scalar;
119 using StorageIndex_ = typename traits<T>::StorageIndex;
120 enum { Options_ = ((evaluator<T>::Flags & RowMajorBit) == RowMajorBit) ? RowMajor : ColMajor };
121
122 public:
123 using type = SparseMatrix<Scalar_, Options_, StorageIndex_>;
124};
125
126template <typename T>
127struct plain_object_eval<T, Sparse>
128 : sparse_eval<T, traits<T>::RowsAtCompileTime, traits<T>::ColsAtCompileTime, evaluator<T>::Flags> {};
129
130template <typename Decomposition, typename RhsType>
131struct solve_traits<Decomposition, RhsType, Sparse> {
132 using PlainObject = typename sparse_eval<RhsType, RhsType::RowsAtCompileTime, RhsType::ColsAtCompileTime,
133 traits<RhsType>::Flags>::type;
134};
135
136template <typename Derived>
137struct generic_xpr_base<Derived, MatrixXpr, Sparse> {
138 using type = SparseMatrixBase<Derived>;
139};
140
141struct SparseTriangularShape {
142 static std::string debugName() { return "SparseTriangularShape"; }
143};
144struct SparseSelfAdjointShape {
145 static std::string debugName() { return "SparseSelfAdjointShape"; }
146};
147
148template <>
149struct glue_shapes<SparseShape, SelfAdjointShape> {
150 using type = SparseSelfAdjointShape;
151};
152template <>
153struct glue_shapes<SparseShape, TriangularShape> {
154 using type = SparseTriangularShape;
155};
156
157// return type of SparseCompressedBase::lower_bound;
158struct LowerBoundIndex {
159 LowerBoundIndex() = default;
160 LowerBoundIndex(Index val, bool ok) : value(val), found(ok) {}
161 Index value = -1;
162 bool found = false;
163};
164
165} // end namespace internal
166
175template <typename Scalar, typename StorageIndex = typename SparseMatrix<Scalar>::StorageIndex>
176class Triplet {
177 public:
178 Triplet() = default;
179
180 Triplet(const StorageIndex& i, const StorageIndex& j, const Scalar& v = Scalar(0)) : m_row(i), m_col(j), m_value(v) {}
181
183 const StorageIndex& row() const { return m_row; }
184
186 const StorageIndex& col() const { return m_col; }
187
189 const Scalar& value() const { return m_value; }
190
191 protected:
192 StorageIndex m_row = 0;
193 StorageIndex m_col = 0;
194 Scalar m_value = Scalar(0);
195};
196
197} // end namespace Eigen
198
199#endif // EIGEN_SPARSEUTIL_H
A versatile sparse matrix representation.
Definition SparseMatrix.h:122
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:186
const Scalar & value() const
Definition SparseUtil.h:189
const StorageIndex & row() const
Definition SparseUtil.h:183
@ ColMajor
Definition Constants.h:319
@ RowMajor
Definition Constants.h:321
constexpr unsigned int RowMajorBit
Definition Constants.h:71