Skip to content

Commit

Permalink
Make abelian closure respect unicode setting
Browse files Browse the repository at this point in the history
  • Loading branch information
thofma committed May 31, 2023
1 parent d92e9e8 commit 555b7f9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
32 changes: 22 additions & 10 deletions src/Rings/AbelianClosure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ import Hecke: conductor, data
################################################################################

@attributes mutable struct QQAbField{T} <: Nemo.Field # union of cyclotomic fields
s::String
fields::Dict{Int, T} # Cache for the cyclotomic fields
s::String

function QQAbField{T}(s::String, fields::Dict{Int, T}) where T
return new(s, fields)
function QQAbField{T}(fields::Dict{Int, T}) where T
return new(fields)
end
end

const _QQAb = QQAbField{AnticNumberField}("ζ", Dict{Int, AnticNumberField}())
const _QQAb_sparse = QQAbField{NfAbsNS}("ζ", Dict{Int, NfAbsNS}())
const _QQAb = QQAbField{AnticNumberField}(Dict{Int, AnticNumberField}())
const _QQAb_sparse = QQAbField{NfAbsNS}(Dict{Int, NfAbsNS}())

mutable struct QQAbElem{T} <: Nemo.FieldElem
data::T # Element in cyclotomic field
Expand Down Expand Up @@ -150,22 +150,33 @@ parent(::QQAbElem{NfAbsNSElem}) = _QQAb_sparse
#
################################################################################

_variable(K::QQAbField) = K.s
function _variable(K::QQAbField)
if isdefined(K, :s)
return K.s
else
if Oscar.is_unicode_allowed()
return "ζ"
else
return "z"
end
end
end

_variable(b::QQAbElem{nf_elem}) = Expr(:call, Symbol(_variable(_QQAb)), b.c)

function _variable(b::QQAbElem{NfAbsNSElem})
k = parent(b.data)
lc = get_attribute(k, :decom)
n = get_attribute(k, :cyclo)
return [Expr(:call, Symbol(_variable(_QQAb)), n, divexact(n, i)) for i = lc]
return [Expr(:call, Symbol(_variable(parent(b))), n, divexact(n, i)) for i = lc]
end

function Hecke.cyclotomic_field(K::QQAbField{AnticNumberField}, c::Int)
if haskey(K.fields, c)
k = K.fields[c]
return k, gen(k)
else
k, z = CyclotomicField(c, string(K.s, "(", c, ")"), cached = false)
k, z = CyclotomicField(c, string("\$", "(", c, ")"), cached = false)
K.fields[c] = k
return k, z
end
Expand All @@ -186,7 +197,7 @@ function Hecke.cyclotomic_field(K::QQAbField{NfAbsNS}, c::Int)
k = K.fields[c]
return k, ns_gen(k)
else
k, _ = cyclotomic_field(NonSimpleNumField, c, string(K.s))
k, _ = cyclotomic_field(NonSimpleNumField, c, "\$")
K.fields[c] = k
return k, ns_gen(k)
end
Expand Down Expand Up @@ -311,6 +322,7 @@ end
function Base.show(io::IO, a::QQAbField{NfAbsNS})
print(io, "(Sparse) abelian closure of Q")
end

function Base.show(io::IO, a::QQAbField{AnticNumberField})
print(io, "Abelian closure of Q")
end
Expand All @@ -330,7 +342,7 @@ Change the printing of the primitive n-th root of the abelian closure of the
rationals to `s(n)`, where `s` is the supplied string.
"""
function set_variable!(K::QQAbField, s::String)
ss = K.s
ss = _variable(K)
K.s = s
return ss
end
Expand Down
24 changes: 17 additions & 7 deletions test/Rings/AbelianClosure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,28 @@ end
@testset "Printing" begin
K, z = abelian_closure(QQ)
@test sprint(show, "text/plain", K) == "Abelian closure of Q"
@test get_variable(K) == "ζ"

@test get_variable(K) == "z"
s = sprint(show, "text/plain", z)

a = z(1)
sprint(show, "text/plain", a) == "1"
a = z(4)
sprint(show, "text/plain", a) == "ζ(4)"

t = set_variable!(K, "z")
@test t == "ζ"
@test get_variable(K) == "z"
sprint(show, "text/plain", a) == "z(4)"
Oscar.with_unicode() do
@test get_variable(K) == "ζ"
s = sprint(show, "text/plain", z)

a = z(1)
sprint(show, "text/plain", a) == "1"
a = z(4)
sprint(show, "text/plain", a) == "ζ(4)"
end

t = set_variable!(K, "zz")
@test t == "z"
@test get_variable(K) == "zz"
sprint(show, "text/plain", a) == "zz(4)"

zz = gen(K, "ω")
@test get_variable(K) == "ω"
Expand All @@ -81,7 +91,7 @@ end
@test isone(a^4) && !isone(a) && !isone(a^2)

# reset variable for any subsequent (doc-)tests
set_variable!(K, "ζ")
@test set_variable!(K, "z") == "ω"
end

@testset "Coercion" begin
Expand Down

0 comments on commit 555b7f9

Please sign in to comment.