SparseUtil.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #ifndef EIGEN_SPARSEUTIL_H
00026 #define EIGEN_SPARSEUTIL_H
00027 
00028 namespace Eigen { 
00029 
00030 #ifdef NDEBUG
00031 #define EIGEN_DBG_SPARSE(X)
00032 #else
00033 #define EIGEN_DBG_SPARSE(X) X
00034 #endif
00035 
00036 #define EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, Op) \
00037 template<typename OtherDerived> \
00038 EIGEN_STRONG_INLINE Derived& operator Op(const Eigen::SparseMatrixBase<OtherDerived>& other) \
00039 { \
00040   return Base::operator Op(other.derived()); \
00041 } \
00042 EIGEN_STRONG_INLINE Derived& operator Op(const Derived& other) \
00043 { \
00044   return Base::operator Op(other); \
00045 }
00046 
00047 #define EIGEN_SPARSE_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Derived, Op) \
00048 template<typename Other> \
00049 EIGEN_STRONG_INLINE Derived& operator Op(const Other& scalar) \
00050 { \
00051   return Base::operator Op(scalar); \
00052 }
00053 
00054 #define EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATORS(Derived) \
00055 EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, =) \
00056 EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, +=) \
00057 EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, -=) \
00058 EIGEN_SPARSE_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Derived, *=) \
00059 EIGEN_SPARSE_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Derived, /=)
00060 
00061 #define _EIGEN_SPARSE_PUBLIC_INTERFACE(Derived, BaseClass) \
00062   typedef BaseClass Base; \
00063   typedef typename Eigen::internal::traits<Derived >::Scalar Scalar; \
00064   typedef typename Eigen::NumTraits<Scalar>::Real RealScalar; \
00065   typedef typename Eigen::internal::nested<Derived >::type Nested; \
00066   typedef typename Eigen::internal::traits<Derived >::StorageKind StorageKind; \
00067   typedef typename Eigen::internal::traits<Derived >::Index Index; \
00068   enum { RowsAtCompileTime = Eigen::internal::traits<Derived >::RowsAtCompileTime, \
00069         ColsAtCompileTime = Eigen::internal::traits<Derived >::ColsAtCompileTime, \
00070         Flags = Eigen::internal::traits<Derived >::Flags, \
00071         CoeffReadCost = Eigen::internal::traits<Derived >::CoeffReadCost, \
00072         SizeAtCompileTime = Base::SizeAtCompileTime, \
00073         IsVectorAtCompileTime = Base::IsVectorAtCompileTime }; \
00074   using Base::derived; \
00075   using Base::const_cast_derived;
00076 
00077 #define EIGEN_SPARSE_PUBLIC_INTERFACE(Derived) \
00078   _EIGEN_SPARSE_PUBLIC_INTERFACE(Derived, Eigen::SparseMatrixBase<Derived >)
00079 
00080 const int CoherentAccessPattern     = 0x1;
00081 const int InnerRandomAccessPattern  = 0x2 | CoherentAccessPattern;
00082 const int OuterRandomAccessPattern  = 0x4 | CoherentAccessPattern;
00083 const int RandomAccessPattern       = 0x8 | OuterRandomAccessPattern | InnerRandomAccessPattern;
00084 
00085 template<typename Derived> class SparseMatrixBase;
00086 template<typename _Scalar, int _Flags = 0, typename _Index = int>  class SparseMatrix;
00087 template<typename _Scalar, int _Flags = 0, typename _Index = int>  class DynamicSparseMatrix;
00088 template<typename _Scalar, int _Flags = 0, typename _Index = int>  class SparseVector;
00089 template<typename _Scalar, int _Flags = 0, typename _Index = int>  class MappedSparseMatrix;
00090 
00091 template<typename MatrixType, int Size>           class SparseInnerVectorSet;
00092 template<typename MatrixType, int Mode>           class SparseTriangularView;
00093 template<typename MatrixType, unsigned int UpLo>  class SparseSelfAdjointView;
00094 template<typename Lhs, typename Rhs>              class SparseDiagonalProduct;
00095 template<typename MatrixType> class SparseView;
00096 
00097 template<typename Lhs, typename Rhs>        class SparseSparseProduct;
00098 template<typename Lhs, typename Rhs>        class SparseTimeDenseProduct;
00099 template<typename Lhs, typename Rhs>        class DenseTimeSparseProduct;
00100 template<typename Lhs, typename Rhs, bool Transpose> class SparseDenseOuterProduct;
00101 
00102 template<typename Lhs, typename Rhs> struct SparseSparseProductReturnType;
00103 template<typename Lhs, typename Rhs, int InnerSize = internal::traits<Lhs>::ColsAtCompileTime> struct DenseSparseProductReturnType;
00104 template<typename Lhs, typename Rhs, int InnerSize = internal::traits<Lhs>::ColsAtCompileTime> struct SparseDenseProductReturnType;
00105 template<typename MatrixType,int UpLo> class SparseSymmetricPermutationProduct;
00106 
00107 namespace internal {
00108 
00109 template<typename T,int Rows,int Cols> struct sparse_eval;
00110 
00111 template<typename T> struct eval<T,Sparse>
00112   : public sparse_eval<T, traits<T>::RowsAtCompileTime,traits<T>::ColsAtCompileTime>
00113 {};
00114 
00115 template<typename T,int Cols> struct sparse_eval<T,1,Cols> {
00116     typedef typename traits<T>::Scalar _Scalar;
00117     enum { _Flags = traits<T>::Flags| RowMajorBit };
00118   public:
00119     typedef SparseVector<_Scalar, _Flags> type;
00120 };
00121 
00122 template<typename T,int Rows> struct sparse_eval<T,Rows,1> {
00123     typedef typename traits<T>::Scalar _Scalar;
00124     enum { _Flags = traits<T>::Flags & (~RowMajorBit) };
00125   public:
00126     typedef SparseVector<_Scalar, _Flags> type;
00127 };
00128 
00129 template<typename T,int Rows,int Cols> struct sparse_eval {
00130     typedef typename traits<T>::Scalar _Scalar;
00131     enum { _Flags = traits<T>::Flags };
00132   public:
00133     typedef SparseMatrix<_Scalar, _Flags> type;
00134 };
00135 
00136 template<typename T> struct sparse_eval<T,1,1> {
00137     typedef typename traits<T>::Scalar _Scalar;
00138   public:
00139     typedef Matrix<_Scalar, 1, 1> type;
00140 };
00141 
00142 template<typename T> struct plain_matrix_type<T,Sparse>
00143 {
00144   typedef typename traits<T>::Scalar _Scalar;
00145     enum {
00146           _Flags = traits<T>::Flags
00147     };
00148 
00149   public:
00150     typedef SparseMatrix<_Scalar, _Flags> type;
00151 };
00152 
00153 } // end namespace internal
00154 
00163 template<typename Scalar, typename Index=unsigned int>
00164 class Triplet
00165 {
00166 public:
00167   Triplet() : m_row(0), m_col(0), m_value(0) {}
00168 
00169   Triplet(const Index& i, const Index& j, const Scalar& v = Scalar(0))
00170     : m_row(i), m_col(j), m_value(v)
00171   {}
00172 
00174   const Index& row() const { return m_row; }
00175 
00177   const Index& col() const { return m_col; }
00178 
00180   const Scalar& value() const { return m_value; }
00181 protected:
00182   Index m_row, m_col;
00183   Scalar m_value;
00184 };
00185 
00186 } // end namespace Eigen
00187 
00188 #endif // EIGEN_SPARSEUTIL_H