Boost logo

Boost :

Subject: Re: [boost] [odeint] Iterator semantics
From: Jeffrey Lee Hellrung, Jr. (jeffrey.hellrung_at_[hidden])
Date: 2012-07-11 13:44:35

On Wed, Jul 11, 2012 at 9:04 AM, Karsten Ahnert

> Hi,
> we have posted a review request for odeint (a library for solving
> ordinary differential equations) on this list some days ago. An ODE is
> dx/dt = f(x(t)) and one is looking for the solution x(t) which is a
> function of t. Solving ODEs is usually an iterative task, so one starts
> with x(0) and iteratively applies a solver to obtain a discretized (and
> approximated) representation of the solution: x(0) -> x(dt) -> x(2*dt)
> -> x(3*dt) -> ...
> I played around with an iterator which is doing exactly this iterative
> procedure. For example, it can be used via
> odeint::runge_kutta4< state_type > stepper; // ode solver
> std::array< double , 3 > x = {{ 10.0 , 10.0 , 10.0 }}; // initial state
> double res = std::accumulate(
> // parameters : solver , ode , state , time , time_step
> make_const_step_iterator_begin(stepper, lorenz(), x, 0.0, 0.01) ,
> make_const_step_iterator_end( stepper, lorenz(), x, 1.0, 0.01) ,
> 0.0 , []( double sum, const state_type &x) {
> return sum + x[0]; } );
> The first iterator increments the time until the time of the second
> iterator is rearched t=0.0 -> t=0.01 -> t=0.02 -> .. t=1.0 and the first
> component of the solution is accumulated.
> In principle, this approach works quite well, but there are some
> problems at least semantically. Maybe someone here has some comments or
> ideas on this:
> The defintion of this iterator is:
> template< class Stepper , class System , class StepperTag >
> class const_step_iterator : public boost::iterator_facade
> <
> const_step_iterator< Stepper , System , StepperTag > ,
> typename Stepper::state_type const ,
> boost::single_pass_traversal_tag
> >
> {
> ...
> };
> It is a single pass iterator, so it can only check for equality of two
> iterators.

It could make sense to define the remaining relational operators yourself,
for convenience, but their existence in standard algorithms wouldn't be
picked up due to the iterator's traversal category.

This is problematic since equality here means that the time
> (t) of the begin iterator is smaller then the time of the end iterator.

Can you elaborate? I don't understand why this is what equality means.

> To ensure commutativity of the != or == operation I needed to flag the
> begin and end iterator explicitly. This is the reason for the two
> factory functions make_const_step_iterator_begin and
> make_const_step_iterator_end, which are doing this flagging.

I might understand your reasoning for doing this better if I understood
your rationale for equality semantics...sorry, maybe I'm being dense :/

> The second semantically problem is that the end iterator in principle
> does not need to know the stepper as well as the system (lorenz() in the
> above example). But all algorithms from the standard library and
> Boost.Range assume that the begin and end iterator are of same type.
> Therefore you have to put the stepper and the system into the end
> iterator too.

Ugh, yes, this is an unfortunate consequence of the design of
iterator-based, STL-like algorithms. Perhaps it would be better to simply
define range-equivalents of your iterators, and encourage use of the
Boost.Range algorithms (which, AFAIK, under the hood, use the STL
iterator-based algorithms)? That would eliminate a lot of the repetition
you're seeing in defining begin and end iterators separately.

Of course, there are techniques like type erasure which
> might solve this problem, but they introduce some run-time overhead
> which we want to avoid here.

I don't see type erasure solving your problem, but regarding the overhead
of type erasure only, it could be minimal relative to the cost of the
actual time stepper. But that observation probably isn't relevant to your

> Any ideas or comments about this?


- Jeff

Boost list run by bdawes at, gregod at, cpdaniel at, john at