Skip to content

Commit

Permalink
buffer preallocation in hcubature
Browse files Browse the repository at this point in the history
replace call to `isnothing` for `v1.0` compat

more fixes to 1.0
  • Loading branch information
maltezfaria committed Dec 6, 2022
1 parent 1127376 commit 588c024
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
15 changes: 5 additions & 10 deletions src/HCubature.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,10 @@ Allocate a buffer that can be used in calls to [`hcubature`](@ref).
# Examples:
```jldoctest
buffer = alloc_buf(;dimension=2, range_type=ComplexF64, domain_type=Float64)
I, E = hcubature(x -> 2+im, (0,0), (2pi, pi); buffer))[1]
I ≈ 4pi^2 + im*2*pi^2
# output
true
```
```julia
buffer = alloc_buf(;dimension=2, range_type=ComplexF64, domain_type=Float64)
I, E = hcubature(x -> 2+im, (0,0), (2pi, pi); buffer))
```
"""
function alloc_buf(;dimension, domain_type=Float64, range_type=Float64, error_type=real(range_type))
Expand All @@ -82,7 +77,7 @@ function hcubature_(f::F, a::SVector{n,T}, b::SVector{n,T}, norm, rtol_, atol, m
I, E, kdiv = rule(f, a,b1, norm)
(n == 0 || iszero(prod(Δ))) && return I,E
firstbox = Box(a,b1, I,E,kdiv)
boxes = isnothing(buf) ? DataStructures.BinaryMaxHeap{typeof(firstbox)}() : (empty!(buf.valtree); buf)
boxes = (buf===nothing) ? DataStructures.BinaryMaxHeap{typeof(firstbox)}() : (empty!(buf.valtree); buf)

push!(boxes, firstbox)

Expand Down
14 changes: 7 additions & 7 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ end

@testset "alloc_buf" begin
buffer = alloc_buf(;dimension=1)
@test @inferred(hcubature(x -> cos(x[1]), (0,), (1,);buffer))[1] sin(1)
@inferred(hquadrature(cos, 0, 1;buffer))[1]
@test @inferred(hcubature(x -> cos(x[1]), (0,), (1,);buffer=buffer))[1] sin(1)
@inferred(hquadrature(cos, 0, 1; buffer=buffer))[1]
buffer = alloc_buf(;dimension=2)
@test hcubature(x -> cos(x[1])*cos(x[2]), [0,0], [1,1];buffer)[1] sin(1)^2
@inferred(hcubature(x -> cos(x[1])*cos(x[2]), (0,0), (1,1);buffer))[1]
@test hcubature(x -> cos(x[1])*cos(x[2]), [0,0], [1,1]; buffer=buffer)[1] sin(1)^2
@inferred(hcubature(x -> cos(x[1])*cos(x[2]), (0,0), (1,1);buffer=buffer))[1]
buffer = alloc_buf(;dimension=1, range_type=Float32, domain_type=Float32)
@test @inferred(hcubature(x -> cos(x[1]), (0.0f0,), (1.0f0,);buffer))[1] sin(1.0f0)
@test @inferred(hcubature(x -> cos(x[1]), (0.0f0,), (1.0f0,);buffer=buffer))[1] sin(1.0f0)
buffer = alloc_buf(;dimension=2, range_type=Float64, domain_type=Float64)
@test @inferred(hcubature(x -> 2, (0,0), (2pi, pi);buffer)[1]) 4pi^2
@test @inferred(hcubature(x -> 2, (0,0), (2pi, pi);buffer=buffer)[1]) 4pi^2
buffer = alloc_buf(;dimension=2, range_type=ComplexF64, domain_type=Float64)
@test @inferred(hcubature(x -> 2+im, (0,0), (2pi, pi);buffer))[1] 4pi^2 + im*2*pi^2
@test @inferred(hcubature(x -> 2+im, (0,0), (2pi, pi);buffer=buffer))[1] 4pi^2 + im*2*pi^2
end

0 comments on commit 588c024

Please sign in to comment.