From ae4705674c6a8b44beddfa5ab7a01bcb497408c3 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Thu, 17 Oct 2024 14:39:41 +0200 Subject: [PATCH 1/2] Unsafe ops for ZZRingElem, QQFieldElem Move flintify calls down into unsafe ops Also reorder method defs to add,sub,mul --- src/flint/fmpq.jl | 111 ++++++++++++++++++++++++---------------------- src/flint/fmpz.jl | 51 ++++++++++++--------- 2 files changed, 88 insertions(+), 74 deletions(-) diff --git a/src/flint/fmpq.jl b/src/flint/fmpq.jl index 74c442222..5b967d83b 100644 --- a/src/flint/fmpq.jl +++ b/src/flint/fmpq.jl @@ -295,7 +295,7 @@ conj(x::QQFieldElem) = x for T in (ZZRingElem, Int, UInt) @eval begin +(a::QQFieldElem, b::$T) = add!(QQFieldElem(), a, b) - +(a::$T, b::QQFieldElem) = b + a + +(a::$T, b::QQFieldElem) = add!(QQFieldElem(), a, b) end end @@ -308,7 +308,7 @@ end for T in (ZZRingElem, Int, UInt) @eval begin -(a::QQFieldElem, b::$T) = sub!(QQFieldElem(), a, b) - -(a::$T, b::QQFieldElem) = neg!(b - a) + -(a::$T, b::QQFieldElem) = sub!(QQFieldElem(), a, b) end end @@ -318,19 +318,13 @@ end -(a::QQFieldElem, b::Rational{T}) where {T <: Integer} = a - QQFieldElem(b) -(a::Rational{T}, b::QQFieldElem) where {T <: Integer} = QQFieldElem(a) - b -for T in (ZZRingElem, Int, UInt) +for T in (ZZRingElem, Int, UInt, Integer, Rational) @eval begin *(a::QQFieldElem, b::$T) = mul!(QQFieldElem(), a, b) - *(a::$T, b::QQFieldElem) = b * a + *(a::$T, b::QQFieldElem) = mul!(QQFieldElem(), a, b) end end -*(a::QQFieldElem, b::Integer) = a * flintify(b) -*(a::Integer, b::QQFieldElem) = b * a - -*(a::QQFieldElem, b::Rational{T}) where {T <: Integer} = a * QQFieldElem(b) -*(a::Rational{T}, b::QQFieldElem) where {T <: Integer} = b * a - *(a::QQFieldElem, b::AbstractFloat) = Rational(a) * b *(a::AbstractFloat, b::QQFieldElem) = a * Rational(b) @@ -1071,7 +1065,7 @@ function set!(c::QQFieldElemOrPtr, a::ZZRingElemOrPtr, b::ZZRingElemOrPtr) return c end -function set!(c::QQFieldElemOrPtr, a::ZZRingElemOrPtr) +function set!(c::QQFieldElemOrPtr, a::Union{Integer,ZZRingElemOrPtr}) GC.@preserve c begin set!(_num_ptr(c), a) one!(_den_ptr(c)) @@ -1079,12 +1073,6 @@ function set!(c::QQFieldElemOrPtr, a::ZZRingElemOrPtr) return c end -function set!(c::Ptr{QQFieldElem}, a::ZZRingElemOrPtr) - set!(_num_ptr(c), a) - one!(_den_ptr(c)) - return c -end - function numerator!(z::ZZRingElem, y::QQFieldElem) ccall((:fmpq_numerator, libflint), Cvoid, (Ref{ZZRingElem}, Ref{QQFieldElem}), z, y) return z @@ -1095,37 +1083,7 @@ function denominator!(z::ZZRingElem, y::QQFieldElem) return z end -function mul!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::QQFieldElemOrPtr) - ccall((:fmpq_mul, libflint), Nothing, - (Ref{QQFieldElem}, Ref{QQFieldElem}, Ref{QQFieldElem}), c, a, b) - return c -end - -function mul!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::ZZRingElemOrPtr) - ccall((:fmpq_mul_fmpz, libflint), Nothing, - (Ref{QQFieldElem}, Ref{QQFieldElem}, Ref{ZZRingElem}), c, a, b) - return c -end - -function mul!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::Int) - ccall((:fmpq_mul_si, libflint), Nothing, - (Ref{QQFieldElem}, Ref{QQFieldElem}, Int), c, a, b) - return c -end - -function mul!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::UInt) - ccall((:fmpq_mul_ui, libflint), Nothing, - (Ref{QQFieldElem}, Ref{QQFieldElem}, Int), c, a, b) - return c -end - -mul!(c::QQFieldElemOrPtr, a::Union{ZZRingElemOrPtr, Int, UInt}, b::QQFieldElemOrPtr) = mul!(c, b, a) - -function addmul!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::QQFieldElemOrPtr) - ccall((:fmpq_addmul, libflint), Nothing, - (Ref{QQFieldElem}, Ref{QQFieldElem}, Ref{QQFieldElem}), c, a, b) - return c -end +# function add!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::QQFieldElemOrPtr) ccall((:fmpq_add, libflint), Nothing, @@ -1151,13 +1109,10 @@ function add!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::UInt) return c end -add!(c::QQFieldElemOrPtr, a::Union{ZZRingElemOrPtr, Int, UInt}, b::QQFieldElemOrPtr) = add!(c, b, a) +add!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::Union{Integer, Rational}) = add!(c, a, flintify(b)) +add!(c::QQFieldElemOrPtr, a::Union{ZZRingElemOrPtr, Integer, Rational}, b::QQFieldElemOrPtr) = add!(c, b, a) -function submul!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::QQFieldElemOrPtr) - ccall((:fmpq_submul, libflint), Nothing, - (Ref{QQFieldElem}, Ref{QQFieldElem}, Ref{QQFieldElem}), c, a, b) - return c -end +# function sub!(z::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::QQFieldElemOrPtr) ccall((:fmpq_sub, libflint), Nothing, (Ref{QQFieldElem}, Ref{QQFieldElem}, Ref{QQFieldElem}), z, a, b) @@ -1182,6 +1137,54 @@ function sub!(z::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::UInt) return z end +sub!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::Union{Integer, Rational}) = sub!(c, a, flintify(b)) +sub!(c::QQFieldElemOrPtr, a::Union{ZZRingElemOrPtr, Integer, Rational}, b::QQFieldElemOrPtr) = neg!(sub!(c, b, a)) + +# + +function mul!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::QQFieldElemOrPtr) + ccall((:fmpq_mul, libflint), Nothing, + (Ref{QQFieldElem}, Ref{QQFieldElem}, Ref{QQFieldElem}), c, a, b) + return c +end + +function mul!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::ZZRingElemOrPtr) + ccall((:fmpq_mul_fmpz, libflint), Nothing, + (Ref{QQFieldElem}, Ref{QQFieldElem}, Ref{ZZRingElem}), c, a, b) + return c +end + +function mul!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::Int) + ccall((:fmpq_mul_si, libflint), Nothing, + (Ref{QQFieldElem}, Ref{QQFieldElem}, Int), c, a, b) + return c +end + +function mul!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::UInt) + ccall((:fmpq_mul_ui, libflint), Nothing, + (Ref{QQFieldElem}, Ref{QQFieldElem}, Int), c, a, b) + return c +end + +mul!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::Union{Integer, Rational}) = mul!(c, a, flintify(b)) +mul!(c::QQFieldElemOrPtr, a::Union{ZZRingElemOrPtr, Integer, Rational}, b::QQFieldElemOrPtr) = mul!(c, b, a) + +# + +function addmul!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::QQFieldElemOrPtr) + ccall((:fmpq_addmul, libflint), Nothing, + (Ref{QQFieldElem}, Ref{QQFieldElem}, Ref{QQFieldElem}), c, a, b) + return c +end + +function submul!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::QQFieldElemOrPtr) + ccall((:fmpq_submul, libflint), Nothing, + (Ref{QQFieldElem}, Ref{QQFieldElem}, Ref{QQFieldElem}), c, a, b) + return c +end + +# + function divexact!(z::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::QQFieldElemOrPtr) ccall((:fmpq_div, libflint), Nothing, (Ref{QQFieldElem}, Ref{QQFieldElem}, Ref{QQFieldElem}), z, a, b) return z diff --git a/src/flint/fmpz.jl b/src/flint/fmpz.jl index 889871d7b..d4e2108b2 100644 --- a/src/flint/fmpz.jl +++ b/src/flint/fmpz.jl @@ -2544,12 +2544,16 @@ function set!(z::ZZRingElemOrPtr, a::UInt) return z end +set!(z::ZZRingElemOrPtr, a::Integer) = set!(z, flintify(a)) + function swap!(a::ZZRingElemOrPtr, b::ZZRingElemOrPtr) ccall((:fmpz_swap, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}), a, b) end +# + function add!(z::ZZRingElemOrPtr, x::ZZRingElemOrPtr, y::ZZRingElemOrPtr) ccall((:fmpz_add, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}), z, x, y) @@ -2574,7 +2578,9 @@ function add!(a::ZZRingElemOrPtr, b::ZZRingElemOrPtr, c::Ptr{Int}) end add!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::Integer) = add!(z, a, flintify(b)) -add!(z::ZZRingElemOrPtr, x::Int, y::ZZRingElemOrPtr) = add!(z, y, x) +add!(z::ZZRingElemOrPtr, x::Integer, y::ZZRingElemOrPtr) = add!(z, y, x) + +# function sub!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::ZZRingElemOrPtr) ccall((:fmpz_sub, libflint), Nothing, @@ -2597,15 +2603,10 @@ function sub!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::UInt) return z end -function sub!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::Integer) - return sub!(z, a, flintify(b)) -end - -function sub!(z::ZZRingElemOrPtr, b::Integer, a::ZZRingElemOrPtr) - sub!(z, a, b) - return neg!(z, z) -end +sub!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::Integer) = sub!(z, a, flintify(b)) +sub!(z::ZZRingElemOrPtr, a::Integer, b::ZZRingElemOrPtr) = neg!(sub!(z, b, a)) +# function mul!(z::ZZRingElemOrPtr, x::ZZRingElemOrPtr, y::ZZRingElemOrPtr) ccall((:fmpz_mul, libflint), Nothing, @@ -2626,7 +2627,6 @@ function mul!(z::ZZRingElemOrPtr, x::ZZRingElemOrPtr, y::UInt) end mul!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::Integer) = mul!(z, a, flintify(b)) - mul!(z::ZZRingElemOrPtr, x::Integer, y::ZZRingElemOrPtr) = mul!(z, y, x) function mul!(a::ZZRingElemOrPtr, b::ZZRingElemOrPtr, c::Ptr{Int}) @@ -2634,6 +2634,8 @@ function mul!(a::ZZRingElemOrPtr, b::ZZRingElemOrPtr, c::Ptr{Int}) return a end +# + function addmul!(z::ZZRingElemOrPtr, x::ZZRingElemOrPtr, y::ZZRingElemOrPtr) ccall((:fmpz_addmul, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}), z, x, y) @@ -2652,6 +2654,9 @@ function addmul!(z::ZZRingElemOrPtr, x::ZZRingElemOrPtr, y::UInt) return z end +addmul!(z::ZZRingElemOrPtr, x::ZZRingElemOrPtr, y::Integer) = addmul!(z, x, flintify(y)) +addmul!(z::ZZRingElemOrPtr, x::Integer, y::ZZRingElemOrPtr) = addmul!(z, y, x) + # ignore fourth argument addmul!(z::ZZRingElemOrPtr, x::ZZRingElemOrPtr, y::Union{ZZRingElemOrPtr,Integer}, ::ZZRingElemOrPtr) = addmul!(z, x, y) @@ -2673,6 +2678,8 @@ function submul!(z::ZZRingElemOrPtr, x::ZZRingElemOrPtr, y::UInt) return z end +submul!(z::ZZRingElemOrPtr, x::ZZRingElemOrPtr, y::Integer) = submul!(z, x, flintify(y)) + # ignore fourth argument submul!(z::ZZRingElemOrPtr, x::ZZRingElemOrPtr, y::Union{ZZRingElemOrPtr,Integer}, ::ZZRingElemOrPtr) = submul!(z, x, y) @@ -2698,24 +2705,28 @@ function fmms!(r::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::ZZRingElemOrPtr, c::ZZ return r end +# -function divexact!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::UInt) - ccall((:fmpz_divexact_ui, libflint), Nothing, - (Ref{ZZRingElem}, Ref{ZZRingElem}, UInt), - z, a, b) +function divexact!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::ZZRingElemOrPtr) + @ccall libflint.fmpz_divexact(z::Ref{ZZRingElem}, a::Ref{ZZRingElem}, b::Ref{ZZRingElem})::Nothing return z end -function divexact!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::ZZRingElemOrPtr) - ccall((:fmpz_divexact, libflint), Nothing, - (Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}), - z, a, b) +function divexact!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::Int) + @ccall libflint.fmpz_divexact_si(z::Ref{ZZRingElem}, a::Ref{ZZRingElem}, b::Int)::Nothing return z end -divexact!(z::ZZRingElemOrPtr, b::ZZRingElemOrPtr) = divexact!(z, z, b) +function divexact!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::UInt) + @ccall libflint.fmpz_divexact_ui(z::Ref{ZZRingElem}, a::Ref{ZZRingElem}, b::UInt)::Nothing + return z +end + +divexact!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::Integer) = divexact!(z, a, flintify(b)) + +# -function pow!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::Union{Int, UInt}) +function pow!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::Integer) ccall((:fmpz_pow_ui, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}, UInt), z, a, UInt(b)) From 9a55fbf7446c6b5b3771917871ff1253d6a44da3 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Thu, 17 Oct 2024 15:44:06 +0200 Subject: [PATCH 2/2] Update src/flint/fmpq.jl --- src/flint/fmpq.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/flint/fmpq.jl b/src/flint/fmpq.jl index 5b967d83b..4c0edb443 100644 --- a/src/flint/fmpq.jl +++ b/src/flint/fmpq.jl @@ -1162,7 +1162,7 @@ end function mul!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::UInt) ccall((:fmpq_mul_ui, libflint), Nothing, - (Ref{QQFieldElem}, Ref{QQFieldElem}, Int), c, a, b) + (Ref{QQFieldElem}, Ref{QQFieldElem}, UInt), c, a, b) return c end