Eigen  3.2.91
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 namespace Eigen {
14 
15 #ifdef NDEBUG
16 #define EIGEN_DBG_SPARSE(X)
17 #else
18 #define EIGEN_DBG_SPARSE(X) X
19 #endif
20 
21 #define EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, Op) \
22 template<typename OtherDerived> \
23 EIGEN_STRONG_INLINE Derived& operator Op(const Eigen::SparseMatrixBase<OtherDerived>& other) \
24 { \
25  return Base::operator Op(other.derived()); \
26 } \
27 EIGEN_STRONG_INLINE Derived& operator Op(const Derived& other) \
28 { \
29  return Base::operator Op(other); \
30 }
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 { \
36  return Base::operator Op(scalar); \
37 }
38 
39 #define EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATORS(Derived) \
40 EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, =)
41 
42 // TODO this is mostly the same as EIGEN_GENERIC_PUBLIC_INTERFACE
43 #define _EIGEN_SPARSE_PUBLIC_INTERFACE(Derived) \
44  typedef typename Eigen::internal::traits<Derived >::Scalar Scalar; \
45  typedef typename Eigen::NumTraits<Scalar>::Real RealScalar; \
46  typedef typename Eigen::internal::ref_selector<Derived >::type Nested; \
47  typedef typename Eigen::internal::traits<Derived >::StorageKind StorageKind; \
48  typedef typename Eigen::internal::traits<Derived >::StorageIndex StorageIndex; \
49  enum { RowsAtCompileTime = Eigen::internal::traits<Derived >::RowsAtCompileTime, \
50  ColsAtCompileTime = Eigen::internal::traits<Derived >::ColsAtCompileTime, \
51  Flags = Eigen::internal::traits<Derived>::Flags, \
52  SizeAtCompileTime = Base::SizeAtCompileTime, \
53  IsVectorAtCompileTime = Base::IsVectorAtCompileTime }; \
54  using Base::derived; \
55  using Base::const_cast_derived; \
56  using Base::convert_index;
57 
58 #define EIGEN_SPARSE_PUBLIC_INTERFACE(Derived) \
59  typedef Eigen::SparseMatrixBase<Derived > Base; \
60  _EIGEN_SPARSE_PUBLIC_INTERFACE(Derived)
61 
62 const int CoherentAccessPattern = 0x1;
63 const int InnerRandomAccessPattern = 0x2 | CoherentAccessPattern;
64 const int OuterRandomAccessPattern = 0x4 | CoherentAccessPattern;
65 const int RandomAccessPattern = 0x8 | OuterRandomAccessPattern | InnerRandomAccessPattern;
66 
67 template<typename Derived> class SparseMatrixBase;
68 template<typename _Scalar, int _Flags = 0, typename _StorageIndex = int> class SparseMatrix;
69 template<typename _Scalar, int _Flags = 0, typename _StorageIndex = int> class DynamicSparseMatrix;
70 template<typename _Scalar, int _Flags = 0, typename _StorageIndex = int> class SparseVector;
71 template<typename _Scalar, int _Flags = 0, typename _StorageIndex = int> class MappedSparseMatrix;
72 
73 template<typename MatrixType, unsigned int UpLo> class SparseSelfAdjointView;
74 template<typename Lhs, typename Rhs> class SparseDiagonalProduct;
75 template<typename MatrixType> class SparseView;
76 
77 template<typename Lhs, typename Rhs> class SparseSparseProduct;
78 template<typename Lhs, typename Rhs> class SparseTimeDenseProduct;
79 template<typename Lhs, typename Rhs> class DenseTimeSparseProduct;
80 template<typename Lhs, typename Rhs, bool Transpose> class SparseDenseOuterProduct;
81 
82 template<typename Lhs, typename Rhs> struct SparseSparseProductReturnType;
83 template<typename Lhs, typename Rhs,
84  int InnerSize = EIGEN_SIZE_MIN_PREFER_FIXED(internal::traits<Lhs>::ColsAtCompileTime,internal::traits<Rhs>::RowsAtCompileTime)> struct DenseSparseProductReturnType;
85 
86 template<typename Lhs, typename Rhs,
87  int InnerSize = EIGEN_SIZE_MIN_PREFER_FIXED(internal::traits<Lhs>::ColsAtCompileTime,internal::traits<Rhs>::RowsAtCompileTime)> struct SparseDenseProductReturnType;
88 template<typename MatrixType,int UpLo> class SparseSymmetricPermutationProduct;
89 
90 namespace internal {
91 
92 template<typename T,int Rows,int Cols> struct sparse_eval;
93 
94 template<typename T> struct eval<T,Sparse>
95  : public sparse_eval<T, traits<T>::RowsAtCompileTime,traits<T>::ColsAtCompileTime>
96 {};
97 
98 template<typename T,int Cols> struct sparse_eval<T,1,Cols> {
99  typedef typename traits<T>::Scalar _Scalar;
100  typedef typename traits<T>::StorageIndex _StorageIndex;
101  public:
103 };
104 
105 template<typename T,int Rows> struct sparse_eval<T,Rows,1> {
106  typedef typename traits<T>::Scalar _Scalar;
107  typedef typename traits<T>::StorageIndex _StorageIndex;
108  public:
109  typedef SparseVector<_Scalar, ColMajor, _StorageIndex> type;
110 };
111 
112 // TODO this seems almost identical to plain_matrix_type<T, Sparse>
113 template<typename T,int Rows,int Cols> struct sparse_eval {
114  typedef typename traits<T>::Scalar _Scalar;
115  typedef typename traits<T>::StorageIndex _StorageIndex;
116  enum { _Options = ((traits<T>::Flags&RowMajorBit)==RowMajorBit) ? RowMajor : ColMajor };
117  public:
118  typedef SparseMatrix<_Scalar, _Options, _StorageIndex> type;
119 };
120 
121 template<typename T> struct sparse_eval<T,1,1> {
122  typedef typename traits<T>::Scalar _Scalar;
123  public:
124  typedef Matrix<_Scalar, 1, 1> type;
125 };
126 
127 template<typename T> struct plain_matrix_type<T,Sparse>
128 {
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  public:
133  typedef SparseMatrix<_Scalar, _Options, _StorageIndex> type;
134 };
135 
136 template<typename Decomposition, typename RhsType>
137 struct solve_traits<Decomposition,RhsType,Sparse>
138 {
139  typedef typename sparse_eval<RhsType, RhsType::RowsAtCompileTime, RhsType::ColsAtCompileTime>::type PlainObject;
140 };
141 
142 template<typename Derived>
143 struct generic_xpr_base<Derived, MatrixXpr, Sparse>
144 {
145  typedef SparseMatrixBase<Derived> type;
146 };
147 
148 struct SparseTriangularShape { static std::string debugName() { return "SparseTriangularShape"; } };
149 struct SparseSelfAdjointShape { static std::string debugName() { return "SparseSelfAdjointShape"; } };
150 
151 template<> struct glue_shapes<SparseShape,SelfAdjointShape> { typedef SparseSelfAdjointShape type; };
152 template<> struct glue_shapes<SparseShape,TriangularShape > { typedef SparseTriangularShape type; };
153 
154 } // end namespace internal
155 
164 template<typename Scalar, typename StorageIndex=typename SparseMatrix<Scalar>::StorageIndex >
165 class Triplet
166 {
167 public:
168  Triplet() : m_row(0), m_col(0), m_value(0) {}
169 
170  Triplet(const StorageIndex& i, const StorageIndex& j, const Scalar& v = Scalar(0))
171  : m_row(i), m_col(j), m_value(v)
172  {}
173 
175  const StorageIndex& row() const { return m_row; }
176 
178  const StorageIndex& col() const { return m_col; }
179 
181  const Scalar& value() const { return m_value; }
182 protected:
183  StorageIndex m_row, m_col;
184  Scalar m_value;
185 };
186 
187 } // end namespace Eigen
188 
189 #endif // EIGEN_SPARSEUTIL_H
Definition: Constants.h:314
const StorageIndex & row() const
Definition: SparseUtil.h:175
Definition: LDLT.h:16
Pseudo expression to manipulate a triangular sparse matrix as a selfadjoint matrix.
Definition: SparseSelfAdjointView.h:43
const unsigned int RowMajorBit
Definition: Constants.h:53
const StorageIndex & col() const
Definition: SparseUtil.h:178
a sparse vector class
Definition: SparseUtil.h:70
Definition: Constants.h:485
const Scalar & value() const
Definition: SparseUtil.h:181
A small structure to hold a non zero as a triplet (i,j,value).
Definition: SparseUtil.h:165
Definition: Eigen_Colamd.h:54
Definition: Constants.h:312
Sparse matrix.
Definition: MappedSparseMatrix.h:32