Skip to content

Commit

Permalink
Add and unify one!, zero!, neg! methods for arb types
Browse files Browse the repository at this point in the history
  • Loading branch information
fingolfin committed Oct 15, 2024
1 parent 677fbcf commit e96aa28
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 157 deletions.
57 changes: 26 additions & 31 deletions src/arb/Complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,9 @@ is_domain_type(::Type{ComplexFieldElem}) = true

is_exact_type(::Type{ComplexFieldElem}) = false

function zero(r::ComplexField)
z = ComplexFieldElem()
return z
end
zero(r::ComplexField) = ComplexFieldElem()

function one(r::ComplexField)
z = ComplexFieldElem()
ccall((:acb_one, libflint), Nothing, (Ref{ComplexFieldElem}, ), z)
return z
end
one(r::ComplexField) = one!(ComplexFieldElem())

@doc raw"""
onei(r::ComplexField)
Expand All @@ -45,7 +38,7 @@ Return exact one times $i$ in the given Arb complex field.
"""
function onei(r::ComplexField)
z = ComplexFieldElem()
ccall((:acb_onei, libflint), Nothing, (Ref{ComplexFieldElem}, ), z)
onei!(z)
return z
end

Expand Down Expand Up @@ -156,11 +149,7 @@ end
#
################################################################################

function -(x::ComplexFieldElem)
z = ComplexFieldElem()
ccall((:acb_neg, libflint), Nothing, (Ref{ComplexFieldElem}, Ref{ComplexFieldElem}), z, x)
return z
end
-(x::ComplexFieldElem) = neg!(ComplexFieldElem(), x)

################################################################################
#
Expand Down Expand Up @@ -239,29 +228,25 @@ end
function -(x::UInt, y::ComplexFieldElem)
z = ComplexFieldElem()
ccall((:acb_sub_ui, libflint), Nothing, (Ref{ComplexFieldElem}, Ref{ComplexFieldElem}, UInt, Int), z, y, x, precision(Balls))
ccall((:acb_neg, libflint), Nothing, (Ref{ComplexFieldElem}, Ref{ComplexFieldElem}), z, z)
return z
return neg!(z)
end

function -(x::Int, y::ComplexFieldElem)
z = ComplexFieldElem()
ccall((:acb_sub_si, libflint), Nothing, (Ref{ComplexFieldElem}, Ref{ComplexFieldElem}, Int, Int), z, y, x, precision(Balls))
ccall((:acb_neg, libflint), Nothing, (Ref{ComplexFieldElem}, Ref{ComplexFieldElem}), z, z)
return z
return neg!(z)
end

function -(x::ZZRingElem, y::ComplexFieldElem)
z = ComplexFieldElem()
ccall((:acb_sub_fmpz, libflint), Nothing, (Ref{ComplexFieldElem}, Ref{ComplexFieldElem}, Ref{ZZRingElem}, Int), z, y, x, precision(Balls))
ccall((:acb_neg, libflint), Nothing, (Ref{ComplexFieldElem}, Ref{ComplexFieldElem}), z, z)
return z
return neg!(z)
end

function -(x::RealFieldElem, y::ComplexFieldElem)
z = ComplexFieldElem()
ccall((:acb_sub_arb, libflint), Nothing, (Ref{ComplexFieldElem}, Ref{ComplexFieldElem}, Ref{RealFieldElem}, Int), z, y, x, precision(Balls))
ccall((:acb_neg, libflint), Nothing, (Ref{ComplexFieldElem}, Ref{ComplexFieldElem}), z, z)
return z
return neg!(z)
end

+(x::ComplexFieldElem, y::Integer) = x + flintify(y)
Expand Down Expand Up @@ -1581,13 +1566,23 @@ end
#
################################################################################

function zero!(z::ComplexFieldElem)
ccall((:acb_zero, libflint), Nothing, (Ref{ComplexFieldElem},), z)
function zero!(z::ComplexFieldElemOrPtr)
@ccall libflint.acb_zero(z::Ref{ComplexFieldElem})::Nothing
return z
end

function one!(z::ComplexFieldElemOrPtr)
@ccall libflint.acb_one(z::Ref{ComplexFieldElem})::Nothing
return z
end

function onei!(z::ComplexFieldElemOrPtr)
@ccall libflint.acb_onei(z::Ref{ComplexFieldElem})::Nothing
return z
end

function one!(z::ComplexFieldElem)
ccall((:acb_one, libflint), Nothing, (Ref{ComplexFieldElem},), z)
function neg!(z::ComplexFieldElemOrPtr, a::ComplexFieldElemOrPtr)
@ccall libflint.acb_neg(z::Ref{ComplexFieldElem}, a::Ref{ComplexFieldElem})::Nothing
return z
end

Expand Down Expand Up @@ -1675,21 +1670,21 @@ for (typeofx, passtoc) in ((ComplexFieldElem, Ref{ComplexFieldElem}), (Ptr{Compl
r = ccall((:acb_real_ptr, libflint), Ptr{RealFieldElem}, (($passtoc), ), x)
_arb_set(r, y, p)
i = ccall((:acb_imag_ptr, libflint), Ptr{RealFieldElem}, (($passtoc), ), x)
ccall((:arb_zero, libflint), Nothing, (Ptr{RealFieldElem}, ), i)
zero!(i)
end

function _acb_set(x::($typeofx), y::BigFloat)
r = ccall((:acb_real_ptr, libflint), Ptr{RealFieldElem}, (($passtoc), ), x)
_arb_set(r, y)
i = ccall((:acb_imag_ptr, libflint), Ptr{RealFieldElem}, (($passtoc), ), x)
ccall((:arb_zero, libflint), Nothing, (Ptr{RealFieldElem}, ), i)
zero!(i)
end

function _acb_set(x::($typeofx), y::BigFloat, p::Int)
r = ccall((:acb_real_ptr, libflint), Ptr{RealFieldElem}, (($passtoc), ), x)
_arb_set(r, y, p)
i = ccall((:acb_imag_ptr, libflint), Ptr{RealFieldElem}, (($passtoc), ), x)
ccall((:arb_zero, libflint), Nothing, (Ptr{RealFieldElem}, ), i)
zero!(i)
end

function _acb_set(x::($typeofx), y::Int, z::Int, p::Int)
Expand Down Expand Up @@ -1728,7 +1723,7 @@ for (typeofx, passtoc) in ((ComplexFieldElem, Ref{ComplexFieldElem}), (Ptr{Compl
r = ccall((:acb_real_ptr, libflint), Ptr{RealFieldElem}, (($passtoc), ), x)
_arb_set(r, y, p)
i = ccall((:acb_imag_ptr, libflint), Ptr{RealFieldElem}, (($passtoc), ), x)
ccall((:arb_zero, libflint), Nothing, (Ptr{ArbFieldElem}, ), i)
zero!(i)
end

function _acb_set(x::($typeofx), y::Complex, p::Int)
Expand Down
29 changes: 18 additions & 11 deletions src/arb/ComplexMat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ setindex!(x::ComplexMatrix, y::Tuple{Rational{T}, Rational{T}}, r::Int, c::Int)
setindex!(x, map(QQFieldElem, y), r, c)

function one(x::ComplexMatrixSpace)
z = x()
ccall((:acb_mat_one, libflint), Nothing, (Ref{ComplexMatrix}, ), z)
return z
return one!(x())
end

number_of_rows(a::ComplexMatrix) = a.r
Expand All @@ -101,11 +99,7 @@ end
#
################################################################################

function -(x::ComplexMatrix)
z = similar(x)
ccall((:acb_mat_neg, libflint), Nothing, (Ref{ComplexMatrix}, Ref{ComplexMatrix}), z, x)
return z
end
-(x::ComplexMatrix) = neg!(similar(x), x)

################################################################################
#
Expand Down Expand Up @@ -723,6 +717,21 @@ end
#
################################################################################

function zero!(z::ComplexMatrixOrPtr)
@ccall libflint.acb_mat_zero(z::Ref{ComplexMatrix})::Nothing
return z
end

function one!(z::ComplexMatrixOrPtr)
@ccall libflint.acb_mat_one(z::Ref{ComplexMatrix})::Nothing
return z
end

function neg!(z::ComplexMatrixOrPtr, a::ComplexMatrixOrPtr)
@ccall libflint.acb_mat_neg(z::Ref{ComplexMatrix}, a::Ref{ComplexMatrix})::Nothing
return z
end

for (s,f) in (("add!","acb_mat_add"), ("mul!","acb_mat_mul"),
("sub!","acb_mat_sub"))
@eval begin
Expand Down Expand Up @@ -893,9 +902,7 @@ function identity_matrix(R::ComplexField, n::Int)
if n < 0
error("dimension must not be negative")
end
z = ComplexMatrix(n, n)
ccall((:acb_mat_one, libflint), Nothing, (Ref{ComplexMatrix}, ), z)
return z
return one!(ComplexMatrix(n, n))
end

################################################################################
Expand Down
21 changes: 11 additions & 10 deletions src/arb/ComplexPoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,7 @@ end
#
################################################################################

function -(x::ComplexPolyRingElem)
z = parent(x)()
ccall((:acb_poly_neg, libflint), Nothing, (Ref{ComplexPolyRingElem}, Ref{ComplexPolyRingElem}), z, x)
return z
end
-(x::ComplexPolyRingElem) = neg!(parent(x)(), x)

################################################################################
#
Expand Down Expand Up @@ -638,7 +634,7 @@ function roots(x::ComplexPolyRingElem; target=0, isolate_real=false, initial_pre
im = ccall((:acb_imag_ptr, libflint), Ptr{arb_struct},
(Ptr{ComplexFieldElem}, ), roots + i * sizeof(acb_struct))
if ccall((:arb_contains_zero, libflint), Bool, (Ptr{arb_struct}, ), im)
ccall((:arb_zero, libflint), Nothing, (Ptr{arb_struct}, ), im)
zero!(im)
end
end
end
Expand Down Expand Up @@ -702,13 +698,18 @@ end
#
###############################################################################

function zero!(z::ComplexPolyRingElem)
ccall((:acb_poly_zero, libflint), Nothing, (Ref{ComplexPolyRingElem},), z)
function zero!(z::ComplexPolyRingElemOrPtr)
@ccall libflint.acb_poly_zero(z::Ref{ComplexPolyRingElem})::Nothing
return z
end

function one!(z::ComplexPolyRingElemOrPtr)
@ccall libflint.acb_poly_one(z::Ref{ComplexPolyRingElem})::Nothing
return z
end

function one!(z::ComplexPolyRingElem)
ccall((:acb_poly_one, libflint), Nothing, (Ref{ComplexPolyRingElem},), z)
function neg!(z::ComplexPolyRingElemOrPtr, a::ComplexPolyRingElemOrPtr)
@ccall libflint.acb_poly_neg(z::Ref{ComplexPolyRingElem}, a::Ref{ComplexPolyRingElem})::Nothing
return z
end

Expand Down
21 changes: 11 additions & 10 deletions src/arb/Real.jl
Original file line number Diff line number Diff line change
Expand Up @@ -586,11 +586,7 @@ end
#
################################################################################

function -(x::RealFieldElem)
z = RealFieldElem()
ccall((:arb_neg, libflint), Nothing, (Ref{RealFieldElem}, Ref{RealFieldElem}), z, x)
return z
end
-(x::RealFieldElem) = neg!(RealFieldElem(), x)

################################################################################
#
Expand Down Expand Up @@ -1929,20 +1925,25 @@ end
#
################################################################################

function zero!(z::RealFieldElem)
ccall((:arb_zero, libflint), Nothing, (Ref{RealFieldElem},), z)
function zero!(z::RealFieldElemOrPtr)
@ccall libflint.arb_zero(z::Ref{RealFieldElem})::Nothing
return z
end

function one!(z::RealFieldElemOrPtr)
@ccall libflint.arb_one(z::Ref{RealFieldElem})::Nothing
return z
end

function one!(z::RealFieldElem)
ccall((:arb_one, libflint), Nothing, (Ref{RealFieldElem},), z)
function neg!(z::RealFieldElemOrPtr, a::RealFieldElemOrPtr)
@ccall libflint.arb_neg(z::Ref{RealFieldElem}, a::Ref{RealFieldElem})::Nothing
return z
end

for (s,f) in (("add!","arb_add"), ("mul!","arb_mul"), ("div!", "arb_div"),
("sub!","arb_sub"))
@eval begin
function ($(Symbol(s)))(z::RealFieldElem, x::RealFieldElem, y::RealFieldElem, prec::Int = precision(Balls))
function ($(Symbol(s)))(z::RealFieldElemOrPtr, x::RealFieldElemOrPtr, y::RealFieldElemOrPtr, prec::Int = precision(Balls))
ccall(($f, libflint), Nothing, (Ref{RealFieldElem}, Ref{RealFieldElem}, Ref{RealFieldElem}, Int),
z, x, y, prec)
return z
Expand Down
29 changes: 18 additions & 11 deletions src/arb/RealMat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ Base.@propagate_inbounds setindex!(x::RealMatrix, y::Rational{T},
setindex!(x, ZZRingElem(y), r, c)

function one(x::RealMatrixSpace)
z = x()
ccall((:arb_mat_one, libflint), Nothing, (Ref{RealMatrix}, ), z)
return z
return one!(x())
end

number_of_rows(a::RealMatrix) = a.r
Expand All @@ -89,11 +87,7 @@ end
#
################################################################################

function -(x::RealMatrix)
z = similar(x)
ccall((:arb_mat_neg, libflint), Nothing, (Ref{RealMatrix}, Ref{RealMatrix}), z, x)
return z
end
-(x::RealMatrix) = neg!(similar(x), x)

################################################################################
#
Expand Down Expand Up @@ -665,6 +659,21 @@ end
#
################################################################################

function zero!(z::RealMatrixOrPtr)
@ccall libflint.arb_mat_zero(z::Ref{RealMatrix})::Nothing
return z
end

function one!(z::RealMatrixOrPtr)
@ccall libflint.arb_mat_one(z::Ref{RealMatrix})::Nothing
return z
end

function neg!(z::RealMatrixOrPtr, a::RealMatrixOrPtr)
@ccall libflint.arb_mat_neg(z::Ref{RealMatrix}, a::Ref{RealMatrix})::Nothing
return z
end

for (s,f) in (("add!","arb_mat_add"), ("mul!","arb_mat_mul"),
("sub!","arb_mat_sub"))
@eval begin
Expand Down Expand Up @@ -783,9 +792,7 @@ function identity_matrix(R::RealField, n::Int)
if n < 0
error("dimension must not be negative")
end
z = RealMatrix(n, n)
ccall((:arb_mat_one, libflint), Nothing, (Ref{RealMatrix}, ), z)
return z
return one!(RealMatrix(n, n))
end

###############################################################################
Expand Down
21 changes: 10 additions & 11 deletions src/arb/RealPoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,7 @@ end
#
################################################################################

function -(x::RealPolyRingElem)
z = parent(x)()
ccall((:arb_poly_neg, libflint), Nothing, (Ref{RealPolyRingElem}, Ref{RealPolyRingElem}), z, x)
return z
end
-(x::RealPolyRingElem) = neg!(parent(x)(), x)

################################################################################
#
Expand Down Expand Up @@ -601,15 +597,18 @@ end
#
###############################################################################

function zero!(z::RealPolyRingElem)
ccall((:arb_poly_zero, libflint), Nothing,
(Ref{RealPolyRingElem}, ), z)
function zero!(z::RealPolyRingElemOrPtr)
@ccall libflint.arb_poly_zero(z::Ref{RealPolyRingElem})::Nothing
return z
end

function one!(z::RealPolyRingElemOrPtr)
@ccall libflint.arb_poly_one(z::Ref{RealPolyRingElem})::Nothing
return z
end

function one!(z::RealPolyRingElem)
ccall((:arb_poly_one, libflint), Nothing,
(Ref{RealPolyRingElem}, ), z)
function neg!(z::RealPolyRingElemOrPtr, a::RealPolyRingElemOrPtr)
@ccall libflint.arb_poly_neg(z::Ref{RealPolyRingElem}, a::Ref{RealPolyRingElem})::Nothing
return z
end

Expand Down
Loading

0 comments on commit e96aa28

Please sign in to comment.