Skip to content

Commit

Permalink
Revise check_parent for ResElem and ResFieldElem (#1741)
Browse files Browse the repository at this point in the history
Also remove check_parent_type.

Previously, we allowed binary operations involving two ResElem
instances of *different type*, with the type of the result
depending on the argument order. For example, this:

    julia> using Nemo

    julia> R = quo(ZZ, 2)[1]
    Integers modulo 2

    julia> S = Nemo.AbstractAlgebra.EuclideanRingResidueRing{UInt}(UInt(2))
    Residue ring of integers modulo 2

    julia> typeof(R)
    zzModRing

    julia> typeof(S)
    EuclideanRingResidueRing{UInt64}

    julia> S(1) + R(1)
    0

    julia> typeof(S(1) + R(1))
    EuclideanRingResidueRingElem{UInt64}

    julia> typeof(R(1) + S(1))
    zzModRingElem

But this seems questionable at best. I think we are better of
forbidding this -- which this PR effectively does.
  • Loading branch information
fingolfin authored Jul 9, 2024
1 parent f97f91d commit f5a52ef
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
15 changes: 6 additions & 9 deletions src/Residue.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,14 @@ function is_exact_type(a::Type{T}) where {S <: RingElement, T <: ResElem{S}}
return is_exact_type(S)
end

function check_parent_type(a::ResidueRing{T}, b::ResidueRing{T}) where {T <: RingElement}
# exists only to check types of parents agree
end

function check_parent(a::ResElem, b::ResElem, throw::Bool = true)
if parent(a) != parent(b)
check_parent_type(parent(a), parent(b))
fl = modulus(parent(a)) != modulus(parent(b))
fl && throw && error("Incompatible moduli in residue operation")
return !fl
Ra = parent(a)
Rb = parent(b)
if Ra != Rb
fl = typeof(Ra) == typeof(Rb) && modulus(Ra) == modulus(Rb)
!fl && throw && error("Incompatible moduli in residue operation")
#CF: maybe extend to divisibility?
return fl
end
return true
end
Expand Down
16 changes: 7 additions & 9 deletions src/ResidueField.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ function is_exact_type(a::Type{T}) where {S <: RingElement, T <: ResFieldElem{S}
return is_exact_type(S)
end

function check_parent_type(a::ResidueField{T}, b::ResidueField{T}) where {T <: RingElement}
# exists only to check types of parents agree
end

function check_parent(a::ResFieldElem, b::ResFieldElem, throw::Bool = true)
if parent(a) != parent(b)
check_parent_type(parent(a), parent(b))
fl = modulus(parent(a)) != modulus(parent(b))
fl && throw && error("Incompatible moduli in residue operation") #CF: maybe extend to divisibility?
return !fl
Ra = parent(a)
Rb = parent(b)
if Ra != Rb
fl = typeof(Ra) == typeof(Rb) && modulus(Ra) == modulus(Rb)
!fl && throw && error("Incompatible moduli in residue operation")
#CF: maybe extend to divisibility?
return fl
end
return true
end
Expand Down

0 comments on commit f5a52ef

Please sign in to comment.