Skip to content

Commit

Permalink
feat: add roots for polynomials over algebraic_closure(QQ)
Browse files Browse the repository at this point in the history
  • Loading branch information
thofma committed Aug 13, 2024
1 parent e593146 commit d5460b8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/Misc/QQBar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,25 @@ end
# TODO: use _qqbar_roots_poly_squarefree as soon as available in FLINT_jll

function factor(f::PolyRingElem{QQBarFieldElem})
# the roots of f in Qbar are contained in the roots of norm(f) in Qbar
@req !is_zero(f) "Polynomial must be non-zero"
rts = roots(f)
facts = [gen(parent(f)) - r for r in rts]
fac = Fac(parent(f)(leading_coefficient(f)), Dict(facts[i] => valuation(f, facts[i]) for i in 1:length(facts)))
@assert degree(f) == sum(e for (_,e) in fac)
return fac
end

function roots(f::PolyRingElem{QQBarFieldElem})
# the roots of f in Qbar are contained in the roots of norm(f) in Qbar
@req !is_zero(f) "Polynomial must be non-zero"
QQbar = base_ring(f)
cfs, pe = _map_to_common_number_field(collect(coefficients(f)))
K = parent(cfs[1])
Kt, t = polynomial_ring(K; cached = false)
fK = Kt(cfs)
rts = roots(QQbar, norm(fK))
facts = [gen(parent(f)) - r for r in rts if is_zero(f(r))]
fac = Fac(parent(f)(leading_coefficient(f)), Dict(facts[i] => valuation(f, facts[i]) for i in 1:length(facts)))
@assert degree(f) == sum(e for (_,e) in fac)
return fac
rts = roots(QQbar, norm(fK)::dense_poly_type(QQ))
rts = unique!(QQBarFieldElem[r for r in rts if is_zero(f(r))])
return rts
end

4 changes: 4 additions & 0 deletions test/Misc/QQBar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ let
fa = factor(f)
@test f == unit(fa) * prod(g^e for (g, e) in fa)
@test all(degree(g) == 1 for (g, e) in fa)

rts = @inferred roots(f)
@test length(rts) == 3
@test all(r -> is_zero(f(r)), rts)
end

0 comments on commit d5460b8

Please sign in to comment.