Skip to content

Commit

Permalink
Add coercion to QQ for all number fields (thofma#1152)
Browse files Browse the repository at this point in the history
  • Loading branch information
thofma authored Jul 5, 2023
1 parent 36ead98 commit dca8a71
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CILong.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
uses: julia-actions/cache@v1
- name: "Build package"
uses: julia-actions/julia-buildpkg@latest
- run: julia --color=yes --project -e 'using Pkg; Pkg.add(PackageSpec(name="GAP")); Pkg.add(PackageSpec(name="Polymake"));'
- run: julia --color=yes --project -e 'using Pkg; Pkg.add(PackageSpec(name="Polymake")); Pkg.add(PackageSpec(name="GAP"));'
#- run: julia --color=yes --project -e 'using Pkg; Pkg.add(PackageSpec(name="Polymake"));'
- name: "Run long tests"
uses: julia-actions/julia-runtest@latest
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Hecke"
uuid = "3e1990a7-5d81-5526-99ce-9ba3ff248f21"
version = "0.18.15"
version = "0.18.16"

[deps]
AbstractAlgebra = "c3fe647b-3220-5bb0-a1ea-a7954cac585d"
Expand Down
2 changes: 1 addition & 1 deletion src/NumField/NfAbs/NfAbs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1320,5 +1320,5 @@ function force_coerce_cyclo(a::AnticNumberField, b::nf_elem, throw_error::Type{V
end

(::QQField)(a::nf_elem) = (is_rational(a) && return coeff(a, 0)) || error("not a rational")
(::ZZRing)(a::nf_elem) = (isinteger(a) && return numerator(coeff(a, 0))) || error("not an integer")

(::ZZRing)(a::nf_elem) = (is_integer(a) && return numerator(coeff(a, 0))) || error("not an integer")
11 changes: 11 additions & 0 deletions src/NumField/NfAbs/NonSimple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1238,3 +1238,14 @@ end
function Base.hash(a::NfAbsNSElem, h::UInt)
return Base.hash(a.data, h)
end

################################################################################
#
# Coercion
#
################################################################################

function (K::QQField)(a::NfAbsNSElem)
@req is_constant(a.data) "Element must be rational"
return constant_coefficient(a.data)
end
6 changes: 6 additions & 0 deletions src/NumField/NfRel/NfRel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,12 @@ function in(a::NfRelElem{nf_elem}, K::AnticNumberField)
return true
end

function (K::QQField)(a::NfRelElem)
for i in 2:degree(parent(a))
@req iszero(coeff(a, i - 1)) "Element must be rational"
end
return QQ(coeff(a, 0))
end

################################################################################
#
Expand Down
11 changes: 11 additions & 0 deletions src/NumField/NfRel/NfRelNS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -838,3 +838,14 @@ end

absolute_discriminant(K::NfRelNS) = discriminant(K, FlintQQ)

################################################################################
#
# Coercion
#
################################################################################

function (K::QQField)(a::NfRelNSElem)
d = data(a)
@req is_constant(d) "Element must be rational"
return QQ(constant_coefficient(d))
end
6 changes: 6 additions & 0 deletions test/NfAbs/NonSimple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,10 @@
@test is_isomorphic(L, K)
end

@testset "coercion" begin
Qx, x = FlintQQ["x"]
K, (a, b) = number_field([x^2 - 2, x^3 - 3])
@test (@inferred QQ(2*a^0)) == 2*one(QQ)
@test_throws ArgumentError QQ(a)
end
end
11 changes: 11 additions & 0 deletions test/NfRel/NfRel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,15 @@
@test chip == sum([x^i for i=0:p-1])
end
end

@testset "coercion" begin
K, a = Hecke.rationals_as_number_field()
Kt, t = K["t"]
L, b = number_field(t - 1, "b")
Lt, t = L["t"]
M, o = number_field(t^3 + 2, "o")
@test QQ(2*b^0) == 2*one(QQ)
@test QQ(2*o^0) == 2*one(QQ)
@test_throws ArgumentError QQ(o)
end
end
6 changes: 5 additions & 1 deletion test/NfRel/NonSimple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@
@test string(a) isa String
end


@testset "coercion" begin
@test QQ(2*a^0) == 2*one(QQ)
@test_throws ArgumentError QQ(a)
@test_throws ErrorException QQ(gen(K) * a^0)
end
end

0 comments on commit dca8a71

Please sign in to comment.