From 5b2186c0df247a49a4d9bd728e6d61ef4af27ad1 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Thu, 19 Dec 2024 19:24:32 +0100 Subject: [PATCH] Move some more things out of HeckeMoreStuff.jl (#1983) --- src/HeckeMoreStuff.jl | 43 ------------------------------------------- src/antic/nf_elem.jl | 35 +++++++++++++++++++++++++++++------ src/flint/fmpz.jl | 9 +++++++++ src/flint/fmpz_mod.jl | 24 ++++++++++++++++++++++++ src/flint/gfp_poly.jl | 8 ++++++-- 5 files changed, 68 insertions(+), 51 deletions(-) diff --git a/src/HeckeMoreStuff.jl b/src/HeckeMoreStuff.jl index 0bb8a65a5..994514333 100644 --- a/src/HeckeMoreStuff.jl +++ b/src/HeckeMoreStuff.jl @@ -225,11 +225,6 @@ function is_squarefree(x::Generic.Poly{AbsSimpleNumFieldElem}) return isone(gcd(x, derivative(x), true)) end -### - -(::QQField)(a::AbsSimpleNumFieldElem) = (is_rational(a) && return coeff(a, 0)) || error("not a rational") -(::ZZRing)(a::AbsSimpleNumFieldElem) = (is_integer(a) && return numerator(coeff(a, 0))) || error("not an integer") - ################################################################################ # # Base case for dot products @@ -312,16 +307,6 @@ function numerator(a::AbsSimpleNumFieldElem) return z end -function divexact!(z::AbsSimpleNumFieldElem, x::AbsSimpleNumFieldElem, y::ZZRingElem) - @ccall libflint.nf_elem_scalar_div_fmpz(z::Ref{AbsSimpleNumFieldElem}, x::Ref{AbsSimpleNumFieldElem}, y::Ref{ZZRingElem}, parent(x)::Ref{AbsSimpleNumField})::Nothing - return z -end - -function sub!(a::AbsSimpleNumFieldElem, b::AbsSimpleNumFieldElem, c::AbsSimpleNumFieldElem) - @ccall libflint.nf_elem_sub(a::Ref{AbsSimpleNumFieldElem}, b::Ref{AbsSimpleNumFieldElem}, c::Ref{AbsSimpleNumFieldElem}, a.parent::Ref{AbsSimpleNumField})::Nothing - return a -end - function lift(R::ZZAbsPowerSeriesRing, f::ZZModAbsPowerSeriesRingElem) r = R() for i = 0:length(f)-1 @@ -702,11 +687,6 @@ function evaluate(f::QQPolyRingElem, a::AbsSimpleNumFieldElem) return s end -function rem!(z::fpPolyRingElem, a::fpPolyRingElem, b::fpPolyRingElem) - @ccall libflint.nmod_poly_rem(z::Ref{fpPolyRingElem}, a::Ref{fpPolyRingElem}, b::Ref{fpPolyRingElem}, (pointer_from_objref(base_ring(z)) + sizeof(ZZRingElem))::Ptr{Nothing})::Nothing - return z -end - function preimage(M::Map{D,C}, a) where {D,C} if isdefined(M.header, :preimage) p = M.header.preimage(a)::elem_type(D) @@ -832,24 +812,6 @@ function (R::QQPolyRing)(a::Generic.RationalFunctionFieldElem{QQFieldElem}) return R(numerator(a)) end -function Base.divrem(a::ZZModRingElem, b::ZZModRingElem) - R = parent(a) - r = rem(a, b) - return divexact(a - r, b), r -end - -function Base.div(a::ZZModRingElem, b::ZZModRingElem) - R = parent(a) - r = rem(a, b) - return divexact(a - r, b) -end - -function Base.rem(a::ZZModRingElem, b::ZZModRingElem) - R = parent(a) - r = R(rem(lift(a), gcd(modulus(R), lift(b)))) - return r -end - @doc raw""" zeros(f::ZZPolyRingElem) -> Vector{ZZRingElem} @@ -1081,11 +1043,6 @@ function (R::FqPolyRepField)(x::FpPolyRingElem) return z end -@inline function rem!(a::ZZRingElem, b::ZZRingElem, c::ZZRingElem) - @ccall libflint.fmpz_mod(a::Ref{ZZRingElem}, b::Ref{ZZRingElem}, c::Ref{ZZRingElem})::Nothing - return a -end - function rem!(a::ZZModPolyRingElem, b::ZZModPolyRingElem, c::ZZModPolyRingElem) @ccall libflint.fmpz_mod_poly_rem(a::Ref{ZZModPolyRingElem}, b::Ref{ZZModPolyRingElem}, c::Ref{ZZModPolyRingElem}, a.parent.base_ring.ninv::Ref{fmpz_mod_ctx_struct})::Nothing return a diff --git a/src/antic/nf_elem.jl b/src/antic/nf_elem.jl index da613c6ab..312f88eff 100644 --- a/src/antic/nf_elem.jl +++ b/src/antic/nf_elem.jl @@ -262,24 +262,21 @@ function +(a::AbsSimpleNumFieldElem, b::AbsSimpleNumFieldElem) parent(a) == parent(b) || return force_op(+, a, b)::AbsSimpleNumFieldElem check_parent(a, b) r = a.parent() - @ccall libflint.nf_elem_add(r::Ref{AbsSimpleNumFieldElem}, a::Ref{AbsSimpleNumFieldElem}, b::Ref{AbsSimpleNumFieldElem}, a.parent::Ref{AbsSimpleNumField})::Nothing - return r + return add!(r, a, b) end function -(a::AbsSimpleNumFieldElem, b::AbsSimpleNumFieldElem) parent(a) == parent(b) || return force_op(-, a, b)::AbsSimpleNumFieldElem check_parent(a, b) r = a.parent() - @ccall libflint.nf_elem_sub(r::Ref{AbsSimpleNumFieldElem}, a::Ref{AbsSimpleNumFieldElem}, b::Ref{AbsSimpleNumFieldElem}, a.parent::Ref{AbsSimpleNumField})::Nothing - return r + return sub!(r, a, b) end function *(a::AbsSimpleNumFieldElem, b::AbsSimpleNumFieldElem) parent(a) == parent(b) || return force_op(*, a, b)::AbsSimpleNumFieldElem check_parent(a, b) r = a.parent() - @ccall libflint.nf_elem_mul(r::Ref{AbsSimpleNumFieldElem}, a::Ref{AbsSimpleNumFieldElem}, b::Ref{AbsSimpleNumFieldElem}, a.parent::Ref{AbsSimpleNumField})::Nothing - return r + return mul!(r, a, b) end ############################################################################### @@ -724,6 +721,11 @@ end add!(c::AbsSimpleNumFieldElem, a::AbsSimpleNumFieldElem, b::Integer) = add!(c, a, flintify(b)) +function sub!(a::AbsSimpleNumFieldElem, b::AbsSimpleNumFieldElem, c::AbsSimpleNumFieldElem) + @ccall libflint.nf_elem_sub(a::Ref{AbsSimpleNumFieldElem}, b::Ref{AbsSimpleNumFieldElem}, c::Ref{AbsSimpleNumFieldElem}, a.parent::Ref{AbsSimpleNumField})::Nothing + return a +end + function sub!(c::AbsSimpleNumFieldElem, a::AbsSimpleNumFieldElem, b::QQFieldElem) @ccall libflint.nf_elem_sub_fmpq(c::Ref{AbsSimpleNumFieldElem}, a::Ref{AbsSimpleNumFieldElem}, b::Ref{QQFieldElem}, a.parent::Ref{AbsSimpleNumField})::Nothing return c @@ -775,6 +777,23 @@ end mul!(c::AbsSimpleNumFieldElem, a::AbsSimpleNumFieldElem, b::Integer) = mul!(c, a, flintify(b)) +function divexact!(z::AbsSimpleNumFieldElem, x::AbsSimpleNumFieldElem, y::Int) + @ccall libflint.nf_elem_scalar_div_si(r::Ref{AbsSimpleNumFieldElem}, a::Ref{AbsSimpleNumFieldElem}, b::Int, parent(x)::Ref{AbsSimpleNumField})::Nothing + return z +end + +function divexact!(z::AbsSimpleNumFieldElem, x::AbsSimpleNumFieldElem, y::ZZRingElem) + @ccall libflint.nf_elem_scalar_div_fmpz(z::Ref{AbsSimpleNumFieldElem}, x::Ref{AbsSimpleNumFieldElem}, y::Ref{ZZRingElem}, parent(x)::Ref{AbsSimpleNumField})::Nothing + return z +end + +function divexact!(z::AbsSimpleNumFieldElem, x::AbsSimpleNumFieldElem, y::QQFieldElem) + @ccall libflint.nf_elem_scalar_div_fmpq(z::Ref{AbsSimpleNumFieldElem}, x::Ref{AbsSimpleNumFieldElem}, y::Ref{QQFieldElem}, parent(x)::Ref{AbsSimpleNumField})::Nothing + return z +end + +divexact!(z::AbsSimpleNumFieldElem, x::AbsSimpleNumFieldElem, y::RationalUnion) = divexact!(z, x, flintify(y)) + ############################################################################### # # Speedups for polynomials over number fields @@ -1016,6 +1035,10 @@ function (a::QQPolyRing)(b::AbsSimpleNumFieldElem) return r end +(::QQField)(a::AbsSimpleNumFieldElem) = (is_rational(a) && return coeff(a, 0)) || error("not a rational") + +(::ZZRing)(a::AbsSimpleNumFieldElem) = (is_integer(a) && return numerator(coeff(a, 0))) || error("not an integer") + ############################################################################### # # Random generation diff --git a/src/flint/fmpz.jl b/src/flint/fmpz.jl index 7c811794f..84dc6fddc 100644 --- a/src/flint/fmpz.jl +++ b/src/flint/fmpz.jl @@ -482,6 +482,7 @@ function rem(x::ZZRingElemOrPtr, ::Type{UInt64}) end function rem(x::ZZRingElem, c::ZZRingElem) + # FIXME: it seems `rem` and `rem!` for `ZZRingElem` do different things? q, r = Base.divrem(x, c) return r end @@ -2609,6 +2610,14 @@ divexact!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::Integer) = divexact!(z, a, # +function rem!(a::ZZRingElem, b::ZZRingElem, c::ZZRingElem) + # FIXME: it seems `rem` and `rem!` for `ZZRingElem` do different things? + @ccall libflint.fmpz_mod(a::Ref{ZZRingElem}, b::Ref{ZZRingElem}, c::Ref{ZZRingElem})::Nothing + return a +end + +# + function pow!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::Integer) @ccall libflint.fmpz_pow_ui(z::Ref{ZZRingElem}, a::Ref{ZZRingElem}, UInt(b)::UInt)::Nothing return z diff --git a/src/flint/fmpz_mod.jl b/src/flint/fmpz_mod.jl index 596037b96..2c36f81c5 100644 --- a/src/flint/fmpz_mod.jl +++ b/src/flint/fmpz_mod.jl @@ -294,6 +294,30 @@ function divides(a::ZZModRingElem, b::ZZModRingElem) return true, rr end +############################################################################### +# +# Division with remainder +# +############################################################################### + +function Base.divrem(a::ZZModRingElem, b::ZZModRingElem) + R = parent(a) + r = rem(a, b) + return divexact(a - r, b), r +end + +function Base.div(a::ZZModRingElem, b::ZZModRingElem) + R = parent(a) + r = rem(a, b) + return divexact(a - r, b) +end + +function Base.rem(a::ZZModRingElem, b::ZZModRingElem) + R = parent(a) + r = R(rem(lift(a), gcd(modulus(R), lift(b)))) + return r +end + ############################################################################### # # GCD diff --git a/src/flint/gfp_poly.jl b/src/flint/gfp_poly.jl index cb1aaddc9..36d68ce52 100644 --- a/src/flint/gfp_poly.jl +++ b/src/flint/gfp_poly.jl @@ -193,12 +193,16 @@ end # ################################################################################ +function rem!(z::fpPolyRingElem, x::fpPolyRingElem, y::fpPolyRingElem) + @ccall libflint.nmod_poly_rem(z::Ref{fpPolyRingElem}, x::Ref{fpPolyRingElem}, y::Ref{fpPolyRingElem})::Nothing + return z +end + function rem(x::fpPolyRingElem, y::fpPolyRingElem) check_parent(x,y) iszero(y) && throw(DivideError()) z = parent(x)() - @ccall libflint.nmod_poly_rem(z::Ref{fpPolyRingElem}, x::Ref{fpPolyRingElem}, y::Ref{fpPolyRingElem})::Nothing - return z + return rem!(z, x, y) end ################################################################################