-
Notifications
You must be signed in to change notification settings - Fork 0
/
polynomial.go
376 lines (310 loc) · 6.6 KB
/
polynomial.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package ecc
import (
"fmt"
"math/big"
)
// https://github.com/jukworks/polynomial
// Poly Data structure for a poly
// Just an array in reverse
// f(x) = 3x^3 + 2x + 1 => [1 2 0 3]
type Poly []*big.Int
// NewPolyFromBigInt generates a poly with given integers.
func NewPolyFromBigInt(a ...*big.Int) Poly {
alen := len(a)
p := make(Poly, alen)
for i := 0; i < alen; i++ {
p[i] = new(big.Int).Set(a[i])
}
return p
}
// NewPolyFromInt generates a poly with given integers.
func NewPolyFromInt(a ...int) Poly {
alen := len(a)
p := make(Poly, alen)
for i := 0; i < alen; i++ {
p[i] = new(big.Int).SetInt64(int64(a[i]))
}
return p
}
// trim makes sure that the highest coefficient never has zero value
// when you add or subtract two polynomials, sometimes the highest coefficient
// goes zero if you don't remove the highest and zero coefficient,
// Deg() returns the wrong result
func (p Poly) trim() Poly {
deg := len(p) - 1
if p[deg].Sign() != 0 {
return p
}
last := 0
for i := deg; i > 0; i-- {
// why i > 0, not i >=0? do not remove the constant
if p[i].Sign() != 0 {
last = i
break
}
}
return p[:last+1]
}
// sanitize does modular arithmetic with m
func (p Poly) sanitize(m *big.Int) Poly {
for i := 0; i < len(p); i++ {
p[i].Mod(p[i], m)
}
return p.trim()
}
// Clone does deep-copy
// adjust increases the degree of copied poly
// adjust cannot have a negative integer
// for example, P = x + 1 and adjust = 2, Clone() returns x^3 + x^2
func (p Poly) Clone(adjust int) Poly {
if adjust < 0 {
return NewPolyFromInt(0)
}
q := make(Poly, len(p)+adjust)
for i := 0; i < adjust; i++ {
q[i] = new(big.Int)
}
copy(q[adjust:], p)
return q
}
// isZero checks if P = 0
func (p Poly) isZero() bool {
return p.Deg() == 0 && p[0].Sign() == 0
}
// Deg returns the degree
// if p = x^3 + 2x^2 + 5, Deg() returns 3
func (p Poly) Deg() int {
return len(p) - 1
}
// String pretty print
func (p Poly) String() string {
s := "["
for i := len(p) - 1; i >= 0; i-- {
switch p[i].Sign() {
case -1:
if i == len(p)-1 {
s += "-"
} else {
s += " - "
}
if i == 0 || p[i].Cmp(big.NewInt(-1)) != 0 {
s += p[i].String()[1:]
}
case 0:
continue
case 1:
if i < len(p)-1 {
s += " + "
}
if i == 0 || p[i].Cmp(big.NewInt(1)) != 0 {
s += p[i].String()
}
}
if i > 0 {
s += "x"
if i > 1 {
s += "^" + fmt.Sprintf("%d", i)
}
}
}
if s == "[" {
s += "0"
}
s += "]"
return s
}
// Cmp compares two polynomials and returns -1, 0, or 1
// if P == Q, returns 0
// if P > Q, returns 1
// if P < Q, returns -1
func (p Poly) Cmp(q Poly) int {
if len(p) > len(q) {
return 1
}
if len(p) < len(q) {
return -1
}
for i := 0; i < len(p); i++ {
s := p[i].Cmp(q[i])
if s != 0 {
return s
}
}
return 0
}
// Add adds two polynomials
// modulo m can be nil
func (p Poly) Add(q Poly, m *big.Int) Poly {
if p.Cmp(q) < 0 {
return q.Add(p, m)
}
r := p.Clone(0)
for i := 0; i < len(q); i++ {
r[i] = new(big.Int).Add(p[i], q[i])
}
for i := 0; i < len(q); i++ {
r[i].Mod(r[i], m)
}
return r.trim()
}
// Neg returns a poly Q = -P
func (p Poly) Neg() Poly {
q := make(Poly, len(p))
for i := 0; i < len(p); i++ {
q[i] = new(big.Int).Neg(p[i])
}
return q
}
// Sub subtracts P from Q
// Since we already have Add(), Sub() does Add(P, -Q)
func (p Poly) Sub(q Poly, m *big.Int) Poly {
swap := false
s, t := p, q
if p.Cmp(q) < 0 {
swap = true
s, t = t, s
}
r := make(Poly, len(s))
ln := len(t)
if !swap {
for i := 0; i < ln; i++ {
r[i] = new(big.Int).Sub(s[i], t[i])
}
copy(r[ln:], s[ln:])
} else {
for i := 0; i < ln; i++ {
r[i] = new(big.Int).Sub(t[i], s[i])
}
for i := ln; i < len(s); i++ {
r[i] = new(big.Int).Neg(s[i])
}
}
for i := 0; i < len(s); i++ {
r[i].Mod(r[i], m)
}
return r.trim()
}
// Mul returns P * Q
func (p Poly) Mul(q Poly, m *big.Int) Poly {
r := make(Poly, len(p)+len(q)-1)
for i := 0; i < len(r); i++ {
r[i] = new(big.Int)
}
for i := 0; i < len(p); i++ {
for j := 0; j < len(q); j++ {
r[i+j].Add(r[i+j], new(big.Int).Mul(p[i], q[j]))
}
}
return r.sanitize(m)
}
func (p Poly) MulInt(a int, m *big.Int) Poly {
return p.Mul(NewPolyFromInt(a), m)
}
// Exp returns P^e mod M
func (p Poly) Exp(e *big.Int, m *big.Int) Poly {
r := NewPolyFromInt(1)
for _, b := range e.Bytes() {
for bitNum := 0; bitNum < 8; bitNum++ {
r = r.Mul(r, m)
if b&0x80 == 0x80 {
r = r.Mul(p, m)
}
b <<= 1
}
}
return r.trim()
}
// Div returns (P / Q, P % Q)
func (p Poly) Div(q Poly, m *big.Int) (Poly, Poly) {
p.sanitize(m)
if len(p) < len(q) {
return NewPolyFromInt(0), p.Clone(0)
}
quo := make(Poly, len(p)-len(q)+1)
for i := 0; i < len(quo); i++ {
quo[i] = new(big.Int)
}
rem := p
qd := q.Deg()
for {
td := len(rem) - 1 // rem.Deg()
rd := td - qd
if rd < 0 || rem.isZero() {
break
}
r := quo[rd]
r.ModInverse(q[qd], m)
r.Mul(r, rem[td]).Mod(r, m)
u := make(Poly, len(q)+rd)
for i := 0; i < rd; i++ {
u[i] = new(big.Int)
}
x := u[rd:]
for i := 0; i < len(q); i++ {
x[i] = new(big.Int).Mul(q[i], r)
}
rem = rem.Sub(u, m)
}
return quo, rem
}
func (p Poly) Monic(m *big.Int) Poly {
q := NewPolyFromBigInt(p[p.Deg()])
q, _ = p.Div(q, m)
return q
}
// Deriv derivative
func (p Poly) Deriv(m *big.Int) Poly {
if len(p) == 1 {
return NewPolyFromInt(0)
}
r := make(Poly, len(p)-1)
for i := 1; i < len(p); i++ {
r[i-1] = new(big.Int).Mul(p[i], big.NewInt(int64(i)))
r[i-1].Mod(r[i-1], m)
}
return r.trim()
}
func (p Poly) GCD(q Poly, m *big.Int) Poly {
g, _, _ := p.ExtendedGCD(q, m)
return g
}
func (p Poly) ExtendedGCD(q Poly, m *big.Int) (Poly, Poly, Poly) {
oldR, r := p, q
oldS, s := NewPolyFromInt(1), NewPolyFromInt(0)
oldT, t := NewPolyFromInt(0), NewPolyFromInt(1)
for !r.isZero() {
quo, _ := oldR.Div(r, m)
oldR, r = r, oldR.Sub(quo.Mul(r, m), m)
oldS, s = s, oldS.Sub(quo.Mul(s, m), m)
oldT, t = t, oldT.Sub(quo.Mul(t, m), m)
}
return oldR.Monic(m), oldS, oldT
}
func (p Poly) ModInverse(h Poly, m *big.Int) Poly {
if m.Cmp(big.NewInt(1)) == 0 {
return NewPolyFromInt(0)
}
mono := NewPolyFromInt(1)
t, newT := NewPolyFromInt(0), mono
r, newR := h, p
for !newR.isZero() {
quo, _ := r.Div(newR, m)
r, newR = newR, r.Sub(quo.Mul(newR, m), m)
t, newT = newT, t.Sub(quo.Mul(newT, m), m)
}
if len(r) > 1 {
return nil
}
x, _ := mono.Div(r, m)
return x.Mul(t, m)
}
// Eval returns p(v) where v is the given big integer
func (p Poly) Eval(x *big.Int, m *big.Int) *big.Int {
ans := new(big.Int).Set(p[p.Deg()])
for i := p.Deg() - 1; i >= 0; i-- {
ans.Mul(ans, x)
ans.Add(ans, p[i])
ans.Mod(ans, m)
}
return ans
}