From 510f5830cb1bee1e32b8d0a3d543319432bbd4d0 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Fri, 18 Oct 2024 16:35:57 +0200 Subject: [PATCH 1/3] Move isequal & isapprox methods to NCRings.jl ... and generalize isequal to NCRingElem --- src/NCRings.jl | 19 +++++++++++++++++++ src/Rings.jl | 19 ------------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/NCRings.jl b/src/NCRings.jl index 1a33ac9e9b..902778df3c 100644 --- a/src/NCRings.jl +++ b/src/NCRings.jl @@ -96,6 +96,25 @@ function ==(x::NCRingElem, y::NCRingElem) ==(x::NCRingElement, y::NCRingElem) = parent(y)(x) == y +function isequal(a::NCRingElem, b::NCRingElem) + return parent(a) == parent(b) && a == b +end + +# Implement `isapprox` for ring elements via equality by default. On the one +# hand, we need isapprox methods to be able to conformance test series rings. +# On the other hand this is essentially the only sensible thing to do in +# positive characteristic so we might as well do it in a generic method. +function Base.isapprox(x::NCRingElem, y::NCRingElem; + atol::Real=0, rtol::Real=0, + nans::Bool=false, norm::Function=abs) + if is_exact_type(typeof(x)) && is_exact_type(typeof(y)) + @req is_zero(atol) "non-zero atol not supported" + @req is_zero(rtol) "non-zero rtol not supported" + return x == y + end + throw(NotImplementedError(:isapprox, x, y)) +end + function divexact_left(x::NCRingElem, y::NCRingElem; check::Bool = true) return divexact_left(promote(x, y)...) end diff --git a/src/Rings.jl b/src/Rings.jl index 7a14c0d773..75a932a422 100644 --- a/src/Rings.jl +++ b/src/Rings.jl @@ -4,25 +4,6 @@ # ############################################################################### -function isequal(a::RingElem, b::RingElem) - return parent(a) == parent(b) && a == b -end - -# Implement `isapprox` for ring elements via equality by default. On the one -# hand, we need isapprox methods to be able to conformance test series rings. -# On the other hand this is essentially the only sensible thing to do in -# positive characteristic so we might as well do it in a generic method. -function Base.isapprox(x::NCRingElem, y::NCRingElem; - atol::Real=0, rtol::Real=0, - nans::Bool=false, norm::Function=abs) - if is_exact_type(typeof(x)) && is_exact_type(typeof(y)) - @req is_zero(atol) "non-zero atol not supported" - @req is_zero(rtol) "non-zero rtol not supported" - return x == y - end - throw(NotImplementedError(:isapprox, x, y)) -end - """ divexact(x, y; check::Bool=true) From b8de597d8f4869b88b2650beac19c9a5c40cb7e2 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Fri, 18 Oct 2024 16:52:57 +0200 Subject: [PATCH 2/3] Fix indentation --- src/NCRings.jl | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/NCRings.jl b/src/NCRings.jl index 902778df3c..7a547cb6ff 100644 --- a/src/NCRings.jl +++ b/src/NCRings.jl @@ -84,13 +84,12 @@ end *(x::NCRingElement, y::NCRingElem) = parent(y)(x)*y function ==(x::NCRingElem, y::NCRingElem) - fl, u, v = try_promote(x, y) - if fl - return u == v - else - return false - end - end + fl, u, v = try_promote(x, y) + if fl + return u == v + end + return false +end ==(x::NCRingElem, y::NCRingElement) = x == parent(x)(y) From 4cb4d7ef10125ce6e8ccb1d38ad26f1d5ef350cc Mon Sep 17 00:00:00 2001 From: Max Horn Date: Fri, 18 Oct 2024 16:53:10 +0200 Subject: [PATCH 3/3] Use promotion in default isapprox methods --- src/NCRings.jl | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/NCRings.jl b/src/NCRings.jl index 7a547cb6ff..0987d1b560 100644 --- a/src/NCRings.jl +++ b/src/NCRings.jl @@ -103,9 +103,9 @@ end # hand, we need isapprox methods to be able to conformance test series rings. # On the other hand this is essentially the only sensible thing to do in # positive characteristic so we might as well do it in a generic method. -function Base.isapprox(x::NCRingElem, y::NCRingElem; +function Base.isapprox(x::T, y::T; atol::Real=0, rtol::Real=0, - nans::Bool=false, norm::Function=abs) + nans::Bool=false, norm::Function=abs) where {T <: NCRingElem} if is_exact_type(typeof(x)) && is_exact_type(typeof(y)) @req is_zero(atol) "non-zero atol not supported" @req is_zero(rtol) "non-zero rtol not supported" @@ -114,6 +114,19 @@ function Base.isapprox(x::NCRingElem, y::NCRingElem; throw(NotImplementedError(:isapprox, x, y)) end +function Base.isapprox(x::NCRingElem, y::NCRingElem; kwarg...) + fl, u, v = try_promote(x, y) + if fl + return isapprox(u, v; kwarg...) + end + throw(NotImplementedError(:isapprox, x, y)) +end + +Base.isapprox(x::NCRingElem, y::Union{Integer, Rational, AbstractFloat}; kwarg...) = isapprox(x, parent(x)(y); kwarg...) + +Base.isapprox(x::Union{Integer, Rational, AbstractFloat}, y::NCRingElem; kwarg...) = isapprox(parent(y)(x), y; kwarg...) + + function divexact_left(x::NCRingElem, y::NCRingElem; check::Bool = true) return divexact_left(promote(x, y)...) end