Skip to content

Commit

Permalink
Use only long version of routines
Browse files Browse the repository at this point in the history
This simplifies the code and reduces confusion on which function should be called
  • Loading branch information
giordano committed May 26, 2017
1 parent a0a4a7f commit 5b16629
Show file tree
Hide file tree
Showing 8 changed files with 274 additions and 293 deletions.
26 changes: 12 additions & 14 deletions src/Cuba.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ __precompile__()

module Cuba

export vegas, llvegas, suave, llsuave, divonne, lldivonne, cuhre, llcuhre
export vegas, suave, divonne, cuhre

# Note: don't use Pkg.dir("PkgName") here because the package may be installed
# elsewhere.
Expand Down Expand Up @@ -100,7 +100,7 @@ integrand_ptr{T}(integrand::T) = cfunction(generic_integrand!, Cint,
Ptr{Cdouble}, # f
Ref{T})) # userdata

abstract Integral{T, I}
abstract Integral{T}

function __init__()
Cuba.cores(0, 10000)
Expand All @@ -111,18 +111,16 @@ include("divonne.jl")
include("suave.jl")
include("vegas.jl")

for CubaInt in (Int32, Int64)
@eval @inline function dointegrate{T}(x::Integral{T, $CubaInt})
integrand = integrand_ptr(x.func)
nregions = Ref{Cint}(0)
neval = Ref{$CubaInt}(0)
fail = Ref{Cint}(0)
integral = Vector{Cdouble}(x.ncomp)
error = Vector{Cdouble}(x.ncomp)
prob = Vector{Cdouble}(x.ncomp)
dointegrate!(x, integrand, nregions, neval, fail, integral, error, prob)
return integral, error, prob, neval[], fail[], nregions[]
end
@inline function dointegrate{T}(x::Integral{T})
integrand = integrand_ptr(x.func)
nregions = Ref{Cint}(0)
neval = Ref{Int64}(0)
fail = Ref{Cint}(0)
integral = Vector{Cdouble}(x.ncomp)
error = Vector{Cdouble}(x.ncomp)
prob = Vector{Cdouble}(x.ncomp)
dointegrate!(x, integrand, nregions, neval, fail, integral, error, prob)
return integral, error, prob, neval[], fail[], nregions[]
end

### Other functions, not exported
Expand Down
94 changes: 45 additions & 49 deletions src/cuhre.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,70 +20,66 @@

### Code:

immutable Cuhre{T, I} <: Integral{T, I}
immutable Cuhre{T} <: Integral{T}
func::T
ndim::Int
ncomp::Int
nvec::I
nvec::Int64
reltol::Cdouble
abstol::Cdouble
flags::Int
minevals::I
maxevals::I
minevals::Int64
maxevals::Int64
key::Int
statefile::String
spin::Ptr{Void}
end

for (CubaInt, prefix) in ((Int32, ""), (Int64, "ll"))
@eval @inline function dointegrate!{T}(x::Cuhre{T, $CubaInt}, integrand, nregions,
neval, fail, integral, error, prob)
ccall(($(prefix * "Cuhre"), libcuba), Cdouble,
(Cint, # ndim
Cint, # ncomp
Ptr{Void}, # integrand
Any, # userdata
$CubaInt, # nvec
Cdouble, # reltol
Cdouble, # abstol
Cint, # flags
$CubaInt, # minevals
$CubaInt, # maxevals
Cint, # key
Ptr{Cchar}, # statefile
Ptr{Void}, # spin
Ptr{Cint}, # nregions
Ptr{$CubaInt}, # neval
Ptr{Cint}, # fail
Ptr{Cdouble}, # integral
Ptr{Cdouble}, # error
Ptr{Cdouble}),# prob
# Input
x.ndim, x.ncomp, integrand, x.func, x.nvec, x.reltol, x.abstol,
x.flags, x.minevals, x.maxevals, x.key, x.statefile, x.spin,
# Output
nregions, neval, fail, integral, error, prob)
end
@inline function dointegrate!{T}(x::Cuhre{T}, integrand, nregions,
neval, fail, integral, error, prob)
ccall((:llCuhre, libcuba), Cdouble,
(Cint, # ndim
Cint, # ncomp
Ptr{Void}, # integrand
Any, # userdata
Int64, # nvec
Cdouble, # reltol
Cdouble, # abstol
Cint, # flags
Int64, # minevals
Int64, # maxevals
Cint, # key
Ptr{Cchar}, # statefile
Ptr{Void}, # spin
Ptr{Cint}, # nregions
Ptr{Int64}, # neval
Ptr{Cint}, # fail
Ptr{Cdouble}, # integral
Ptr{Cdouble}, # error
Ptr{Cdouble}),# prob
# Input
x.ndim, x.ncomp, integrand, x.func, x.nvec, x.reltol, x.abstol,
x.flags, x.minevals, x.maxevals, x.key, x.statefile, x.spin,
# Output
nregions, neval, fail, integral, error, prob)
end

func = Symbol(prefix, "cuhre")
@eval function $func{T}(integrand::T, ndim::Integer=1, ncomp::Integer=1;
nvec::Integer=NVEC, reltol::Real=RELTOL, abstol::Real=ABSTOL,
flags::Integer=FLAGS, minevals::Real=MINEVALS,
maxevals::Real=MAXEVALS, key::Integer=KEY,
statefile::AbstractString=STATEFILE, spin::Ptr{Void}=SPIN)
# Cuhre requires "ndim" to be at least 2, even for an integral over a one
# dimensional domain. Instead, we don't prevent users from setting wrong
# "ndim" values like 0 or negative ones.
ndim == 1 && (ndim = 2)
return dointegrate(Cuhre(integrand, ndim, ncomp, $CubaInt(nvec), Cdouble(reltol),
Cdouble(abstol), flags, trunc($CubaInt, minevals),
trunc($CubaInt, maxevals), key, String(statefile), spin))
end
function cuhre{T}(integrand::T, ndim::Integer=1, ncomp::Integer=1;
nvec::Integer=NVEC, reltol::Real=RELTOL, abstol::Real=ABSTOL,
flags::Integer=FLAGS, minevals::Real=MINEVALS,
maxevals::Real=MAXEVALS, key::Integer=KEY,
statefile::AbstractString=STATEFILE, spin::Ptr{Void}=SPIN)
# Cuhre requires "ndim" to be at least 2, even for an integral over a one
# dimensional domain. Instead, we don't prevent users from setting wrong
# "ndim" values like 0 or negative ones.
ndim == 1 && (ndim = 2)
return dointegrate(Cuhre(integrand, ndim, ncomp, Int64(nvec), Cdouble(reltol),
Cdouble(abstol), flags, trunc(Int64, minevals),
trunc(Int64, maxevals), key, String(statefile), spin))
end

"""
cuhre(integrand, ndim=1, ncomp=1[, keywords]) -> integral, error, probability, neval, fail, nregions
llcuhre(integrand, ndim=1, ncomp=1[, keywords]) -> integral, error, probability, neval, fail, nregions
Calculate integral of `integrand` over the unit hypercube in `ndim` dimensions
using Cuhre algorithm. `integrand` is a vectorial function with `ncomp`
Expand All @@ -101,4 +97,4 @@ Accepted keywords:
* `statefile`
* `spin`
"""
cuhre, llcuhre
cuhre
154 changes: 75 additions & 79 deletions src/divonne.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,110 +20,106 @@

### Code:

immutable Divonne{T, I} <: Integral{T, I}
immutable Divonne{T} <: Integral{T}
func::T
ndim::Int
ncomp::Int
nvec::I
nvec::Int64
reltol::Cdouble
abstol::Cdouble
flags::Int
seed::Int
minevals::I
maxevals::I
minevals::Int64
maxevals::Int64
key1::Int
key2::Int
key3::Int
maxpass::Int
border::Cdouble
maxchisq::Cdouble
mindeviation::Cdouble
ngiven::I
ngiven::Int64
ldxgiven::Int
xgiven::Array{Cdouble, 2}
nextra::I
nextra::Int64
peakfinder::Ptr{Void}
statefile::String
spin::Ptr{Void}
end

for (CubaInt, prefix) in ((Int32, ""), (Int64, "ll"))
@eval @inline function dointegrate!{T}(x::Divonne{T, $CubaInt}, integrand, nregions,
neval, fail, integral, error, prob)
ccall(($(prefix * "Divonne"), libcuba), Cdouble,
(Cint, # ndim
Cint, # ncomp
Ptr{Void}, # integrand
Any, # userdata
$CubaInt, # nvec
Cdouble, # reltol
Cdouble, # abstol
Cint, # flags
Cint, # seed
$CubaInt, # minevals
$CubaInt, # maxevals
Cint, # key1
Cint, # key2
Cint, # key3
Cint, # maxpass
Cdouble, # border
Cdouble, # maxchisq
Cdouble, # mindeviation
$CubaInt, # ngiven
Cint, # ldxgiven
Ptr{Cdouble}, # xgiven
$CubaInt, # nextra
Ptr{Void}, # peakfinder
Ptr{Cchar}, # statefile
Ptr{Void}, # spin
Ptr{Cint}, # nregions
Ptr{$CubaInt}, # neval
Ptr{Cint}, # fail
Ptr{Cdouble}, # integral
Ptr{Cdouble}, # error
Ptr{Cdouble}),# prob
# Input
x.ndim, x.ncomp, integrand, x.func, x.nvec, x.reltol,
x.abstol, x.flags, x.seed, x.minevals, x.maxevals, x.key1, x.key2,
x.key3, x.maxpass, x.border, x.maxchisq, x.mindeviation, x.ngiven,
x.ldxgiven, x.xgiven, x.nextra, x.peakfinder, x.statefile, x.spin,
# Output
nregions, neval, fail, integral, error, prob)
end
@inline function dointegrate!{T}(x::Divonne{T}, integrand, nregions,
neval, fail, integral, error, prob)
ccall((:llDivonne, libcuba), Cdouble,
(Cint, # ndim
Cint, # ncomp
Ptr{Void}, # integrand
Any, # userdata
Int64, # nvec
Cdouble, # reltol
Cdouble, # abstol
Cint, # flags
Cint, # seed
Int64, # minevals
Int64, # maxevals
Cint, # key1
Cint, # key2
Cint, # key3
Cint, # maxpass
Cdouble, # border
Cdouble, # maxchisq
Cdouble, # mindeviation
Int64, # ngiven
Cint, # ldxgiven
Ptr{Cdouble}, # xgiven
Int64, # nextra
Ptr{Void}, # peakfinder
Ptr{Cchar}, # statefile
Ptr{Void}, # spin
Ptr{Cint}, # nregions
Ptr{Int64}, # neval
Ptr{Cint}, # fail
Ptr{Cdouble}, # integral
Ptr{Cdouble}, # error
Ptr{Cdouble}),# prob
# Input
x.ndim, x.ncomp, integrand, x.func, x.nvec, x.reltol,
x.abstol, x.flags, x.seed, x.minevals, x.maxevals, x.key1, x.key2,
x.key3, x.maxpass, x.border, x.maxchisq, x.mindeviation, x.ngiven,
x.ldxgiven, x.xgiven, x.nextra, x.peakfinder, x.statefile, x.spin,
# Output
nregions, neval, fail, integral, error, prob)
end

func = Symbol(prefix, "divonne")
@eval function $func{T}(integrand::T, ndim::Integer=1, ncomp::Integer=1;
nvec::Integer=NVEC, reltol::Real=RELTOL,
abstol::Real=ABSTOL, flags::Integer=FLAGS,
seed::Integer=SEED, minevals::Real=MINEVALS,
maxevals::Real=MAXEVALS, key1::Integer=KEY1,
key2::Integer=KEY2, key3::Integer=KEY3,
maxpass::Integer=MAXPASS, border::Real=BORDER,
maxchisq::Real=MAXCHISQ,
mindeviation::Real=MINDEVIATION,
ngiven::Integer=NGIVEN, ldxgiven::Integer=LDXGIVEN,
xgiven::Array{Cdouble,2}=zeros(Cdouble, ldxgiven,
ngiven),
nextra::Integer=NEXTRA,
peakfinder::Ptr{Void}=PEAKFINDER,
statefile::AbstractString=STATEFILE,
spin::Ptr{Void}=SPIN)
# Divonne requires "ndim" to be at least 2, even for an integral over a one
# dimensional domain. Instead, we don't prevent users from setting wrong
# "ndim" values like 0 or negative ones.
ndim == 1 && (ndim = 2)
return dointegrate(Divonne(integrand, ndim, ncomp, $CubaInt(nvec), Cdouble(reltol),
Cdouble(abstol), flags, seed, trunc($CubaInt, minevals),
trunc($CubaInt, maxevals), key1, key2, key3, maxpass,
Cdouble(border), Cdouble(maxchisq), Cdouble(mindeviation),
$CubaInt(ngiven), ldxgiven, xgiven, $CubaInt(nextra),
peakfinder, String(statefile), spin))
end
function divonne{T}(integrand::T, ndim::Integer=1, ncomp::Integer=1;
nvec::Integer=NVEC, reltol::Real=RELTOL,
abstol::Real=ABSTOL, flags::Integer=FLAGS,
seed::Integer=SEED, minevals::Real=MINEVALS,
maxevals::Real=MAXEVALS, key1::Integer=KEY1,
key2::Integer=KEY2, key3::Integer=KEY3,
maxpass::Integer=MAXPASS, border::Real=BORDER,
maxchisq::Real=MAXCHISQ,
mindeviation::Real=MINDEVIATION,
ngiven::Integer=NGIVEN, ldxgiven::Integer=LDXGIVEN,
xgiven::Array{Cdouble,2}=zeros(Cdouble, ldxgiven,
ngiven),
nextra::Integer=NEXTRA,
peakfinder::Ptr{Void}=PEAKFINDER,
statefile::AbstractString=STATEFILE,
spin::Ptr{Void}=SPIN)
# Divonne requires "ndim" to be at least 2, even for an integral over a one
# dimensional domain. Instead, we don't prevent users from setting wrong
# "ndim" values like 0 or negative ones.
ndim == 1 && (ndim = 2)
return dointegrate(Divonne(integrand, ndim, ncomp, Int64(nvec), Cdouble(reltol),
Cdouble(abstol), flags, seed, trunc(Int64, minevals),
trunc(Int64, maxevals), key1, key2, key3, maxpass,
Cdouble(border), Cdouble(maxchisq), Cdouble(mindeviation),
Int64(ngiven), ldxgiven, xgiven, Int64(nextra),
peakfinder, String(statefile), spin))
end

"""
divonne(integrand, ndim=1, ncomp=1[, keywords]) -> integral, error, probability, neval, fail, nregions
lldivonne(integrand, ndim=1, ncomp=1[, keywords]) -> integral, error, probability, neval, fail, nregions
Calculate integral of `integrand` over the unit hypercube in `ndim` dimensions
using Divonne algorithm. `integrand` is a vectorial function with `ncomp`
Expand Down Expand Up @@ -153,4 +149,4 @@ Accepted keywords:
* `statefile`
* `spin`
"""
divonne, lldivonne
divonne
Loading

0 comments on commit 5b16629

Please sign in to comment.