# Integration of ~1/r and ~1/r^2 functions

**URL:** <https://discourse.julialang.org/t/integration-of-1-r-and-1-r-2-functions/109200>\
**Category:** General Usage\
**Created:** [January 24, 2024, 3:15pm UTC](https://discourse.julialang.org/t/integration-of-1-r-and-1-r-2-functions/109200 "2024-01-24T15:15:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![svretina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/svretina/32/206157_2.png) [@svretina](https://discourse.julialang.org/u/svretina)\
**Post date:** [January 24, 2024, 3:15pm UTC](https://discourse.julialang.org/t/integration-of-1-r-and-1-r-2-functions/109200/1 "2024-01-24T15:15:44Z")

</div>

I would like to integrate some rational functions I have which scale  
as 1/r and 1/r^2 respectively. Gaussian quadratures are exact for polynomial functions. Do you know any julia implementation of a rational quadrature?

---

<div class="post-metadata">

**Author:** ![tom-plaa](https://avatars.discourse-cdn.com/v4/letter/t/d26b3c/32.png) [@tom-plaa](https://discourse.julialang.org/u/tom-plaa)\
**Post date:** [January 24, 2024, 3:21pm UTC](https://discourse.julialang.org/t/integration-of-1-r-and-1-r-2-functions/109200/2 "2024-01-24T15:21:06Z")

</div>

Assuming that you’re dealing with possible poles/singularities, don’t the functions from the QuadGK.jl package work in your case?

---

<div class="post-metadata">

**Author:** ![svretina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/svretina/32/206157_2.png) [@svretina](https://discourse.julialang.org/u/svretina)\
**Post date:** [January 24, 2024, 3:25pm UTC](https://discourse.julialang.org/t/integration-of-1-r-and-1-r-2-functions/109200/3 "2024-01-24T15:25:42Z")

</div>

sorry I forgot to mention about the poles/singularities. I want to integrate those functions in the region where they are regular.

Any quadrature will do its job there. But using a specialized for rational functions quadrature would provide exact integration, thus I would need less points to get a similar error from a usual quadrature.

I am performing these integrations in every time step in my simulation  
for a 3D grid, for each cell. This explodes exponentially and I am seeking methods to improve.

---

<div class="post-metadata">

**Author:** ![tom-plaa](https://avatars.discourse-cdn.com/v4/letter/t/d26b3c/32.png) [@tom-plaa](https://discourse.julialang.org/u/tom-plaa)\
**Post date:** [January 24, 2024, 3:33pm UTC](https://discourse.julialang.org/t/integration-of-1-r-and-1-r-2-functions/109200/4 "2024-01-24T15:33:27Z")

</div>

Is it impossible to approximate the integral analytically? You could try to expand the integrand in a way that allows you to further integrate analytically and just leaving what you need to actually numerically integrate as something cheaper than the original integral. Or by parts.

---

<div class="post-metadata">

**Author:** ![tom-plaa](https://avatars.discourse-cdn.com/v4/letter/t/d26b3c/32.png) [@tom-plaa](https://discourse.julialang.org/u/tom-plaa)\
**Post date:** [January 24, 2024, 3:35pm UTC](https://discourse.julialang.org/t/integration-of-1-r-and-1-r-2-functions/109200/5 "2024-01-24T15:35:21Z")

</div>

Also, check on [Gaussian Quadrature · FastGaussQuadrature.jl](https://juliaapproximation.github.io/FastGaussQuadrature.jl/stable/gaussquadrature/) if there is any scheme that fits your problem better.

---

<div class="post-metadata">

**Author:** ![svretina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/svretina/32/206157_2.png) [@svretina](https://discourse.julialang.org/u/svretina)\
**Post date:** [January 24, 2024, 4:43pm UTC](https://discourse.julialang.org/t/integration-of-1-r-and-1-r-2-functions/109200/6 "2024-01-24T16:43:39Z")

</div>

i doubt it can be approximated analytically, and if it could ( i mean you can approximate anything ), its outside of my skills.

The functions are very complex because of multiple derivatives wrt to position and velocity and you have a Lorentz matrix in the func to begin with. So it will be a nightmare to tackle this analytically.

---

<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:** [January 24, 2024, 8:25pm UTC](https://discourse.julialang.org/t/integration-of-1-r-and-1-r-2-functions/109200/7 "2024-01-24T20:25:25Z")

</div>

> [@svretina](#):
>
> I would like to integrate some rational functions I have which scale  
> as 1/r and 1/r^2 respectively. Gaussian quadratures are exact for polynomial functions. Do you know any julia implementation of a rational quadrature?

If the integration domain is fixed, then you can build 1/r^n into the weight function for your quadrature rule. See [weighted quadrature in QuadGK](https://juliamath.github.io/QuadGK.jl/stable/weighted-gauss/), for example.

There are also some papers on other algorithms for quadrature of rational functions with known poles, e.g. [Gautschi (2001)](https://www.sciencedirect.com/science/article/pii/S0377042700006373) and [Tomanovic (2023)](https://www.sciencedirect.com/science/article/abs/pii/S0168927423001228), but I don’t have experience with these.

In general, however, I don’t know offhand of any practical quadrature algorithm that is guaranteed to be exact for _all rational functions_ of a given degree, analogous to Gaussian quadrature for polynomials. (There’s been a lot of recent progress in rational approximation via the [AAA algorithm](https://people.maths.ox.ac.uk/trefethen/nak_sete_tref_revised.pdf), but it still has few theoretical guarantees.)

---

<div class="post-metadata">

**Author:** ![fph](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fph/32/17159_2.png) [@fph](https://discourse.julialang.org/u/fph)\
**Post date:** [January 25, 2024, 12:49pm UTC](https://discourse.julialang.org/t/integration-of-1-r-and-1-r-2-functions/109200/8 "2024-01-25T12:49:12Z")

</div>

> [@stevengj](#):
>
> In general, however, I don’t know offhand of any practical quadrature algorithm that is guaranteed to be exact for _all rational functions_ of a given degree, analogous to Gaussian quadrature for polynomials.

I don’t think it’s possible, at least with classical rules of the form \sum\_{i=1}^n w\_i f(x\_i): the set of functions for which a certain rule is exact must be a vector subspace (it’s closed under sum and multiplication by scalars), while the smallest vector subspace that contains all rational functions of a given degree is the space of all rational functions.
