Debian Codegraph @

texmf-dist/tex/lualatex/luacas/algebra/polynomialring/luacas-berlekampfactoring.lua

raw
1 -- Methods related to the Berlekamp factoring algorithm.
2
3 -- Square-free factorization in the modular field Zp.
4 function PolynomialRing:modularsquarefreefactorization()
5 local monic = self / self:lc()
6 local terms = {}
7 terms[0] = PolynomialRing.gcd(monic, monic:derivative())
8 local b = monic // terms[0]
9 local c = monic:derivative() // terms[0]
10 local d = c - b:derivative()
11 local i = 1
12 while b ~= Integer.one() do
13 terms[i] = PolynomialRing.gcd(b, d)
14 b, c = b // terms[i], d // terms[i]
15 i = i + 1
16 d = c - b:derivative()
17 end
18
19 if not (terms[i-1]:derivative().degree == Integer.zero() and terms[i-1]:derivative().coefficients[0] == Integer.zero()) then
20 return terms
21 end
22
23 local recursiveterms = terms[i-1]:collapseterms(self.ring.modulus):modularsquarefreefactorization()
24 for k, poly in ipairs(recursiveterms) do
25 recursiveterms[k] = poly:expandterms(self.ring.modulus)
26 end
27 return JoinArrays(terms, recursiveterms)
28 end
29
30 -- Returns a new polnomial consisting of every nth term of the old one - helper method for square-free factorization
31 function PolynomialRing:collapseterms(n)
32 local new = {}
33 local loc = 0
34 local i = 0
35 local nn = n:asnumber()
36 while loc <= self.degree:asnumber() do
37 new[i] = self.coefficients[loc]
38 loc = loc + nn
39 i = i + 1
40 end
41
42 return PolynomialRing(new, self.symbol, self.degree // n)
43 end
44
45 -- Returns a new polnomial consisting of every nth term of the old one - helper method for square-free factorization
46 function PolynomialRing:expandterms(n)
47 local new = {}
48 local loc = 0
49 local i = 0
50 local nn = n:asnumber()
51 while i <= self.degree:asnumber() do
52 new[loc] = self.coefficients[i]
53 for j = 1, nn do
54 new[loc + j] = IntegerModN(Integer.zero(), n)
55 end
56 loc = loc + nn
57 i = i + 1
58 end
59
60 return PolynomialRing(new, self.symbol, self.degree * n)
61 end
62
63 -- Uses Berlekamp's Algorithm to factor polynomials in mod p
64 function PolynomialRing:berlekampfactor()
65 if self.degree == 0 or self.degree == 1 then
66 return {self}
67 end
68
69 local R = self:RMatrix()
70 local S = self:auxillarybasis(R)
71 if #S == 1 then
72 return {self}
73 end
74 return self:findfactors(S)
75 end
76
77 -- Gets the R Matrix for Berlekamp factorization
78 function PolynomialRing:RMatrix()
79 local R = {}
80 for i = 1, self.degree:asnumber() do
81 R[i] = {}
82 end
83 for i = 0, self.degree:asnumber()-1 do
84 local remainder = PolynomialRing({IntegerModN(Integer.one(), self.ring.modulus)}, self.symbol):multiplyDegree(self.ring.modulus:asnumber()*i) % self
85 for j = 0, self.degree:asnumber()-1 do
86 R[j + 1][i + 1] = remainder.coefficients[j]
87 if j == i then
88 R[j + 1][i + 1] = R[j + 1][i + 1] - IntegerModN(Integer.one(), self.ring.modulus)
89 end
90 end
91 end
92 return R
93 end
94
95 -- Creates an auxillary basis using the R matrix
96 function PolynomialRing:auxillarybasis(R)
97 local P = {}
98 local n = self.degree:asnumber()
99 for i = 1, n do
100 P[i] = 0
101 end
102 S = {}
103 local q = 1
104 for j = 1, n do
105 local i = 1
106 local pivotfound = false
107 while not pivotfound and i <= n do
108 if R[i][j] ~= self:zeroc() and P[i] == 0 then
109 pivotfound = true
110 else
111 i = i + 1
112 end
113 end
114 if pivotfound then
115 P[i] = j
116 local a = R[i][j]:inv()
117 for l = 1, n do
118 R[i][l] = a * R[i][l]
119 end
120 for k = 1, n do
121 if k ~= i then
122 local f = R[k][j]
123 for l = 1, n do
124 R[k][l] = R[k][l] - f*R[i][l]
125 end
126 end
127 end
128 else
129 local s = {}
130 s[j] = self:onec()
131 for l = 1, j - 1 do
132 local e = 0
133 i = 1
134 while e == 0 and i <= n do
135 if l == P[i] then
136 e = i
137 else
138 i = i + 1
139 end
140 end
141 if e > 0 then
142 local c = -R[e][j]
143 s[l] = c
144 else
145 s[l] = self:zeroc()
146 end
147 end
148 S[#S+1] = PolynomialRing(s, self.symbol)
149 end
150 end
151 return S
152 end
153
154 -- Uses the auxilary basis to find the irreirrducible factors of the polynomial.
155 function PolynomialRing:findfactors(S)
156 local r = #S
157 local p = self.ring.modulus
158 local factors = {self}
159 for k = 2,r do
160 local b = S[k]
161 local old_factors = Copy(factors)
162 for i = 1,#old_factors do
163 local w = old_factors[i]
164 local j = 0
165 while j <= p:asnumber() - 1 do
166 local g = PolynomialRing.gcd(b-IntegerModN(Integer(j), p), w)
167 if g == Integer.one() then
168 j = j + 1
169 elseif g == w then
170 j = p:asnumber()
171 else
172 factors = Remove(factors, w)
173 local q = w // g
174 factors[#factors+1] = g
175 factors[#factors+1] = q
176 if #factors == r then
177 return factors
178 else
179 j = j + 1
180 w = q
181 end
182 end
183
184 end
185 end
186 end
187 end