SparseUtil.h
Go to the documentation of this file.
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 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
17 #ifdef NDEBUG
18 #define EIGEN_DBG_SPARSE(X)
19 #else
20 #define EIGEN_DBG_SPARSE(X) X
21 #endif
22 
23 #define EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, Op) \
24 template<typename OtherDerived> \
25 EIGEN_STRONG_INLINE Derived& operator Op(const Eigen::SparseMatrixBase<OtherDerived>& other) \
26 { \
27  return Base::operator Op(other.derived()); \
28 } \
29 EIGEN_STRONG_INLINE Derived& operator Op(const Derived& other) \
30 { \
31  return Base::operator Op(other); \
32 }
33 
34 #define EIGEN_SPARSE_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Derived, Op) \
35 template<typename Other> \
36 EIGEN_STRONG_INLINE Derived& operator Op(const Other& scalar) \
37 { \
38  return Base::operator Op(scalar); \
39 }
40 
41 #define EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATORS(Derived) \
42 EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, =)
43 
44 
45 #define EIGEN_SPARSE_PUBLIC_INTERFACE(Derived) \
46  EIGEN_GENERIC_PUBLIC_INTERFACE(Derived)
47 
48 
49 const int CoherentAccessPattern = 0x1;
53 
54 template<typename Scalar_, int Flags_ = 0, typename StorageIndex_ = int> class SparseMatrix;
55 template<typename Scalar_, int Flags_ = 0, typename StorageIndex_ = int> class SparseVector;
56 
57 template<typename MatrixType, unsigned int UpLo> class SparseSelfAdjointView;
58 template<typename Lhs, typename Rhs> class SparseDiagonalProduct;
59 template<typename MatrixType> class SparseView;
60 
61 template<typename Lhs, typename Rhs> class SparseSparseProduct;
62 template<typename Lhs, typename Rhs> class SparseTimeDenseProduct;
63 template<typename Lhs, typename Rhs> class DenseTimeSparseProduct;
64 template<typename Lhs, typename Rhs, bool Transpose> class SparseDenseOuterProduct;
65 
66 template<typename Lhs, typename Rhs> struct SparseSparseProductReturnType;
67 template<typename Lhs, typename Rhs,
68  int InnerSize = internal::min_size_prefer_fixed(internal::traits<Lhs>::ColsAtCompileTime, internal::traits<Rhs>::RowsAtCompileTime)> struct DenseSparseProductReturnType;
69 
70 template<typename Lhs, typename Rhs,
71  int InnerSize = internal::min_size_prefer_fixed(internal::traits<Lhs>::ColsAtCompileTime, internal::traits<Rhs>::RowsAtCompileTime)> struct SparseDenseProductReturnType;
72 template<typename MatrixType,int UpLo> class SparseSymmetricPermutationProduct;
73 
74 namespace internal {
75 
76 template<typename T,int Rows,int Cols,int Flags> struct sparse_eval;
77 
78 template<typename T> struct eval<T,Sparse>
79  : sparse_eval<T, traits<T>::RowsAtCompileTime,traits<T>::ColsAtCompileTime,traits<T>::Flags>
80 {};
81 
82 template<typename T,int Cols,int Flags> struct sparse_eval<T,1,Cols,Flags> {
83  typedef typename traits<T>::Scalar Scalar_;
84  typedef typename traits<T>::StorageIndex StorageIndex_;
85  public:
87 };
88 
89 template<typename T,int Rows,int Flags> struct sparse_eval<T,Rows,1,Flags> {
90  typedef typename traits<T>::Scalar Scalar_;
91  typedef typename traits<T>::StorageIndex StorageIndex_;
92  public:
93  typedef SparseVector<Scalar_, ColMajor, StorageIndex_> type;
94 };
95 
96 // TODO this seems almost identical to plain_matrix_type<T, Sparse>
97 template<typename T,int Rows,int Cols,int Flags> struct sparse_eval {
98  typedef typename traits<T>::Scalar Scalar_;
99  typedef typename traits<T>::StorageIndex StorageIndex_;
100  enum { Options_ = ((Flags&RowMajorBit)==RowMajorBit) ? RowMajor : ColMajor };
101  public:
102  typedef SparseMatrix<Scalar_, Options_, StorageIndex_> type;
103 };
104 
105 template<typename T,int Flags> struct sparse_eval<T,1,1,Flags> {
106  typedef typename traits<T>::Scalar Scalar_;
107  public:
108  typedef Matrix<Scalar_, 1, 1> type;
109 };
110 
111 template<typename T> struct plain_matrix_type<T,Sparse>
112 {
113  typedef typename traits<T>::Scalar Scalar_;
114  typedef typename traits<T>::StorageIndex StorageIndex_;
115  enum { Options_ = ((evaluator<T>::Flags&RowMajorBit)==RowMajorBit) ? RowMajor : ColMajor };
116  public:
117  typedef SparseMatrix<Scalar_, Options_, StorageIndex_> type;
118 };
119 
120 template<typename T>
121 struct plain_object_eval<T,Sparse>
122  : sparse_eval<T, traits<T>::RowsAtCompileTime,traits<T>::ColsAtCompileTime, evaluator<T>::Flags>
123 {};
124 
125 template<typename Decomposition, typename RhsType>
126 struct solve_traits<Decomposition,RhsType,Sparse>
127 {
128  typedef typename sparse_eval<RhsType, RhsType::RowsAtCompileTime, RhsType::ColsAtCompileTime,traits<RhsType>::Flags>::type PlainObject;
129 };
130 
131 template<typename Derived>
132 struct generic_xpr_base<Derived, MatrixXpr, Sparse>
133 {
134  typedef SparseMatrixBase<Derived> type;
135 };
136 
137 struct SparseTriangularShape { static std::string debugName() { return "SparseTriangularShape"; } };
138 struct SparseSelfAdjointShape { static std::string debugName() { return "SparseSelfAdjointShape"; } };
139 
140 template<> struct glue_shapes<SparseShape,SelfAdjointShape> { typedef SparseSelfAdjointShape type; };
141 template<> struct glue_shapes<SparseShape,TriangularShape > { typedef SparseTriangularShape type; };
142 
143 // return type of SparseCompressedBase::lower_bound;
144 struct LowerBoundIndex {
145  LowerBoundIndex() : value(-1), found(false) {}
146  LowerBoundIndex(Index val, bool ok) : value(val), found(ok) {}
147  Index value;
148  bool found;
149 };
150 
151 } // end namespace internal
152 
161 template<typename Scalar, typename StorageIndex=typename SparseMatrix<Scalar>::StorageIndex >
162 class Triplet
163 {
164 public:
165  Triplet() : m_row(0), m_col(0), m_value(0) {}
166 
167  Triplet(const StorageIndex& i, const StorageIndex& j, const Scalar& v = Scalar(0))
168  : m_row(i), m_col(j), m_value(v)
169  {}
170 
172  const StorageIndex& row() const { return m_row; }
173 
175  const StorageIndex& col() const { return m_col; }
176 
178  const Scalar& value() const { return m_value; }
179 protected:
180  StorageIndex m_row, m_col;
181  Scalar m_value;
182 };
183 
184 } // end namespace Eigen
185 
186 #endif // EIGEN_SPARSEUTIL_H
Array< int, Dynamic, 1 > v
A versatible sparse matrix representation.
Definition: SparseMatrix.h:125
Pseudo expression to manipulate a triangular sparse matrix as a selfadjoint matrix.
a sparse vector class
Definition: SparseVector.h:68
Expression of a dense or sparse matrix with zero or too small values removed.
Definition: SparseView.h:48
const Scalar & value() const
Definition: SparseUtil.h:178
StorageIndex m_row
Definition: SparseUtil.h:180
const StorageIndex & col() const
Definition: SparseUtil.h:175
StorageIndex m_col
Definition: SparseUtil.h:180
Triplet(const StorageIndex &i, const StorageIndex &j, const Scalar &v=Scalar(0))
Definition: SparseUtil.h:167
Scalar m_value
Definition: SparseUtil.h:181
const StorageIndex & row() const
Definition: SparseUtil.h:172
@ ColMajor
Definition: Constants.h:321
@ RowMajor
Definition: Constants.h:323
const unsigned int RowMajorBit
Definition: Constants.h:68
constexpr int min_size_prefer_fixed(A a, B b)
Definition: Meta.h:553
: InteropHeaders
Definition: Core:139
const int OuterRandomAccessPattern
Definition: SparseUtil.h:51
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
const int InnerRandomAccessPattern
Definition: SparseUtil.h:50
const int RandomAccessPattern
Definition: SparseUtil.h:52
const int CoherentAccessPattern
Definition: SparseUtil.h:49
std::ptrdiff_t j