# How do I do a fast cumulative integration

**URL:** <https://discourse.julialang.org/t/how-do-i-do-a-fast-cumulative-integration/85574>\
**Category:** Numerics\
**Tags:** integral\
**Created:** [August 10, 2022, 8:27am UTC](https://discourse.julialang.org/t/how-do-i-do-a-fast-cumulative-integration/85574 "2022-08-10T08:27:16Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 10, 2022, 5:02pm UTC](https://discourse.julialang.org/t/how-do-i-do-a-fast-cumulative-integration/85574/6 "2022-08-10T17:02:26Z")

</div>

> [@owiecc](#):
>
> Is there a reason why most integration packages don’t provide an easy interface for a cumulative sum?

The basic reason is that numerical integration algorithms are formulated to produce a single number, the _definite_ integral over the entire interval. For a “cumulative sum” you want an algorithm that produces a _function_, and such algorithms generally look very different.

Of course, it’s easy to get a cumsum from something like a trapezoidal rule or similar low-order integration methods that just sum a bunch of local integral estimates over small intervals, typically from equally-spaced samples. But more sophisticated integration techniques like Gaussian quadrature are typically _exponentially_ more accurate than the trapezoidal rule, so you are sacrificing a lot if you use the simpler local methods (ala NumericalIntegration.jl or Trapz.jl).

The more sophisticated integration methods typically work by implicitly constructing one or more high-order polynomial approximations of your function (carefully sampled at non-equispaced points) and then integrating that polynomial, but the methods are cleverly formulated to avoid _explicitly_ constructing the polynomial(s). They instead just sample the function at carefully chosen points, multiply by some precomputed weights (that correspond mathematically to integrating an interpolating polynomial), and sum to get the estimated integral (over the whole interval only).

To instead get the “cumsum” accurately, i.e. to get the integrated _function_ at _arbitrary_ points in the interval, you need a very different type of formulation that _explicitly_ constructs the interpolating polynomial(s) in some form, at which point you can integrate the polynomial to obtain another polynomial and evaluate it wherever you want. This is exactly what ApproxFun.jl does.

---

_[View the full topic](https://discourse.julialang.org/t/how-do-i-do-a-fast-cumulative-integration/85574)._
