SparseUtil.h 6.45 KB
Newer Older
LM's avatar
LM committed
1 2 3
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
4
// Copyright (C) 2008-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
LM's avatar
LM committed
5
//
Don Gagne's avatar
Don Gagne committed
6 7 8
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
LM's avatar
LM committed
9 10 11 12

#ifndef EIGEN_SPARSEUTIL_H
#define EIGEN_SPARSEUTIL_H

Don Gagne's avatar
Don Gagne committed
13 14
namespace Eigen { 

LM's avatar
LM committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#ifdef NDEBUG
#define EIGEN_DBG_SPARSE(X)
#else
#define EIGEN_DBG_SPARSE(X) X
#endif

#define EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, Op) \
template<typename OtherDerived> \
EIGEN_STRONG_INLINE Derived& operator Op(const Eigen::SparseMatrixBase<OtherDerived>& other) \
{ \
  return Base::operator Op(other.derived()); \
} \
EIGEN_STRONG_INLINE Derived& operator Op(const Derived& other) \
{ \
  return Base::operator Op(other); \
}

#define EIGEN_SPARSE_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Derived, Op) \
template<typename Other> \
EIGEN_STRONG_INLINE Derived& operator Op(const Other& scalar) \
{ \
  return Base::operator Op(scalar); \
}

#define EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATORS(Derived) \
40 41
EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, =)

LM's avatar
LM committed
42 43

#define EIGEN_SPARSE_PUBLIC_INTERFACE(Derived) \
44
  EIGEN_GENERIC_PUBLIC_INTERFACE(Derived)
LM's avatar
LM committed
45

46
  
LM's avatar
LM committed
47 48 49 50 51
const int CoherentAccessPattern     = 0x1;
const int InnerRandomAccessPattern  = 0x2 | CoherentAccessPattern;
const int OuterRandomAccessPattern  = 0x4 | CoherentAccessPattern;
const int RandomAccessPattern       = 0x8 | OuterRandomAccessPattern | InnerRandomAccessPattern;

52 53 54 55
template<typename _Scalar, int _Flags = 0, typename _StorageIndex = int>  class SparseMatrix;
template<typename _Scalar, int _Flags = 0, typename _StorageIndex = int>  class DynamicSparseMatrix;
template<typename _Scalar, int _Flags = 0, typename _StorageIndex = int>  class SparseVector;
template<typename _Scalar, int _Flags = 0, typename _StorageIndex = int>  class MappedSparseMatrix;
LM's avatar
LM committed
56 57 58 59 60 61 62 63 64 65 66

template<typename MatrixType, unsigned int UpLo>  class SparseSelfAdjointView;
template<typename Lhs, typename Rhs>              class SparseDiagonalProduct;
template<typename MatrixType> class SparseView;

template<typename Lhs, typename Rhs>        class SparseSparseProduct;
template<typename Lhs, typename Rhs>        class SparseTimeDenseProduct;
template<typename Lhs, typename Rhs>        class DenseTimeSparseProduct;
template<typename Lhs, typename Rhs, bool Transpose> class SparseDenseOuterProduct;

template<typename Lhs, typename Rhs> struct SparseSparseProductReturnType;
67
template<typename Lhs, typename Rhs,
68 69
         int InnerSize = EIGEN_SIZE_MIN_PREFER_FIXED(internal::traits<Lhs>::ColsAtCompileTime,internal::traits<Rhs>::RowsAtCompileTime)> struct DenseSparseProductReturnType;
         
70 71
template<typename Lhs, typename Rhs,
         int InnerSize = EIGEN_SIZE_MIN_PREFER_FIXED(internal::traits<Lhs>::ColsAtCompileTime,internal::traits<Rhs>::RowsAtCompileTime)> struct SparseDenseProductReturnType;
Don Gagne's avatar
Don Gagne committed
72
template<typename MatrixType,int UpLo> class SparseSymmetricPermutationProduct;
LM's avatar
LM committed
73 74 75

namespace internal {

76
template<typename T,int Rows,int Cols,int Flags> struct sparse_eval;
Don Gagne's avatar
Don Gagne committed
77

LM's avatar
LM committed
78
template<typename T> struct eval<T,Sparse>
79
  : sparse_eval<T, traits<T>::RowsAtCompileTime,traits<T>::ColsAtCompileTime,traits<T>::Flags>
Don Gagne's avatar
Don Gagne committed
80 81
{};

82
template<typename T,int Cols,int Flags> struct sparse_eval<T,1,Cols,Flags> {
LM's avatar
LM committed
83
    typedef typename traits<T>::Scalar _Scalar;
84
    typedef typename traits<T>::StorageIndex _StorageIndex;
Don Gagne's avatar
Don Gagne committed
85
  public:
86
    typedef SparseVector<_Scalar, RowMajor, _StorageIndex> type;
Don Gagne's avatar
Don Gagne committed
87
};
LM's avatar
LM committed
88

89
template<typename T,int Rows,int Flags> struct sparse_eval<T,Rows,1,Flags> {
Don Gagne's avatar
Don Gagne committed
90
    typedef typename traits<T>::Scalar _Scalar;
91
    typedef typename traits<T>::StorageIndex _StorageIndex;
Don Gagne's avatar
Don Gagne committed
92
  public:
93
    typedef SparseVector<_Scalar, ColMajor, _StorageIndex> type;
Don Gagne's avatar
Don Gagne committed
94 95
};

96 97
// TODO this seems almost identical to plain_matrix_type<T, Sparse>
template<typename T,int Rows,int Cols,int Flags> struct sparse_eval {
Don Gagne's avatar
Don Gagne committed
98
    typedef typename traits<T>::Scalar _Scalar;
99 100
    typedef typename traits<T>::StorageIndex _StorageIndex;
    enum { _Options = ((Flags&RowMajorBit)==RowMajorBit) ? RowMajor : ColMajor };
LM's avatar
LM committed
101
  public:
102
    typedef SparseMatrix<_Scalar, _Options, _StorageIndex> type;
Don Gagne's avatar
Don Gagne committed
103 104
};

105
template<typename T,int Flags> struct sparse_eval<T,1,1,Flags> {
Don Gagne's avatar
Don Gagne committed
106 107 108
    typedef typename traits<T>::Scalar _Scalar;
  public:
    typedef Matrix<_Scalar, 1, 1> type;
LM's avatar
LM committed
109 110 111 112 113
};

template<typename T> struct plain_matrix_type<T,Sparse>
{
  typedef typename traits<T>::Scalar _Scalar;
114 115
  typedef typename traits<T>::StorageIndex _StorageIndex;
  enum { _Options = ((evaluator<T>::Flags&RowMajorBit)==RowMajorBit) ? RowMajor : ColMajor };
LM's avatar
LM committed
116
  public:
117 118 119 120 121 122 123 124 125 126 127 128
    typedef SparseMatrix<_Scalar, _Options, _StorageIndex> type;
};

template<typename T>
struct plain_object_eval<T,Sparse>
  : sparse_eval<T, traits<T>::RowsAtCompileTime,traits<T>::ColsAtCompileTime, evaluator<T>::Flags>
{};

template<typename Decomposition, typename RhsType>
struct solve_traits<Decomposition,RhsType,Sparse>
{
  typedef typename sparse_eval<RhsType, RhsType::RowsAtCompileTime, RhsType::ColsAtCompileTime,traits<RhsType>::Flags>::type PlainObject;
LM's avatar
LM committed
129 130
};

131 132 133 134 135 136 137 138 139 140 141 142
template<typename Derived>
struct generic_xpr_base<Derived, MatrixXpr, Sparse>
{
  typedef SparseMatrixBase<Derived> type;
};

struct SparseTriangularShape  { static std::string debugName() { return "SparseTriangularShape"; } };
struct SparseSelfAdjointShape { static std::string debugName() { return "SparseSelfAdjointShape"; } };

template<> struct glue_shapes<SparseShape,SelfAdjointShape> { typedef SparseSelfAdjointShape type;  };
template<> struct glue_shapes<SparseShape,TriangularShape > { typedef SparseTriangularShape  type;  };

LM's avatar
LM committed
143 144
} // end namespace internal

Don Gagne's avatar
Don Gagne committed
145 146 147 148 149 150 151 152
/** \ingroup SparseCore_Module
  *
  * \class Triplet
  *
  * \brief A small structure to hold a non zero as a triplet (i,j,value).
  *
  * \sa SparseMatrix::setFromTriplets()
  */
153
template<typename Scalar, typename StorageIndex=typename SparseMatrix<Scalar>::StorageIndex >
Don Gagne's avatar
Don Gagne committed
154 155 156 157 158
class Triplet
{
public:
  Triplet() : m_row(0), m_col(0), m_value(0) {}

159
  Triplet(const StorageIndex& i, const StorageIndex& j, const Scalar& v = Scalar(0))
Don Gagne's avatar
Don Gagne committed
160 161 162 163
    : m_row(i), m_col(j), m_value(v)
  {}

  /** \returns the row index of the element */
164
  const StorageIndex& row() const { return m_row; }
Don Gagne's avatar
Don Gagne committed
165 166

  /** \returns the column index of the element */
167
  const StorageIndex& col() const { return m_col; }
Don Gagne's avatar
Don Gagne committed
168 169 170 171

  /** \returns the value of the element */
  const Scalar& value() const { return m_value; }
protected:
172
  StorageIndex m_row, m_col;
Don Gagne's avatar
Don Gagne committed
173 174 175 176 177
  Scalar m_value;
};

} // end namespace Eigen

LM's avatar
LM committed
178
#endif // EIGEN_SPARSEUTIL_H