$darkmode
Eigen  5.0.1-dev
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 
10 #ifndef EIGEN_SPARSEUTIL_H
11 #define EIGEN_SPARSEUTIL_H
12 
13 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 #ifdef NDEBUG
19 #define EIGEN_DBG_SPARSE(X)
20 #else
21 #define EIGEN_DBG_SPARSE(X) X
22 #endif
23 
24 #define EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, Op) \
25  template <typename OtherDerived> \
26  EIGEN_STRONG_INLINE Derived& operator Op(const Eigen::SparseMatrixBase<OtherDerived>& other) { \
27  return Base::operator Op(other.derived()); \
28  } \
29  EIGEN_STRONG_INLINE Derived& operator Op(const Derived & other) { return Base::operator Op(other); }
30 
31 #define EIGEN_SPARSE_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Derived, Op) \
32  template <typename Other> \
33  EIGEN_STRONG_INLINE Derived& operator Op(const Other & scalar) { \
34  return Base::operator Op(scalar); \
35  }
36 
37 #define EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATORS(Derived) EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, =)
38 
39 #define EIGEN_SPARSE_PUBLIC_INTERFACE(Derived) EIGEN_GENERIC_PUBLIC_INTERFACE(Derived)
40 
41 const int CoherentAccessPattern = 0x1;
42 const int InnerRandomAccessPattern = 0x2 | CoherentAccessPattern;
43 const int OuterRandomAccessPattern = 0x4 | CoherentAccessPattern;
44 const int RandomAccessPattern = 0x8 | OuterRandomAccessPattern | InnerRandomAccessPattern;
45 
46 template <typename Scalar_, int Flags_ = 0, typename StorageIndex_ = int>
47 class SparseMatrix;
48 template <typename Scalar_, int Flags_ = 0, typename StorageIndex_ = int>
50 
51 template <typename MatrixType, unsigned int UpLo>
53 template <typename Lhs, typename Rhs>
54 class SparseDiagonalProduct;
55 template <typename MatrixType>
56 class SparseView;
57 
58 template <typename Lhs, typename Rhs>
59 class SparseSparseProduct;
60 template <typename Lhs, typename Rhs>
61 class SparseTimeDenseProduct;
62 template <typename Lhs, typename Rhs>
63 class DenseTimeSparseProduct;
64 template <typename Lhs, typename Rhs, bool Transpose>
65 class SparseDenseOuterProduct;
66 
67 template <typename Lhs, typename Rhs>
68 struct SparseSparseProductReturnType;
69 template <typename Lhs, typename Rhs,
70  int InnerSize = internal::min_size_prefer_fixed(internal::traits<Lhs>::ColsAtCompileTime,
71  internal::traits<Rhs>::RowsAtCompileTime)>
72 struct DenseSparseProductReturnType;
73 
74 template <typename Lhs, typename Rhs,
75  int InnerSize = internal::min_size_prefer_fixed(internal::traits<Lhs>::ColsAtCompileTime,
76  internal::traits<Rhs>::RowsAtCompileTime)>
77 struct SparseDenseProductReturnType;
78 template <typename MatrixType, int UpLo>
79 class SparseSymmetricPermutationProduct;
80 
81 namespace internal {
82 
83 template <typename T, int Rows, int Cols, int Flags>
84 struct sparse_eval;
85 
86 template <typename T>
87 struct eval<T, Sparse> : sparse_eval<T, traits<T>::RowsAtCompileTime, traits<T>::ColsAtCompileTime, traits<T>::Flags> {
88 };
89 
90 template <typename T, int Cols, int Flags>
91 struct sparse_eval<T, 1, Cols, Flags> {
92  typedef typename traits<T>::Scalar Scalar_;
93  typedef typename traits<T>::StorageIndex StorageIndex_;
94 
95  public:
97 };
98 
99 template <typename T, int Rows, int Flags>
100 struct sparse_eval<T, Rows, 1, Flags> {
101  typedef typename traits<T>::Scalar Scalar_;
102  typedef typename traits<T>::StorageIndex StorageIndex_;
103 
104  public:
105  typedef SparseVector<Scalar_, ColMajor, StorageIndex_> type;
106 };
107 
108 // TODO this seems almost identical to plain_matrix_type<T, Sparse>
109 template <typename T, int Rows, int Cols, int Flags>
110 struct sparse_eval {
111  typedef typename traits<T>::Scalar Scalar_;
112  typedef typename traits<T>::StorageIndex StorageIndex_;
113  enum { Options_ = ((Flags & RowMajorBit) == RowMajorBit) ? RowMajor : ColMajor };
114 
115  public:
116  typedef SparseMatrix<Scalar_, Options_, StorageIndex_> type;
117 };
118 
119 template <typename T, int Flags>
120 struct sparse_eval<T, 1, 1, Flags> {
121  typedef typename traits<T>::Scalar Scalar_;
122 
123  public:
124  typedef Matrix<Scalar_, 1, 1> type;
125 };
126 
127 template <typename T>
128 struct plain_matrix_type<T, Sparse> {
129  typedef typename traits<T>::Scalar Scalar_;
130  typedef typename traits<T>::StorageIndex StorageIndex_;
131  enum { Options_ = ((evaluator<T>::Flags & RowMajorBit) == RowMajorBit) ? RowMajor : ColMajor };
132 
133  public:
134  typedef SparseMatrix<Scalar_, Options_, StorageIndex_> type;
135 };
136 
137 template <typename T>
138 struct plain_object_eval<T, Sparse>
139  : sparse_eval<T, traits<T>::RowsAtCompileTime, traits<T>::ColsAtCompileTime, evaluator<T>::Flags> {};
140 
141 template <typename Decomposition, typename RhsType>
142 struct solve_traits<Decomposition, RhsType, Sparse> {
143  typedef typename sparse_eval<RhsType, RhsType::RowsAtCompileTime, RhsType::ColsAtCompileTime,
144  traits<RhsType>::Flags>::type PlainObject;
145 };
146 
147 template <typename Derived>
148 struct generic_xpr_base<Derived, MatrixXpr, Sparse> {
149  typedef SparseMatrixBase<Derived> type;
150 };
151 
152 struct SparseTriangularShape {
153  static std::string debugName() { return "SparseTriangularShape"; }
154 };
155 struct SparseSelfAdjointShape {
156  static std::string debugName() { return "SparseSelfAdjointShape"; }
157 };
158 
159 template <>
160 struct glue_shapes<SparseShape, SelfAdjointShape> {
161  typedef SparseSelfAdjointShape type;
162 };
163 template <>
164 struct glue_shapes<SparseShape, TriangularShape> {
165  typedef SparseTriangularShape type;
166 };
167 
168 // return type of SparseCompressedBase::lower_bound;
169 struct LowerBoundIndex {
170  LowerBoundIndex() : value(-1), found(false) {}
171  LowerBoundIndex(Index val, bool ok) : value(val), found(ok) {}
172  Index value;
173  bool found;
174 };
175 
176 } // end namespace internal
177 
186 template <typename Scalar, typename StorageIndex = typename SparseMatrix<Scalar>::StorageIndex>
187 class Triplet {
188  public:
189  Triplet() : m_row(0), m_col(0), m_value(0) {}
190 
191  Triplet(const StorageIndex& i, const StorageIndex& j, const Scalar& v = Scalar(0)) : m_row(i), m_col(j), m_value(v) {}
192 
194  const StorageIndex& row() const { return m_row; }
195 
197  const StorageIndex& col() const { return m_col; }
198 
200  const Scalar& value() const { return m_value; }
201 
202  protected:
203  StorageIndex m_row, m_col;
204  Scalar m_value;
205 };
206 
207 } // end namespace Eigen
208 
209 #endif // EIGEN_SPARSEUTIL_H
const StorageIndex & col() const
Definition: SparseUtil.h:197
Definition: Constants.h:318
const StorageIndex & row() const
Definition: SparseUtil.h:194
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
Pseudo expression to manipulate a triangular sparse matrix as a selfadjoint matrix.
Definition: SparseSelfAdjointView.h:52
const unsigned int RowMajorBit
Definition: Constants.h:70
a sparse vector class
Definition: SparseUtil.h:49
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
Expression of a dense or sparse matrix with zero or too small values removed.
Definition: ForwardDeclarations.h:177
Definition: Constants.h:522
A small structure to hold a non zero as a triplet (i,j,value).
Definition: SparseUtil.h:187
const Scalar & value() const
Definition: SparseUtil.h:200
Definition: Constants.h:320