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 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // Eigen is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // Alternatively, you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 2 of
14 // the License, or (at your option) any later version.
15 //
16 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License and a copy of the GNU General Public License along with
23 // Eigen. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef EIGEN_SPARSEUTIL_H
26 #define EIGEN_SPARSEUTIL_H
27 
28 namespace Eigen {
29 
30 #ifdef NDEBUG
31 #define EIGEN_DBG_SPARSE(X)
32 #else
33 #define EIGEN_DBG_SPARSE(X) X
34 #endif
35 
36 #define EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, Op) \
37 template<typename OtherDerived> \
38 EIGEN_STRONG_INLINE Derived& operator Op(const Eigen::SparseMatrixBase<OtherDerived>& other) \
39 { \
40  return Base::operator Op(other.derived()); \
41 } \
42 EIGEN_STRONG_INLINE Derived& operator Op(const Derived& other) \
43 { \
44  return Base::operator Op(other); \
45 }
46 
47 #define EIGEN_SPARSE_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Derived, Op) \
48 template<typename Other> \
49 EIGEN_STRONG_INLINE Derived& operator Op(const Other& scalar) \
50 { \
51  return Base::operator Op(scalar); \
52 }
53 
54 #define EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATORS(Derived) \
55 EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, =) \
56 EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, +=) \
57 EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, -=) \
58 EIGEN_SPARSE_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Derived, *=) \
59 EIGEN_SPARSE_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Derived, /=)
60 
61 #define _EIGEN_SPARSE_PUBLIC_INTERFACE(Derived, BaseClass) \
62  typedef BaseClass Base; \
63  typedef typename Eigen::internal::traits<Derived >::Scalar Scalar; \
64  typedef typename Eigen::NumTraits<Scalar>::Real RealScalar; \
65  typedef typename Eigen::internal::nested<Derived >::type Nested; \
66  typedef typename Eigen::internal::traits<Derived >::StorageKind StorageKind; \
67  typedef typename Eigen::internal::traits<Derived >::Index Index; \
68  enum { RowsAtCompileTime = Eigen::internal::traits<Derived >::RowsAtCompileTime, \
69  ColsAtCompileTime = Eigen::internal::traits<Derived >::ColsAtCompileTime, \
70  Flags = Eigen::internal::traits<Derived >::Flags, \
71  CoeffReadCost = Eigen::internal::traits<Derived >::CoeffReadCost, \
72  SizeAtCompileTime = Base::SizeAtCompileTime, \
73  IsVectorAtCompileTime = Base::IsVectorAtCompileTime }; \
74  using Base::derived; \
75  using Base::const_cast_derived;
76 
77 #define EIGEN_SPARSE_PUBLIC_INTERFACE(Derived) \
78  _EIGEN_SPARSE_PUBLIC_INTERFACE(Derived, Eigen::SparseMatrixBase<Derived >)
79 
80 const int CoherentAccessPattern = 0x1;
84 
85 template<typename Derived> class SparseMatrixBase;
86 template<typename _Scalar, int _Flags = 0, typename _Index = int> class SparseMatrix;
87 template<typename _Scalar, int _Flags = 0, typename _Index = int> class DynamicSparseMatrix;
88 template<typename _Scalar, int _Flags = 0, typename _Index = int> class SparseVector;
89 template<typename _Scalar, int _Flags = 0, typename _Index = int> class MappedSparseMatrix;
90 
91 template<typename MatrixType, int Size> class SparseInnerVectorSet;
92 template<typename MatrixType, int Mode> class SparseTriangularView;
93 template<typename MatrixType, unsigned int UpLo> class SparseSelfAdjointView;
94 template<typename Lhs, typename Rhs> class SparseDiagonalProduct;
95 template<typename MatrixType> class SparseView;
96 
97 template<typename Lhs, typename Rhs> class SparseSparseProduct;
98 template<typename Lhs, typename Rhs> class SparseTimeDenseProduct;
99 template<typename Lhs, typename Rhs> class DenseTimeSparseProduct;
100 template<typename Lhs, typename Rhs, bool Transpose> class SparseDenseOuterProduct;
101 
102 template<typename Lhs, typename Rhs> struct SparseSparseProductReturnType;
103 template<typename Lhs, typename Rhs, int InnerSize = internal::traits<Lhs>::ColsAtCompileTime> struct DenseSparseProductReturnType;
104 template<typename Lhs, typename Rhs, int InnerSize = internal::traits<Lhs>::ColsAtCompileTime> struct SparseDenseProductReturnType;
105 template<typename MatrixType,int UpLo> class SparseSymmetricPermutationProduct;
106 
107 namespace internal {
108 
109 template<typename T,int Rows,int Cols> struct sparse_eval;
110 
111 template<typename T> struct eval<T,Sparse>
112  : public sparse_eval<T, traits<T>::RowsAtCompileTime,traits<T>::ColsAtCompileTime>
113 {};
114 
115 template<typename T,int Cols> struct sparse_eval<T,1,Cols> {
116  typedef typename traits<T>::Scalar _Scalar;
117  enum { _Flags = traits<T>::Flags| RowMajorBit };
118  public:
119  typedef SparseVector<_Scalar, _Flags> type;
120 };
121 
122 template<typename T,int Rows> struct sparse_eval<T,Rows,1> {
123  typedef typename traits<T>::Scalar _Scalar;
124  enum { _Flags = traits<T>::Flags & (~RowMajorBit) };
125  public:
126  typedef SparseVector<_Scalar, _Flags> type;
127 };
128 
129 template<typename T,int Rows,int Cols> struct sparse_eval {
130  typedef typename traits<T>::Scalar _Scalar;
131  enum { _Flags = traits<T>::Flags };
132  public:
133  typedef SparseMatrix<_Scalar, _Flags> type;
134 };
135 
136 template<typename T> struct sparse_eval<T,1,1> {
137  typedef typename traits<T>::Scalar _Scalar;
138  public:
139  typedef Matrix<_Scalar, 1, 1> type;
140 };
141 
142 template<typename T> struct plain_matrix_type<T,Sparse>
143 {
144  typedef typename traits<T>::Scalar _Scalar;
145  enum {
146  _Flags = traits<T>::Flags
147  };
148 
149  public:
150  typedef SparseMatrix<_Scalar, _Flags> type;
151 };
152 
153 } // end namespace internal
154 
163 template<typename Scalar, typename Index=unsigned int>
164 class Triplet
165 {
166 public:
167  Triplet() : m_row(0), m_col(0), m_value(0) {}
168 
169  Triplet(const Index& i, const Index& j, const Scalar& v = Scalar(0))
170  : m_row(i), m_col(j), m_value(v)
171  {}
172 
174  const Index& row() const { return m_row; }
175 
177  const Index& col() const { return m_col; }
178 
180  const Scalar& value() const { return m_value; }
181 protected:
182  Index m_row, m_col;
183  Scalar m_value;
184 };
185 
186 } // end namespace Eigen
187 
188 #endif // EIGEN_SPARSEUTIL_H