# Integrating over piecewise approxfun

**URL:** https://discourse.julialang.org/t/integrating-over-piecewise-approxfun/127309
**Category:** Numerics
**Tags:** integral, approxfun
**Created:** [March 24, 2025, 1:40pm UTC](https://discourse.julialang.org/t/integrating-over-piecewise-approxfun/127309 "2025-03-24T13:40:37Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![nilsbecker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilsbecker/32/12157_2.png) [@nilsbecker](https://discourse.julialang.org/u/nilsbecker)
#### Post date: [March 24, 2025, 1:40pm UTC](https://discourse.julialang.org/t/integrating-over-piecewise-approxfun/127309/1 "2025-03-24T13:40:37Z")

</div>

Following up on my [earlier post](https://discourse.julialang.org/t/numeric-antiderivative-for-a-noncontinuous-function-using-odeproblem/126751/8), I now have an ApproxFun.jl `Fun` defined over a piecewise domain:

```julia
using ApproxFun
f_pw = Fun(x -> (x < 1.0 ? exp(x) : 1.), union(Segment(0.,1.), Segment(1.,2.)))
f_pw_i = integrate(f_pw)

```

The integrated `f_pw_i` does not quite behave like the integral, as each piece is separately centered to yield a zero constant term.  
Is there a way to define an operator that integrates from the leftmost boundary onwards, and over the discontinuity to yield a continuous function on the joint domain of `f_pw`?

---

<div class="post-metadata">

### Author: ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)
#### Post date: [March 24, 2025, 1:55pm UTC](https://discourse.julialang.org/t/integrating-over-piecewise-approxfun/127309/2 "2025-03-24T13:55:21Z")

</div>

Try `cumsum`? Also I think there’s a `PiecewiseSegment`.

---

<div class="post-metadata">

### Author: ![nilsbecker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilsbecker/32/12157_2.png) [@nilsbecker](https://discourse.julialang.org/u/nilsbecker)
#### Post date: [March 24, 2025, 1:59pm UTC](https://discourse.julialang.org/t/integrating-over-piecewise-approxfun/127309/3 "2025-03-24T13:59:20Z")

</div>

Ah yes that’s better.

I was using `integrate` rather than `cumsum` because in the actual application in need an integral over one dimension of a two-dimensional `Fun`. AFAIR `cumsum` did not work for that when I tried it. Currently I am doing that like this (and that’s where I ran into the issue):

```julia
Integral(factor(space(twodim_fun), 1)) ⊗ I

```

Would `cumsum` generalize like this?

Also, I recall PiecewiseSegment not combining well into a two-dimensional domain, but I can retry to be sure.

---

<div class="post-metadata">

### Author: ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)
#### Post date: [March 24, 2025, 2:00pm UTC](https://discourse.julialang.org/t/integrating-over-piecewise-approxfun/127309/4 "2025-03-24T14:00:59Z")

</div>

Maybe the confusion stems from the fact that `integrate` gives you the antiderivative. So to get the function that is `the integral from the leftmost boundary` you need to subtract that:

```julia
antiderivative = integrate(f_pw)
integral_from_left = x -> antiderivative(x) - antiderivative(0) # IIUC that 0 is left boundary

```

I know that is confused me for a short while when starting with ApproxFun.jl 😅

---

<div class="post-metadata">

### Author: ![nilsbecker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilsbecker/32/12157_2.png) [@nilsbecker](https://discourse.julialang.org/u/nilsbecker)
#### Post date: [March 24, 2025, 2:03pm UTC](https://discourse.julialang.org/t/integrating-over-piecewise-approxfun/127309/5 "2025-03-24T14:03:06Z")

</div>

That’s useful, but it covers only the left boundary, not the jump in the middle.

---

<div class="post-metadata">

### Author: ![nilsbecker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilsbecker/32/12157_2.png) [@nilsbecker](https://discourse.julialang.org/u/nilsbecker)
#### Post date: [March 24, 2025, 2:09pm UTC](https://discourse.julialang.org/t/integrating-over-piecewise-approxfun/127309/6 "2025-03-24T14:09:19Z")

</div>

> [@nilsbecker](#):
>
> Also, I recall PiecewiseSegment not combining well into a two-dimensional domain, but I can retry to be sure.

here is a non-working example:

```julia
d2 = cross(Segment(0.,1), PiecewiseSegment(0.,1.,2.))
f_pw2 = Fun((t, x) -> t * (x < 1.0 ? exp(x) : 1.), d2)

```

where the product domain with one piecewise factor can be constructed but the `Fun` cannot.

---

<div class="post-metadata">

### Author: ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)
#### Post date: [March 24, 2025, 2:12pm UTC](https://discourse.julialang.org/t/integrating-over-piecewise-approxfun/127309/7 "2025-03-24T14:12:00Z")

</div>

I think you are pushing ApproxFun.jl beyond what it’s been tested on…

It’s possible that PiecewiseOrthogonalPolynomials.jl might be better suited to your needs, eventually, though it’s pretty half-baked at the moment. There’s hidden support for 2D (it allows [quasi-optimal complexity hp-FEM in a rectangle](https://scholar.google.com/citations?view_op=view_citation&hl=en&user=ZszMLyAAAAAJ&sortby=pubdate&citation_for_view=ZszMLyAAAAAJ:P5F9QuxV20EC) ) but it’s not cleaned up in a user-friendly way.

In the future the following would work:

```julia
julia> using PiecewiseOrthogonalPolynomials, ClassicalOrthogonalPolynomials

julia> f = expand(ContinuousPolynomial{0}([0,1,2]), x -> x < 1 ? exp(x) : 1.0)
ContinuousPolynomial{0}([0, 1, 2]) * [1.718281828459045, 1.0, 0.8451545146228638, 0.0, 0.13986399606658317, 0.0, 0.013931255854518054, 0.0, 0.0009925875385252538, 0.0 …]

julia> cumsum(f)
ERROR: Not implemented

```

---

<div class="post-metadata">

### Author: ![nilsbecker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilsbecker/32/12157_2.png) [@nilsbecker](https://discourse.julialang.org/u/nilsbecker)
#### Post date: [March 24, 2025, 2:24pm UTC](https://discourse.julialang.org/t/integrating-over-piecewise-approxfun/127309/8 "2025-03-24T14:24:28Z")

</div>

> [@dlfivefifty](#):
>
> I think you are pushing [ApproxFun.jl](https://juliaregistries.github.io/General/packages/redirect_to_repo/ApproxFun) beyond what it’s been tested on…

too bad… It’s _almost_ working already:

```julia
d3 = cross(Segment(0.,1), union(Segment(0.,1.), Segment(1., 2.)))
f_pw3 = Fun((t, x) -> t * (x < 1.0 ? exp(x) : 1.), d3)

```

is accepted – only acting on it with `I ⊗ Integral(factor(d3, 2))` has the jumpy behavior that motivated the topic.

---

<div class="post-metadata">

### Author: ![nilsbecker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilsbecker/32/12157_2.png) [@nilsbecker](https://discourse.julialang.org/u/nilsbecker)
#### Post date: [March 25, 2025, 4:08pm UTC](https://discourse.julialang.org/t/integrating-over-piecewise-approxfun/127309/9 "2025-03-25T16:08:26Z")

</div>

After way too much spelunking and trying to coerce operators to combine, I stumbled upon `cumsum(::ProductFunction, n)` which does integration over one direction in a multidimensional function, _with correct handling of piecewise dimensions_! I could not imagine it being that easy before, unfortunately.

I think I will get by using that, like so:

```julia
cumsum(ProductFun(f_pw3), 2)

```

Thanks everyone.
