Skip to content

Commit

Permalink
expressify n_Z, n_Q , n_Zn, n_Zp
Browse files Browse the repository at this point in the history
  • Loading branch information
thofma committed Dec 30, 2020
1 parent b64e3f1 commit b8b87a7
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/number/n_Q.jl
Expand Up @@ -106,6 +106,10 @@ function show(io::IO, c::Rationals)
print(io, "Rational Field")
end

function AbstractAlgebra.expressify(n::n_Q; context = nothing)::Any
return AbstractAlgebra.expressify(Rational{BigInt}(n), context = context)
end

function show(io::IO, n::n_Q)
libSingular.StringSetS("")

Expand Down
4 changes: 4 additions & 0 deletions src/number/n_Z.jl
Expand Up @@ -102,6 +102,10 @@ function show(io::IO, c::Integers)
print(io, "Integer Ring")
end

function AbstractAlgebra.expressify(n::n_Z; context = nothing)::Any
return AbstractAlgebra.expressify(BigInt(n), context = context)
end

function show(io::IO, n::n_Z)
libSingular.StringSetS("")

Expand Down
12 changes: 12 additions & 0 deletions src/number/n_Zn.jl
Expand Up @@ -89,6 +89,18 @@ function show(io::IO, c::N_ZnRing)
print(io, "Residue Ring of Integer Ring modulo ", characteristic(c))
end

function Base.show(io::IO, ::MIME"text/plain", a::n_Zp)
print(io, AbstractAlgebra.obj_to_string(a, context = io))
end

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

function show(io::IO, n::n_Zn)
libSingular.StringSetS("")
libSingular.n_Write(n.ptr, parent(n).ptr, false)
Expand Down
21 changes: 21 additions & 0 deletions src/number/n_Zp.jl
Expand Up @@ -80,6 +80,27 @@ function show(io::IO, c::N_ZpField)
print(io, "Finite Field of Characteristic ", characteristic(c))
end

function Base.show(io::IO, ::MIME"text/plain", a::n_Zp)
print(io, AbstractAlgebra.obj_to_string(a, context = io))
end

if VERSION >= v"1.5"
function AbstractAlgebra.expressify(n::n_Zp; context = nothing)::Any
nn = rem(Int(n), Int(characteristic(parent(n))), RoundNearest)
return AbstractAlgebra.expressify(nn, context = context)
end
else
function AbstractAlgebra.expressify(n::n_Zp; context = nothing)::Any
p = Int(characteristic(parent(n)))
nn = Int(n)
if 2*nn > p
nn = nn - p
end

return AbstractAlgebra.expressify(nn, context = context)
end
end

function show(io::IO, n::n_Zp)
libSingular.StringSetS("")
libSingular.n_Write(n.ptr, parent(n).ptr, false)
Expand Down
6 changes: 6 additions & 0 deletions src/poly/poly.jl
Expand Up @@ -362,6 +362,12 @@ function show(io::IO, a::spoly)
print(io, s)
end

# TODO: Remove this once we can properly expressify elements of type n_GF
function show(io::IO, ::MIME"text/plain", a::spoly{n_GF})
s = libSingular.p_String(a.ptr, parent(a).ptr)
print(io, s)
end

show_minus_one(::Type{spoly{T}}) where T <: Nemo.RingElem = show_minus_one(T)

needs_parentheses(x::spoly) = length(x) > 1
Expand Down
1 change: 1 addition & 0 deletions test/number/n_Q-test.jl
Expand Up @@ -65,6 +65,7 @@ end

@testset "n_Q.printing..." begin
@test string(QQ(123)) == "123"
@test sprint(show, "text/plain", (QQ(123))) == "123"
end

@testset "n_Q.manipulation..." begin
Expand Down
1 change: 1 addition & 0 deletions test/number/n_Z-test.jl
Expand Up @@ -37,6 +37,7 @@ end

@testset "n_Z.printing..." begin
@test string(ZZ(123)) == "123"
@test sprint(show, "text/plain", (ZZ(123))) == "123"
end

@testset "n_Z.manipulation..." begin
Expand Down
2 changes: 2 additions & 0 deletions test/number/n_Zn-test.jl
Expand Up @@ -40,6 +40,8 @@ end
R = ResidueRing(ZZ, 5)

@test string(R(3)) == "3"

@test sprint(show, "text/plain", (R(3))) == "3"
end

@testset "n_Zn.manipulation..." begin
Expand Down
2 changes: 2 additions & 0 deletions test/number/n_Zp-test.jl
Expand Up @@ -40,6 +40,8 @@ end
R = Fp(5)

@test string(R(3)) == "-2"

@test sprint(show, "text/plain", (R(3))) == "-2"
end

@testset "n_Zp.manipulation..." begin
Expand Down
6 changes: 6 additions & 0 deletions test/poly/spoly-test.jl
Expand Up @@ -87,6 +87,12 @@ end
R, (x, ) = PolynomialRing(ZZ, ["x", ])

@test length(string(3x^2 + 2x + 1)) > 3
@test length(sprint(show, "text/plain", 3x^2 + 2x + 1)) > 3

R, (x, ) = PolynomialRing(QQ, ["x", ])

@test length(string(3x^2 + 2x + 1)) > 3
@test length(sprint(show, "text/plain", 3x^2 + 2x + 1)) > 3
end

@testset "spoly.manipulation..." begin
Expand Down

0 comments on commit b8b87a7

Please sign in to comment.