Skip to content

Instantly share code, notes, and snippets.

@kalmarek
Created February 8, 2018 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kalmarek/9ef55d0f3dc0df70d724c817814af8c4 to your computer and use it in GitHub Desktop.
Save kalmarek/9ef55d0f3dc0df70d724c817814af8c4 to your computer and use it in GitHub Desktop.
tests for acb_calc_integrate
using Nemo
using Base.Test
const PREC = 200
const FF = AcbField(PREC)
const acb_struct = Nemo.acb_struct
import Base.*
function *(x::acb_struct, d::S, prec::Int=PREC) where S<:Int
res = acb_struct()
mul!(res, x, d, prec)
return res
end
*(d::S, x::acb_struct, prec::Int=PREC) where S<:Int = *(x, d, prec)
function *(x::acb_struct, y::acb_struct, prec::Int=PREC)
res = acb_struct()
mul!(res, x, y, prec)
return res
end
@testset "integration" begin
@testset "Basic Arithmetic" begin
@test isa(acb_struct(), Nemo.acb_struct)
@test isa(acb_struct(1), Nemo.acb_struct)
@test isa(one(acb_struct), Nemo.acb_struct)
@test isa(FF(one(acb_struct)), FieldElem)
@test isa(FF(one(acb_struct)), acb)
@test FF(one(acb_struct)) == one(FF)
@test isa(2*one(acb_struct), Nemo.acb_struct)
x = FF(3)
a = 2acb_struct(x)
@test isa(a, Nemo.acb_struct)
y = FF(a)
@test isa(y, acb)
@test x.parent == y.parent
@test 2x == y
a = acb_struct(3)
res = acb_struct()
mul!(res, a,a, prec(FF))
@test FF(res) == FF(a)*FF(a)
end
@testset "cfunction interface" begin
function square_acbs(res::acb_struct, x::acb_struct, param, order::Int, prec::Int)
mul!(res, x, x, prec)
return convert(Cint, 0)::Cint
end
csquare_acbs = cfunction(square_acbs, Cint,
(Ref{acb_struct}, Ref{acb_struct}, Ptr{Void}, Int, Int))
a = 3one(acb_struct)
res = acb_struct()
# 3*3 == 9
ccall(csquare_acbs, Cint,
(Ref{acb_struct}, Ref{acb_struct}, Ptr{Void}, Int, Int),
res, a, C_NULL, 1, prec(FF))
@test FF(res) == FF(9)
@test FF(res) == FF(9one(acb_struct))
# 2im*2im = -4
ccall(csquare_acbs, Cint,
(Ref{acb_struct}, Ref{acb_struct}, Ptr{Void}, Int, Int),
res, 2onei(acb_struct), C_NULL, 1, prec(FF))
@test FF(res) == FF(-4)
@test FF(res) == -FF(4one(acb_struct))
end
@testset "acb_calc_func_wrap" begin
f(x) = (x+1)^2
acbFunc = Nemo.acb_calc_func(FF, f)
@test isa(acbFunc, Nemo.acb_calc_func)
@test acbFunc(FF(3)) == FF(f(3))
@test acbFunc(acb_struct(FF(3))) == FF(f(3))
let acbFunc_c = cfunction(Nemo.acb_calc_func_wrap, Cint,
(Ref{acb_struct}, Ref{acb_struct}, Ptr{Void}, Int, Int))
res = acb_struct()
@test ccall(acbFunc_c, Cint,
(Ref{acb_struct}, Ref{acb_struct}, Ptr{Nemo.acb_calc_func}, Int, Int),
res, acb_struct(3), pointer_from_objref(acbFunc), 1, prec(FF)) == Cint(0)
@test FF(res) == FF(f(3))
res = acb_struct()
@test Nemo.acb_calc_func_wrap(res, acb_struct(3), pointer_from_objref(acbFunc), 1, prec(FF)) == Cint(0)
@test FF(res) == FF(f(3))
res = acb_struct()
@test ccall(acbFunc_c, Cint,
(Ref{acb_struct}, Ref{acb_struct}, Ptr{Nemo.acb_calc_func}, Int, Int),
res, 2onei(acb_struct), pointer_from_objref(acbFunc), 1, prec(FF)) == Cint(0)
@test FF(res) == f(FF(0,2))
res = acb_struct()
@test Nemo.acb_calc_func_wrap(res, 2onei(acb_struct), pointer_from_objref(acbFunc), 1, prec(FF)) == Cint(0)
@test FF(res) == f(FF(0,2))
end
end
@testset "cfunction" begin
f(x) = (x+1)^2
acbFunc = Nemo.acb_calc_func(FF, f)
@test isa(acbFunc, Nemo.acb_calc_func)
@test acbFunc(FF(3)) == FF(f(3))
@test acbFunc(acb_struct(FF(3))) == FF(f(3))
let acbFunc_c = cfunction(acbFunc)
res = acb_struct()
@test ccall(acbFunc_c, Cint,
(Ref{acb_struct}, Ref{acb_struct}, Ptr{Void}, Int, Int),
res, acb_struct(3), pointer_from_objref(acbFunc), 1, prec(FF)) == Cint(0)
@test FF(res) == FF(f(3))
res = acb_struct()
@test Nemo.acb_calc_func_wrap(res, acb_struct(3), pointer_from_objref(acbFunc), 1, prec(FF)) == Cint(0)
@test FF(res) == FF(f(3))
res = acb_struct()
@test ccall(acbFunc_c, Cint,
(Ref{acb_struct}, Ref{acb_struct}, Ptr{Void}, Int, Int),
res, 2onei(acb_struct), pointer_from_objref(acbFunc), 1, prec(FF)) == Cint(0)
@test FF(res) == f(FF(0,2))
res = acb_struct()
@test Nemo.acb_calc_func_wrap(res, 2onei(acb_struct), pointer_from_objref(acbFunc), 1, prec(FF)) == Cint(0)
@test FF(res) == f(FF(0,2))
end
end
@testset "integrate simple function" begin
@test isa(Nemo.mag_struct(), Nemo.mag_struct)
@test isa(Nemo.mag_struct(2.0^-20), Nemo.mag_struct)
mag = Nemo.mag_struct(2.0^-prec(FF))
function square_acbs(res::acb_struct, x::acb_struct, param, order::Int, prec::Int)
mul!(res, x, x, prec)
return convert(Cint, 0)::Cint
end
square_acbs_cf = cfunction(square_acbs, Cint,
(Ref{acb_struct}, Ref{acb_struct}, Ptr{Void}, Int, Int))
opts = Nemo.acb_calc_integrate_opts()
@test isa(opts, Nemo.acb_calc_integrate_opts)
res = acb_struct()
status = ccall((:acb_calc_integrate, Nemo.libarb), UInt,
(Ref{acb_struct}, #res
Ptr{Void}, #func
Ptr{Void}, #params
Ref{acb_struct}, #a
Ref{acb_struct}, #b
Int, #rel_goal
Ref{Nemo.mag_struct}, #abs_tol
Ref{Nemo.acb_calc_integrate_opts}, #opts
Int),
res, square_acbs_cf, C_NULL, acb_struct(0), acb_struct(2), prec(FF), mag, opts, prec(FF))
@test status == Nemo.ARB_CALC_SUCCESS
@test contains(real(FF(res)), 8//3)
@test imag(FF(res)) == 0
end
end # of testset "integration"
@testset "integration acb_calc_func" begin
opts = Nemo.acb_calc_integrate_opts()
mag = Nemo.mag_struct(2.0^-prec(FF))
f(x) = (x+1)^2
acb_f = Nemo.acb_calc_func(FF, f)
acb_cf = cfunction(acb_f)
res = zero(acb_struct)
#=
status = ccall((:acb_calc_integrate, Nemo.libarb), UInt,
(Ref{acb_struct}, #res -> store the result
Ptr{Void}, #func -> cfunction(acb_calc_func)
Ptr{Void}, #params-> pointer to actual function to evaluate
Ref{acb_struct}, #a
Ref{acb_struct}, #b
Int, #rel_goal
Ref{Nemo.mag_struct}, #abs_tol
Ref{Nemo.acb_calc_integrate_opts}, #opts
Int # prec
),
res, acb_cf, pointer_from_objref(acb_f), acb_struct(-1), acb_struct(1), prec(FF), mag, opts, prec(FF))
@show res
@test status == Nemo.ARB_CALC_SUCCESS
=#
@test contains(real(FF(res)), 8//3)
@test imag(FF(res)) == 0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment