Skip to content

Commit

Permalink
Add optimized is_trivial methods for zzModRing and ZZModRing (#1949)
Browse files Browse the repository at this point in the history
  • Loading branch information
fingolfin authored Nov 26, 2024
1 parent 73e3de8 commit fd3ff47
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/flint/FlintTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,10 @@ For the modulus being an [`Int`](@ref) see [`zzModRing`](@ref).
ninv::fmpz_mod_ctx_struct

function ZZModRing(n::ZZRingElem, cached::Bool=true)
# Modulus of zero cannot be supported. E.g. Flint library could not be expected to
# do matrices over Z/0 using a Z/nZ type. The former is multiprecision, the latter not.
n <= 0 && throw(DomainError(n, "Modulus must be positive"))

return get_cached!(FmpzModRingID, n, cached) do
ninv = fmpz_mod_ctx_struct()
@ccall libflint.fmpz_mod_ctx_init(ninv::Ref{fmpz_mod_ctx_struct}, n::Ref{ZZRingElem})::Nothing
Expand Down
9 changes: 4 additions & 5 deletions src/flint/fmpz_mod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ is_unit(a::ZZModRingElem) = a.parent.n == 1 ? iszero(a.data) : isone(gcd(a.data,

modulus(R::ZZModRing) = R.n

characteristic(R::ZZModRing) = modulus(R)

is_trivial(a::ZZModRing) = is_one(modulus(a)) # constructor ensures the modulus is > 0

function deepcopy_internal(a::ZZModRingElem, dict::IdDict)
R = parent(a)
return ZZModRingElem(deepcopy(a.data), R)
end

characteristic(R::ZZModRing) = modulus(R)

function _reduce(a::ZZRingElem, ctx::fmpz_mod_ctx_struct)
b = ZZRingElem()
@ccall libflint.fmpz_mod_set_fmpz(b::Ref{ZZRingElem}, a::Ref{ZZRingElem}, ctx::Ref{fmpz_mod_ctx_struct})::Nothing
Expand Down Expand Up @@ -449,9 +451,6 @@ end
###############################################################################

function residue_ring(R::ZZRing, n::ZZRingElem; cached::Bool=true)
# Modulus of zero cannot be supported. E.g. Flint library could not be expected to
# do matrices over Z/0 using a Z/nZ type. The former is multiprecision, the latter not.
n <= 0 && throw(DomainError(n, "Modulus must be positive"))
S = ZZModRing(n, cached)
f = Generic.EuclideanRingResidueMap(R, S)
return S, f
Expand Down
6 changes: 4 additions & 2 deletions src/flint/nmod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ is_unit(a::zzModRingElem) = a.parent.n == 1 ? a.data == 0 : gcd(a.data, a.parent

modulus(R::zzModRing) = R.n

characteristic(R::zzModRing) = ZZRingElem(modulus(R))

is_trivial(a::zzModRing) = is_unit(modulus(a))

function deepcopy_internal(a::zzModRingElem, dict::IdDict)
R = parent(a)
return zzModRingElem(deepcopy(a.data), R)
end

characteristic(R::zzModRing) = ZZRingElem(modulus(R))

###############################################################################
#
# Canonicalisation
Expand Down

0 comments on commit fd3ff47

Please sign in to comment.