Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:56:09

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_ARITHMETIC_SEQUENCE_H
0011 #define EIGEN_ARITHMETIC_SEQUENCE_H
0012 
0013 namespace Eigen {
0014 
0015 namespace internal {
0016 
0017 #if (!EIGEN_HAS_CXX11) || !((!EIGEN_COMP_GNUC) || EIGEN_COMP_GNUC>=48)
0018 template<typename T> struct aseq_negate {};
0019 
0020 template<> struct aseq_negate<Index> {
0021   typedef Index type;
0022 };
0023 
0024 template<int N> struct aseq_negate<FixedInt<N> > {
0025   typedef FixedInt<-N> type;
0026 };
0027 
0028 // Compilation error in the following case:
0029 template<> struct aseq_negate<FixedInt<DynamicIndex> > {};
0030 
0031 template<typename FirstType,typename SizeType,typename IncrType,
0032          bool FirstIsSymbolic=symbolic::is_symbolic<FirstType>::value,
0033          bool SizeIsSymbolic =symbolic::is_symbolic<SizeType>::value>
0034 struct aseq_reverse_first_type {
0035   typedef Index type;
0036 };
0037 
0038 template<typename FirstType,typename SizeType,typename IncrType>
0039 struct aseq_reverse_first_type<FirstType,SizeType,IncrType,true,true> {
0040   typedef symbolic::AddExpr<FirstType,
0041                             symbolic::ProductExpr<symbolic::AddExpr<SizeType,symbolic::ValueExpr<FixedInt<-1> > >,
0042                                                   symbolic::ValueExpr<IncrType> >
0043                            > type;
0044 };
0045 
0046 template<typename SizeType,typename IncrType,typename EnableIf = void>
0047 struct aseq_reverse_first_type_aux {
0048   typedef Index type;
0049 };
0050 
0051 template<typename SizeType,typename IncrType>
0052 struct aseq_reverse_first_type_aux<SizeType,IncrType,typename internal::enable_if<bool((SizeType::value+IncrType::value)|0x1)>::type> {
0053   typedef FixedInt<(SizeType::value-1)*IncrType::value> type;
0054 };
0055 
0056 template<typename FirstType,typename SizeType,typename IncrType>
0057 struct aseq_reverse_first_type<FirstType,SizeType,IncrType,true,false> {
0058   typedef typename aseq_reverse_first_type_aux<SizeType,IncrType>::type Aux;
0059   typedef symbolic::AddExpr<FirstType,symbolic::ValueExpr<Aux> > type;
0060 };
0061 
0062 template<typename FirstType,typename SizeType,typename IncrType>
0063 struct aseq_reverse_first_type<FirstType,SizeType,IncrType,false,true> {
0064   typedef symbolic::AddExpr<symbolic::ProductExpr<symbolic::AddExpr<SizeType,symbolic::ValueExpr<FixedInt<-1> > >,
0065                                                   symbolic::ValueExpr<IncrType> >,
0066                             symbolic::ValueExpr<> > type;
0067 };
0068 #endif
0069 
0070 // Helper to cleanup the type of the increment:
0071 template<typename T> struct cleanup_seq_incr {
0072   typedef typename cleanup_index_type<T,DynamicIndex>::type type;
0073 };
0074 
0075 }
0076 
0077 //--------------------------------------------------------------------------------
0078 // seq(first,last,incr) and seqN(first,size,incr)
0079 //--------------------------------------------------------------------------------
0080 
0081 template<typename FirstType=Index,typename SizeType=Index,typename IncrType=internal::FixedInt<1> >
0082 class ArithmeticSequence;
0083 
0084 template<typename FirstType,typename SizeType,typename IncrType>
0085 ArithmeticSequence<typename internal::cleanup_index_type<FirstType>::type,
0086                    typename internal::cleanup_index_type<SizeType>::type,
0087                    typename internal::cleanup_seq_incr<IncrType>::type >
0088 seqN(FirstType first, SizeType size, IncrType incr);
0089 
0090 /** \class ArithmeticSequence
0091   * \ingroup Core_Module
0092   *
0093   * This class represents an arithmetic progression \f$ a_0, a_1, a_2, ..., a_{n-1}\f$ defined by
0094   * its \em first value \f$ a_0 \f$, its \em size (aka length) \em n, and the \em increment (aka stride)
0095   * that is equal to \f$ a_{i+1}-a_{i}\f$ for any \em i.
0096   *
0097   * It is internally used as the return type of the Eigen::seq and Eigen::seqN functions, and as the input arguments
0098   * of DenseBase::operator()(const RowIndices&, const ColIndices&), and most of the time this is the
0099   * only way it is used.
0100   *
0101   * \tparam FirstType type of the first element, usually an Index,
0102   *                   but internally it can be a symbolic expression
0103   * \tparam SizeType type representing the size of the sequence, usually an Index
0104   *                  or a compile time integral constant. Internally, it can also be a symbolic expression
0105   * \tparam IncrType type of the increment, can be a runtime Index, or a compile time integral constant (default is compile-time 1)
0106   *
0107   * \sa Eigen::seq, Eigen::seqN, DenseBase::operator()(const RowIndices&, const ColIndices&), class IndexedView
0108   */
0109 template<typename FirstType,typename SizeType,typename IncrType>
0110 class ArithmeticSequence
0111 {
0112 public:
0113   ArithmeticSequence(FirstType first, SizeType size) : m_first(first), m_size(size) {}
0114   ArithmeticSequence(FirstType first, SizeType size, IncrType incr) : m_first(first), m_size(size), m_incr(incr) {}
0115 
0116   enum {
0117     SizeAtCompileTime = internal::get_fixed_value<SizeType>::value,
0118     IncrAtCompileTime = internal::get_fixed_value<IncrType,DynamicIndex>::value
0119   };
0120 
0121   /** \returns the size, i.e., number of elements, of the sequence */
0122   Index size()  const { return m_size; }
0123 
0124   /** \returns the first element \f$ a_0 \f$ in the sequence */
0125   Index first()  const { return m_first; }
0126 
0127   /** \returns the value \f$ a_i \f$ at index \a i in the sequence. */
0128   Index operator[](Index i) const { return m_first + i * m_incr; }
0129 
0130   const FirstType& firstObject() const { return m_first; }
0131   const SizeType&  sizeObject()  const { return m_size; }
0132   const IncrType&  incrObject()  const { return m_incr; }
0133 
0134 protected:
0135   FirstType m_first;
0136   SizeType  m_size;
0137   IncrType  m_incr;
0138 
0139 public:
0140 
0141 #if EIGEN_HAS_CXX11 && ((!EIGEN_COMP_GNUC) || EIGEN_COMP_GNUC>=48)
0142   auto reverse() const -> decltype(Eigen::seqN(m_first+(m_size+fix<-1>())*m_incr,m_size,-m_incr)) {
0143     return seqN(m_first+(m_size+fix<-1>())*m_incr,m_size,-m_incr);
0144   }
0145 #else
0146 protected:
0147   typedef typename internal::aseq_negate<IncrType>::type ReverseIncrType;
0148   typedef typename internal::aseq_reverse_first_type<FirstType,SizeType,IncrType>::type ReverseFirstType;
0149 public:
0150   ArithmeticSequence<ReverseFirstType,SizeType,ReverseIncrType>
0151   reverse() const {
0152     return seqN(m_first+(m_size+fix<-1>())*m_incr,m_size,-m_incr);
0153   }
0154 #endif
0155 };
0156 
0157 /** \returns an ArithmeticSequence starting at \a first, of length \a size, and increment \a incr
0158   *
0159   * \sa seqN(FirstType,SizeType), seq(FirstType,LastType,IncrType) */
0160 template<typename FirstType,typename SizeType,typename IncrType>
0161 ArithmeticSequence<typename internal::cleanup_index_type<FirstType>::type,typename internal::cleanup_index_type<SizeType>::type,typename internal::cleanup_seq_incr<IncrType>::type >
0162 seqN(FirstType first, SizeType size, IncrType incr)  {
0163   return ArithmeticSequence<typename internal::cleanup_index_type<FirstType>::type,typename internal::cleanup_index_type<SizeType>::type,typename internal::cleanup_seq_incr<IncrType>::type>(first,size,incr);
0164 }
0165 
0166 /** \returns an ArithmeticSequence starting at \a first, of length \a size, and unit increment
0167   *
0168   * \sa seqN(FirstType,SizeType,IncrType), seq(FirstType,LastType) */
0169 template<typename FirstType,typename SizeType>
0170 ArithmeticSequence<typename internal::cleanup_index_type<FirstType>::type,typename internal::cleanup_index_type<SizeType>::type >
0171 seqN(FirstType first, SizeType size)  {
0172   return ArithmeticSequence<typename internal::cleanup_index_type<FirstType>::type,typename internal::cleanup_index_type<SizeType>::type>(first,size);
0173 }
0174 
0175 #ifdef EIGEN_PARSED_BY_DOXYGEN
0176 
0177 /** \returns an ArithmeticSequence starting at \a f, up (or down) to \a l, and with positive (or negative) increment \a incr
0178   *
0179   * It is essentially an alias to:
0180   * \code
0181   * seqN(f, (l-f+incr)/incr, incr);
0182   * \endcode
0183   *
0184   * \sa seqN(FirstType,SizeType,IncrType), seq(FirstType,LastType)
0185   */
0186 template<typename FirstType,typename LastType, typename IncrType>
0187 auto seq(FirstType f, LastType l, IncrType incr);
0188 
0189 /** \returns an ArithmeticSequence starting at \a f, up (or down) to \a l, and unit increment
0190   *
0191   * It is essentially an alias to:
0192   * \code
0193   * seqN(f,l-f+1);
0194   * \endcode
0195   *
0196   * \sa seqN(FirstType,SizeType), seq(FirstType,LastType,IncrType)
0197   */
0198 template<typename FirstType,typename LastType>
0199 auto seq(FirstType f, LastType l);
0200 
0201 #else // EIGEN_PARSED_BY_DOXYGEN
0202 
0203 #if EIGEN_HAS_CXX11
0204 template<typename FirstType,typename LastType>
0205 auto seq(FirstType f, LastType l) -> decltype(seqN(typename internal::cleanup_index_type<FirstType>::type(f),
0206                                                    (  typename internal::cleanup_index_type<LastType>::type(l)
0207                                                     - typename internal::cleanup_index_type<FirstType>::type(f)+fix<1>())))
0208 {
0209   return seqN(typename internal::cleanup_index_type<FirstType>::type(f),
0210               (typename internal::cleanup_index_type<LastType>::type(l)
0211                -typename internal::cleanup_index_type<FirstType>::type(f)+fix<1>()));
0212 }
0213 
0214 template<typename FirstType,typename LastType, typename IncrType>
0215 auto seq(FirstType f, LastType l, IncrType incr)
0216   -> decltype(seqN(typename internal::cleanup_index_type<FirstType>::type(f),
0217                    (   typename internal::cleanup_index_type<LastType>::type(l)
0218                      - typename internal::cleanup_index_type<FirstType>::type(f)+typename internal::cleanup_seq_incr<IncrType>::type(incr)
0219                    ) / typename internal::cleanup_seq_incr<IncrType>::type(incr),
0220                    typename internal::cleanup_seq_incr<IncrType>::type(incr)))
0221 {
0222   typedef typename internal::cleanup_seq_incr<IncrType>::type CleanedIncrType;
0223   return seqN(typename internal::cleanup_index_type<FirstType>::type(f),
0224               ( typename internal::cleanup_index_type<LastType>::type(l)
0225                -typename internal::cleanup_index_type<FirstType>::type(f)+CleanedIncrType(incr)) / CleanedIncrType(incr),
0226               CleanedIncrType(incr));
0227 }
0228 
0229 #else // EIGEN_HAS_CXX11
0230 
0231 template<typename FirstType,typename LastType>
0232 typename internal::enable_if<!(symbolic::is_symbolic<FirstType>::value || symbolic::is_symbolic<LastType>::value),
0233                              ArithmeticSequence<typename internal::cleanup_index_type<FirstType>::type,Index> >::type
0234 seq(FirstType f, LastType l)
0235 {
0236   return seqN(typename internal::cleanup_index_type<FirstType>::type(f),
0237               Index((typename internal::cleanup_index_type<LastType>::type(l)-typename internal::cleanup_index_type<FirstType>::type(f)+fix<1>())));
0238 }
0239 
0240 template<typename FirstTypeDerived,typename LastType>
0241 typename internal::enable_if<!symbolic::is_symbolic<LastType>::value,
0242     ArithmeticSequence<FirstTypeDerived, symbolic::AddExpr<symbolic::AddExpr<symbolic::NegateExpr<FirstTypeDerived>,symbolic::ValueExpr<> >,
0243                                                             symbolic::ValueExpr<internal::FixedInt<1> > > > >::type
0244 seq(const symbolic::BaseExpr<FirstTypeDerived> &f, LastType l)
0245 {
0246   return seqN(f.derived(),(typename internal::cleanup_index_type<LastType>::type(l)-f.derived()+fix<1>()));
0247 }
0248 
0249 template<typename FirstType,typename LastTypeDerived>
0250 typename internal::enable_if<!symbolic::is_symbolic<FirstType>::value,
0251     ArithmeticSequence<typename internal::cleanup_index_type<FirstType>::type,
0252                         symbolic::AddExpr<symbolic::AddExpr<LastTypeDerived,symbolic::ValueExpr<> >,
0253                                           symbolic::ValueExpr<internal::FixedInt<1> > > > >::type
0254 seq(FirstType f, const symbolic::BaseExpr<LastTypeDerived> &l)
0255 {
0256   return seqN(typename internal::cleanup_index_type<FirstType>::type(f),(l.derived()-typename internal::cleanup_index_type<FirstType>::type(f)+fix<1>()));
0257 }
0258 
0259 template<typename FirstTypeDerived,typename LastTypeDerived>
0260 ArithmeticSequence<FirstTypeDerived,
0261                     symbolic::AddExpr<symbolic::AddExpr<LastTypeDerived,symbolic::NegateExpr<FirstTypeDerived> >,symbolic::ValueExpr<internal::FixedInt<1> > > >
0262 seq(const symbolic::BaseExpr<FirstTypeDerived> &f, const symbolic::BaseExpr<LastTypeDerived> &l)
0263 {
0264   return seqN(f.derived(),(l.derived()-f.derived()+fix<1>()));
0265 }
0266 
0267 
0268 template<typename FirstType,typename LastType, typename IncrType>
0269 typename internal::enable_if<!(symbolic::is_symbolic<FirstType>::value || symbolic::is_symbolic<LastType>::value),
0270     ArithmeticSequence<typename internal::cleanup_index_type<FirstType>::type,Index,typename internal::cleanup_seq_incr<IncrType>::type> >::type
0271 seq(FirstType f, LastType l, IncrType incr)
0272 {
0273   typedef typename internal::cleanup_seq_incr<IncrType>::type CleanedIncrType;
0274   return seqN(typename internal::cleanup_index_type<FirstType>::type(f),
0275               Index((typename internal::cleanup_index_type<LastType>::type(l)-typename internal::cleanup_index_type<FirstType>::type(f)+CleanedIncrType(incr))/CleanedIncrType(incr)), incr);
0276 }
0277 
0278 template<typename FirstTypeDerived,typename LastType, typename IncrType>
0279 typename internal::enable_if<!symbolic::is_symbolic<LastType>::value,
0280     ArithmeticSequence<FirstTypeDerived,
0281                         symbolic::QuotientExpr<symbolic::AddExpr<symbolic::AddExpr<symbolic::NegateExpr<FirstTypeDerived>,
0282                                                                                    symbolic::ValueExpr<> >,
0283                                                                  symbolic::ValueExpr<typename internal::cleanup_seq_incr<IncrType>::type> >,
0284                                               symbolic::ValueExpr<typename internal::cleanup_seq_incr<IncrType>::type> >,
0285                         typename internal::cleanup_seq_incr<IncrType>::type> >::type
0286 seq(const symbolic::BaseExpr<FirstTypeDerived> &f, LastType l, IncrType incr)
0287 {
0288   typedef typename internal::cleanup_seq_incr<IncrType>::type CleanedIncrType;
0289   return seqN(f.derived(),(typename internal::cleanup_index_type<LastType>::type(l)-f.derived()+CleanedIncrType(incr))/CleanedIncrType(incr), incr);
0290 }
0291 
0292 template<typename FirstType,typename LastTypeDerived, typename IncrType>
0293 typename internal::enable_if<!symbolic::is_symbolic<FirstType>::value,
0294     ArithmeticSequence<typename internal::cleanup_index_type<FirstType>::type,
0295                         symbolic::QuotientExpr<symbolic::AddExpr<symbolic::AddExpr<LastTypeDerived,symbolic::ValueExpr<> >,
0296                                                                  symbolic::ValueExpr<typename internal::cleanup_seq_incr<IncrType>::type> >,
0297                                                symbolic::ValueExpr<typename internal::cleanup_seq_incr<IncrType>::type> >,
0298                         typename internal::cleanup_seq_incr<IncrType>::type> >::type
0299 seq(FirstType f, const symbolic::BaseExpr<LastTypeDerived> &l, IncrType incr)
0300 {
0301   typedef typename internal::cleanup_seq_incr<IncrType>::type CleanedIncrType;
0302   return seqN(typename internal::cleanup_index_type<FirstType>::type(f),
0303               (l.derived()-typename internal::cleanup_index_type<FirstType>::type(f)+CleanedIncrType(incr))/CleanedIncrType(incr), incr);
0304 }
0305 
0306 template<typename FirstTypeDerived,typename LastTypeDerived, typename IncrType>
0307 ArithmeticSequence<FirstTypeDerived,
0308                     symbolic::QuotientExpr<symbolic::AddExpr<symbolic::AddExpr<LastTypeDerived,
0309                                                                                symbolic::NegateExpr<FirstTypeDerived> >,
0310                                                              symbolic::ValueExpr<typename internal::cleanup_seq_incr<IncrType>::type> >,
0311                                           symbolic::ValueExpr<typename internal::cleanup_seq_incr<IncrType>::type> >,
0312                     typename internal::cleanup_seq_incr<IncrType>::type>
0313 seq(const symbolic::BaseExpr<FirstTypeDerived> &f, const symbolic::BaseExpr<LastTypeDerived> &l, IncrType incr)
0314 {
0315   typedef typename internal::cleanup_seq_incr<IncrType>::type CleanedIncrType;
0316   return seqN(f.derived(),(l.derived()-f.derived()+CleanedIncrType(incr))/CleanedIncrType(incr), incr);
0317 }
0318 #endif // EIGEN_HAS_CXX11
0319 
0320 #endif // EIGEN_PARSED_BY_DOXYGEN
0321 
0322 
0323 #if EIGEN_HAS_CXX11 || defined(EIGEN_PARSED_BY_DOXYGEN)
0324 /** \cpp11
0325   * \returns a symbolic ArithmeticSequence representing the last \a size elements with increment \a incr.
0326   *
0327   * It is a shortcut for: \code seqN(last-(size-fix<1>)*incr, size, incr) \endcode
0328   * 
0329   * \sa lastN(SizeType), seqN(FirstType,SizeType), seq(FirstType,LastType,IncrType) */
0330 template<typename SizeType,typename IncrType>
0331 auto lastN(SizeType size, IncrType incr)
0332 -> decltype(seqN(Eigen::last-(size-fix<1>())*incr, size, incr))
0333 {
0334   return seqN(Eigen::last-(size-fix<1>())*incr, size, incr);
0335 }
0336 
0337 /** \cpp11
0338   * \returns a symbolic ArithmeticSequence representing the last \a size elements with a unit increment.
0339   *
0340   *  It is a shortcut for: \code seq(last+fix<1>-size, last) \endcode
0341   * 
0342   * \sa lastN(SizeType,IncrType, seqN(FirstType,SizeType), seq(FirstType,LastType) */
0343 template<typename SizeType>
0344 auto lastN(SizeType size)
0345 -> decltype(seqN(Eigen::last+fix<1>()-size, size))
0346 {
0347   return seqN(Eigen::last+fix<1>()-size, size);
0348 }
0349 #endif
0350 
0351 namespace internal {
0352 
0353 // Convert a symbolic span into a usable one (i.e., remove last/end "keywords")
0354 template<typename T>
0355 struct make_size_type {
0356   typedef typename internal::conditional<symbolic::is_symbolic<T>::value, Index, T>::type type;
0357 };
0358 
0359 template<typename FirstType,typename SizeType,typename IncrType,int XprSize>
0360 struct IndexedViewCompatibleType<ArithmeticSequence<FirstType,SizeType,IncrType>, XprSize> {
0361   typedef ArithmeticSequence<Index,typename make_size_type<SizeType>::type,IncrType> type;
0362 };
0363 
0364 template<typename FirstType,typename SizeType,typename IncrType>
0365 ArithmeticSequence<Index,typename make_size_type<SizeType>::type,IncrType>
0366 makeIndexedViewCompatible(const ArithmeticSequence<FirstType,SizeType,IncrType>& ids, Index size,SpecializedType) {
0367   return ArithmeticSequence<Index,typename make_size_type<SizeType>::type,IncrType>(
0368             eval_expr_given_size(ids.firstObject(),size),eval_expr_given_size(ids.sizeObject(),size),ids.incrObject());
0369 }
0370 
0371 template<typename FirstType,typename SizeType,typename IncrType>
0372 struct get_compile_time_incr<ArithmeticSequence<FirstType,SizeType,IncrType> > {
0373   enum { value = get_fixed_value<IncrType,DynamicIndex>::value };
0374 };
0375 
0376 } // end namespace internal
0377 
0378 /** \namespace Eigen::indexing
0379   * \ingroup Core_Module
0380   * 
0381   * The sole purpose of this namespace is to be able to import all functions
0382   * and symbols that are expected to be used within operator() for indexing
0383   * and slicing. If you already imported the whole Eigen namespace:
0384   * \code using namespace Eigen; \endcode
0385   * then you are already all set. Otherwise, if you don't want/cannot import
0386   * the whole Eigen namespace, the following line:
0387   * \code using namespace Eigen::indexing; \endcode
0388   * is equivalent to:
0389   * \code
0390   using Eigen::all;
0391   using Eigen::seq;
0392   using Eigen::seqN;
0393   using Eigen::lastN; // c++11 only
0394   using Eigen::last;
0395   using Eigen::lastp1;
0396   using Eigen::fix;
0397   \endcode
0398   */
0399 namespace indexing {
0400   using Eigen::all;
0401   using Eigen::seq;
0402   using Eigen::seqN;
0403   #if EIGEN_HAS_CXX11
0404   using Eigen::lastN;
0405   #endif
0406   using Eigen::last;
0407   using Eigen::lastp1;
0408   using Eigen::fix;
0409 }
0410 
0411 } // end namespace Eigen
0412 
0413 #endif // EIGEN_ARITHMETIC_SEQUENCE_H