Hi all ~.
Currently, I want to make a function that returns numerical integration through interpolation. But my array is large(~10^7) which takes extreme amounts of allocations & memory.
Here’s my code.
unit_q = collect(0:0.0001:1)
#Function for numerical integration
function Kernel_Weight_Function(r::Float64)
if r <1/2.
return 8./(pi)*(1-6.*r^2.+6.*r^3.)
elseif r<1.
return 8./(pi)*2*(1-r)^3.
else
return 0
end
end
#Numerical integration
function integr_ker(q_xy::Float64)
dq = 0.0001
F = 0
qz = 0
while qz < sqrt(1.-q_xy^2.)
F += Kernel_Weight_Function(sqrt(qz^2.+q_xy^2.))*dq
qz += dq
end
return 2*F
end
F_qxy = zeros(length(unit_q))
#Obtaining Value for interpolation
for i in 1:length(unit_q)
F_qxy[i] = integr_ker(unit_q[i])
end
using Interpolations
knotsq = (unit_q,)
Numerical_Kernel = interpolate(knotsq, F_qxy, Gridded(Linear()))
#For time test
function f(Times::Int64)
A = rand(0:2,Times)
for AA in A
F = Numerical_Kernel[A]
end
end
@time f(1)
@time f(100)
@time f(1000)
The results are following and first one can be ignored.
1.395746 seconds (236.13 k allocations: 12.455 MiB, 0.96% gc time)
0.001321 seconds (205 allocations: 176.031 KiB)
0.127418 seconds (2.00 k allocations: 15.511 MiB, 12.43% gc time)
So code takes ~100 times more time for calculating interpolation 10 times more.(My array ~ 10^7 so it will take 10^8*0.1 sec…)
Are there any ways to speed up this interpolation?
Thanks.