Hi everyone, this package is not exactly new, but i never announced it, and recently i had the time&need to make it even faster and flexible
a copy of readme.md:
Trapz.jl
A simple Julia package to perform trapezoidal integration over common Julia arrays.
the package is now registered on Julia Registry, so it can be added as follows
import Pkg
Pkg.pkg"add Trapz"
Example Usage:
using Trapz
vx=range(0,1,length=100)
vy=range(0,2,length=200)
vz=range(0,3,length=300)
M=[x^2+y^2+z^2 for x=vx,y=vy,z=vz]
I=trapz((vx,vy,vz),M)
print("result: ",I)
result: 28.000303707970264
Benchmarks
using BenchmarkTools
@btime trapz($(vx,vy,vz),$M);
2.793 ms (4 allocations: 157.30 KiB)
@btime trapz($(:,vy, vz),$M);
3.046 ms (3 allocations: 157.20 KiB)
@btime trapz($(:,vy,:),$M);
4.022 ms (2 allocations: 234.45 KiB)
Comparison to Numpy
using PyCall
np=pyimport("numpy")
timenumpy = @belapsed np.trapz(np.trapz(np.trapz($M,$vz),$vy),$vx)
timejulia = @belapsed trapz($(vx,vy,vz),$M)
how_faster=timenumpy/timejulia
print("Trapz.jl is ~ ",how_faster," times faster than numpy's trapz")
Trapz.jl is ~ 7.2802292929009 times faster than numpy's trapz