From 89755d3cc75f44bfdb2e88a76bce1a6167cb6b82 Mon Sep 17 00:00:00 2001 From: dan Date: Fri, 22 Apr 2022 13:23:22 +0200 Subject: [PATCH] add factor for Laurent polynomials (#1149) --- src/Generic.jl | 3 ++- src/generic/LaurentMPoly.jl | 22 ++++++++++++++++++++++ src/generic/LaurentPoly.jl | 26 ++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/Generic.jl b/src/Generic.jl index 5bc0aa50b0..c74d9d4078 100644 --- a/src/Generic.jl +++ b/src/Generic.jl @@ -43,7 +43,8 @@ import ..AbstractAlgebra: add!, addeq!, addmul!, base_ring, canonical_unit, content, data, deflate, deflation, degree, degrees, degrees_range, denominator, derivative, div, divexact, divides, divrem, domain, elem_type, - evaluate, exp, exponent_vectors, expressify, factor, + evaluate, exp, exponent_vectors, expressify, + factor, factor_squarefree, gen, gens, get_field, identity_matrix, inflate, integral, inv, isconstant, isdomain_type, isexact_type, isgen, ismonomial, isreduced_form, diff --git a/src/generic/LaurentMPoly.jl b/src/generic/LaurentMPoly.jl index 1da3b50a7e..23297aa2dd 100644 --- a/src/generic/LaurentMPoly.jl +++ b/src/generic/LaurentMPoly.jl @@ -189,6 +189,28 @@ function divrem(a::LaurentMPolyWrap, b::LaurentMPolyWrap) error("divrem not implemented for LaurentMPoly") end +function factor(a::LaurentMPolyWrap) + R = parent(a) + ap, ad = _normalize(a) + f = factor(ap) + d = Dict{typeof(a), Int}() + for (p, e) in f + d[LaurentMPolyWrap(R, p)] = e + end + return Fac(LaurentMPolyWrap(R, unit(f), ad), d) +end + +function factor_squarefree(a::LaurentMPolyWrap) + R = parent(a) + ap, ad = _normalize(a) + f = factor_squarefree(ap) + d = Dict{typeof(a), Int}() + for (p, e) in f + d[LaurentMPolyWrap(R, p)] = e + end + return Fac(LaurentMPolyWrap(R, unit(f), ad), d) +end + ############################################################################### # # Canonicalisation diff --git a/src/generic/LaurentPoly.jl b/src/generic/LaurentPoly.jl index eaa875762e..6a4bc9cc86 100644 --- a/src/generic/LaurentPoly.jl +++ b/src/generic/LaurentPoly.jl @@ -165,6 +165,10 @@ function _remove_gen(a::LaurentPolyWrap) return i, (i == 0 ? a.poly : shift_right(a.poly, i)) end end + # we need something finite on zero input + if iszero(a.poly) + return (0, a.poly) + end return remove(a.poly, gen(parent(a.poly))) end @@ -255,6 +259,28 @@ function lcm(p::LaurentPolyWrap{T}, q::LaurentPolyWrap{T}) where T return LaurentPolyWrap(parent(p), lcm(p.poly, q.poly), 0) end +function factor(a::LaurentPolyWrap) + R = parent(a) + va, ua = _remove_gen(a) + f = factor(ua) + d = Dict{typeof(a), Int}() + for (p, e) in f + d[LaurentPolyWrap(R, p, 0)] = e + end + return Fac(LaurentPolyWrap(R, unit(f), a.mindeg + va), d) +end + +function factor_squarefree(a::LaurentPolyWrap) + R = parent(a) + va, ua = _remove_gen(a) + f = factor_squarefree(ua) + d = Dict{typeof(a), Int}() + for (p, e) in f + d[LaurentPolyWrap(R, p, 0)] = e + end + return Fac(LaurentPolyWrap(R, unit(f), a.mindeg + va), d) +end + ############################################################################### # # Ad hoc binary operators