Skip to content
Why is a raven like a writing desk?

Thoughts both confusing and enlightening.

Why is a raven like a writing desk?

Thoughts both confusing and enlightening.

Exercising Ranges (part 7)

elbeno, 1 July, 20151 July, 2015

(Start at the beginning of the series if you want more context.)

Calculus operations

It’s relatively simple to do differentiation and integration of power series, since they’re just regular polynomial-style. No trigonometric identities, logarithms or other tricky integrals here. Just the plain rule for positive powers of x. For differentiation, we have:

[latex]\frac{d}{dx}kx^{n} = knx^{n-1}[/latex]

Which is straightforwardly expressed with ranges: we simply take the tail of the power series and pairwise multiply with n, the power:

template 
auto differentiate(Rng&& r)
{
  return ranges::view::zip_with(
      std::multiplies<>(),
      ranges::view::iota(1),
      ranges::view::tail(std::forward(r)));
}

Integration is almost as easy. The fly in the ointment is that up until now, we have been working entirely with integers, or whatever underlying arithmetic type is present in the range. Here is where Haskell wins with its built in rational numbers and polymorphic fromIntegral for conversion from integral types. Integration brings division into the picture:

[latex]\int_{}{} kx^n dx = \frac{kx^{n+1}}{n+1}[/latex]

Which means we need to turn a range of ints into a range of floats or doubles. Or use a rational number library. But anyway, sweeping these concerns under the rug, the actual code for integration is as easy as that for differentiation:

template 
auto integrate(Rng&& r)
{
  return ranges::view::concat(
      ranges::view::single(0),
      ranges::view::zip_with(
          [] (auto x, auto y) { return (float)x / (float)y; },
          std::forward(r),
          ranges::view::iota(1)));
}

We prepend a zero as the constant of integration.

Calculus: in real life, more difficult than arithmetic, but with ranges, considerably easier. That was a welcome break from extreme mental effort. In the next part, I round out my range experiments for now, with a look at a couple more functions from the Haskell Prelude.

C++ Programming

Post navigation

Previous post
Next post

Related Posts

Thoughts on Modern C++ and Game Dev

1 January, 20191 January, 2019

TL;DR: The C++ committee isn’t following some sort of agenda to ignore the needs of game programmers, and “modern” C++ isn’t going to become undebuggable. — Over the past week there has been an ongoing conversation on Twitter about how many people — especially those in the games industry —…

Read More

Compile-time RNG tricks

15 October, 201518 October, 2015

(Start at the beginning of the series – and all the source can be found in my github repo) Compile-time random number generation is quite useful. For instance, we could generate GUIDs (version 4 UUIDs): namespace cx { struct guid_t { uint32_t data1; uint16_t data2; uint16_t data3; uint64_t data4; };…

Read More

Webular lisp

9 October, 2007

I’ve decided to pick up the reins of lisp again and see if I can’t make some lispy web goodness. So to that end, I installed apache2, mod_lisp2, and did an asdf-install of cl-modlisp tonight. It seems to be correctly installed: next step is to go back to Practical Common…

Read More

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

©2026 Why is a raven like a writing desk? | WordPress Theme by SuperbThemes