# Slow numerical integration of scalar function

**URL:** <https://discourse.julialang.org/t/slow-numerical-integration-of-scalar-function/114976>\
**Category:** Performance\
**Tags:** integral\
**Created:** [May 30, 2024, 5:01pm UTC](https://discourse.julialang.org/t/slow-numerical-integration-of-scalar-function/114976 "2024-05-30T17:01:33Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [May 30, 2024, 5:16pm UTC](https://discourse.julialang.org/t/slow-numerical-integration-of-scalar-function/114976/2 "2024-05-30T17:16:24Z")

</div>

You have a highly oscillatory integrand — your integrand is oscillating around 30000 times in your integration domain, and you should expect that any generic quadrature scheme will need several quadrature points per oscillation, so you should figure on 100k+ quadrature points for just a 1d integral.

Indeed, calling QuadGK.jl explicitly to get an evaluation count, I find:

```julia-repl
julia> using QuadGK

julia> quadgk_count(x -> integrand(x, 0), 0, 4500, rtol=1e-5, atol=1e-5)
(0.001980687766630197, 9.987036693025001e-6, 968415)

```

or almost 10^6 evaluation points for your 1d integral. There are ways to speed this up (e.g. increasing the quadrature order, pre-allocating buffers, etc.) but fundamentally this is a hard problem for a generic quadrature routine.

> [@golanor](#):
>
> I’m trying to get up to a 4-dimensional integral of a product of these functions and I can’t even calculate a two-dimensional version since it takes too long.

If your multidimensional integrand is similar along each dimension, you can see why — in 2d it would take \approx 10^{12} function evaluations.

You really need to re-think what you are doing — this is not something you are going to be able to brute force without exploiting more structure of your problem.

In certain cases, highly oscillatory integrals can be computed very efficiently — for example, if the oscillatory part is known and simple (e.g. a cosine), but it is multiplied by some complicated function (as in a Fourier transform), there are efficient methods. See e.g. [[RFC/ANN] OscillatoryIntegralsODE.jl: Levin method + OrdinaryDiffEq](https://discourse.julialang.org/t/rfc-ann-oscillatoryintegralsode-jl-levin-method-ordinarydiffeq/55601) and [Integrate High Dimension highly oscillating integral](https://discourse.julialang.org/t/integrate-high-dimension-highly-oscillating-integral/75825)

---

_[View the full topic](https://discourse.julialang.org/t/slow-numerical-integration-of-scalar-function/114976)._
