It is probably related to this topic this topic.
I find the performances of HCubature.jl not intuitive.
Lets take the following function as a test case for our MWE:
internal(k,x) = exp(-x^2)*sin(k*x)^2
I tried performing the integration in 2 ways.
First the “bad way” by using nested quadgk calls
using QuadGK
function integrand(k)
f1(x) = internal(k,x)
(sol, er) = quadgk(f1, -Inf, Inf)
return sol
end
function final(rtol)
(sol, err) = quadgk(integrand, 0.0, 100, rtol = rtol)
return sol, err
end
And the “right way” using the multidimensional integration from HCubature.jl
function final2(rtol)
f2(k,t) = internal(k,t/(1-t^2))*(1+t^2)/(1-t^2)^2
f3(p) = f2(p[1],p[2])
(sol, err) = hcubature(f3, (0.0, -1), (100,1), rtol=rtol)
return sol,err
end
julia> using Benchmarktools
julia> @time final(1e-9)
0.014318 seconds (789 allocations: 1.185 MiB)
(87.83729389116553, 8.133626228090662e-8)
julia> @time final2(1e-9)
12.298572 seconds (166.92 M allocations: 5.849 GiB, 9.48% gc time)
(87.83729438189894, 8.78372122849345e-8)
I guess the result from final2() is more accurate than from final() but still I find the difference in time and allocations huge between both approaches.
It’s not clear to me how we can compare the accuracy for both methods.
Thanks in advance
Olivier