diff options
author | Gael Guennebaud <g.gael@free.fr> | 2018-07-23 16:20:25 +0200 |
---|---|---|
committer | Gael Guennebaud <g.gael@free.fr> | 2018-07-23 16:20:25 +0200 |
commit | c747cde69a9ab286984a09223c3f477b782a6617 (patch) | |
tree | 570a6a08bd400a4511a1e1d63de32fb1663b72d7 | |
parent | 2bf864f1eb1b2497d28747c7f77ec3e2f1e8d9d2 (diff) |
Add lastN shorcuts to seq/seqN.
-rw-r--r-- | Eigen/src/Core/ArithmeticSequence.h | 34 | ||||
-rw-r--r-- | test/indexed_view.cpp | 8 |
2 files changed, 40 insertions, 2 deletions
diff --git a/Eigen/src/Core/ArithmeticSequence.h b/Eigen/src/Core/ArithmeticSequence.h index ada1571f1..2083cf178 100644 --- a/Eigen/src/Core/ArithmeticSequence.h +++ b/Eigen/src/Core/ArithmeticSequence.h @@ -225,7 +225,8 @@ auto seq(FirstType f, LastType l, IncrType incr) -typename internal::cleanup_index_type<FirstType>::type(f)+CleanedIncrType(incr)) / CleanedIncrType(incr), CleanedIncrType(incr)); } -#else + +#else // EIGEN_HAS_CXX11 template<typename FirstType,typename LastType> typename internal::enable_if<!(Symbolic::is_symbolic<FirstType>::value || Symbolic::is_symbolic<LastType>::value), @@ -314,10 +315,39 @@ seq(const Symbolic::BaseExpr<FirstTypeDerived> &f, const Symbolic::BaseExpr<Last typedef typename internal::cleanup_seq_incr<IncrType>::type CleanedIncrType; return seqN(f.derived(),(l.derived()-f.derived()+CleanedIncrType(incr))/CleanedIncrType(incr), incr); } -#endif +#endif // EIGEN_HAS_CXX11 #endif // EIGEN_PARSED_BY_DOXYGEN + +#if EIGEN_HAS_CXX11 +/** \cpp11 + * \returns a symbolic ArithmeticSequence representing the last \a size elements with increment \a incr. + * + * It is a shortcut for: \code seqN(last-(size-fix<1>)*incr, size, incr) \endcode + * + * \sa lastN(SizeType), seqN(FirstType,SizeType), seq(FirstType,LastType,IncrType) */ +template<typename SizeType,typename IncrType> +auto lastN(SizeType size, IncrType incr) +-> decltype(seqN(Eigen::placeholders::last-(size-fix<1>())*incr, size, incr)) +{ + return seqN(Eigen::placeholders::last-(size-fix<1>())*incr, size, incr); +} + +/** \cpp11 + * \returns a symbolic ArithmeticSequence representing the last \a size elements with a unit increment. + * + * It is a shortcut for: \code seq(last+fix<1>-size, last) \endcode + * + * \sa lastN(SizeType,IncrType, seqN(FirstType,SizeType), seq(FirstType,LastType) */ +template<typename SizeType> +auto lastN(SizeType size) +-> decltype(seqN(Eigen::placeholders::last+fix<1>()-size, size)) +{ + return seqN(Eigen::placeholders::last+fix<1>()-size, size); +} +#endif + namespace internal { // Convert a symbolic span into a usable one (i.e., remove last/end "keywords") diff --git a/test/indexed_view.cpp b/test/indexed_view.cpp index 4cc0954f7..7219c777f 100644 --- a/test/indexed_view.cpp +++ b/test/indexed_view.cpp @@ -293,6 +293,14 @@ void check_indexed_view() } #if EIGEN_HAS_CXX11 + // check lastN + VERIFY_IS_APPROX( a(lastN(3)), a.tail(3) ); + VERIFY( MATCH( a(lastN(3)), "7\n8\n9" ) ); + VERIFY_IS_APPROX( a(lastN(fix<3>())), a.tail<3>() ); + VERIFY( MATCH( a(lastN(3,2)), "5\n7\n9" ) ); + VERIFY( MATCH( a(lastN(3,fix<2>())), "5\n7\n9" ) ); + VERIFY( a(lastN(fix<3>())).SizeAtCompileTime == 3 ); + VERIFY( (A(all, std::array<int,4>{{1,3,2,4}})).ColsAtCompileTime == 4); VERIFY_IS_APPROX( (A(std::array<int,3>{{1,3,5}}, std::array<int,4>{{9,6,3,0}})), A(seqN(1,3,2), seqN(9,4,-3)) ); |