Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2017 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_INDEXED_VIEW_H
0011 #define EIGEN_INDEXED_VIEW_H
0012 
0013 namespace RivetEigen {
0014 
0015 namespace internal {
0016 
0017 template<typename XprType, typename RowIndices, typename ColIndices>
0018 struct traits<IndexedView<XprType, RowIndices, ColIndices> >
0019  : traits<XprType>
0020 {
0021   enum {
0022     RowsAtCompileTime = int(array_size<RowIndices>::value),
0023     ColsAtCompileTime = int(array_size<ColIndices>::value),
0024     MaxRowsAtCompileTime = RowsAtCompileTime != Dynamic ? int(RowsAtCompileTime) : Dynamic,
0025     MaxColsAtCompileTime = ColsAtCompileTime != Dynamic ? int(ColsAtCompileTime) : Dynamic,
0026 
0027     XprTypeIsRowMajor = (int(traits<XprType>::Flags)&RowMajorBit) != 0,
0028     IsRowMajor = (MaxRowsAtCompileTime==1&&MaxColsAtCompileTime!=1) ? 1
0029                : (MaxColsAtCompileTime==1&&MaxRowsAtCompileTime!=1) ? 0
0030                : XprTypeIsRowMajor,
0031 
0032     RowIncr = int(get_compile_time_incr<RowIndices>::value),
0033     ColIncr = int(get_compile_time_incr<ColIndices>::value),
0034     InnerIncr = IsRowMajor ? ColIncr : RowIncr,
0035     OuterIncr = IsRowMajor ? RowIncr : ColIncr,
0036 
0037     HasSameStorageOrderAsXprType = (IsRowMajor == XprTypeIsRowMajor),
0038     XprInnerStride = HasSameStorageOrderAsXprType ? int(inner_stride_at_compile_time<XprType>::ret) : int(outer_stride_at_compile_time<XprType>::ret),
0039     XprOuterstride = HasSameStorageOrderAsXprType ? int(outer_stride_at_compile_time<XprType>::ret) : int(inner_stride_at_compile_time<XprType>::ret),
0040 
0041     InnerSize = XprTypeIsRowMajor ? ColsAtCompileTime : RowsAtCompileTime,
0042     IsBlockAlike = InnerIncr==1 && OuterIncr==1,
0043     IsInnerPannel = HasSameStorageOrderAsXprType && is_same<AllRange<InnerSize>,typename conditional<XprTypeIsRowMajor,ColIndices,RowIndices>::type>::value,
0044 
0045     InnerStrideAtCompileTime = InnerIncr<0 || InnerIncr==DynamicIndex || XprInnerStride==Dynamic ? Dynamic : XprInnerStride * InnerIncr,
0046     OuterStrideAtCompileTime = OuterIncr<0 || OuterIncr==DynamicIndex || XprOuterstride==Dynamic ? Dynamic : XprOuterstride * OuterIncr,
0047 
0048     ReturnAsScalar = is_same<RowIndices,SingleRange>::value && is_same<ColIndices,SingleRange>::value,
0049     ReturnAsBlock = (!ReturnAsScalar) && IsBlockAlike,
0050     ReturnAsIndexedView = (!ReturnAsScalar) && (!ReturnAsBlock),
0051 
0052     // FIXME we deal with compile-time strides if and only if we have DirectAccessBit flag,
0053     // but this is too strict regarding negative strides...
0054     DirectAccessMask = (int(InnerIncr)!=UndefinedIncr && int(OuterIncr)!=UndefinedIncr && InnerIncr>=0 && OuterIncr>=0) ? DirectAccessBit : 0,
0055     FlagsRowMajorBit = IsRowMajor ? RowMajorBit : 0,
0056     FlagsLvalueBit = is_lvalue<XprType>::value ? LvalueBit : 0,
0057     FlagsLinearAccessBit = (RowsAtCompileTime == 1 || ColsAtCompileTime == 1) ? LinearAccessBit : 0,
0058     Flags = (traits<XprType>::Flags & (HereditaryBits | DirectAccessMask )) | FlagsLvalueBit | FlagsRowMajorBit | FlagsLinearAccessBit
0059   };
0060 
0061   typedef Block<XprType,RowsAtCompileTime,ColsAtCompileTime,IsInnerPannel> BlockType;
0062 };
0063 
0064 }
0065 
0066 template<typename XprType, typename RowIndices, typename ColIndices, typename StorageKind>
0067 class IndexedViewImpl;
0068 
0069 
0070 /** \class IndexedView
0071   * \ingroup Core_Module
0072   *
0073   * \brief Expression of a non-sequential sub-matrix defined by arbitrary sequences of row and column indices
0074   *
0075   * \tparam XprType the type of the expression in which we are taking the intersections of sub-rows and sub-columns
0076   * \tparam RowIndices the type of the object defining the sequence of row indices
0077   * \tparam ColIndices the type of the object defining the sequence of column indices
0078   *
0079   * This class represents an expression of a sub-matrix (or sub-vector) defined as the intersection
0080   * of sub-sets of rows and columns, that are themself defined by generic sequences of row indices \f$ \{r_0,r_1,..r_{m-1}\} \f$
0081   * and column indices \f$ \{c_0,c_1,..c_{n-1} \}\f$. Let \f$ A \f$  be the nested matrix, then the resulting matrix \f$ B \f$ has \c m
0082   * rows and \c n columns, and its entries are given by: \f$ B(i,j) = A(r_i,c_j) \f$.
0083   *
0084   * The \c RowIndices and \c ColIndices types must be compatible with the following API:
0085   * \code
0086   * <integral type> operator[](Index) const;
0087   * Index size() const;
0088   * \endcode
0089   *
0090   * Typical supported types thus include:
0091   *  - std::vector<int>
0092   *  - std::valarray<int>
0093   *  - std::array<int>
0094   *  - Plain C arrays: int[N]
0095   *  - RivetEigen::ArrayXi
0096   *  - decltype(ArrayXi::LinSpaced(...))
0097   *  - Any view/expressions of the previous types
0098   *  - RivetEigen::ArithmeticSequence
0099   *  - RivetEigen::internal::AllRange      (helper for RivetEigen::all)
0100   *  - RivetEigen::internal::SingleRange  (helper for single index)
0101   *  - etc.
0102   *
0103   * In typical usages of %Eigen, this class should never be used directly. It is the return type of
0104   * DenseBase::operator()(const RowIndices&, const ColIndices&).
0105   *
0106   * \sa class Block
0107   */
0108 template<typename XprType, typename RowIndices, typename ColIndices>
0109 class IndexedView : public IndexedViewImpl<XprType, RowIndices, ColIndices, typename internal::traits<XprType>::StorageKind>
0110 {
0111 public:
0112   typedef typename IndexedViewImpl<XprType, RowIndices, ColIndices, typename internal::traits<XprType>::StorageKind>::Base Base;
0113   EIGEN_GENERIC_PUBLIC_INTERFACE(IndexedView)
0114   EIGEN_INHERIT_ASSIGNMENT_OPERATORS(IndexedView)
0115 
0116   typedef typename internal::ref_selector<XprType>::non_const_type MatrixTypeNested;
0117   typedef typename internal::remove_all<XprType>::type NestedExpression;
0118 
0119   template<typename T0, typename T1>
0120   IndexedView(XprType& xpr, const T0& rowIndices, const T1& colIndices)
0121     : m_xpr(xpr), m_rowIndices(rowIndices), m_colIndices(colIndices)
0122   {}
0123 
0124   /** \returns number of rows */
0125   Index rows() const { return internal::size(m_rowIndices); }
0126 
0127   /** \returns number of columns */
0128   Index cols() const { return internal::size(m_colIndices); }
0129 
0130   /** \returns the nested expression */
0131   const typename internal::remove_all<XprType>::type&
0132   nestedExpression() const { return m_xpr; }
0133 
0134   /** \returns the nested expression */
0135   typename internal::remove_reference<XprType>::type&
0136   nestedExpression() { return m_xpr; }
0137 
0138   /** \returns a const reference to the object storing/generating the row indices */
0139   const RowIndices& rowIndices() const { return m_rowIndices; }
0140 
0141   /** \returns a const reference to the object storing/generating the column indices */
0142   const ColIndices& colIndices() const { return m_colIndices; }
0143 
0144 protected:
0145   MatrixTypeNested m_xpr;
0146   RowIndices m_rowIndices;
0147   ColIndices m_colIndices;
0148 };
0149 
0150 
0151 // Generic API dispatcher
0152 template<typename XprType, typename RowIndices, typename ColIndices, typename StorageKind>
0153 class IndexedViewImpl
0154   : public internal::generic_xpr_base<IndexedView<XprType, RowIndices, ColIndices> >::type
0155 {
0156 public:
0157   typedef typename internal::generic_xpr_base<IndexedView<XprType, RowIndices, ColIndices> >::type Base;
0158 };
0159 
0160 namespace internal {
0161 
0162 
0163 template<typename ArgType, typename RowIndices, typename ColIndices>
0164 struct unary_evaluator<IndexedView<ArgType, RowIndices, ColIndices>, IndexBased>
0165   : evaluator_base<IndexedView<ArgType, RowIndices, ColIndices> >
0166 {
0167   typedef IndexedView<ArgType, RowIndices, ColIndices> XprType;
0168 
0169   enum {
0170     CoeffReadCost = evaluator<ArgType>::CoeffReadCost /* TODO + cost of row/col index */,
0171 
0172     FlagsLinearAccessBit = (traits<XprType>::RowsAtCompileTime == 1 || traits<XprType>::ColsAtCompileTime == 1) ? LinearAccessBit : 0,
0173 
0174     FlagsRowMajorBit = traits<XprType>::FlagsRowMajorBit, 
0175 
0176     Flags = (evaluator<ArgType>::Flags & (HereditaryBits & ~RowMajorBit /*| LinearAccessBit | DirectAccessBit*/)) | FlagsLinearAccessBit | FlagsRowMajorBit,
0177 
0178     Alignment = 0
0179   };
0180 
0181   EIGEN_DEVICE_FUNC explicit unary_evaluator(const XprType& xpr) : m_argImpl(xpr.nestedExpression()), m_xpr(xpr)
0182   {
0183     EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
0184   }
0185 
0186   typedef typename XprType::Scalar Scalar;
0187   typedef typename XprType::CoeffReturnType CoeffReturnType;
0188 
0189   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0190   CoeffReturnType coeff(Index row, Index col) const
0191   {
0192     return m_argImpl.coeff(m_xpr.rowIndices()[row], m_xpr.colIndices()[col]);
0193   }
0194 
0195   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0196   Scalar& coeffRef(Index row, Index col)
0197   {
0198     return m_argImpl.coeffRef(m_xpr.rowIndices()[row], m_xpr.colIndices()[col]);
0199   }
0200 
0201   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0202   Scalar& coeffRef(Index index)
0203   {
0204     EIGEN_STATIC_ASSERT_LVALUE(XprType)
0205     Index row = XprType::RowsAtCompileTime == 1 ? 0 : index;
0206     Index col = XprType::RowsAtCompileTime == 1 ? index : 0;
0207     return m_argImpl.coeffRef( m_xpr.rowIndices()[row], m_xpr.colIndices()[col]);
0208   }
0209 
0210   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0211   const Scalar& coeffRef(Index index) const
0212   {
0213     Index row = XprType::RowsAtCompileTime == 1 ? 0 : index;
0214     Index col = XprType::RowsAtCompileTime == 1 ? index : 0;
0215     return m_argImpl.coeffRef( m_xpr.rowIndices()[row], m_xpr.colIndices()[col]);
0216   }
0217 
0218   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0219   const CoeffReturnType coeff(Index index) const
0220   {
0221     Index row = XprType::RowsAtCompileTime == 1 ? 0 : index;
0222     Index col = XprType::RowsAtCompileTime == 1 ? index : 0;
0223     return m_argImpl.coeff( m_xpr.rowIndices()[row], m_xpr.colIndices()[col]);
0224   }
0225 
0226 protected:
0227 
0228   evaluator<ArgType> m_argImpl;
0229   const XprType& m_xpr;
0230 
0231 };
0232 
0233 } // end namespace internal
0234 
0235 } // end namespace RivetEigen
0236 
0237 #endif // EIGEN_INDEXED_VIEW_H