| 1 |
-- Methods related to polynomial decomposition. |
| 2 |
|
| 3 |
-- Returns a list of polynomials that form a complete decomposition of a polynomial. |
| 4 |
function PolynomialRing:decompose() |
| 5 |
local U = self - self.coefficients[0] |
| 6 |
local S = U:divisors() |
| 7 |
local decomposition = {} |
| 8 |
local C = PolynomialRing({Integer.zero(), Integer.one()}, self.symbol) |
| 9 |
local finalcomponent |
| 10 |
|
| 11 |
while S[1] do |
| 12 |
local w = S[1] |
| 13 |
for _, poly in ipairs(S) do |
| 14 |
if poly.degree < w.degree then |
| 15 |
w = poly |
| 16 |
end |
| 17 |
end |
| 18 |
S = Remove(S, w) |
| 19 |
if C.degree < w.degree and w.degree < self.degree and self.degree % w.degree == Integer.zero() then |
| 20 |
local g = w:polyexpand(C, self.symbol) |
| 21 |
local R = self:polyexpand(w, self.symbol) |
| 22 |
if g.degree == Integer.zero() and R.degree == Integer.zero() then |
| 23 |
g.symbol = self.symbol |
| 24 |
decomposition[#decomposition+1] = g.coefficients[0] |
| 25 |
decomposition[#decomposition].symbol = self.symbol |
| 26 |
C = w |
| 27 |
finalcomponent = R.coefficients[0] |
| 28 |
end |
| 29 |
end |
| 30 |
end |
| 31 |
|
| 32 |
if not decomposition[1] then |
| 33 |
return {self} |
| 34 |
end |
| 35 |
|
| 36 |
finalcomponent.symbol = self.symbol |
| 37 |
decomposition[#decomposition+1] = finalcomponent |
| 38 |
return decomposition |
| 39 |
end |
| 40 |
|
| 41 |
-- Returns a list of all monic divisors of positive degree of the polynomial, assuming the polynomial ring is a Euclidean Domain. |
| 42 |
function PolynomialRing:divisors() |
| 43 |
local factors = self:factor() |
| 44 |
-- Converts each factor to a monic factor (we don't need to worry updating the constant term) |
| 45 |
for i, factor in ipairs(factors.expressions) do |
| 46 |
if i > 1 then |
| 47 |
factor.expressions[1] = factor.expressions[1] / factor.expressions[1]:lc() |
| 48 |
end |
| 49 |
end |
| 50 |
|
| 51 |
local terms = {} |
| 52 |
for i, _ in ipairs(factors.expressions) do |
| 53 |
if i > 1 then |
| 54 |
terms[i] = Integer.zero() |
| 55 |
end |
| 56 |
end |
| 57 |
|
| 58 |
local divisors = {} |
| 59 |
local divisor = PolynomialRing({self:onec()}, self.symbol) |
| 60 |
while true do |
| 61 |
for i, factor in ipairs(factors.expressions) do |
| 62 |
if i > 1 then |
| 63 |
local base = factor.expressions[1] |
| 64 |
local power = factor.expressions[2] |
| 65 |
if terms[i] < power then |
| 66 |
terms[i] = terms[i] + Integer.one() |
| 67 |
divisor = divisor * base |
| 68 |
break |
| 69 |
else |
| 70 |
terms[i] = Integer.zero() |
| 71 |
divisor = divisor // (base ^ power) |
| 72 |
end |
| 73 |
end |
| 74 |
end |
| 75 |
if divisor == Integer.one() then |
| 76 |
break |
| 77 |
end |
| 78 |
divisors[#divisors+1] = divisor |
| 79 |
end |
| 80 |
|
| 81 |
return divisors |
| 82 |
|
| 83 |
end |
| 84 |
|
| 85 |
-- Polynomial expansion as a subroutine of decomposition. |
| 86 |
function PolynomialRing:polyexpand(v, x) |
| 87 |
local u = self |
| 88 |
if u == Integer.zero() then |
| 89 |
return Integer.zero() |
| 90 |
end |
| 91 |
local q,r = u:divremainder(v) |
| 92 |
return PolynomialRing({PolynomialRing({Integer.zero(), Integer.one()}, "_")}, x) * q:polyexpand(v, x) + r |
| 93 |
end |