Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add and unify one!, zero!, neg! methods for arb types #1900

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_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 @@
"""
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 @@
#
################################################################################

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 @@
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)

Check warning on line 231 in src/arb/Complex.jl

View check run for this annotation

Codecov / codecov/patch

src/arb/Complex.jl#L231

Added line #L231 was not covered by tests
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 @@
#
################################################################################

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

Check warning on line 1571 in src/arb/Complex.jl

View check run for this annotation

Codecov / codecov/patch

src/arb/Complex.jl#L1569-L1571

Added lines #L1569 - L1571 were not covered by tests
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 @@
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)

Check warning on line 1680 in src/arb/Complex.jl

View check run for this annotation

Codecov / codecov/patch

src/arb/Complex.jl#L1680

Added line #L1680 was not covered by tests
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 @@
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, 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 @@
#
################################################################################

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 @@
#
################################################################################

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

Check warning on line 722 in src/arb/ComplexMat.jl

View check run for this annotation

Codecov / codecov/patch

src/arb/ComplexMat.jl#L720-L722

Added lines #L720 - L722 were not covered by tests
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 @@
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
25 changes: 13 additions & 12 deletions src/arb/ComplexPoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,7 @@
#
################################################################################

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 @@ -635,10 +631,10 @@

if real_ok
for i = 0 : deg - 1
im = ccall((:acb_imag_ptr, libflint), Ptr{arb_struct},
im = ccall((:acb_imag_ptr, libflint), Ptr{RealFieldElem},
(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)
if ccall((:arb_contains_zero, libflint), Bool, (Ptr{RealFieldElem}, ), im)
zero!(im)
end
end
end
Expand Down Expand Up @@ -702,13 +698,18 @@
#
###############################################################################

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

Check warning on line 703 in src/arb/ComplexPoly.jl

View check run for this annotation

Codecov / codecov/patch

src/arb/ComplexPoly.jl#L701-L703

Added lines #L701 - L703 were not covered by tests
end

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

Check warning on line 707 in src/arb/ComplexPoly.jl

View check run for this annotation

Codecov / codecov/patch

src/arb/ComplexPoly.jl#L706-L707

Added lines #L706 - L707 were not covered by tests
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 @@
#
################################################################################

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 @@
#
################################################################################

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

Check warning on line 1934 in src/arb/Real.jl

View check run for this annotation

Codecov / codecov/patch

src/arb/Real.jl#L1933-L1934

Added lines #L1933 - L1934 were not covered by tests
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 @@
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 @@
#
################################################################################

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 @@
#
################################################################################

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

Check warning on line 664 in src/arb/RealMat.jl

View check run for this annotation

Codecov / codecov/patch

src/arb/RealMat.jl#L662-L664

Added lines #L662 - L664 were not covered by tests
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 @@
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 @@
#
################################################################################

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 @@
#
###############################################################################

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

Check warning on line 602 in src/arb/RealPoly.jl

View check run for this annotation

Codecov / codecov/patch

src/arb/RealPoly.jl#L600-L602

Added lines #L600 - L602 were not covered by tests
end

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

Check warning on line 606 in src/arb/RealPoly.jl

View check run for this annotation

Codecov / codecov/patch

src/arb/RealPoly.jl#L605-L606

Added lines #L605 - L606 were not covered by tests
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
Loading