SparseUtil.h revision c981c48f5bc9aefeffc0bcb0cc3934c2fae179dd
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// 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
13namespace 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) \
22template<typename OtherDerived> \
23EIGEN_STRONG_INLINE Derived& operator Op(const Eigen::SparseMatrixBase<OtherDerived>& other) \
24{ \
25  return Base::operator Op(other.derived()); \
26} \
27EIGEN_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) \
33template<typename Other> \
34EIGEN_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) \
40EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, =) \
41EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, +=) \
42EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, -=) \
43EIGEN_SPARSE_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Derived, *=) \
44EIGEN_SPARSE_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Derived, /=)
45
46#define _EIGEN_SPARSE_PUBLIC_INTERFACE(Derived, BaseClass) \
47  typedef BaseClass Base; \
48  typedef typename Eigen::internal::traits<Derived >::Scalar Scalar; \
49  typedef typename Eigen::NumTraits<Scalar>::Real RealScalar; \
50  typedef typename Eigen::internal::nested<Derived >::type Nested; \
51  typedef typename Eigen::internal::traits<Derived >::StorageKind StorageKind; \
52  typedef typename Eigen::internal::traits<Derived >::Index Index; \
53  enum { RowsAtCompileTime = Eigen::internal::traits<Derived >::RowsAtCompileTime, \
54        ColsAtCompileTime = Eigen::internal::traits<Derived >::ColsAtCompileTime, \
55        Flags = Eigen::internal::traits<Derived >::Flags, \
56        CoeffReadCost = Eigen::internal::traits<Derived >::CoeffReadCost, \
57        SizeAtCompileTime = Base::SizeAtCompileTime, \
58        IsVectorAtCompileTime = Base::IsVectorAtCompileTime }; \
59  using Base::derived; \
60  using Base::const_cast_derived;
61
62#define EIGEN_SPARSE_PUBLIC_INTERFACE(Derived) \
63  _EIGEN_SPARSE_PUBLIC_INTERFACE(Derived, Eigen::SparseMatrixBase<Derived >)
64
65const int CoherentAccessPattern     = 0x1;
66const int InnerRandomAccessPattern  = 0x2 | CoherentAccessPattern;
67const int OuterRandomAccessPattern  = 0x4 | CoherentAccessPattern;
68const int RandomAccessPattern       = 0x8 | OuterRandomAccessPattern | InnerRandomAccessPattern;
69
70template<typename Derived> class SparseMatrixBase;
71template<typename _Scalar, int _Flags = 0, typename _Index = int>  class SparseMatrix;
72template<typename _Scalar, int _Flags = 0, typename _Index = int>  class DynamicSparseMatrix;
73template<typename _Scalar, int _Flags = 0, typename _Index = int>  class SparseVector;
74template<typename _Scalar, int _Flags = 0, typename _Index = int>  class MappedSparseMatrix;
75
76template<typename MatrixType, int Size>           class SparseInnerVectorSet;
77template<typename MatrixType, int Mode>           class SparseTriangularView;
78template<typename MatrixType, unsigned int UpLo>  class SparseSelfAdjointView;
79template<typename Lhs, typename Rhs>              class SparseDiagonalProduct;
80template<typename MatrixType> class SparseView;
81
82template<typename Lhs, typename Rhs>        class SparseSparseProduct;
83template<typename Lhs, typename Rhs>        class SparseTimeDenseProduct;
84template<typename Lhs, typename Rhs>        class DenseTimeSparseProduct;
85template<typename Lhs, typename Rhs, bool Transpose> class SparseDenseOuterProduct;
86
87template<typename Lhs, typename Rhs> struct SparseSparseProductReturnType;
88template<typename Lhs, typename Rhs, int InnerSize = internal::traits<Lhs>::ColsAtCompileTime> struct DenseSparseProductReturnType;
89template<typename Lhs, typename Rhs, int InnerSize = internal::traits<Lhs>::ColsAtCompileTime> struct SparseDenseProductReturnType;
90template<typename MatrixType,int UpLo> class SparseSymmetricPermutationProduct;
91
92namespace internal {
93
94template<typename T,int Rows,int Cols> struct sparse_eval;
95
96template<typename T> struct eval<T,Sparse>
97  : public sparse_eval<T, traits<T>::RowsAtCompileTime,traits<T>::ColsAtCompileTime>
98{};
99
100template<typename T,int Cols> struct sparse_eval<T,1,Cols> {
101    typedef typename traits<T>::Scalar _Scalar;
102    enum { _Flags = traits<T>::Flags| RowMajorBit };
103  public:
104    typedef SparseVector<_Scalar, _Flags> type;
105};
106
107template<typename T,int Rows> struct sparse_eval<T,Rows,1> {
108    typedef typename traits<T>::Scalar _Scalar;
109    enum { _Flags = traits<T>::Flags & (~RowMajorBit) };
110  public:
111    typedef SparseVector<_Scalar, _Flags> type;
112};
113
114template<typename T,int Rows,int Cols> struct sparse_eval {
115    typedef typename traits<T>::Scalar _Scalar;
116    enum { _Flags = traits<T>::Flags };
117  public:
118    typedef SparseMatrix<_Scalar, _Flags> type;
119};
120
121template<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
127template<typename T> struct plain_matrix_type<T,Sparse>
128{
129  typedef typename traits<T>::Scalar _Scalar;
130    enum {
131          _Flags = traits<T>::Flags
132    };
133
134  public:
135    typedef SparseMatrix<_Scalar, _Flags> type;
136};
137
138} // end namespace internal
139
140/** \ingroup SparseCore_Module
141  *
142  * \class Triplet
143  *
144  * \brief A small structure to hold a non zero as a triplet (i,j,value).
145  *
146  * \sa SparseMatrix::setFromTriplets()
147  */
148template<typename Scalar, typename Index=unsigned int>
149class Triplet
150{
151public:
152  Triplet() : m_row(0), m_col(0), m_value(0) {}
153
154  Triplet(const Index& i, const Index& j, const Scalar& v = Scalar(0))
155    : m_row(i), m_col(j), m_value(v)
156  {}
157
158  /** \returns the row index of the element */
159  const Index& row() const { return m_row; }
160
161  /** \returns the column index of the element */
162  const Index& col() const { return m_col; }
163
164  /** \returns the value of the element */
165  const Scalar& value() const { return m_value; }
166protected:
167  Index m_row, m_col;
168  Scalar m_value;
169};
170
171} // end namespace Eigen
172
173#endif // EIGEN_SPARSEUTIL_H
174