1c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This file is part of Eigen, a lightweight C++ template library
2c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// for linear algebra.
3c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//
4c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//
6c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This Source Code Form is subject to the terms of the Mozilla
7c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Public License v. 2.0. If a copy of the MPL was not distributed
8c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
10c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#ifndef EIGEN_SPARSEUTIL_H
11c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#define EIGEN_SPARSEUTIL_H
12c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
13c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathnamespace Eigen {
14c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
15c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#ifdef NDEBUG
16c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#define EIGEN_DBG_SPARSE(X)
17c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#else
18c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#define EIGEN_DBG_SPARSE(X) X
19c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#endif
20c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
21c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#define EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, Op) \
22c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename OtherDerived> \
23c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathEIGEN_STRONG_INLINE Derived& operator Op(const Eigen::SparseMatrixBase<OtherDerived>& other) \
24c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{ \
25c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return Base::operator Op(other.derived()); \
26c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath} \
27c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathEIGEN_STRONG_INLINE Derived& operator Op(const Derived& other) \
28c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{ \
29c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return Base::operator Op(other); \
30c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
31c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
32c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#define EIGEN_SPARSE_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Derived, Op) \
33c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Other> \
34c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathEIGEN_STRONG_INLINE Derived& operator Op(const Other& scalar) \
35c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{ \
36c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return Base::operator Op(scalar); \
37c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
38c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
39c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#define EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATORS(Derived) \
40c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathEIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, =) \
41c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathEIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, +=) \
42c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathEIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, -=) \
43c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathEIGEN_SPARSE_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Derived, *=) \
44c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathEIGEN_SPARSE_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Derived, /=)
45c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
46c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#define _EIGEN_SPARSE_PUBLIC_INTERFACE(Derived, BaseClass) \
47c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef BaseClass Base; \
48c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename Eigen::internal::traits<Derived >::Scalar Scalar; \
49c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename Eigen::NumTraits<Scalar>::Real RealScalar; \
50c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename Eigen::internal::nested<Derived >::type Nested; \
51c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename Eigen::internal::traits<Derived >::StorageKind StorageKind; \
52c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename Eigen::internal::traits<Derived >::Index Index; \
53c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  enum { RowsAtCompileTime = Eigen::internal::traits<Derived >::RowsAtCompileTime, \
54c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        ColsAtCompileTime = Eigen::internal::traits<Derived >::ColsAtCompileTime, \
55c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        Flags = Eigen::internal::traits<Derived >::Flags, \
56c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        CoeffReadCost = Eigen::internal::traits<Derived >::CoeffReadCost, \
57c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        SizeAtCompileTime = Base::SizeAtCompileTime, \
58c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        IsVectorAtCompileTime = Base::IsVectorAtCompileTime }; \
59c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  using Base::derived; \
60c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  using Base::const_cast_derived;
61c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
62c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#define EIGEN_SPARSE_PUBLIC_INTERFACE(Derived) \
63c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  _EIGEN_SPARSE_PUBLIC_INTERFACE(Derived, Eigen::SparseMatrixBase<Derived >)
64c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
65c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathconst int CoherentAccessPattern     = 0x1;
66c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathconst int InnerRandomAccessPattern  = 0x2 | CoherentAccessPattern;
67c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathconst int OuterRandomAccessPattern  = 0x4 | CoherentAccessPattern;
68c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathconst int RandomAccessPattern       = 0x8 | OuterRandomAccessPattern | InnerRandomAccessPattern;
69c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
70c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Derived> class SparseMatrixBase;
71c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename _Scalar, int _Flags = 0, typename _Index = int>  class SparseMatrix;
72c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename _Scalar, int _Flags = 0, typename _Index = int>  class DynamicSparseMatrix;
73c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename _Scalar, int _Flags = 0, typename _Index = int>  class SparseVector;
74c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename _Scalar, int _Flags = 0, typename _Index = int>  class MappedSparseMatrix;
75c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
76c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename MatrixType, int Mode>           class SparseTriangularView;
77c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename MatrixType, unsigned int UpLo>  class SparseSelfAdjointView;
78c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Lhs, typename Rhs>              class SparseDiagonalProduct;
79c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename MatrixType> class SparseView;
80c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
81c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Lhs, typename Rhs>        class SparseSparseProduct;
82c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Lhs, typename Rhs>        class SparseTimeDenseProduct;
83c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Lhs, typename Rhs>        class DenseTimeSparseProduct;
84c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Lhs, typename Rhs, bool Transpose> class SparseDenseOuterProduct;
85c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
86c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Lhs, typename Rhs> struct SparseSparseProductReturnType;
877faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandeztemplate<typename Lhs, typename Rhs,
887faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez         int InnerSize = EIGEN_SIZE_MIN_PREFER_FIXED(internal::traits<Lhs>::ColsAtCompileTime,internal::traits<Rhs>::RowsAtCompileTime)> struct DenseSparseProductReturnType;
897faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandeztemplate<typename Lhs, typename Rhs,
907faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez         int InnerSize = EIGEN_SIZE_MIN_PREFER_FIXED(internal::traits<Lhs>::ColsAtCompileTime,internal::traits<Rhs>::RowsAtCompileTime)> struct SparseDenseProductReturnType;
91c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename MatrixType,int UpLo> class SparseSymmetricPermutationProduct;
92c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
93c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathnamespace internal {
94c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
95c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename T,int Rows,int Cols> struct sparse_eval;
96c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
97c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename T> struct eval<T,Sparse>
98c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  : public sparse_eval<T, traits<T>::RowsAtCompileTime,traits<T>::ColsAtCompileTime>
99c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{};
100c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
101c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename T,int Cols> struct sparse_eval<T,1,Cols> {
102c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    typedef typename traits<T>::Scalar _Scalar;
1037faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    typedef typename traits<T>::Index _Index;
104c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  public:
1057faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    typedef SparseVector<_Scalar, RowMajor, _Index> type;
106c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
107c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
108c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename T,int Rows> struct sparse_eval<T,Rows,1> {
109c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    typedef typename traits<T>::Scalar _Scalar;
1107faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    typedef typename traits<T>::Index _Index;
111c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  public:
1127faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    typedef SparseVector<_Scalar, ColMajor, _Index> type;
113c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
114c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
115c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename T,int Rows,int Cols> struct sparse_eval {
116c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    typedef typename traits<T>::Scalar _Scalar;
1177faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    typedef typename traits<T>::Index _Index;
1187faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    enum { _Options = ((traits<T>::Flags&RowMajorBit)==RowMajorBit) ? RowMajor : ColMajor };
119c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  public:
1207faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    typedef SparseMatrix<_Scalar, _Options, _Index> type;
121c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
122c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
123c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename T> struct sparse_eval<T,1,1> {
124c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    typedef typename traits<T>::Scalar _Scalar;
125c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  public:
126c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    typedef Matrix<_Scalar, 1, 1> type;
127c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
128c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
129c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename T> struct plain_matrix_type<T,Sparse>
130c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
131c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename traits<T>::Scalar _Scalar;
1327faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  typedef typename traits<T>::Index _Index;
1337faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  enum { _Options = ((traits<T>::Flags&RowMajorBit)==RowMajorBit) ? RowMajor : ColMajor };
134c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  public:
1357faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    typedef SparseMatrix<_Scalar, _Options, _Index> type;
136c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
137c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
138c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath} // end namespace internal
139c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
140c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/** \ingroup SparseCore_Module
141c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
142c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \class Triplet
143c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
144c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \brief A small structure to hold a non zero as a triplet (i,j,value).
145c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
146c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \sa SparseMatrix::setFromTriplets()
147c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  */
1487faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandeztemplate<typename Scalar, typename Index=typename SparseMatrix<Scalar>::Index >
149c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathclass Triplet
150c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
151c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathpublic:
152c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  Triplet() : m_row(0), m_col(0), m_value(0) {}
153c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
154c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  Triplet(const Index& i, const Index& j, const Scalar& v = Scalar(0))
155c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    : m_row(i), m_col(j), m_value(v)
156c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {}
157c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
158c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  /** \returns the row index of the element */
159c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  const Index& row() const { return m_row; }
160c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
161c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  /** \returns the column index of the element */
162c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  const Index& col() const { return m_col; }
163c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
164c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  /** \returns the value of the element */
165c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  const Scalar& value() const { return m_value; }
166c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathprotected:
167c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  Index m_row, m_col;
168c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  Scalar m_value;
169c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
170c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
171c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath} // end namespace Eigen
172c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
173c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#endif // EIGEN_SPARSEUTIL_H
174