From 276ac56da998fb5ace7239e82cc241009619682a Mon Sep 17 00:00:00 2001 From: Max Horn Date: Thu, 17 Oct 2024 22:42:24 +0200 Subject: [PATCH] Use set!, zero!, etc. in YET more places (#1650) Also switch to Nemo.denominator! and Nemo.numerator!, and remove add_two! which now is equivalent to `add!(x, 2)`. --- examples/Det.jl | 6 +-- src/AlgAss/Elem.jl | 2 +- src/AlgAss/Map.jl | 2 +- src/AlgAssAbsOrd/PIP/bley_johnston.jl | 14 +++---- src/Hecke.jl | 2 +- src/LinearAlgebra/LatEnum.jl | 4 +- src/Misc/CRT.jl | 6 +-- src/Misc/Matrix.jl | 29 +++++++-------- src/NumFieldOrd/NfOrd/Ideal/Arithmetic.jl | 2 +- src/NumFieldOrd/NfOrd/ResidueRing.jl | 6 +-- src/QuadForm/Enumeration.jl | 45 +++++------------------ 11 files changed, 46 insertions(+), 72 deletions(-) diff --git a/examples/Det.jl b/examples/Det.jl index 390f6b0014..2fc89c2108 100644 --- a/examples/Det.jl +++ b/examples/Det.jl @@ -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 @@ -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 @@ -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 diff --git a/src/AlgAss/Elem.jl b/src/AlgAss/Elem.jl index 86eefa2243..6ea54af3b3 100644 --- a/src/AlgAss/Elem.jl +++ b/src/AlgAss/Elem.jl @@ -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 diff --git a/src/AlgAss/Map.jl b/src/AlgAss/Map.jl index 79fb4a2f30..34cb6cb0e8 100644 --- a/src/AlgAss/Map.jl +++ b/src/AlgAss/Map.jl @@ -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 diff --git a/src/AlgAssAbsOrd/PIP/bley_johnston.jl b/src/AlgAssAbsOrd/PIP/bley_johnston.jl index fdc8fa7675..ed302d9e3c 100644 --- a/src/AlgAssAbsOrd/PIP/bley_johnston.jl +++ b/src/AlgAssAbsOrd/PIP/bley_johnston.jl @@ -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 @@ -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) @@ -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) @@ -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 @@ -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 diff --git a/src/Hecke.jl b/src/Hecke.jl index 7bf61b03f6..19b97db45d 100644 --- a/src/Hecke.jl +++ b/src/Hecke.jl @@ -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() diff --git a/src/LinearAlgebra/LatEnum.jl b/src/LinearAlgebra/LatEnum.jl index 1afa615224..a48683a838 100644 --- a/src/LinearAlgebra/LatEnum.jl +++ b/src/LinearAlgebra/LatEnum.jl @@ -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} diff --git a/src/Misc/CRT.jl b/src/Misc/CRT.jl index 46605fce8f..5a11ff495d 100644 --- a/src/Misc/CRT.jl +++ b/src/Misc/CRT.jl @@ -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. diff --git a/src/Misc/Matrix.jl b/src/Misc/Matrix.jl index d41a6786b9..74d2c17f4b 100644 --- a/src/Misc/Matrix.jl +++ b/src/Misc/Matrix.jl @@ -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 @@ -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 @@ -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)) @@ -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 @@ -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 diff --git a/src/NumFieldOrd/NfOrd/Ideal/Arithmetic.jl b/src/NumFieldOrd/NfOrd/Ideal/Arithmetic.jl index f2e35fbfb6..42a0f89085 100644 --- a/src/NumFieldOrd/NfOrd/Ideal/Arithmetic.jl +++ b/src/NumFieldOrd/NfOrd/Ideal/Arithmetic.jl @@ -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 diff --git a/src/NumFieldOrd/NfOrd/ResidueRing.jl b/src/NumFieldOrd/NfOrd/ResidueRing.jl index fa90a34f9b..95fa452def 100644 --- a/src/NumFieldOrd/NfOrd/ResidueRing.jl +++ b/src/NumFieldOrd/NfOrd/ResidueRing.jl @@ -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 @@ -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 diff --git a/src/QuadForm/Enumeration.jl b/src/QuadForm/Enumeration.jl index 11a1028d9a..da2105dab8 100644 --- a/src/QuadForm/Enumeration.jl +++ b/src/QuadForm/Enumeration.jl @@ -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) @@ -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 @@ -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 @@ -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(), @@ -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 @@ -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) @@ -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) @@ -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 @@ -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)