From 4a59336e9bf374af257625d41978f478ddfbe7ec Mon Sep 17 00:00:00 2001 From: Michael Helton Date: Wed, 12 Oct 2022 12:27:47 -0400 Subject: [PATCH] fix allocation and readme --- README.md | 16 +--------------- src/besseli.jl | 3 ++- src/besselj.jl | 3 ++- src/besselk.jl | 3 ++- 4 files changed, 7 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 48c62c4..21f4447 100644 --- a/README.md +++ b/README.md @@ -145,21 +145,7 @@ In general, this provides a fast way to generate a sequence of Bessel functions julia> @btime besselj(0:100, 50.0) 443.328 ns (2 allocations: 1.75 KiB) ``` -This function will allocate so it is recommended that you calculate the Bessel functions at the top level of your function outside any hot loop. For example, - -```julia -function bessel_sequence(x, orders) - J_nu = besselj(orders, x) - out = zero(x) - for i in eachindex(J_nu) - out += sin(x*i)*J_nu[i] - end - return out -end - -julia> bessel_sequence(10.2, 1:400) -0.11404996570230919 -``` +This function will allocate so it is recommended that you calculate the Bessel functions at the top level of your function outside any hot loop. ### Support for negative arguments diff --git a/src/besseli.jl b/src/besseli.jl index 877abe9..fc5d3fc 100644 --- a/src/besseli.jl +++ b/src/besseli.jl @@ -225,7 +225,8 @@ function _besseli(nu::AbstractRange, x::T) where T end if k > 1 out[k] = _besseli(nu[k], x) - out[1:k+1] = besselk_down_recurrence!(out[1:k+1], x, nu[1:k+1]) + tmp = @view out[1:k+1] + out[1:k+1] = besselk_down_recurrence!(tmp, x, nu[1:k+1]) return out else return out diff --git a/src/besselj.jl b/src/besselj.jl index 4d3f9ed..b2959e7 100644 --- a/src/besselj.jl +++ b/src/besselj.jl @@ -279,7 +279,8 @@ function _besselj(nu::AbstractRange, x::T) where T end if k > 1 out[k] = _besselj(nu[k], x) - out[1:k+1] = besselj_down_recurrence!(out[1:k+1], x, nu[1:k+1]) + tmp = @view out[1:k+1] + besselj_down_recurrence!(tmp, x, nu[1:k+1]) return out else return out diff --git a/src/besselk.jl b/src/besselk.jl index 6dce47e..62d6c16 100644 --- a/src/besselk.jl +++ b/src/besselk.jl @@ -235,7 +235,8 @@ function _besselk(nu::AbstractRange, x::T) where T end if k < len out[k] = _besselk(nu[k], x) - out[k-1:end] = besselk_up_recurrence!(out[k-1:end], x, nu[k-1:end]) + tmp = @view out[k-1:end] + out[k-1:end] = besselk_up_recurrence!(tmp, x, nu[k-1:end]) return out else return out