Skip to content

Commit

Permalink
Use set!, zero!, etc. in YET more places (#1650)
Browse files Browse the repository at this point in the history
Also switch to Nemo.denominator! and Nemo.numerator!,
and remove add_two! which now is equivalent to `add!(x, 2)`.
  • Loading branch information
fingolfin authored Oct 17, 2024
1 parent 4ffee6e commit 276ac56
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 72 deletions.
6 changes: 3 additions & 3 deletions examples/Det.jl
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ function add_into!(A::ZZMatrix, C::ZZMatrix, c::Int)
A_ptr = Nemo.mat_entry_ptr(A, i+c, 1)
C_ptr = Nemo.mat_entry_ptr(C, i, 1)
for j=1:ncols(A)
ccall((:fmpz_add, Nemo.libflint), Cvoid, (Ptr{ZZRingElem}, Ptr{ZZRingElem}, Ptr{ZZRingElem}), A_ptr, A_ptr, C_ptr)
add!(A_ptr, C_ptr)
A_ptr += sizeof(Clong)
C_ptr += sizeof(Clong)
end
Expand Down Expand Up @@ -1247,7 +1247,7 @@ function dixon_solve(D::DixonCtx{T}, B::ZZMatrix; block::Int = 10) where T
for i=1:nrows(D.x)
x_ptr = Nemo.mat_entry_ptr(D.x, i, 1)
for j=1:ncols(D.x)
ccall((:fmpz_addmul_si, Nemo.libflint), Cvoid, (Ptr{ZZRingElem}, Ref{ZZRingElem}, Int), x_ptr, ppow, Int(D.y_mod[i, j]))
addmul!(x_ptr, ppow, Int(D.y_mod[i, j]))
x_ptr += 8
end
end
Expand Down Expand Up @@ -1302,7 +1302,7 @@ function dixon_solve(D::DixonCtx{T}, B::ZZMatrix; block::Int = 10) where T
ccall((:fmpz_zero, Nemo.libflint), Cvoid, (Ptr{ZZRingElem},), Ay_ptr)
for j=1:n
y_ptr = Nemo.mat_entry_ptr(D.y_mod, j, 1)
ccall((:fmpz_addmul_ui, Nemo.libflint), Cvoid, (Ptr{ZZRingElem}, Ptr{ZZRingElem}, UInt), Ay_ptr, A_ptr, unsafe_load(y_ptr))
addmul!(Ay_ptr, A_ptr, unsafe_load(y_ptr))
A_ptr += sizeof(ZZRingElem)
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/AlgAss/Elem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ end
function _addmul!(M::QQMatrix, i, j, a::QQFieldElem, b::QQFieldElem, temp = nothing)
GC.@preserve M begin
c = mat_entry_ptr(M, i, j)
ccall((:fmpq_addmul, libflint), Nothing, (Ptr{QQFieldElem}, Ref{QQFieldElem}, Ref{QQFieldElem}), c, a, b)
addmul!(c, a, b)
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/AlgAss/Map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function __set!(c_t::QQMatrix, ca)
GC.@preserve c_t begin
for i in 1:length(ca)
t = mat_entry_ptr(c_t, 1, i)
ccall((:fmpq_set, libflint), Cvoid, (Ptr{QQFieldElem}, Ref{QQFieldElem}), t, ca[i])
set!(t, ca[i])
end
end
end
Expand Down
14 changes: 7 additions & 7 deletions src/AlgAssAbsOrd/PIP/bley_johnston.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function _is_principal_with_data_bj(I, O; side = :right, _alpha = nothing, local

F = Hecke._get_a_twosided_conductor(O, M)
# Now make corpime to conductor

Iorig = I
if F == 1*O || I + F == one(A) * O
# I should improve this
Expand Down Expand Up @@ -377,7 +377,7 @@ function _old_optimization(dd, local_coeffs, dec, bases_offsets_and_lengths, H,
end
w = local_coeffs[1][idx[1]]
for i in 1:dd
ccall((:fmpq_set, libflint), Ref{Nothing}, (Ref{QQFieldElem}, Ref{QQFieldElem}), vtemp[i], w[i])
set!(vtemp[i], w[i])
end
#@show vtemp
for j in 2:(length(dec) - 1)
Expand All @@ -404,7 +404,7 @@ function _old_optimization(dd, local_coeffs, dec, bases_offsets_and_lengths, H,
#@show length(ids)
for j in ids
#for i in 1:dd
# ccall((:fmpq_set, libflint), Ref{Nothing}, (Ref{QQFieldElem}, Ref{QQFieldElem}), vtemp[i], _vtempcopy[i])
# set!(vtemp[i], _vtempcopy[i])
#end
_vtemp = deepcopy(vtemp) .+ local_coeffs[end][j]
if all(is_integral, _vtemp)
Expand All @@ -420,7 +420,7 @@ function _old_optimization(dd, local_coeffs, dec, bases_offsets_and_lengths, H,
end

#
function _recursive_iterator!(x, lengths, d, elts::Vector, bases_offsets, indices_integral, indices_nonintegral, k, i, vtemp)
function _recursive_iterator!(x, lengths, d, elts::Vector, bases_offsets, indices_integral, indices_nonintegral, k, i, vtemp::Vector{QQFieldElem})
if i > k
println("2", x)
elseif i == k # unroll 1-loop base case for speed
Expand Down Expand Up @@ -466,18 +466,18 @@ function _recursive_iterator!(x, lengths, d, elts::Vector, bases_offsets, indice
end
end

function _is_admissible(x, i, d, elts, bases_offsets, vtemp)
function _is_admissible(x, i, d, elts, bases_offsets, vtemp::Vector{QQFieldElem})
# Test if x[1,...,i] is admissible
w = elts[1][x[1]]
for k in 1:d
ccall((:fmpq_set, libflint), Ref{Nothing}, (Ref{QQFieldElem}, Ref{QQFieldElem}), vtemp[k], w[k])
set!(vtemp[k], w[k])
end
#@show vtemp
for j in 2:i
w = elts[j][x[j]]
#@assert all(iszero, @view w[1:bases_offsets[j][1] - 1])
for k in bases_offsets[j][1]:d
add!(vtemp[k], vtemp[k], w[k])
add!(vtemp[k], w[k])
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/Hecke.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ import Nemo: acb_struct, Ring, Group, Field, zzModRing, zzModRingElem, arf_struc
FpField, acb_vec, array, acb_vec_clear, force_coerce,
force_op, fmpz_mod_ctx_struct, divisors, is_zero_entry, IntegerUnion, remove!,
valuation!, is_cyclo_type, is_embedded, is_maxreal_type,
mat_entry_ptr, factor_trial_range, set!
mat_entry_ptr, factor_trial_range, set!, numerator!, denominator!

AbstractAlgebra.@include_deprecated_bindings()
Nemo.@include_deprecated_bindings()
Expand Down
4 changes: 2 additions & 2 deletions src/LinearAlgebra/LatEnum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,12 @@ end

@inline function fmpz_mat_entry_incref!(a::ZZMatrix, r::Int, c::Int)
z = Nemo.mat_entry_ptr(a, r, c)
ccall((:fmpz_add_ui, libflint), Nothing, (Ptr{ZZRingElem}, Ptr{ZZRingElem}, Int), z, z, 1)
add!(z, 1)
end

function fmpz_mat_entry_add_ui!(a::ZZMatrix, r::Int, c::Int, v::UInt)
z = Nemo.mat_entry_ptr(a, r, c)
ccall((:fmpz_add_ui, libflint), Nothing, (Ptr{ZZRingElem}, Ptr{ZZRingElem}, Int), z, z, v)
add!(z, v)
end

function enum_ctx_advance_level(E::enum_ctx{A,B,C}, i::Int) where {A,B,C}
Expand Down
6 changes: 3 additions & 3 deletions src/Misc/CRT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -494,13 +494,13 @@ function _num_setcoeff!(a::AbsSimpleNumFieldElem, n::Int, c::UInt)
K = a.parent
@assert n < degree(K) && n >=0

ra = pointer_from_objref(a)
ra = Ptr{ZZRingElem}(pointer_from_objref(a))

if degree(K) == 1
ccall((:fmpz_set_ui, libflint), Nothing, (Ref{Nothing}, UInt), ra, c)
set!(ra, c)
ccall((:fmpq_canonicalise, libflint), Nothing, (Ref{AbsSimpleNumFieldElem}, ), a)
elseif degree(K) == 2
ccall((:fmpz_set_ui, libflint), Nothing, (Ref{Nothing}, UInt), ra+n*sizeof(Int), c)
set!(ra+n*sizeof(Int), c)
else
ccall((:fmpq_poly_set_coeff_ui, libflint), Nothing, (Ref{AbsSimpleNumFieldElem}, Int, UInt), a, n, c)
# includes canonicalisation and treatment of den.
Expand Down
29 changes: 14 additions & 15 deletions src/Misc/Matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,14 @@ function reduce_mod_hnf_ur!(a::ZZMatrix, H::ZZMatrix)
q = ZZRingElem()
ccall((:fmpz_fdiv_q, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}), q, n, m)
#q = fdiv(a[c, j], H[i, j])
fl = ccall((:fmpz_is_zero, libflint), Bool, (Ref{ZZRingElem},), q)
fl = is_zero(q)
if fl
continue
end
for k = j:ncols(a)
t = mat_entry_ptr(a, c, k)
l = mat_entry_ptr(H, i, k)
ccall((:fmpz_submul, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}), t, q, l)
submul!(t, q, l)
#a[c, k] = a[c, k] - q * H[i, k]
end
end
Expand All @@ -275,14 +275,14 @@ function reduce_mod_hnf_ll!(a::ZZMatrix, H::ZZMatrix)
q = ZZRingElem()
ccall((:fmpz_fdiv_q, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}), q, n, m)
#q = fdiv(a[c, j], H[i, j])
fl = ccall((:fmpz_is_zero, libflint), Bool, (Ref{ZZRingElem},), q)
fl = is_zero(q)
if fl
continue
end
for k = 1:j
t = mat_entry_ptr(a, c, k)
l = mat_entry_ptr(H, i, k)
ccall((:fmpz_submul, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}), t, q, l)
submul!(t, q, l)
end
end
end
Expand Down Expand Up @@ -486,17 +486,17 @@ function snf_for_groups(A::ZZMatrix, mod::ZZRingElem)
GC.@preserve S R begin
for i=1:min(nrows(S), ncols(S))
Sii = mat_entry_ptr(S, i, i)
fl = ccall((:fmpz_is_one, libflint), Bool, (Ref{ZZRingElem},), Sii)
fl = is_one(Sii)
if fl
continue
end
for j=i+1:min(nrows(S), ncols(S))
Sjj = mat_entry_ptr(S, j, j)
fl = ccall((:fmpz_is_zero, libflint), Bool, (Ref{ZZRingElem},), Sjj)
fl = is_zero(Sjj)
if fl
continue
end
fl1 = ccall((:fmpz_is_zero, libflint), Bool, (Ref{ZZRingElem},), Sii)
fl1 = is_zero(Sii)
if !fl1
fl2 = Bool(ccall((:fmpz_divisible, libflint), Cint,
(Ref{ZZRingElem}, Ref{ZZRingElem}), Sjj, Sii))
Expand All @@ -517,13 +517,13 @@ function snf_for_groups(A::ZZMatrix, mod::ZZRingElem)
ccall((:fmpz_tdiv_qr, libflint), Nothing,
(Ref{ZZRingElem}, Ref{ZZRingElem}, Ptr{ZZRingElem}, Ref{ZZRingElem}), a, r, Sii, g)
#a = divexact(S[i,i], g)
ccall((:fmpz_set, libflint), Nothing, (Ptr{ZZRingElem}, Ref{ZZRingElem}), Sii, g)
set!(Sii, g)
#S[i,i] = g
b = ZZRingElem()
ccall((:fmpz_tdiv_qr, libflint), Nothing,
(Ref{ZZRingElem}, Ref{ZZRingElem}, Ptr{ZZRingElem}, Ref{ZZRingElem}), b, r, Sjj, g)
#b = divexact(S[j,j], g)
ccall((:fmpz_mul, libflint), Nothing, (Ptr{ZZRingElem}, Ptr{ZZRingElem}, Ref{ZZRingElem}), Sjj, Sjj, a)
Sjj = mul!(Sjj, a)
#S[j,j] *= a
# V = [e -b ; f a];
# so col i and j of R will be transformed. We do it naively
Expand All @@ -532,19 +532,18 @@ function snf_for_groups(A::ZZMatrix, mod::ZZRingElem)
Rik = mat_entry_ptr(R, i, k)
Rjk = mat_entry_ptr(R, j, k)
aux = ZZRingElem()
mul!(aux, Rik, e)
ccall((:fmpz_addmul, libflint), Nothing, (Ref{ZZRingElem}, Ptr{ZZRingElem}, Ref{ZZRingElem}), aux, Rjk, f)
aux = mul!(aux, Rik, e)
aux = addmul!(aux, Rjk, f)
aux1 = ZZRingElem()
mul!(aux1, Rjk, a)
ccall((:fmpz_submul, libflint), Nothing, (Ref{ZZRingElem}, Ptr{ZZRingElem}, Ref{ZZRingElem}), aux1, Rik, b)
aux1 = mul!(aux1, Rjk, a)
aux1 = submul!(aux1, Rik, b)
set!(Rik, aux)
set!(Rjk, aux1)
#R[i, k], R[j, k] = e*R[i,k]+f*R[j,k], -b*R[i,k]+a*R[j,k]
end
end
end
ccall((:fmpz_mat_transpose, libflint), Nothing,
(Ref{ZZMatrix}, Ref{ZZMatrix}), R, R)
transpose!(R)
end
return S, R
end
Expand Down
2 changes: 1 addition & 1 deletion src/NumFieldOrd/NfOrd/Ideal/Arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ function _idempotents_via_matrices(x::AbsNumFieldOrderIdeal, y::AbsNumFieldOrder
@hassert :AbsNumFieldOrder 2 -z in x
@hassert :AbsNumFieldOrder 2 1 + z in y

ccall((:fmpz_mat_zero, libflint), Nothing, (Ref{ZZMatrix}, ), V)
zero!(V)

return -z, 1 + z
end
Expand Down
6 changes: 3 additions & 3 deletions src/NumFieldOrd/NfOrd/ResidueRing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -498,14 +498,14 @@ function is_divisible(x::AbsOrdQuoRingElem, y::AbsOrdQuoRingElem)
for i in 2:(d + 1)
if !iszero(V[1, i])
#if !iszero(sub(V, 1:1, 2:(d + 1)))
ccall((:fmpz_mat_zero, libflint), Nothing, (Ref{ZZMatrix}, ), V)
zero!(V)
return false, zero(parent(x))
end
end

z = R(-base_ring(R)(ZZRingElem[ V[1, i] for i in (d + 2):(2*d + 1)])) # V[1, i] is always a copy

ccall((:fmpz_mat_zero, libflint), Nothing, (Ref{ZZMatrix}, ), V)
zero!(V)
@hassert :AbsOrdQuoRing 1 z*y == x
return true, z
end
Expand Down Expand Up @@ -776,7 +776,7 @@ function AbstractAlgebra.gcdxx(x::AbsSimpleNumFieldOrderQuoRingElem, y::AbsSimpl

@hassert :AbsOrdQuoRing 1 Q(O(1)) == u*e - (v*(-f))

ccall((:fmpz_mat_zero, libflint), Nothing, (Ref{ZZMatrix}, ), V)
zero!(V)

return g, u, v, -f, e
end
Expand Down
45 changes: 10 additions & 35 deletions src/QuadForm/Enumeration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,7 @@ function Base.iterate(C::LatEnumCtx{X, Y, elem_type}) where {X, Y, elem_type}

if _short_enough
# t1 must be a UInt
#z = QQFieldElem()
#ccall((:fmpq_set, libflint), Cvoid, (Ref{QQFieldElem}, Ref{QQFieldElem}), z, t1)
#z = QQFieldElem(t1)
# Todo
return (pp_vector(x), pp_length(len)), i
#y = Vector{S}(undef, n)
Expand Down Expand Up @@ -398,8 +397,7 @@ function Base.iterate(C::LatEnumCtx{S, W, V}, it::Int) where {S, W, V}

if _short_enough
# t1 must be a UInt
#z = QQFieldElem()
#ccall((:fmpq_set, libflint), Cvoid, (Ref{QQFieldElem}, Ref{QQFieldElem}), z, t1)
#z = QQFieldElem(t1)
# Todo
#y = __clean_and_assemble(x, transform)
#return (y, len//scale), i
Expand Down Expand Up @@ -504,7 +502,7 @@ function ___enumerate_cholesky(::Type{Vector}, Q::Matrix{QQFieldElem}, l::Union{
t2 = numerator!(t2, t1)
_short_enough = false
if S === Int
_len = ccall((:fmpz_get_si, libflint), Int, (Ref{ZZRingElem}, ), t2)
_len = Int(t2)
_short_enough = _len <= c
if !(l isa Nothing)
_short_enough = _short_enough && _len >= l
Expand Down Expand Up @@ -638,8 +636,6 @@ function ___enumerate_cholesky(::Type{Vector}, Q::Matrix{S}, l::Union{Int, Nothi
end
end

@noinline _bound_error() = error("Overflow in bound computation")

@inline function _compute_bounds(Ti::QQFieldElem, Qi, Ui, t1 = QQFieldElem(),
t2 = ZZRingElem(),
t3 = ZZRingElem(),
Expand All @@ -649,20 +645,18 @@ end
t2 = floor!(t2, t1, t3, t4)
t2 = isqrt!(t2, t2)
# Now t2 = ceil(sqrt(Ti/Qi))
t2 = add_two!(t2, t2)
t2 = add!(t2, 2)
# Now t2 = ceil(sqrt(Ti/Qi)) + 2

t1 = sub!(t1, Ui, t2)
t1 = neg!(t1, t1)
t1 = neg!(t1)
t5 = ceil!(t5, t1, t3, t4)
!fits(Int, t5) && _bound_error()
ub = ccall((:fmpz_get_si, libflint), Int, (Ref{ZZRingElem}, ), t5)
ub = Int(t5) # will throw if t5 does not fit into an Int

t1 = add!(t1, Ui, t2)
t1 = neg!(t1, t1)
t1 = neg!(t1)
t5 = floor!(t5, t1, t3, t4)
!fits(Int, t5) && _bound_error()
lb = ccall((:fmpz_get_si, libflint), Int, (Ref{ZZRingElem}, ), t5) - 1
lb = Int(t5) - 1 # will throw if t5 does not fit into an Int

return ub, lb
end
Expand Down Expand Up @@ -707,10 +701,10 @@ end
@inline function update_U!(U, QQ::Matrix{QQFieldElem}, i, n, x, t = QQFieldElem(), t2 = ZZRingElem())
# U[i] = sum(QQ[i, j] * x[j] for j in (i + 1):n)
s = @inbounds U[i]
@inbounds ccall((:fmpz_set_si, libflint), Cvoid, (Ref{ZZRingElem}, Int), t2, x[i + 1])
@inbounds set!(t2, x[i + 1])
@inbounds s = mul!(s, QQ[i, i + 1], t2)
for j in (i + 2):n
@inbounds ccall((:fmpz_set_si, libflint), Cvoid, (Ref{ZZRingElem}, Int), t2, x[j])
@inbounds set!(t2, x[j])
@inbounds mul!(t, QQ[i, j], t2)
s = add!(s, s, t)
#addmul!(s, QQ[i, j], t2, t)
Expand Down Expand Up @@ -1021,16 +1015,6 @@ end
#
################################################################################

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

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

@inline function floor!(z::ZZRingElem, y::QQFieldElem, t0::ZZRingElem, t1::ZZRingElem)
numerator!(t0, y)
denominator!(t1, y)
Expand All @@ -1045,11 +1029,6 @@ end
return z
end

@inline function add_two!(z::ZZRingElem, x::ZZRingElem)
add!(z, x, 2)
return z
end

@inline function isqrt!(z::ZZRingElem, a::ZZRingElem)
ccall((:fmpz_sqrt, libflint), Cvoid, (Ref{ZZRingElem}, Ref{ZZRingElem}), z, a)
return z
Expand All @@ -1059,8 +1038,4 @@ floor!(z::Int, x::Rational{Int}, y::Int, w::Int) = Int(floor(x))

isqrt!(z::Int, x::Int) = isqrt(x)

add_two!(z::Int, x::Int) = x + 2

ceil!(z::Int, x::Rational{Int}, y::Int, w::Int) = Int(ceil(x))

numerator!(z::Int, x::Rational{Int}) = numerator(x)

0 comments on commit 276ac56

Please sign in to comment.