From 55c35d90c8a36217b353daa0d540feac88ae3e60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Wed, 2 Oct 2024 22:51:35 +0200 Subject: [PATCH] Correct mutating op fallbacks (#1823) * Fix docstrings of `inv!` and `div!` * Move methods from `AA.*` to `Base.*` the `AA.*` should only implement methods for julia types and delegate everything else --- src/Fields.jl | 2 +- src/Poly.jl | 5 ----- src/fundamental_interface.jl | 27 ++++++++++++++++++++++++--- src/generic/LaurentMPoly.jl | 4 ++-- src/generic/Misc/Localization.jl | 4 ---- 5 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/Fields.jl b/src/Fields.jl index b659bb650f..68961924b5 100644 --- a/src/Fields.jl +++ b/src/Fields.jl @@ -16,7 +16,7 @@ is_zero_divisor(a::T) where T <: FieldElem = is_zero(a) Base.divrem(a::T, b::T) where {T <: FieldElem} = divexact(a, b), zero(parent(a)) -div(a::T, b::T) where {T <: FieldElem} = divexact(a, b) +Base.div(a::T, b::T) where {T <: FieldElem} = divexact(a, b) function gcd(x::T, y::T) where {T <: FieldElem} check_parent(x, y) diff --git a/src/Poly.jl b/src/Poly.jl index 2bacc1643f..f87db58c39 100644 --- a/src/Poly.jl +++ b/src/Poly.jl @@ -1479,11 +1479,6 @@ function Base.divrem(f::PolyRingElem{T}, g::PolyRingElem{T}) where T <: RingElem return q, f end -function Base.div(f::PolyRingElem{T}, g::PolyRingElem{T}) where T <: RingElement - q, r = divrem(f, g) - return q -end - ############################################################################## # # Ad hoc Euclidean division diff --git a/src/fundamental_interface.jl b/src/fundamental_interface.jl index 45124af686..38187c0128 100644 --- a/src/fundamental_interface.jl +++ b/src/fundamental_interface.jl @@ -328,11 +328,16 @@ neg!(a) = neg!(a, a) inv!(z, a) inv!(a) -Return `inv(a)`, possibly modifying the object `z` in the process. +Return `AbstractAlgebra.inv(a)`, possibly modifying the object `z` in the process. Aliasing is permitted. The unary version is a shorthand for `inv!(a, a)`. + +!!! note + `AbstractAlgebra.inv` and `Base.inv` differ only in their behavior on julia + types like `Integer` and `Rational{Int}`. The former makes it adhere to the + Ring interface. """ -inv!(z, a) = inv(a) +inv!(z, a) = AbstractAlgebra.inv(a) inv!(a) = inv!(a, a) for (name, op) in ((:add!, :+), (:sub!, :-), (:mul!, :*)) @@ -378,7 +383,7 @@ submul!(z, a, b) = submul!(z, a, b, parent(z)()) function divexact end -for name in (:divexact, :div, :rem, :mod, :gcd, :lcm) +for name in (:divexact, :rem, :mod, :gcd, :lcm) name_bang = Symbol(name, "!") @eval begin @doc """ @@ -394,6 +399,22 @@ for name in (:divexact, :div, :rem, :mod, :gcd, :lcm) end end +@doc """ + div!(z, a, b) + div!(a, b) + +Return `div(a, b)`, possibly modifying the object `z` in the process. +Aliasing is permitted. +The two argument version is a shorthand for `div(a, a, b)`. + +!!! note + `AbstractAlgebra.div` and `Base.div` differ only in their behavior on julia + types like `Integer` and `Rational{Int}`. The former makes it adhere to the + Ring interface. +""" +div!(z, a, b) = AbstractAlgebra.div(a, b) +div!(a, b) = div!(a, a, b) + @doc raw""" canonical_injection(D, i) diff --git a/src/generic/LaurentMPoly.jl b/src/generic/LaurentMPoly.jl index 04396a3bb3..62fb1c1a2f 100644 --- a/src/generic/LaurentMPoly.jl +++ b/src/generic/LaurentMPoly.jl @@ -106,7 +106,7 @@ function is_gen(a::LaurentMPolyWrap) return !iszero(_var_index(a)) end -function inv(a::LaurentMPolyWrap) +function Base.inv(a::LaurentMPolyWrap) (ap, ad) = _normalize(a) return LaurentMPolyWrap(parent(a), inv(ap), neg!(ad, ad)) end @@ -204,7 +204,7 @@ function gcd(a::LaurentMPolyWrap, b::LaurentMPolyWrap) return LaurentMPolyWrap(parent(a), gcd(ap, bp), zero!(ad)) end -function divrem(a::LaurentMPolyWrap, b::LaurentMPolyWrap) +function Base.divrem(a::LaurentMPolyWrap, b::LaurentMPolyWrap) check_parent(a, b) error("divrem not implemented for LaurentMPoly") end diff --git a/src/generic/Misc/Localization.jl b/src/generic/Misc/Localization.jl index 1f7c27a466..490f768b67 100644 --- a/src/generic/Misc/Localization.jl +++ b/src/generic/Misc/Localization.jl @@ -263,10 +263,6 @@ function Base.divrem(a::LocalizedEuclideanRingElem{T}, b::LocalizedEuclideanRing end end -function Base.div(a::LocalizedEuclideanRingElem{T}, b::LocalizedEuclideanRingElem{T}) where {T} - return divrem(a, b)[1] -end - function rem(a::LocalizedEuclideanRingElem{T}, b::LocalizedEuclideanRingElem{T}) where {T} return divrem(a, b)[2] end