Eigen  3.3.3
SparseUtil.h
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #ifndef EIGEN_SPARSEUTIL_H
00011 #define EIGEN_SPARSEUTIL_H
00012 
00013 namespace Eigen { 
00014 
00015 #ifdef NDEBUG
00016 #define EIGEN_DBG_SPARSE(X)
00017 #else
00018 #define EIGEN_DBG_SPARSE(X) X
00019 #endif
00020 
00021 #define EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, Op) \
00022 template<typename OtherDerived> \
00023 EIGEN_STRONG_INLINE Derived& operator Op(const Eigen::SparseMatrixBase<OtherDerived>& other) \
00024 { \
00025   return Base::operator Op(other.derived()); \
00026 } \
00027 EIGEN_STRONG_INLINE Derived& operator Op(const Derived& other) \
00028 { \
00029   return Base::operator Op(other); \
00030 }
00031 
00032 #define EIGEN_SPARSE_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Derived, Op) \
00033 template<typename Other> \
00034 EIGEN_STRONG_INLINE Derived& operator Op(const Other& scalar) \
00035 { \
00036   return Base::operator Op(scalar); \
00037 }
00038 
00039 #define EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATORS(Derived) \
00040 EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, =)
00041 
00042 
00043 #define EIGEN_SPARSE_PUBLIC_INTERFACE(Derived) \
00044   EIGEN_GENERIC_PUBLIC_INTERFACE(Derived)
00045 
00046   
00047 const int CoherentAccessPattern     = 0x1;
00048 const int InnerRandomAccessPattern  = 0x2 | CoherentAccessPattern;
00049 const int OuterRandomAccessPattern  = 0x4 | CoherentAccessPattern;
00050 const int RandomAccessPattern       = 0x8 | OuterRandomAccessPattern | InnerRandomAccessPattern;
00051 
00052 template<typename _Scalar, int _Flags = 0, typename _StorageIndex = int>  class SparseMatrix;
00053 template<typename _Scalar, int _Flags = 0, typename _StorageIndex = int>  class DynamicSparseMatrix;
00054 template<typename _Scalar, int _Flags = 0, typename _StorageIndex = int>  class SparseVector;
00055 template<typename _Scalar, int _Flags = 0, typename _StorageIndex = int>  class MappedSparseMatrix;
00056 
00057 template<typename MatrixType, unsigned int UpLo>  class SparseSelfAdjointView;
00058 template<typename Lhs, typename Rhs>              class SparseDiagonalProduct;
00059 template<typename MatrixType> class SparseView;
00060 
00061 template<typename Lhs, typename Rhs>        class SparseSparseProduct;
00062 template<typename Lhs, typename Rhs>        class SparseTimeDenseProduct;
00063 template<typename Lhs, typename Rhs>        class DenseTimeSparseProduct;
00064 template<typename Lhs, typename Rhs, bool Transpose> class SparseDenseOuterProduct;
00065 
00066 template<typename Lhs, typename Rhs> struct SparseSparseProductReturnType;
00067 template<typename Lhs, typename Rhs,
00068          int InnerSize = EIGEN_SIZE_MIN_PREFER_FIXED(internal::traits<Lhs>::ColsAtCompileTime,internal::traits<Rhs>::RowsAtCompileTime)> struct DenseSparseProductReturnType;
00069          
00070 template<typename Lhs, typename Rhs,
00071          int InnerSize = EIGEN_SIZE_MIN_PREFER_FIXED(internal::traits<Lhs>::ColsAtCompileTime,internal::traits<Rhs>::RowsAtCompileTime)> struct SparseDenseProductReturnType;
00072 template<typename MatrixType,int UpLo> class SparseSymmetricPermutationProduct;
00073 
00074 namespace internal {
00075 
00076 template<typename T,int Rows,int Cols,int Flags> struct sparse_eval;
00077 
00078 template<typename T> struct eval<T,Sparse>
00079   : sparse_eval<T, traits<T>::RowsAtCompileTime,traits<T>::ColsAtCompileTime,traits<T>::Flags>
00080 {};
00081 
00082 template<typename T,int Cols,int Flags> struct sparse_eval<T,1,Cols,Flags> {
00083     typedef typename traits<T>::Scalar _Scalar;
00084     typedef typename traits<T>::StorageIndex _StorageIndex;
00085   public:
00086     typedef SparseVector<_Scalar, RowMajor, _StorageIndex> type;
00087 };
00088 
00089 template<typename T,int Rows,int Flags> struct sparse_eval<T,Rows,1,Flags> {
00090     typedef typename traits<T>::Scalar _Scalar;
00091     typedef typename traits<T>::StorageIndex _StorageIndex;
00092   public:
00093     typedef SparseVector<_Scalar, ColMajor, _StorageIndex> type;
00094 };
00095 
00096 // TODO this seems almost identical to plain_matrix_type<T, Sparse>
00097 template<typename T,int Rows,int Cols,int Flags> struct sparse_eval {
00098     typedef typename traits<T>::Scalar _Scalar;
00099     typedef typename traits<T>::StorageIndex _StorageIndex;
00100     enum { _Options = ((Flags&RowMajorBit)==RowMajorBit) ? RowMajor : ColMajor };
00101   public:
00102     typedef SparseMatrix<_Scalar, _Options, _StorageIndex> type;
00103 };
00104 
00105 template<typename T,int Flags> struct sparse_eval<T,1,1,Flags> {
00106     typedef typename traits<T>::Scalar _Scalar;
00107   public:
00108     typedef Matrix<_Scalar, 1, 1> type;
00109 };
00110 
00111 template<typename T> struct plain_matrix_type<T,Sparse>
00112 {
00113   typedef typename traits<T>::Scalar _Scalar;
00114   typedef typename traits<T>::StorageIndex _StorageIndex;
00115   enum { _Options = ((evaluator<T>::Flags&RowMajorBit)==RowMajorBit) ? RowMajor : ColMajor };
00116   public:
00117     typedef SparseMatrix<_Scalar, _Options, _StorageIndex> type;
00118 };
00119 
00120 template<typename T>
00121 struct plain_object_eval<T,Sparse>
00122   : sparse_eval<T, traits<T>::RowsAtCompileTime,traits<T>::ColsAtCompileTime, evaluator<T>::Flags>
00123 {};
00124 
00125 template<typename Decomposition, typename RhsType>
00126 struct solve_traits<Decomposition,RhsType,Sparse>
00127 {
00128   typedef typename sparse_eval<RhsType, RhsType::RowsAtCompileTime, RhsType::ColsAtCompileTime,traits<RhsType>::Flags>::type PlainObject;
00129 };
00130 
00131 template<typename Derived>
00132 struct generic_xpr_base<Derived, MatrixXpr, Sparse>
00133 {
00134   typedef SparseMatrixBase<Derived> type;
00135 };
00136 
00137 struct SparseTriangularShape  { static std::string debugName() { return "SparseTriangularShape"; } };
00138 struct SparseSelfAdjointShape { static std::string debugName() { return "SparseSelfAdjointShape"; } };
00139 
00140 template<> struct glue_shapes<SparseShape,SelfAdjointShape> { typedef SparseSelfAdjointShape type;  };
00141 template<> struct glue_shapes<SparseShape,TriangularShape > { typedef SparseTriangularShape  type;  };
00142 
00143 } // end namespace internal
00144 
00153 template<typename Scalar, typename StorageIndex=typename SparseMatrix<Scalar>::StorageIndex >
00154 class Triplet
00155 {
00156 public:
00157   Triplet() : m_row(0), m_col(0), m_value(0) {}
00158 
00159   Triplet(const StorageIndex& i, const StorageIndex& j, const Scalar& v = Scalar(0))
00160     : m_row(i), m_col(j), m_value(v)
00161   {}
00162 
00164   const StorageIndex& row() const { return m_row; }
00165 
00167   const StorageIndex& col() const { return m_col; }
00168 
00170   const Scalar& value() const { return m_value; }
00171 protected:
00172   StorageIndex m_row, m_col;
00173   Scalar m_value;
00174 };
00175 
00176 } // end namespace Eigen
00177 
00178 #endif // EIGEN_SPARSEUTIL_H
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends