Skip to content

Commit

Permalink
z_Zn supports multiprecision
Browse files Browse the repository at this point in the history
  • Loading branch information
tthsqe12 authored and thofma committed Jul 6, 2021
1 parent 65dbe9f commit 9bd465f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/number/NumberTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,18 @@ mutable struct ZnmInfo
exp::UInt
end

const N_ZnRingID = Dict{Int, Ring}()
const N_ZnRingID = Dict{BigInt, Ring}()

mutable struct N_ZnRing <: Ring
ptr::libSingular.coeffs_ptr
from_n_Z::Ptr{Nothing}
to_n_Z::Ptr{Nothing}
refcount::Int

function N_ZnRing(n::Int, cached::Bool = true)
function N_ZnRing(n::BigInt, cached::Bool = true)
n <= 0 && throw(DomainError(n, "modulus must be positive"))
return AbstractAlgebra.get_cached!(N_ZnRingID, n, cached) do
info = ZnmInfo(BigInt(n), UInt(1))
info = ZnmInfo(n, UInt(1))
GC.@preserve info begin
ptr = libSingular.nInitChar(libSingular.n_Zn, pointer_from_objref(info))
d = new(ptr, libSingular.n_SetMap(ZZ.ptr, ptr), libSingular.n_SetMap(ptr, ZZ.ptr), 1)
Expand Down
29 changes: 20 additions & 9 deletions src/number/n_Zn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,18 @@ function Base.show(io::IO, ::MIME"text/plain", a::n_Zn)
print(io, AbstractAlgebra.obj_to_string(a, context = io))
end

function AbstractAlgebra.expressify(n::n_Zn; context = nothing)::Any
x = BigInt()
function BigInt(n::n_Zn)
z = BigInt()
GC.@preserve n begin
ccall((:__gmpz_set, :libgmp), Cvoid, (Ref{BigInt}, Ptr{Any}), x, n.ptr.cpp_object)
ccall((:__gmpz_set, :libgmp), Cvoid,
(Ref{BigInt}, Ptr{Any}),
z, n.ptr.cpp_object)
end
return AbstractAlgebra.expressify(x, context = context)
return z
end

function AbstractAlgebra.expressify(n::n_Zn; context = nothing)
return BigInt(n)
end

function show(io::IO, n::n_Zn)
Expand Down Expand Up @@ -291,15 +297,20 @@ promote_rule(C::Type{n_Zn}, ::Type{n_Z}) = n_Zn

(R::N_ZnRing)(n::libSingular.number_ptr) = n_Zn(R, n)

(R::N_ZnRing)(n::Nemo.nmod) = n_Zn(R, lift(n))

(R::N_ZnRing)(n::Nemo.fmpz_mod) = n_Zn(R, lift(n))

(R::Nemo.NmodRing)(n::n_Zn) = R(BigInt(n))

(R::Nemo.FmpzModRing)(n::n_Zn) = R(BigInt(n))

###############################################################################
#
# SingularResidueRing constructor
#
###############################################################################

function ResidueRing(R::Integers, a::Int; cached=true)
a == 0 && throw(DivideError())
a < 0 && throw(DomainError(a, "modulus must be non-negative"))
ResidueRing(R::Integers, a::Int; cached=true) = N_ZnRing(BigInt(a), cached)

return N_ZnRing(a, cached)
end
ResidueRing(R::Integers, a::BigInt; cached=true) = N_ZnRing(a, cached)
20 changes: 20 additions & 0 deletions test/number/n_Zn-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,19 @@
@test F != F2
@test F1 != F2

F = ResidueRing(ZZ, BigInt(10)^50)
F1 = ResidueRing(ZZ, BigInt(10)^50)
F2 = ResidueRing(ZZ, BigInt(10)^50, cached = false)

@test F isa Singular.Ring
@test F1 isa Singular.Ring
@test F2 isa Singular.Ring
@test F == F1
@test F != F2
@test F1 != F2

@test_throws DomainError ResidueRing(ZZ, -rand(1:99))

end

@testset "n_Zn.printing" begin
Expand All @@ -32,6 +44,14 @@ end
@test characteristic(R) == 6

@test deepcopy(R(2)) == R(2)

NR = Nemo.ResidueRing(Nemo.ZZ, 6)
@test R(2) == R(NR(2))
@test NR(2) == NR(R(2))

NR = Nemo.ResidueRing(Nemo.ZZ, Nemo.ZZ(6))
@test R(2) == R(NR(2))
@test NR(2) == NR(R(2))
end

@testset "n_Zn.unary_ops" begin
Expand Down

0 comments on commit 9bd465f

Please sign in to comment.