Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-19 09:06:05

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2008-2014 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_COREITERATORS_H
0011 #define EIGEN_COREITERATORS_H
0012 
0013 namespace RivetEigen { 
0014 
0015 /* This file contains the respective InnerIterator definition of the expressions defined in Eigen/Core
0016  */
0017 
0018 namespace internal {
0019 
0020 template<typename XprType, typename EvaluatorKind>
0021 class inner_iterator_selector;
0022 
0023 }
0024 
0025 /** \class InnerIterator
0026   * \brief An InnerIterator allows to loop over the element of any matrix expression.
0027   * 
0028   * \warning To be used with care because an evaluator is constructed every time an InnerIterator iterator is constructed.
0029   * 
0030   * TODO: add a usage example
0031   */
0032 template<typename XprType>
0033 class InnerIterator
0034 {
0035 protected:
0036   typedef internal::inner_iterator_selector<XprType, typename internal::evaluator_traits<XprType>::Kind> IteratorType;
0037   typedef internal::evaluator<XprType> EvaluatorType;
0038   typedef typename internal::traits<XprType>::Scalar Scalar;
0039 public:
0040   /** Construct an iterator over the \a outerId -th row or column of \a xpr */
0041   InnerIterator(const XprType &xpr, const Index &outerId)
0042     : m_eval(xpr), m_iter(m_eval, outerId, xpr.innerSize())
0043   {}
0044   
0045   /// \returns the value of the current coefficient.
0046   EIGEN_STRONG_INLINE Scalar value() const          { return m_iter.value(); }
0047   /** Increment the iterator \c *this to the next non-zero coefficient.
0048     * Explicit zeros are not skipped over. To skip explicit zeros, see class SparseView
0049     */
0050   EIGEN_STRONG_INLINE InnerIterator& operator++()   { m_iter.operator++(); return *this; }
0051   EIGEN_STRONG_INLINE InnerIterator& operator+=(Index i) { m_iter.operator+=(i); return *this; }
0052   EIGEN_STRONG_INLINE InnerIterator operator+(Index i) 
0053   { InnerIterator result(*this); result+=i; return result; }
0054     
0055 
0056   /// \returns the column or row index of the current coefficient.
0057   EIGEN_STRONG_INLINE Index index() const           { return m_iter.index(); }
0058   /// \returns the row index of the current coefficient.
0059   EIGEN_STRONG_INLINE Index row() const             { return m_iter.row(); }
0060   /// \returns the column index of the current coefficient.
0061   EIGEN_STRONG_INLINE Index col() const             { return m_iter.col(); }
0062   /// \returns \c true if the iterator \c *this still references a valid coefficient.
0063   EIGEN_STRONG_INLINE operator bool() const         { return m_iter; }
0064   
0065 protected:
0066   EvaluatorType m_eval;
0067   IteratorType m_iter;
0068 private:
0069   // If you get here, then you're not using the right InnerIterator type, e.g.:
0070   //   SparseMatrix<double,RowMajor> A;
0071   //   SparseMatrix<double>::InnerIterator it(A,0);
0072   template<typename T> InnerIterator(const EigenBase<T>&,Index outer);
0073 };
0074 
0075 namespace internal {
0076 
0077 // Generic inner iterator implementation for dense objects
0078 template<typename XprType>
0079 class inner_iterator_selector<XprType, IndexBased>
0080 {
0081 protected:
0082   typedef evaluator<XprType> EvaluatorType;
0083   typedef typename traits<XprType>::Scalar Scalar;
0084   enum { IsRowMajor = (XprType::Flags&RowMajorBit)==RowMajorBit };
0085   
0086 public:
0087   EIGEN_STRONG_INLINE inner_iterator_selector(const EvaluatorType &eval, const Index &outerId, const Index &innerSize)
0088     : m_eval(eval), m_inner(0), m_outer(outerId), m_end(innerSize)
0089   {}
0090 
0091   EIGEN_STRONG_INLINE Scalar value() const
0092   {
0093     return (IsRowMajor) ? m_eval.coeff(m_outer, m_inner)
0094                         : m_eval.coeff(m_inner, m_outer);
0095   }
0096 
0097   EIGEN_STRONG_INLINE inner_iterator_selector& operator++() { m_inner++; return *this; }
0098 
0099   EIGEN_STRONG_INLINE Index index() const { return m_inner; }
0100   inline Index row() const { return IsRowMajor ? m_outer : index(); }
0101   inline Index col() const { return IsRowMajor ? index() : m_outer; }
0102 
0103   EIGEN_STRONG_INLINE operator bool() const { return m_inner < m_end && m_inner>=0; }
0104 
0105 protected:
0106   const EvaluatorType& m_eval;
0107   Index m_inner;
0108   const Index m_outer;
0109   const Index m_end;
0110 };
0111 
0112 // For iterator-based evaluator, inner-iterator is already implemented as
0113 // evaluator<>::InnerIterator
0114 template<typename XprType>
0115 class inner_iterator_selector<XprType, IteratorBased>
0116  : public evaluator<XprType>::InnerIterator
0117 {
0118 protected:
0119   typedef typename evaluator<XprType>::InnerIterator Base;
0120   typedef evaluator<XprType> EvaluatorType;
0121   
0122 public:
0123   EIGEN_STRONG_INLINE inner_iterator_selector(const EvaluatorType &eval, const Index &outerId, const Index &/*innerSize*/)
0124     : Base(eval, outerId)
0125   {}  
0126 };
0127 
0128 } // end namespace internal
0129 
0130 } // end namespace RivetEigen
0131 
0132 #endif // EIGEN_COREITERATORS_H