WPILibC++ 2023.4.3-108-ge5452e3
ArithmeticSequence.h
Go to the documentation of this file.
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2017 Gael Guennebaud <gael.guennebaud@inria.fr>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10#ifndef EIGEN_ARITHMETIC_SEQUENCE_H
11#define EIGEN_ARITHMETIC_SEQUENCE_H
12
13namespace Eigen {
14
15namespace internal {
16
17#if (!EIGEN_HAS_CXX11) || !((!EIGEN_COMP_GNUC) || EIGEN_COMP_GNUC>=48)
18template<typename T> struct aseq_negate {};
19
20template<> struct aseq_negate<Index> {
21 typedef Index type;
22};
23
24template<int N> struct aseq_negate<FixedInt<N> > {
25 typedef FixedInt<-N> type;
26};
27
28// Compilation error in the following case:
29template<> struct aseq_negate<FixedInt<DynamicIndex> > {};
30
31template<typename FirstType,typename SizeType,typename IncrType,
32 bool FirstIsSymbolic=symbolic::is_symbolic<FirstType>::value,
33 bool SizeIsSymbolic =symbolic::is_symbolic<SizeType>::value>
35 typedef Index type;
36};
37
38template<typename FirstType,typename SizeType,typename IncrType>
39struct aseq_reverse_first_type<FirstType,SizeType,IncrType,true,true> {
40 typedef symbolic::AddExpr<FirstType,
44};
45
46template<typename SizeType,typename IncrType,typename EnableIf = void>
48 typedef Index type;
49};
50
51template<typename SizeType,typename IncrType>
52struct aseq_reverse_first_type_aux<SizeType,IncrType,typename internal::enable_if<bool((SizeType::value+IncrType::value)|0x1)>::type> {
53 typedef FixedInt<(SizeType::value-1)*IncrType::value> type;
54};
55
56template<typename FirstType,typename SizeType,typename IncrType>
57struct aseq_reverse_first_type<FirstType,SizeType,IncrType,true,false> {
60};
61
62template<typename FirstType,typename SizeType,typename IncrType>
63struct aseq_reverse_first_type<FirstType,SizeType,IncrType,false,true> {
67};
68#endif
69
70// Helper to cleanup the type of the increment:
71template<typename T> struct cleanup_seq_incr {
73};
74
75}
76
77//--------------------------------------------------------------------------------
78// seq(first,last,incr) and seqN(first,size,incr)
79//--------------------------------------------------------------------------------
80
81template<typename FirstType=Index,typename SizeType=Index,typename IncrType=internal::FixedInt<1> >
83
84template<typename FirstType,typename SizeType,typename IncrType>
88seqN(FirstType first, SizeType size, IncrType incr);
89
90/** \class ArithmeticSequence
91 * \ingroup Core_Module
92 *
93 * This class represents an arithmetic progression \f$ a_0, a_1, a_2, ..., a_{n-1}\f$ defined by
94 * its \em first value \f$ a_0 \f$, its \em size (aka length) \em n, and the \em increment (aka stride)
95 * that is equal to \f$ a_{i+1}-a_{i}\f$ for any \em i.
96 *
97 * It is internally used as the return type of the Eigen::seq and Eigen::seqN functions, and as the input arguments
98 * of DenseBase::operator()(const RowIndices&, const ColIndices&), and most of the time this is the
99 * only way it is used.
100 *
101 * \tparam FirstType type of the first element, usually an Index,
102 * but internally it can be a symbolic expression
103 * \tparam SizeType type representing the size of the sequence, usually an Index
104 * or a compile time integral constant. Internally, it can also be a symbolic expression
105 * \tparam IncrType type of the increment, can be a runtime Index, or a compile time integral constant (default is compile-time 1)
106 *
107 * \sa Eigen::seq, Eigen::seqN, DenseBase::operator()(const RowIndices&, const ColIndices&), class IndexedView
108 */
109template<typename FirstType,typename SizeType,typename IncrType>
111{
112public:
113 ArithmeticSequence(FirstType first, SizeType size) : m_first(first), m_size(size) {}
114 ArithmeticSequence(FirstType first, SizeType size, IncrType incr) : m_first(first), m_size(size), m_incr(incr) {}
115
116 enum {
119 };
120
121 /** \returns the size, i.e., number of elements, of the sequence */
122 Index size() const { return m_size; }
123
124 /** \returns the first element \f$ a_0 \f$ in the sequence */
125 Index first() const { return m_first; }
126
127 /** \returns the value \f$ a_i \f$ at index \a i in the sequence. */
128 Index operator[](Index i) const { return m_first + i * m_incr; }
129
130 const FirstType& firstObject() const { return m_first; }
131 const SizeType& sizeObject() const { return m_size; }
132 const IncrType& incrObject() const { return m_incr; }
133
134protected:
135 FirstType m_first;
136 SizeType m_size;
137 IncrType m_incr;
138
139public:
140
141#if EIGEN_HAS_CXX11 && ((!EIGEN_COMP_GNUC) || EIGEN_COMP_GNUC>=48)
142 auto reverse() const -> decltype(Eigen::seqN(m_first+(m_size+fix<-1>())*m_incr,m_size,-m_incr)) {
143 return seqN(m_first+(m_size+fix<-1>())*m_incr,m_size,-m_incr);
144 }
145#else
146protected:
149public:
151 reverse() const {
152 return seqN(m_first+(m_size+fix<-1>())*m_incr,m_size,-m_incr);
153 }
154#endif
155};
156
157/** \returns an ArithmeticSequence starting at \a first, of length \a size, and increment \a incr
158 *
159 * \sa seqN(FirstType,SizeType), seq(FirstType,LastType,IncrType) */
160template<typename FirstType,typename SizeType,typename IncrType>
162seqN(FirstType first, SizeType size, IncrType incr) {
164}
165
166/** \returns an ArithmeticSequence starting at \a first, of length \a size, and unit increment
167 *
168 * \sa seqN(FirstType,SizeType,IncrType), seq(FirstType,LastType) */
169template<typename FirstType,typename SizeType>
171seqN(FirstType first, SizeType size) {
173}
174
175#ifdef EIGEN_PARSED_BY_DOXYGEN
176
177/** \returns an ArithmeticSequence starting at \a f, up (or down) to \a l, and with positive (or negative) increment \a incr
178 *
179 * It is essentially an alias to:
180 * \code
181 * seqN(f, (l-f+incr)/incr, incr);
182 * \endcode
183 *
184 * \sa seqN(FirstType,SizeType,IncrType), seq(FirstType,LastType)
185 */
186template<typename FirstType,typename LastType, typename IncrType>
187auto seq(FirstType f, LastType l, IncrType incr);
188
189/** \returns an ArithmeticSequence starting at \a f, up (or down) to \a l, and unit increment
190 *
191 * It is essentially an alias to:
192 * \code
193 * seqN(f,l-f+1);
194 * \endcode
195 *
196 * \sa seqN(FirstType,SizeType), seq(FirstType,LastType,IncrType)
197 */
198template<typename FirstType,typename LastType>
199auto seq(FirstType f, LastType l);
200
201#else // EIGEN_PARSED_BY_DOXYGEN
202
203#if EIGEN_HAS_CXX11
204template<typename FirstType,typename LastType>
205auto seq(FirstType f, LastType l) -> decltype(seqN(typename internal::cleanup_index_type<FirstType>::type(f),
207 - typename internal::cleanup_index_type<FirstType>::type(f)+fix<1>())))
208{
211 -typename internal::cleanup_index_type<FirstType>::type(f)+fix<1>()));
212}
213
214template<typename FirstType,typename LastType, typename IncrType>
215auto seq(FirstType f, LastType l, IncrType incr)
221{
222 typedef typename internal::cleanup_seq_incr<IncrType>::type CleanedIncrType;
225 -typename internal::cleanup_index_type<FirstType>::type(f)+CleanedIncrType(incr)) / CleanedIncrType(incr),
226 CleanedIncrType(incr));
227}
228
229#else // EIGEN_HAS_CXX11
230
231template<typename FirstType,typename LastType>
234seq(FirstType f, LastType l)
235{
238}
239
240template<typename FirstTypeDerived,typename LastType>
241typename internal::enable_if<!symbolic::is_symbolic<LastType>::value,
242 ArithmeticSequence<FirstTypeDerived, symbolic::AddExpr<symbolic::AddExpr<symbolic::NegateExpr<FirstTypeDerived>,symbolic::ValueExpr<> >,
243 symbolic::ValueExpr<internal::FixedInt<1> > > > >::type
245{
246 return seqN(f.derived(),(typename internal::cleanup_index_type<LastType>::type(l)-f.derived()+fix<1>()));
247}
248
249template<typename FirstType,typename LastTypeDerived>
250typename internal::enable_if<!symbolic::is_symbolic<FirstType>::value,
252 symbolic::AddExpr<symbolic::AddExpr<LastTypeDerived,symbolic::ValueExpr<> >,
253 symbolic::ValueExpr<internal::FixedInt<1> > > > >::type
255{
257}
258
259template<typename FirstTypeDerived,typename LastTypeDerived>
260ArithmeticSequence<FirstTypeDerived,
261 symbolic::AddExpr<symbolic::AddExpr<LastTypeDerived,symbolic::NegateExpr<FirstTypeDerived> >,symbolic::ValueExpr<internal::FixedInt<1> > > >
263{
264 return seqN(f.derived(),(l.derived()-f.derived()+fix<1>()));
265}
266
267
268template<typename FirstType,typename LastType, typename IncrType>
271seq(FirstType f, LastType l, IncrType incr)
272{
273 typedef typename internal::cleanup_seq_incr<IncrType>::type CleanedIncrType;
275 Index((typename internal::cleanup_index_type<LastType>::type(l)-typename internal::cleanup_index_type<FirstType>::type(f)+CleanedIncrType(incr))/CleanedIncrType(incr)), incr);
276}
277
278template<typename FirstTypeDerived,typename LastType, typename IncrType>
279typename internal::enable_if<!symbolic::is_symbolic<LastType>::value,
280 ArithmeticSequence<FirstTypeDerived,
281 symbolic::QuotientExpr<symbolic::AddExpr<symbolic::AddExpr<symbolic::NegateExpr<FirstTypeDerived>,
282 symbolic::ValueExpr<> >,
286seq(const symbolic::BaseExpr<FirstTypeDerived> &f, LastType l, IncrType incr)
287{
288 typedef typename internal::cleanup_seq_incr<IncrType>::type CleanedIncrType;
289 return seqN(f.derived(),(typename internal::cleanup_index_type<LastType>::type(l)-f.derived()+CleanedIncrType(incr))/CleanedIncrType(incr), incr);
290}
291
292template<typename FirstType,typename LastTypeDerived, typename IncrType>
293typename internal::enable_if<!symbolic::is_symbolic<FirstType>::value,
295 symbolic::QuotientExpr<symbolic::AddExpr<symbolic::AddExpr<LastTypeDerived,symbolic::ValueExpr<> >,
299seq(FirstType f, const symbolic::BaseExpr<LastTypeDerived> &l, IncrType incr)
300{
301 typedef typename internal::cleanup_seq_incr<IncrType>::type CleanedIncrType;
303 (l.derived()-typename internal::cleanup_index_type<FirstType>::type(f)+CleanedIncrType(incr))/CleanedIncrType(incr), incr);
304}
305
306template<typename FirstTypeDerived,typename LastTypeDerived, typename IncrType>
307ArithmeticSequence<FirstTypeDerived,
308 symbolic::QuotientExpr<symbolic::AddExpr<symbolic::AddExpr<LastTypeDerived,
309 symbolic::NegateExpr<FirstTypeDerived> >,
314{
315 typedef typename internal::cleanup_seq_incr<IncrType>::type CleanedIncrType;
316 return seqN(f.derived(),(l.derived()-f.derived()+CleanedIncrType(incr))/CleanedIncrType(incr), incr);
317}
318#endif // EIGEN_HAS_CXX11
319
320#endif // EIGEN_PARSED_BY_DOXYGEN
321
322
323#if EIGEN_HAS_CXX11 || defined(EIGEN_PARSED_BY_DOXYGEN)
324/** \cpp11
325 * \returns a symbolic ArithmeticSequence representing the last \a size elements with increment \a incr.
326 *
327 * It is a shortcut for: \code seqN(last-(size-fix<1>)*incr, size, incr) \endcode
328 *
329 * \sa lastN(SizeType), seqN(FirstType,SizeType), seq(FirstType,LastType,IncrType) */
330template<typename SizeType,typename IncrType>
331auto lastN(SizeType size, IncrType incr)
332-> decltype(seqN(Eigen::last-(size-fix<1>())*incr, size, incr))
333{
334 return seqN(Eigen::last-(size-fix<1>())*incr, size, incr);
335}
336
337/** \cpp11
338 * \returns a symbolic ArithmeticSequence representing the last \a size elements with a unit increment.
339 *
340 * It is a shortcut for: \code seq(last+fix<1>-size, last) \endcode
341 *
342 * \sa lastN(SizeType,IncrType, seqN(FirstType,SizeType), seq(FirstType,LastType) */
343template<typename SizeType>
344auto lastN(SizeType size)
345-> decltype(seqN(Eigen::last+fix<1>()-size, size))
346{
347 return seqN(Eigen::last+fix<1>()-size, size);
348}
349#endif
350
351namespace internal {
352
353// Convert a symbolic span into a usable one (i.e., remove last/end "keywords")
354template<typename T>
357};
358
359template<typename FirstType,typename SizeType,typename IncrType,int XprSize>
360struct IndexedViewCompatibleType<ArithmeticSequence<FirstType,SizeType,IncrType>, XprSize> {
362};
363
364template<typename FirstType,typename SizeType,typename IncrType>
369}
370
371template<typename FirstType,typename SizeType,typename IncrType>
372struct get_compile_time_incr<ArithmeticSequence<FirstType,SizeType,IncrType> > {
374};
375
376} // end namespace internal
377
378/** \namespace Eigen::indexing
379 * \ingroup Core_Module
380 *
381 * The sole purpose of this namespace is to be able to import all functions
382 * and symbols that are expected to be used within operator() for indexing
383 * and slicing. If you already imported the whole Eigen namespace:
384 * \code using namespace Eigen; \endcode
385 * then you are already all set. Otherwise, if you don't want/cannot import
386 * the whole Eigen namespace, the following line:
387 * \code using namespace Eigen::indexing; \endcode
388 * is equivalent to:
389 * \code
390 using Eigen::all;
391 using Eigen::seq;
392 using Eigen::seqN;
393 using Eigen::lastN; // c++11 only
394 using Eigen::last;
395 using Eigen::lastp1;
396 using Eigen::fix;
397 \endcode
398 */
399namespace indexing {
400 using Eigen::all;
401 using Eigen::seq;
402 using Eigen::seqN;
403 #if EIGEN_HAS_CXX11
404 using Eigen::lastN;
405 #endif
406 using Eigen::last;
407 using Eigen::lastp1;
408 using Eigen::fix;
409}
410
411} // end namespace Eigen
412
413#endif // EIGEN_ARITHMETIC_SEQUENCE_H
This class represents an arithmetic progression defined by its first value , its size (aka length) n...
Definition: ArithmeticSequence.h:111
SizeType m_size
Definition: ArithmeticSequence.h:136
const SizeType & sizeObject() const
Definition: ArithmeticSequence.h:131
IncrType m_incr
Definition: ArithmeticSequence.h:137
ArithmeticSequence< ReverseFirstType, SizeType, ReverseIncrType > reverse() const
Definition: ArithmeticSequence.h:151
ArithmeticSequence(FirstType first, SizeType size, IncrType incr)
Definition: ArithmeticSequence.h:114
Index operator[](Index i) const
Definition: ArithmeticSequence.h:128
@ IncrAtCompileTime
Definition: ArithmeticSequence.h:118
@ SizeAtCompileTime
Definition: ArithmeticSequence.h:117
ArithmeticSequence(FirstType first, SizeType size)
Definition: ArithmeticSequence.h:113
FirstType m_first
Definition: ArithmeticSequence.h:135
Index first() const
Definition: ArithmeticSequence.h:125
internal::aseq_reverse_first_type< FirstType, SizeType, IncrType >::type ReverseFirstType
Definition: ArithmeticSequence.h:148
const FirstType & firstObject() const
Definition: ArithmeticSequence.h:130
internal::aseq_negate< IncrType >::type ReverseIncrType
Definition: ArithmeticSequence.h:147
const IncrType & incrObject() const
Definition: ArithmeticSequence.h:132
Index size() const
Definition: ArithmeticSequence.h:122
Definition: IntegralConstant.h:52
Definition: SymbolicIndex.h:252
Common base class of any symbolic expressions.
Definition: SymbolicIndex.h:78
const Derived & derived() const
Definition: SymbolicIndex.h:80
Definition: SymbolicIndex.h:265
Definition: SymbolicIndex.h:52
Definition: core.h:1240
type
Definition: core.h:575
static const symbolic::SymbolExpr< internal::symbolic_last_tag > last
Can be used as a parameter to Eigen::seq and Eigen::seqN functions to symbolically reference the last...
Definition: IndexedViewHelper.h:38
static const Eigen::internal::all_t all
Can be used as a parameter to DenseBase::operator()(const RowIndices&, const ColIndices&) to index al...
Definition: IndexedViewHelper.h:171
ArithmeticSequence< Index, typename make_size_type< SizeType >::type, IncrType > makeIndexedViewCompatible(const ArithmeticSequence< FirstType, SizeType, IncrType > &ids, Index size, SpecializedType)
Definition: ArithmeticSequence.h:366
Index eval_expr_given_size(Index x, Index)
Definition: IndexedViewHelper.h:63
EIGEN_CONSTEXPR Index size(const T &x)
Definition: Meta.h:479
EIGEN_CONSTEXPR Index first(const T &x) EIGEN_NOEXCEPT
Definition: IndexedViewHelper.h:81
Namespace containing all symbols from the Eigen library.
Definition: Core:141
ArithmeticSequence< typename internal::cleanup_index_type< FirstType >::type, typename internal::cleanup_index_type< SizeType >::type, typename internal::cleanup_seq_incr< IncrType >::type > seqN(FirstType first, SizeType size, IncrType incr)
Definition: ArithmeticSequence.h:162
static const symbolic::AddExpr< symbolic::SymbolExpr< internal::symbolic_last_tag >, symbolic::ValueExpr< Eigen::internal::FixedInt< 1 > > > lastp1(last+fix< 1 >())
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
internal::enable_if<!(symbolic::is_symbolic< FirstType >::value||symbolic::is_symbolic< LastType >::value), ArithmeticSequence< typenameinternal::cleanup_index_type< FirstType >::type, Index > >::type seq(FirstType f, LastType l)
Definition: ArithmeticSequence.h:234
const int DynamicIndex
This value means that a signed quantity (e.g., a signed index) is not known at compile-time,...
Definition: Constants.h:27
internal::FixedInt< N > fix()
Definition: IntegralConstant.h:192
SpecializedType
Definition: Constants.h:309
Definition: Eigen_Colamd.h:50
ArithmeticSequence< Index, typename make_size_type< SizeType >::type, IncrType > type
Definition: ArithmeticSequence.h:361
Definition: IndexedViewHelper.h:86
FixedInt<-N > type
Definition: ArithmeticSequence.h:25
Index type
Definition: ArithmeticSequence.h:21
Definition: ArithmeticSequence.h:18
symbolic::AddExpr< symbolic::ProductExpr< symbolic::AddExpr< SizeType, symbolic::ValueExpr< FixedInt<-1 > > >, symbolic::ValueExpr< IncrType > >, symbolic::ValueExpr<> > type
Definition: ArithmeticSequence.h:66
symbolic::AddExpr< FirstType, symbolic::ValueExpr< Aux > > type
Definition: ArithmeticSequence.h:59
aseq_reverse_first_type_aux< SizeType, IncrType >::type Aux
Definition: ArithmeticSequence.h:58
symbolic::AddExpr< FirstType, symbolic::ProductExpr< symbolic::AddExpr< SizeType, symbolic::ValueExpr< FixedInt<-1 > > >, symbolic::ValueExpr< IncrType > > > type
Definition: ArithmeticSequence.h:43
Definition: ArithmeticSequence.h:47
Index type
Definition: ArithmeticSequence.h:48
Definition: ArithmeticSequence.h:34
Index type
Definition: ArithmeticSequence.h:35
T type
Definition: IntegralConstant.h:164
Definition: ArithmeticSequence.h:71
cleanup_index_type< T, DynamicIndex >::type type
Definition: ArithmeticSequence.h:72
Definition: Meta.h:109
Definition: Meta.h:273
Definition: IndexedViewHelper.h:75
Definition: IntegralConstant.h:133
Definition: ArithmeticSequence.h:355
internal::conditional< symbolic::is_symbolic< T >::value, Index, T >::type type
Definition: ArithmeticSequence.h:356
Definition: SymbolicIndex.h:189
@ value
Definition: SymbolicIndex.h:191