Skip to content

Commit

Permalink
Don't use ccalls where wrappers already exist (fmpq edition) (#1914)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoettgens authored Oct 25, 2024
1 parent 8cade50 commit 2a54dbe
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 26 deletions.
34 changes: 14 additions & 20 deletions src/flint/fmpq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,11 @@ end
###############################################################################

function numerator(a::QQFieldElem)
z = ZZRingElem()
ccall((:fmpq_numerator, libflint), Nothing, (Ref{ZZRingElem}, Ref{QQFieldElem}), z, a)
return z
return numerator!(ZZRingElem(), a)
end

function denominator(a::QQFieldElem)
z = ZZRingElem()
ccall((:fmpq_denominator, libflint), Nothing, (Ref{ZZRingElem}, Ref{QQFieldElem}), z, a)
return z
return denominator!(ZZRingElem(), a)
end

@doc raw"""
Expand Down Expand Up @@ -355,8 +351,7 @@ cmp(a::QQFieldElemOrPtr, b::Integer) = cmp(a, flintify(b))
cmp(a::Union{ZZRingElemOrPtr, Integer}, b::QQFieldElemOrPtr) = -cmp(b, a)

function ==(a::QQFieldElem, b::QQFieldElem)
return ccall((:fmpq_equal, libflint), Bool,
(Ref{QQFieldElem}, Ref{QQFieldElem}), a, b)
return @ccall libflint.fmpq_equal(a::Ref{QQFieldElem}, b::Ref{QQFieldElem})::Bool
end

function isless(a::QQFieldElem, b::QQFieldElem)
Expand Down Expand Up @@ -671,25 +666,25 @@ remove(a::QQFieldElem, b::Integer) = remove(a, ZZRingElem(b))
valuation(a::QQFieldElem, b::Integer) = valuation(a, ZZRingElem(b))

function remove!(a::QQFieldElem, b::ZZRingElem)
nr = ccall((:fmpq_numerator_ptr, libflint), Ptr{ZZRingElem}, (Ref{QQFieldElem},), a)
nr = _num_ptr(a)
vn = ccall((:fmpz_remove, libflint), Clong, (Ptr{ZZRingElem}, Ptr{ZZRingElem}, Ref{ZZRingElem}), nr, nr, b)
#QQFieldElem's are simplified: either num OR den will be non-trivial
if vn != 0
if !is_zero(vn)
return vn, a
end
nr = ccall((:fmpq_denominator_ptr, libflint), Ptr{ZZRingElem}, (Ref{QQFieldElem},), a)
nr = _den_ptr(a)
vn = ccall((:fmpz_remove, libflint), Clong, (Ptr{ZZRingElem}, Ptr{ZZRingElem}, Ref{ZZRingElem}), nr, nr, b)
return -vn, a
end

function valuation!(a::QQFieldElem, b::ZZRingElem)
nr = ccall((:fmpq_numerator_ptr, libflint), Ptr{ZZRingElem}, (Ref{QQFieldElem},), a)
nr = _num_ptr(a)
vn = ccall((:fmpz_remove, libflint), Clong, (Ptr{ZZRingElem}, Ptr{ZZRingElem}, Ref{ZZRingElem}), nr, nr, b)
#QQFieldElem's are simplified: either num OR den will be non-trivial
if vn != 0
if !is_zero(vn)
return vn
end
nr = ccall((:fmpq_denominator_ptr, libflint), Ptr{ZZRingElem}, (Ref{QQFieldElem},), a)
nr = _den_ptr(a)
vn = ccall((:fmpz_remove, libflint), Clong, (Ptr{ZZRingElem}, Ptr{ZZRingElem}, Ref{ZZRingElem}), nr, nr, b)
return -vn
end
Expand Down Expand Up @@ -725,9 +720,8 @@ julia> c = reconstruct(ZZ(123), ZZ(237))
```
"""
function reconstruct(a::ZZRingElem, m::ZZRingElem)
c = QQFieldElem()
if !Bool(ccall((:fmpq_reconstruct_fmpz, libflint), Cint,
(Ref{QQFieldElem}, Ref{ZZRingElem}, Ref{ZZRingElem}), c, a, m))
success, c = unsafe_reconstruct(a, m)
if !success
error("Impossible rational reconstruction")
end
return c
Expand Down Expand Up @@ -1074,12 +1068,12 @@ function set!(c::QQFieldElemOrPtr, a::Union{Integer,ZZRingElemOrPtr})
end

function numerator!(z::ZZRingElem, y::QQFieldElem)
ccall((:fmpq_numerator, libflint), Cvoid, (Ref{ZZRingElem}, Ref{QQFieldElem}), z, y)
@ccall libflint.fmpq_numerator(z::Ref{ZZRingElem}, y::Ref{QQFieldElem})::Nothing
return z
end

function denominator!(z::ZZRingElem, y::QQFieldElem)
ccall((:fmpq_denominator, libflint), Cvoid, (Ref{ZZRingElem}, Ref{QQFieldElem}), z, y)
function denominator!(z::ZZRingElemOrPtr, y::QQFieldElemOrPtr)
@ccall libflint.fmpq_denominator(z::Ref{ZZRingElem}, y::Ref{QQFieldElem})::Nothing
return z
end

Expand Down
3 changes: 1 addition & 2 deletions src/flint/fmpq_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ end

function transpose(x::QQMatrix)
z = similar(x, ncols(x), nrows(x))
ccall((:fmpq_mat_transpose, libflint), Nothing,
(Ref{QQMatrix}, Ref{QQMatrix}), z, x)
transpose!(z, x)
return z
end

Expand Down
3 changes: 1 addition & 2 deletions src/flint/fmpq_poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,7 @@ function ==(x::QQPolyRingElem, y::QQFieldElem)
z = QQFieldElem()
ccall((:fmpq_poly_get_coeff_fmpq, libflint), Nothing,
(Ref{QQFieldElem}, Ref{QQPolyRingElem}, Int), z, x, 0)
return ccall((:fmpq_equal, libflint), Bool,
(Ref{QQFieldElem}, Ref{QQFieldElem}, Int), z, y, 0)
return z == y
else
return iszero(y)
end
Expand Down
3 changes: 1 addition & 2 deletions src/flint/fmpq_rel_series.jl
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,7 @@ function ==(x::QQRelPowerSeriesRingElem, y::QQFieldElem)
z = QQFieldElem()
ccall((:fmpq_poly_get_coeff_fmpq, libflint), Nothing,
(Ref{QQFieldElem}, Ref{QQRelPowerSeriesRingElem}, Int), z, x, 0)
return ccall((:fmpq_equal, libflint), Bool,
(Ref{QQFieldElem}, Ref{QQFieldElem}, Int), z, y, 0)
return z == y
else
return false
end
Expand Down

0 comments on commit 2a54dbe

Please sign in to comment.