| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
function PolynomialRing:rationalsquarefreefactorization(keeplc) |
| 6 |
local monic = self / self:lc() |
| 7 |
local terms = {} |
| 8 |
terms[0] = PolynomialRing.gcd(monic, monic:derivative()) |
| 9 |
local b = monic // terms[0] |
| 10 |
local c = monic:derivative() // terms[0] |
| 11 |
local d = c - b:derivative() |
| 12 |
local i = 1 |
| 13 |
while b.degree ~= Integer.zero() or b.coefficients[0] ~= Integer.one() do |
| 14 |
terms[i] = PolynomialRing.gcd(b, d) |
| 15 |
b, c = b // terms[i], d // terms[i] |
| 16 |
i = i + 1 |
| 17 |
d = c - b:derivative() |
| 18 |
end |
| 19 |
if keeplc and terms[1] then |
| 20 |
terms[1] = terms[1] * self:lc() |
| 21 |
end |
| 22 |
return terms |
| 23 |
end |
| 24 |
|
| 25 |
|
| 26 |
function PolynomialRing:factorconstant() |
| 27 |
local gcd = Integer.zero() |
| 28 |
for i = 0, self.degree:asnumber() do |
| 29 |
gcd = self.ring.gcd(gcd, self.coefficients[i]) |
| 30 |
end |
| 31 |
if gcd == Integer.zero() then |
| 32 |
return Integer.one(), self |
| 33 |
end |
| 34 |
return gcd, self / gcd |
| 35 |
end |
| 36 |
|
| 37 |
|
| 38 |
function PolynomialRing:rationaltointeger() |
| 39 |
local lcm = Integer.one() |
| 40 |
for i = 0, self.degree:asnumber() do |
| 41 |
if self.coefficients[i]:getring() == Rational:getring() then |
| 42 |
lcm = lcm * self.coefficients[i].denominator / Integer.gcd(lcm, self.coefficients[i].denominator) |
| 43 |
end |
| 44 |
end |
| 45 |
return Integer.one() / lcm, self * lcm |
| 46 |
end |
| 47 |
|
| 48 |
|
| 49 |
function PolynomialRing:zassenhausfactor() |
| 50 |
|
| 51 |
|
| 52 |
local V = {} |
| 53 |
local n = self.degree:asnumber() |
| 54 |
local l = self:lc() |
| 55 |
for i = 0, n - 1 do |
| 56 |
V[i] = l ^ Integer(n - 1 - i) * self.coefficients[i] |
| 57 |
end |
| 58 |
V[n] = Integer.one() |
| 59 |
V = PolynomialRing(V, "y", self.degree) |
| 60 |
|
| 61 |
|
| 62 |
local p = V:findprime() |
| 63 |
local S = V:inring(PolynomialRing.R("y", p)):berlekampfactor() |
| 64 |
|
| 65 |
|
| 66 |
if #S == 1 then |
| 67 |
return {self} |
| 68 |
end |
| 69 |
|
| 70 |
|
| 71 |
local k = V:findmaxlifts(p) |
| 72 |
local W = V:henselift(S, k) |
| 73 |
local M = {} |
| 74 |
|
| 75 |
|
| 76 |
for i, factor in ipairs(W) do |
| 77 |
local w = {} |
| 78 |
for j = 0, factor.degree:asnumber() do |
| 79 |
w[j] = factor.coefficients[j]:inring(Integer.getring()) * l ^ Integer(j) |
| 80 |
end |
| 81 |
_, M[i] = PolynomialRing(w, self.symbol, factor.degree):factorconstant() |
| 82 |
end |
| 83 |
|
| 84 |
return M |
| 85 |
|
| 86 |
end |
| 87 |
|
| 88 |
|
| 89 |
function PolynomialRing:findprime() |
| 90 |
|
| 91 |
local smallprimes = {Integer(2), Integer(3), Integer(5), Integer(7), Integer(11), Integer(13), Integer(17), Integer(19), Integer(23), |
| 92 |
Integer(29), Integer(31), Integer(37), Integer(41), Integer(43), Integer(47), Integer(53), Integer(59)} |
| 93 |
|
| 94 |
for _, p in pairs(smallprimes) do |
| 95 |
local P = PolynomialRing({IntegerModN(Integer.one(), p)}, self.symbol) |
| 96 |
local s = self:inring(P:getring()) |
| 97 |
if PolynomialRing.gcd(s, s:derivative()) == P then |
| 98 |
return p |
| 99 |
end |
| 100 |
end |
| 101 |
|
| 102 |
error("Execution error: No suitable prime found for factoring.") |
| 103 |
end |
| 104 |
|
| 105 |
|
| 106 |
function PolynomialRing:findmaxlifts(p) |
| 107 |
local n = self.degree:asnumber() |
| 108 |
local h = self.coefficients[0] |
| 109 |
for i=0 , n do |
| 110 |
if self.coefficients[i] > h then |
| 111 |
h = self.coefficients[i] |
| 112 |
end |
| 113 |
end |
| 114 |
|
| 115 |
local B = 2^n * math.sqrt(n) * h:asnumber() |
| 116 |
return Integer(math.ceil(math.log(2*B, p:asnumber()))) |
| 117 |
end |
| 118 |
|
| 119 |
|
| 120 |
function PolynomialRing:henselift(S, k) |
| 121 |
local p = S[1].ring.modulus |
| 122 |
if k == Integer.one() then |
| 123 |
return self:truefactors(S, p, k) |
| 124 |
end |
| 125 |
G = self:genextendsigma(S) |
| 126 |
local V = S |
| 127 |
for j = 2, k:asnumber() do |
| 128 |
local Vp = V[1]:inring(PolynomialRing.R("y")) |
| 129 |
for i = 2, #V do |
| 130 |
Vp = Vp * V[i]:inring(PolynomialRing.R("y")) |
| 131 |
end |
| 132 |
local E = self - Vp:inring(PolynomialRing.R("y")) |
| 133 |
if E == Integer.zero() then |
| 134 |
return V |
| 135 |
end |
| 136 |
E = E:inring(PolynomialRing.R("y", p ^ Integer(j))):inring(PolynomialRing.R("y")) |
| 137 |
F = E / p ^ (Integer(j) - Integer.one()) |
| 138 |
R = self:genextendR(V, G, F) |
| 139 |
local Vnew = {} |
| 140 |
for i, v in ipairs(V) do |
| 141 |
local vnew = v:inring(PolynomialRing.R("y", p ^ Integer(j))) |
| 142 |
local rnew = R[i]:inring(PolynomialRing.R("y", p ^ Integer(j))) |
| 143 |
Vnew[i] = vnew + (p) ^ (Integer(j) - Integer.one()) * rnew |
| 144 |
end |
| 145 |
V = Vnew |
| 146 |
end |
| 147 |
return self:truefactors(V, p, k) |
| 148 |
end |
| 149 |
|
| 150 |
|
| 151 |
function PolynomialRing:genextendsigma(S) |
| 152 |
local v = S[1] * S[2] |
| 153 |
local _, A, B = PolynomialRing.extendedgcd(S[2], S[1]) |
| 154 |
local SIGMA = {A, B} |
| 155 |
for i, _ in ipairs(S) do |
| 156 |
if i >= 3 then |
| 157 |
v = v * S[i] |
| 158 |
local sum = SIGMA[1] * (v // S[1]) |
| 159 |
for j = 2, i-1 do |
| 160 |
sum = sum + SIGMA[j] * (v // S[j]) |
| 161 |
end |
| 162 |
_, A, B = PolynomialRing.extendedgcd(sum, v // S[i]) |
| 163 |
for j = 1, i-1 do |
| 164 |
SIGMA[j] = SIGMA[j] * A |
| 165 |
end |
| 166 |
SIGMA[i] = B |
| 167 |
end |
| 168 |
end |
| 169 |
|
| 170 |
return SIGMA |
| 171 |
end |
| 172 |
|
| 173 |
|
| 174 |
function PolynomialRing:genextendR(V, G, F) |
| 175 |
R = {} |
| 176 |
for i, v in ipairs(V) do |
| 177 |
local pring = G[1]:getring() |
| 178 |
R[i] = F:inring(pring) * G[i] % v:inring(pring) |
| 179 |
end |
| 180 |
return R |
| 181 |
end |
| 182 |
|
| 183 |
|
| 184 |
function PolynomialRing:truefactors(l, p, k) |
| 185 |
local U = self |
| 186 |
local L = l |
| 187 |
local factors = {} |
| 188 |
local m = 1 |
| 189 |
while m <= #L / 2 do |
| 190 |
local C = Subarrays(L, m) |
| 191 |
while #C > 0 do |
| 192 |
local t = C[1] |
| 193 |
local prod = t[1] |
| 194 |
for i = 2, #t do |
| 195 |
prod = prod * t[i] |
| 196 |
end |
| 197 |
local T = prod:inring(PolynomialRing.R("y", p ^ k)):inring(PolynomialRing.R("y")) |
| 198 |
|
| 199 |
for i = 0, T.degree:asnumber() do |
| 200 |
if T.coefficients[i] > p ^ k / Integer(2) then |
| 201 |
T.coefficients[i] = T.coefficients[i] - p^k |
| 202 |
end |
| 203 |
end |
| 204 |
local Q, R = U:divremainder(T) |
| 205 |
if R == Integer.zero() then |
| 206 |
factors[#factors+1] = T |
| 207 |
U = Q |
| 208 |
L = RemoveAll(L, t) |
| 209 |
C = RemoveAny(C, t) |
| 210 |
else |
| 211 |
C = Remove(C, t) |
| 212 |
end |
| 213 |
end |
| 214 |
m = m + 1 |
| 215 |
end |
| 216 |
if U ~= Integer.one() then |
| 217 |
factors[#factors+1] = U |
| 218 |
end |
| 219 |
return factors |
| 220 |
end |