Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optimized is_trivial methods for zzModRing and ZZModRing #1949

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

modulus(R::ZZModRing) = R.n

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

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

Check warning on line 61 in src/flint/fmpz_mod.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/fmpz_mod.jl#L61

Added line #L61 was not covered by tests
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps this makes it clearer?

Suggested change
is_trivial(a::ZZModRing) = is_one(modulus(a)) # constructor ensures the modulus is > 0
is_trivial(a::ZZModRing) = is_one(modulus(a)) # this is correct because the 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 @@
###############################################################################

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"))
lgoettgens marked this conversation as resolved.
Show resolved Hide resolved
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 @@

modulus(R::zzModRing) = R.n

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

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

Check warning on line 61 in src/flint/nmod.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/nmod.jl#L61

Added line #L61 was not covered by tests
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason you use is_unit here but is_one in the other file?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_unit is mathematically correct. here it gets optimized to just checking for one as the modulus is unsigned . for the ZZ case using isone is an optimization to save 1 or 2 machine instructions checking for -1

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok. thanks for the explanation


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
Loading