using SymPy
using QuadGK
using BenchmarkTools
@vars x real=true
@vars a positive=true
f(a, x) = √(a^2 + x^2)
expr_sym = sympy.Integral(f(a, x), x).doit()
str_sym = string(expr_sym)
"a^2*asinh(x/a)/2 + x*sqrt(a^2 + x^2)/2"
expr_julia = Meta.parse("F(a, x) = $expr_sym")
:(F(a, x) = begin
#= none:1 =#
(a ^ 2 * asinh(x / a)) / 2 + (x * sqrt(a ^ 2 + x ^ 2)) / 2
end)
@eval $expr_julia
F (generic function with 1 method)
f(x) = f(2, x)
F_quadgk(t) = quadgk(f, 0, t)[1]
F(t) = F(2, t) - F(2, 0)
t = 1:10^4
A = @btime F_quadgk.(t)
B = @btime F.(t);
A ≈ B
6.352 ms (19995 allocations: 3.58 MiB) 314.800 μs (5 allocations: 78.27 KiB)
true