Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 10:34:42

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2015 Gael Guennebaud <gael.guennebaud@inria.fr>
0005 //
0006 // This Source Code Form is subject to the terms of the Mozilla
0007 // Public License v. 2.0. If a copy of the MPL was not distributed
0008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0009 
0010 #ifndef EIGEN_SPARSE_MAP_H
0011 #define EIGEN_SPARSE_MAP_H
0012 
0013 namespace Eigen {
0014 
0015 namespace internal {
0016 
0017 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
0018 struct traits<Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
0019   : public traits<SparseMatrix<MatScalar,MatOptions,MatIndex> >
0020 {
0021   typedef SparseMatrix<MatScalar,MatOptions,MatIndex> PlainObjectType;
0022   typedef traits<PlainObjectType> TraitsBase;
0023   enum {
0024     Flags = TraitsBase::Flags & (~NestByRefBit)
0025   };
0026 };
0027 
0028 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
0029 struct traits<Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
0030   : public traits<SparseMatrix<MatScalar,MatOptions,MatIndex> >
0031 {
0032   typedef SparseMatrix<MatScalar,MatOptions,MatIndex> PlainObjectType;
0033   typedef traits<PlainObjectType> TraitsBase;
0034   enum {
0035     Flags = TraitsBase::Flags & (~ (NestByRefBit | LvalueBit))
0036   };
0037 };
0038 
0039 } // end namespace internal
0040 
0041 template<typename Derived,
0042          int Level = internal::accessors_level<Derived>::has_write_access ? WriteAccessors : ReadOnlyAccessors
0043 > class SparseMapBase;
0044 
0045 /** \ingroup SparseCore_Module
0046   * class SparseMapBase
0047   * \brief Common base class for Map and Ref instance of sparse matrix and vector.
0048   */
0049 template<typename Derived>
0050 class SparseMapBase<Derived,ReadOnlyAccessors>
0051   : public SparseCompressedBase<Derived>
0052 {
0053   public:
0054     typedef SparseCompressedBase<Derived> Base;
0055     typedef typename Base::Scalar Scalar;
0056     typedef typename Base::StorageIndex StorageIndex;
0057     enum { IsRowMajor = Base::IsRowMajor };
0058     using Base::operator=;
0059   protected:
0060     
0061     typedef typename internal::conditional<
0062                          bool(internal::is_lvalue<Derived>::value),
0063                          Scalar *, const Scalar *>::type ScalarPointer;
0064     typedef typename internal::conditional<
0065                          bool(internal::is_lvalue<Derived>::value),
0066                          StorageIndex *, const StorageIndex *>::type IndexPointer;
0067 
0068     Index   m_outerSize;
0069     Index   m_innerSize;
0070     Array<StorageIndex,2,1>  m_zero_nnz;
0071     IndexPointer  m_outerIndex;
0072     IndexPointer  m_innerIndices;
0073     ScalarPointer m_values;
0074     IndexPointer  m_innerNonZeros;
0075 
0076   public:
0077 
0078     /** \copydoc SparseMatrixBase::rows() */
0079     inline Index rows() const { return IsRowMajor ? m_outerSize : m_innerSize; }
0080     /** \copydoc SparseMatrixBase::cols() */
0081     inline Index cols() const { return IsRowMajor ? m_innerSize : m_outerSize; }
0082     /** \copydoc SparseMatrixBase::innerSize() */
0083     inline Index innerSize() const { return m_innerSize; }
0084     /** \copydoc SparseMatrixBase::outerSize() */
0085     inline Index outerSize() const { return m_outerSize; }
0086     /** \copydoc SparseCompressedBase::nonZeros */
0087     inline Index nonZeros() const { return m_zero_nnz[1]; }
0088     
0089     /** \copydoc SparseCompressedBase::isCompressed */
0090     bool isCompressed() const { return m_innerNonZeros==0; }
0091 
0092     //----------------------------------------
0093     // direct access interface
0094     /** \copydoc SparseMatrix::valuePtr */
0095     inline const Scalar* valuePtr() const { return m_values; }
0096     /** \copydoc SparseMatrix::innerIndexPtr */
0097     inline const StorageIndex* innerIndexPtr() const { return m_innerIndices; }
0098     /** \copydoc SparseMatrix::outerIndexPtr */
0099     inline const StorageIndex* outerIndexPtr() const { return m_outerIndex; }
0100     /** \copydoc SparseMatrix::innerNonZeroPtr */
0101     inline const StorageIndex* innerNonZeroPtr() const { return m_innerNonZeros; }
0102     //----------------------------------------
0103 
0104     /** \copydoc SparseMatrix::coeff */
0105     inline Scalar coeff(Index row, Index col) const
0106     {
0107       const Index outer = IsRowMajor ? row : col;
0108       const Index inner = IsRowMajor ? col : row;
0109 
0110       Index start = m_outerIndex[outer];
0111       Index end = isCompressed() ? m_outerIndex[outer+1] : start + m_innerNonZeros[outer];
0112       if (start==end)
0113         return Scalar(0);
0114       else if (end>0 && inner==m_innerIndices[end-1])
0115         return m_values[end-1];
0116       // ^^  optimization: let's first check if it is the last coefficient
0117       // (very common in high level algorithms)
0118 
0119       const StorageIndex* r = std::lower_bound(&m_innerIndices[start],&m_innerIndices[end-1],inner);
0120       const Index id = r-&m_innerIndices[0];
0121       return ((*r==inner) && (id<end)) ? m_values[id] : Scalar(0);
0122     }
0123 
0124     inline SparseMapBase(Index rows, Index cols, Index nnz, IndexPointer outerIndexPtr, IndexPointer innerIndexPtr,
0125                               ScalarPointer valuePtr, IndexPointer innerNonZerosPtr = 0)
0126       : m_outerSize(IsRowMajor?rows:cols), m_innerSize(IsRowMajor?cols:rows), m_zero_nnz(0,internal::convert_index<StorageIndex>(nnz)), m_outerIndex(outerIndexPtr),
0127         m_innerIndices(innerIndexPtr), m_values(valuePtr), m_innerNonZeros(innerNonZerosPtr)
0128     {}
0129 
0130     // for vectors
0131     inline SparseMapBase(Index size, Index nnz, IndexPointer innerIndexPtr, ScalarPointer valuePtr)
0132       : m_outerSize(1), m_innerSize(size), m_zero_nnz(0,internal::convert_index<StorageIndex>(nnz)), m_outerIndex(m_zero_nnz.data()),
0133         m_innerIndices(innerIndexPtr), m_values(valuePtr), m_innerNonZeros(0)
0134     {}
0135 
0136     /** Empty destructor */
0137     inline ~SparseMapBase() {}
0138 
0139   protected:
0140     inline SparseMapBase() {}
0141 };
0142 
0143 /** \ingroup SparseCore_Module
0144   * class SparseMapBase
0145   * \brief Common base class for writable Map and Ref instance of sparse matrix and vector.
0146   */
0147 template<typename Derived>
0148 class SparseMapBase<Derived,WriteAccessors>
0149   : public SparseMapBase<Derived,ReadOnlyAccessors>
0150 {
0151     typedef MapBase<Derived, ReadOnlyAccessors> ReadOnlyMapBase;
0152     
0153   public:
0154     typedef SparseMapBase<Derived, ReadOnlyAccessors> Base;
0155     typedef typename Base::Scalar Scalar;
0156     typedef typename Base::StorageIndex StorageIndex;
0157     enum { IsRowMajor = Base::IsRowMajor };
0158     
0159     using Base::operator=;
0160 
0161   public:
0162     
0163     //----------------------------------------
0164     // direct access interface
0165     using Base::valuePtr;
0166     using Base::innerIndexPtr;
0167     using Base::outerIndexPtr;
0168     using Base::innerNonZeroPtr;
0169     /** \copydoc SparseMatrix::valuePtr */
0170     inline Scalar* valuePtr()              { return Base::m_values; }
0171     /** \copydoc SparseMatrix::innerIndexPtr */
0172     inline StorageIndex* innerIndexPtr()   { return Base::m_innerIndices; }
0173     /** \copydoc SparseMatrix::outerIndexPtr */
0174     inline StorageIndex* outerIndexPtr()   { return Base::m_outerIndex; }
0175     /** \copydoc SparseMatrix::innerNonZeroPtr */
0176     inline StorageIndex* innerNonZeroPtr() { return Base::m_innerNonZeros; }
0177     //----------------------------------------
0178 
0179     /** \copydoc SparseMatrix::coeffRef */
0180     inline Scalar& coeffRef(Index row, Index col)
0181     {
0182       const Index outer = IsRowMajor ? row : col;
0183       const Index inner = IsRowMajor ? col : row;
0184 
0185       Index start = Base::m_outerIndex[outer];
0186       Index end = Base::isCompressed() ? Base::m_outerIndex[outer+1] : start + Base::m_innerNonZeros[outer];
0187       eigen_assert(end>=start && "you probably called coeffRef on a non finalized matrix");
0188       eigen_assert(end>start && "coeffRef cannot be called on a zero coefficient");
0189       StorageIndex* r = std::lower_bound(&Base::m_innerIndices[start],&Base::m_innerIndices[end],inner);
0190       const Index id = r - &Base::m_innerIndices[0];
0191       eigen_assert((*r==inner) && (id<end) && "coeffRef cannot be called on a zero coefficient");
0192       return const_cast<Scalar*>(Base::m_values)[id];
0193     }
0194     
0195     inline SparseMapBase(Index rows, Index cols, Index nnz, StorageIndex* outerIndexPtr, StorageIndex* innerIndexPtr,
0196                          Scalar* valuePtr, StorageIndex* innerNonZerosPtr = 0)
0197       : Base(rows, cols, nnz, outerIndexPtr, innerIndexPtr, valuePtr, innerNonZerosPtr)
0198     {}
0199 
0200     // for vectors
0201     inline SparseMapBase(Index size, Index nnz, StorageIndex* innerIndexPtr, Scalar* valuePtr)
0202       : Base(size, nnz, innerIndexPtr, valuePtr)
0203     {}
0204 
0205     /** Empty destructor */
0206     inline ~SparseMapBase() {}
0207 
0208   protected:
0209     inline SparseMapBase() {}
0210 };
0211 
0212 /** \ingroup SparseCore_Module
0213   *
0214   * \brief Specialization of class Map for SparseMatrix-like storage.
0215   *
0216   * \tparam SparseMatrixType the equivalent sparse matrix type of the referenced data, it must be a template instance of class SparseMatrix.
0217   *
0218   * \sa class Map, class SparseMatrix, class Ref<SparseMatrixType,Options>
0219   */
0220 #ifndef EIGEN_PARSED_BY_DOXYGEN
0221 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
0222 class Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType>
0223   : public SparseMapBase<Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
0224 #else
0225 template<typename SparseMatrixType>
0226 class Map<SparseMatrixType>
0227   : public SparseMapBase<Derived,WriteAccessors>
0228 #endif
0229 {
0230   public:
0231     typedef SparseMapBase<Map> Base;
0232     EIGEN_SPARSE_PUBLIC_INTERFACE(Map)
0233     enum { IsRowMajor = Base::IsRowMajor };
0234 
0235   public:
0236 
0237     /** Constructs a read-write Map to a sparse matrix of size \a rows x \a cols, containing \a nnz non-zero coefficients,
0238       * stored as a sparse format as defined by the pointers \a outerIndexPtr, \a innerIndexPtr, and \a valuePtr.
0239       * If the optional parameter \a innerNonZerosPtr is the null pointer, then a standard compressed format is assumed.
0240       *
0241       * This constructor is available only if \c SparseMatrixType is non-const.
0242       *
0243       * More details on the expected storage schemes are given in the \ref TutorialSparse "manual pages".
0244       */
0245     inline Map(Index rows, Index cols, Index nnz, StorageIndex* outerIndexPtr,
0246                StorageIndex* innerIndexPtr, Scalar* valuePtr, StorageIndex* innerNonZerosPtr = 0)
0247       : Base(rows, cols, nnz, outerIndexPtr, innerIndexPtr, valuePtr, innerNonZerosPtr)
0248     {}
0249 #ifndef EIGEN_PARSED_BY_DOXYGEN
0250     /** Empty destructor */
0251     inline ~Map() {}
0252 };
0253 
0254 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
0255 class Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType>
0256   : public SparseMapBase<Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
0257 {
0258   public:
0259     typedef SparseMapBase<Map> Base;
0260     EIGEN_SPARSE_PUBLIC_INTERFACE(Map)
0261     enum { IsRowMajor = Base::IsRowMajor };
0262 
0263   public:
0264 #endif
0265     /** This is the const version of the above constructor.
0266       *
0267       * This constructor is available only if \c SparseMatrixType is const, e.g.:
0268       * \code Map<const SparseMatrix<double> >  \endcode
0269       */
0270     inline Map(Index rows, Index cols, Index nnz, const StorageIndex* outerIndexPtr,
0271                const StorageIndex* innerIndexPtr, const Scalar* valuePtr, const StorageIndex* innerNonZerosPtr = 0)
0272       : Base(rows, cols, nnz, outerIndexPtr, innerIndexPtr, valuePtr, innerNonZerosPtr)
0273     {}
0274 
0275     /** Empty destructor */
0276     inline ~Map() {}
0277 };
0278 
0279 namespace internal {
0280 
0281 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
0282 struct evaluator<Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
0283   : evaluator<SparseCompressedBase<Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > >
0284 {
0285   typedef evaluator<SparseCompressedBase<Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > > Base;
0286   typedef Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> XprType;  
0287   evaluator() : Base() {}
0288   explicit evaluator(const XprType &mat) : Base(mat) {}
0289 };
0290 
0291 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
0292 struct evaluator<Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
0293   : evaluator<SparseCompressedBase<Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > >
0294 {
0295   typedef evaluator<SparseCompressedBase<Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > > Base;
0296   typedef Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> XprType;  
0297   evaluator() : Base() {}
0298   explicit evaluator(const XprType &mat) : Base(mat) {}
0299 };
0300 
0301 }
0302 
0303 } // end namespace Eigen
0304 
0305 #endif // EIGEN_SPARSE_MAP_H