File indexing completed on 2025-01-18 09:56:09
0001
0002
0003
0004
0005
0006
0007
0008
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
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
0071 template<typename T> struct cleanup_seq_incr {
0072 typedef typename cleanup_index_type<T,DynamicIndex>::type type;
0073 };
0074
0075 }
0076
0077
0078
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
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
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
0122 Index size() const { return m_size; }
0123
0124
0125 Index first() const { return m_first; }
0126
0127
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
0158
0159
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
0167
0168
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
0178
0179
0180
0181
0182
0183
0184
0185
0186 template<typename FirstType,typename LastType, typename IncrType>
0187 auto seq(FirstType f, LastType l, IncrType incr);
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198 template<typename FirstType,typename LastType>
0199 auto seq(FirstType f, LastType l);
0200
0201 #else
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
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
0319
0320 #endif
0321
0322
0323 #if EIGEN_HAS_CXX11 || defined(EIGEN_PARSED_BY_DOXYGEN)
0324
0325
0326
0327
0328
0329
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
0338
0339
0340
0341
0342
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
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 }
0377
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397
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 }
0412
0413 #endif