I hope to compute an intergration of a function with intergration inside, however the efficiency is extremely slow.
using HCubature,QuadGK,NumericalIntegration
spinonenergy(kx::Float64,ky::Float64)::Float64=2*(cos(kx)+cos(-kx/2+sqrt(3)/2*ky)+cos(kx/2+sqrt(3)/2*ky))+0.04
spinongf(omega::Float64,kx::Float64,ky::Float64)::ComplexF64=inv(omega+im*1e-3-spinonenergy(kx,ky))
spinonsd(omega::Float64)::Float64=imag(hcubature(x->Float64(-1/pi*sqrt(3)/2*1/(2*pi)^2)*spinongf(omega,x[1],x[2]), [0.0,0.0], [2*pi,4*pi/sqrt(3)])[1])
@time @fastmath spinonsd(1.0)
0.000957 seconds (12.91 k allocations: 629.141 KiB)
0.00036616523639087714
@time @fastmath quadgk(x ->spinonsd(x), -3.0, 3.0)
357.872404 seconds (5.17 G allocations: 217.831 GiB, 7.37% gc time, 0.08% compilation time)
(0.9997873995051424, 1.385776717295205e-8)
Note that the first time running of @time @fastmath spinonsd(1.0)
is about 2 seconds in my PC, very slow. Even I turn to use NumericalIntegration
, thatβs efficiency is still low, not comparable with Matlab and other programming language (one can check that Matlab only spend 1e-2 seconds on intergrating).
@time @fastmath integrate(-3.0:0.1:3.0,spinonsd.(-3.0:0.1:3.0))
4.714930 seconds (66.61 M allocations: 2.798 GiB, 5.49% gc time)
1.048344779235644
How to optimize this problem?