Last active
May 27, 2021 18:10
-
-
Save kalmarek/cf4b2b9be4d2300e6060018a60fed049 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Arblib: arb_struct, arf_struct, @libarb, ArbLike | |
struct arf_interval | |
lo::arf_struct | |
hi::arf_struct | |
# function arf_interval() | |
# v = new() | |
# init!(v) | |
# finalizer(clear!, v) | |
# return v | |
# end | |
end | |
# these three function don't work | |
init!(v::arf_interval) = | |
(ccall(@libarb(arf_interval_init), Cvoid, (Ref{arf_interval},), v); v) | |
clear!(v::arf_interval) = | |
ccall(@libarb(arf_interval_clear), Cvoid, (Ref{arf_interval},), v) | |
function Base.get!(a::ArbLike, int::arf_interval; prec = precision(a)) | |
ccall( | |
@libarb(arf_interval_get_arb), | |
Cvoid, | |
(Ref{arb_struct}, Ref{arf_interval}, Clong), | |
a, | |
int, | |
prec, | |
) | |
return a | |
end | |
arf_interval(a::Arf, b::Arf) = arf_interval(a.arf, b.arf) | |
arf_interval(a::Real, b::Real) = interval(Arf(a), Arf(b)) | |
function isolate_roots( | |
f, | |
a::Real, | |
b::Real; | |
maxdepth::Int = 20, | |
maxeval::Int = 100, | |
maxfound::Int, | |
prec = max(precision(a), precision(b)), | |
) | |
found = [arf_interval(arf_struct(), arf_struct()) for _ in 1:maxfound] | |
flags = [Cint[] for _ in 1:maxfound] | |
f! = (res, x; kwargs...) -> Arblib.set!(res, f(x; kwargs...)) | |
nroots_found = calc_isolate_roots!( | |
found, | |
flags, | |
_arb_calc_cfunc!(), | |
f!, | |
arf_interval(a, b), | |
maxdepth, | |
maxeval, | |
maxfound, | |
prec, | |
) | |
return found[1:nroots_found], flags[1:nroots_found] | |
end | |
function _arb_calc_func!( | |
out::Ptr{arb_struct}, | |
inp::Ptr{arb_struct}, | |
param::Ptr{Cvoid}, # pointer to the actual function | |
order::Int, | |
prec::Int, | |
) | |
@assert iszero(order) || isone(order) | |
x = ArbRef(inp, nothing, prec = prec) | |
res = ArbRef(out, nothing, prec = prec) | |
f! = unsafe_pointer_to_objref(param) | |
f!(res, x, analytic = isone(order), prec = prec) | |
return zero(Cint) | |
end | |
_arb_calc_cfunc!() = @cfunction( | |
_arb_calc_func!, | |
Cint, | |
(Ptr{arb_struct}, Ptr{arb_struct}, Ptr{Cvoid}, Int, Int) | |
) | |
function calc_isolate_roots!( | |
found::Vector{arf_interval}, | |
flags::Vector{Vector{Cint}}, | |
arb_calc_cfunc::Ptr{Cvoid}, # cfunction | |
param, | |
interval::arf_interval, | |
maxdepth::Int, | |
maxeval::Int, | |
maxfound::Int, | |
prec::Int, | |
) | |
@assert length(found) == length(flags) == maxfound | |
return ccall( | |
@libarb(arb_calc_isolate_roots), | |
Int, | |
( | |
Ptr{arf_interval}, | |
Ptr{Cint}, | |
Ptr{Cvoid}, | |
Any, | |
Ref{arf_interval}, | |
Int, | |
Int, | |
Int, | |
Int, | |
), | |
found, | |
flags, | |
arb_calc_cfunc, | |
param, | |
interval, | |
maxdepth, | |
maxeval, | |
maxfound, | |
prec, | |
) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment