Skip to content

Commit

Permalink
Use set!, zero!, etc. in yet more places (#1649)
Browse files Browse the repository at this point in the history
  • Loading branch information
fingolfin authored Oct 16, 2024
1 parent 29108df commit 4ffee6e
Show file tree
Hide file tree
Showing 16 changed files with 38 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ LazyArtifacts = "1.6"
Libdl = "1.6"
LinearAlgebra = "1.6"
Markdown = "1.6"
Nemo = "^0.47.1"
Nemo = "^0.47.2"
Pkg = "1.6"
Polymake = "0.10, 0.11"
Printf = "1.6"
Expand Down
22 changes: 11 additions & 11 deletions examples/Det.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ function perturb!(A, n::Int = 100, c::AbstractRange = -10:10; side::Symbol = :bo
else
error("side has to be :right, :left or :both")
end
for i=1:n
for i=1:n
x = rand(c)
while iszero(x)
x = rand(c)
end
if rnd()
if rnd()
k = rand(1:nrows(A))
l = rand(1:nrows(A))
while l == k
Expand Down Expand Up @@ -583,7 +583,7 @@ function UniSolve(A::ZZMatrix, bb::ZZMatrix)
EntrySize = maximum(abs, A)
e = max(16, Int(2+ceil(2*log2(n)+log2(EntrySize))));
println("Modulus has $e bits");

B0 = zero_matrix(ZZ,n,n) ## will be computed by crt in loop below
m = ZZ(1); p = 2^29; # MAGIC NUMBER (initial prime, probably should be about 2^29?)
@time while (nbits(m) < e)
Expand Down Expand Up @@ -688,9 +688,9 @@ function renorm(U::ZZMatrix, m::ZZRingElem; start::Int = 1, last::Int = nrows(U)
R_ptr = Nemo.mat_entry_ptr(R, 1, 1)
U_ptr = Nemo.mat_entry_ptr(U, i, 1)
for j=1:ncols(U)
ccall((:fmpz_add, Nemo.libflint), Cvoid, (Ptr{ZZRingElem}, Ptr{ZZRingElem}, Ptr{ZZRingElem}), R_ptr, R_ptr, U_ptr)
add!(R_ptr, R_ptr, U_ptr)
mod_sym!(U_ptr, R_ptr, m, t)
ccall((:fmpz_sub, Nemo.libflint), Cvoid, (Ptr{ZZRingElem}, Ptr{ZZRingElem}, Ptr{ZZRingElem}), R_ptr, R_ptr, U_ptr)
sub!(R_ptr, R_ptr, U_ptr)
ccall((:fmpz_divexact, Nemo.libflint), Cvoid, (Ptr{ZZRingElem}, Ptr{ZZRingElem}, Ref{ZZRingElem}), R_ptr, R_ptr, m)
R_ptr += sizeof(Clong)
U_ptr += sizeof(Clong)
Expand All @@ -705,8 +705,8 @@ function renorm(U::ZZMatrix, m::ZZRingElem; start::Int = 1, last::Int = nrows(U)
U_ptr = Nemo.mat_entry_ptr(U, i, 1)
R_ptr = Nemo.mat_entry_ptr(R, 1, 1)
for j=1:ncols(U)
ccall((:fmpz_set, Nemo.libflint), Cvoid, (Ptr{ZZRingElem}, Ptr{ZZRingElem}), U_ptr, R_ptr)
ccall((:fmpz_zero, Nemo.libflint), Cvoid, (Ptr{ZZRingElem},), R_ptr)
set!(U_ptr, R_ptr)
zero!(R_ptr)
R_ptr += sizeof(Clong)
U_ptr += sizeof(Clong)
end
Expand Down Expand Up @@ -957,7 +957,7 @@ function DetS(A::ZZMatrix, U::AbstractArray= -100:100; use_rns::Bool = false)
isone(d) && break
d1 *= d
@show (Had - nbits(d1) - nbits(prod_p))/60
@show mod_sym(d1, prod_p) , det_p
@show mod_sym(d1, prod_p) , det_p
if Had - nbits(d1) < nbits(prod_p)
@show "at H-bound", (Had - nbits(d1) - nbits(prod_p))/60
return d1
Expand Down Expand Up @@ -1039,7 +1039,7 @@ function induce_rational_reconstruction(a::ZZMatrix, b::ZZRingElem)
A_ptr = Nemo.mat_entry_ptr(A, i, 1)
for j=1:ncols(a)
# @show i, j
ccall((:fmpz_set, Nemo.libflint), Cvoid, (Ref{ZZRingElem}, Ptr{ZZRingElem}), T, a_ptr)
set!(T, a_ptr)
Nemo.mul!(T, T, D)
Nemo.mod!(T, T, b)
fl = ratrec!(n, d, T, b)
Expand All @@ -1048,7 +1048,7 @@ function induce_rational_reconstruction(a::ZZMatrix, b::ZZRingElem)
D = D*d
Nemo.mul!(A, A, d)
end
ccall((:fmpz_set, Nemo.libflint), Cvoid, (Ptr{ZZRingElem}, Ref{ZZRingElem}), A_ptr, n)
set!(A_ptr, n)

a_ptr += sizeof(ZZRingElem)
A_ptr += sizeof(ZZRingElem)
Expand Down Expand Up @@ -1179,7 +1179,7 @@ function dixon_init(A::ZZMatrix, B::ZZMatrix, T::DataType = fpMatrix)
D.bound = 2*maximum(abs, [_D, _N])^2 * 2^30
D.crt_primes = UInt[]
D.A_mod = T[]

pr = ZZ(1)
xp = next_prime(p)
maxA = maximum(abs, A) *(p-1)*n*2
Expand Down
6 changes: 3 additions & 3 deletions src/AlgAss/AlgGrp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ function __set_row!(y::QQMatrix, k, c)
begin
for i in 1:length(c)
t = mat_entry_ptr(y, k, i)
ccall((:fmpq_set, libflint), Cvoid, (Ptr{QQFieldElem}, Ref{QQFieldElem}), t, c[i])
set!(t, c[i])
end
end
nothing
Expand All @@ -886,7 +886,7 @@ function __set_row!(c::Vector{QQFieldElem}, y::QQMatrix, k)
begin
for i in 1:length(c)
t = mat_entry_ptr(y, k, i)
ccall((:fmpq_set, libflint), Cvoid, (Ref{QQFieldElem}, Ptr{QQFieldElem}), c[i], t)
set!(c[i], t)
end
end
nothing
Expand All @@ -895,7 +895,7 @@ end
function __set!(y, k, c)
GC.@preserve y begin
t = mat_entry_ptr(y, 1, k)
ccall((:fmpq_set, libflint), Cvoid, (Ptr{QQFieldElem}, Ref{QQFieldElem}), t, c)
set!(t, c)
end
nothing
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 @@ -836,7 +836,7 @@ function elem_to_mat_row!(x::ZZMatrix, i::Int, d::ZZRingElem, a::AbstractAssocia
x[i, j] = z_q.num[1, j]
end

ccall((:fmpz_set, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}), d, z_q.den)
set!(d, z_q.den)
return nothing
end

Expand Down
3 changes: 2 additions & 1 deletion src/AlgAssRelOrd/Eichler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ function _eichler_find_transforming_unit(I::AlgAssRelOrdIdl, J::AlgAssRelOrdIdl)
end

function get_coeff_fmpz!(x::fqPolyRepFieldElem, n::Int, z::ZZRingElem)
ccall((:fmpz_set_ui, libflint), Nothing, (Ref{ZZRingElem}, UInt), z, ccall((:nmod_poly_get_coeff_ui, libflint), UInt, (Ref{fqPolyRepFieldElem}, Int), x, n))
c = ccall((:nmod_poly_get_coeff_ui, libflint), UInt, (Ref{fqPolyRepFieldElem}, Int), x, n)
set!(z, c)
return z
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
mat_entry_ptr, factor_trial_range, set!

AbstractAlgebra.@include_deprecated_bindings()
Nemo.@include_deprecated_bindings()
Expand Down
4 changes: 2 additions & 2 deletions src/LargeField/misc2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function local_norm!(n::ZZRingElem, ap::Vector{fqPolyRepFieldElem}, me::Hecke.mo
ccall((:fq_nmod_norm, libflint), Nothing, (Ref{ZZRingElem}, Ref{fqPolyRepFieldElem}, Ref{fqPolyRepField}), n, ap[j], ap[j].parent)
nn = ccall((:n_mulmod2_preinv, libflint), UInt, (UInt, UInt, UInt, UInt), nn, UInt(n), me.up, me.upinv)
end
ccall((:fmpz_set_ui, libflint), Nothing, (Ref{ZZRingElem}, UInt), n, nn)
set!(n, nn)
return n
end

Expand Down Expand Up @@ -259,7 +259,7 @@ function local_norm!(n::ZZRingElem, ap::zzModMatrix, me::Hecke.modular_env)
np = Nemo.getindex_raw(ap, j, 1)
nn = ccall((:n_mulmod2_preinv, libflint), UInt, (UInt, UInt, UInt, UInt), nn, np, me.up, me.upinv)
end
ccall((:fmpz_set_ui, libflint), Nothing, (Ref{ZZRingElem}, UInt), n, nn)
set!(n, nn)
return n
end

Expand Down
2 changes: 1 addition & 1 deletion src/LocalField/Conjugates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ function completion(K::AbsSimpleNumField, ca::QadicFieldElem)
# djj = lift_root(f, ajj, bjj, p, 10)
# d = K(parent(K.pol)(djj))
ccall((:nf_elem_set, libantic), Nothing, (Ref{AbsSimpleNumFieldElem}, Ref{AbsSimpleNumFieldElem}, Ref{AbsSimpleNumField}), c, d, K)
ccall((:fmpz_set_si, libflint), Nothing, (Ref{ZZRingElem}, Cint), pc, precision(x))
set!(pc, precision(x))
elseif precision(x) < pc
d = mod_sym(c, p^precision(x))
else
Expand Down
2 changes: 1 addition & 1 deletion src/LocalField/qAdic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ end

function getUnit(a::PadicFieldElem)
u = ZZRingElem()
ccall((:fmpz_set, libflint), Cvoid, (Ref{ZZRingElem}, Ref{Int}), u, a.u)
u = set!(u, a.u)
return u, a.v, a.N
end

Expand Down
12 changes: 6 additions & 6 deletions src/Misc/Matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ function _copy_matrix_into_matrix(A::ZZMatrix, i::Int, j::Int, B::ZZMatrix)
for l in 0:ncols(B) - 1
d = mat_entry_ptr(B, 1 + k, 1 + l)
t = mat_entry_ptr(A, i + k, j + l)
ccall((:fmpz_set, libflint), Nothing, (Ptr{ZZRingElem}, Ptr{ZZRingElem}), t, d)
set!(t, d)
end
end
end
Expand All @@ -342,7 +342,7 @@ function _copy_matrix_into_matrix(A::QQMatrix, i::Int, j::Int, B::QQMatrix)
for l in 0:ncols(B) - 1
d = mat_entry_ptr(B, 1 + k, 1 + l)
t = mat_entry_ptr(A, i + k, j + l)
ccall((:fmpq_set, libflint), Nothing, (Ptr{QQFieldElem}, Ptr{QQFieldElem}), t, d)
set!(t, d)
end
end
end
Expand Down Expand Up @@ -532,13 +532,13 @@ function snf_for_groups(A::ZZMatrix, mod::ZZRingElem)
Rik = mat_entry_ptr(R, i, k)
Rjk = mat_entry_ptr(R, j, k)
aux = ZZRingElem()
ccall((:fmpz_mul, libflint), Nothing, (Ref{ZZRingElem}, Ptr{ZZRingElem}, Ref{ZZRingElem}), aux, Rik, e)
mul!(aux, Rik, e)
ccall((:fmpz_addmul, libflint), Nothing, (Ref{ZZRingElem}, Ptr{ZZRingElem}, Ref{ZZRingElem}), aux, Rjk, f)
aux1 = ZZRingElem()
ccall((:fmpz_mul, libflint), Nothing, (Ref{ZZRingElem}, Ptr{ZZRingElem}, Ref{ZZRingElem}), aux1, Rjk, a)
mul!(aux1, Rjk, a)
ccall((:fmpz_submul, libflint), Nothing, (Ref{ZZRingElem}, Ptr{ZZRingElem}, Ref{ZZRingElem}), aux1, Rik, b)
ccall((:fmpz_set, libflint), Nothing, (Ptr{ZZRingElem}, Ref{ZZRingElem}), Rik, aux)
ccall((:fmpz_set, libflint), Nothing, (Ptr{ZZRingElem}, Ref{ZZRingElem}), Rjk, aux1)
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
Expand Down
4 changes: 2 additions & 2 deletions src/Misc/coprime.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ function copy_into!(a::MPolyRingElem, b::MPolyRingElem)
end

function copy_into!(a::ZZRingElem, b::ZZRingElem)
ccall((:fmpz_set, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}), a, b)
set!(a, b)
return a
end

function copy_into!(a::QQFieldElem, b::QQFieldElem)
ccall((:fmpq_set, libflint), Nothing, (Ref{QQFieldElem}, Ref{QQFieldElem}), a, b)
set!(a, b)
return a
end

Expand Down
2 changes: 1 addition & 1 deletion src/NumField/NfAbs/NonSimple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ function elem_to_mat_row!(M::ZZMatrix, i::Int, d::ZZRingElem, a::AbsNonSimpleNum
M[i, j] = z_q.num[1, j]
end

ccall((:fmpz_set, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}), d, z_q.den)
set!(d, z_q.den)

return nothing
end
Expand Down
2 changes: 1 addition & 1 deletion src/NumFieldOrd/NfOrd/Ideal/TwoElement.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function _assure_weakly_normal_presentation(A::AbsNumFieldOrderIdeal)
# Put the entries of B into the (1 x d)-Matrix m
for i in 1:degree(O)
s = mat_entry_ptr(m, 1, i)
ccall((:fmpz_set, libflint), Nothing, (Ptr{ZZRingElem}, Ref{ZZRingElem}), s, B[i])
set!(s, B[i])
end

if iszero(m)
Expand Down
2 changes: 1 addition & 1 deletion src/NumFieldOrd/NfOrd/MaxOrd/Polygons.jl
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ function find_random_second_gen(A::AbsNumFieldOrderIdeal{S, T}) where {S, T}
# Put the entries of B into the (1 x d)-Matrix m
for i in 1:degree(O)
s = mat_entry_ptr(m, 1, i)
ccall((:fmpz_set, libflint), Nothing, (Ptr{ZZRingElem}, Ref{ZZRingElem}), s, B[i])
set!(s, B[i])
end
if iszero(m)
continue
Expand Down
10 changes: 3 additions & 7 deletions src/QuadForm/Enumeration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ function ___enumerate_cholesky(::Type{Vector}, Q::Matrix{QQFieldElem}, l::Union{
if _short_enough
# t1 must be a UInt
#z = QQFieldElem()
#ccall((:fmpq_set, libflint), Cvoid, (Ref{QQFieldElem}, Ref{QQFieldElem}), z, t1)
#set!(z, t1)
# Todo
y = Vector{elem_type}(undef, n)
if S === elem_type
Expand Down Expand Up @@ -752,11 +752,7 @@ end
return s
end

function _deepcopy_cheap(x::QQFieldElem)
z = QQFieldElem()
ccall((:fmpq_set, libflint), Cvoid, (Ref{QQFieldElem}, Ref{QQFieldElem}), z, x)
return z
end
_deepcopy_cheap(x::QQFieldElem) = set!(QQFieldElem(), x)

function is_lessorequal(x::QQFieldElem, y::UInt)
c = ccall((:fmpq_cmp_ui, libflint), Cint, (Ref{QQFieldElem}, UInt), x, y)
Expand Down Expand Up @@ -1050,7 +1046,7 @@ end
end

@inline function add_two!(z::ZZRingElem, x::ZZRingElem)
ccall((:fmpz_add_ui, libflint), Cvoid, (Ref{ZZRingElem}, Ref{ZZRingElem}, Int), z, x, 2)
add!(z, x, 2)
return z
end

Expand Down
2 changes: 1 addition & 1 deletion src/Sparse/Matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ end

@inline function _get(x::Ptr{ZZRingElem})
z = ZZRingElem()
ccall((:fmpz_set, libflint), Cvoid, (Ref{ZZRingElem}, Ptr{ZZRingElem}), z, x)
set!(z, x)
return z
end

Expand Down

0 comments on commit 4ffee6e

Please sign in to comment.