mirror of
https://github.com/arnaucube/math.git
synced 2026-01-11 16:31:32 +01:00
Add fast polynomial multiplication using FFT
This commit is contained in:
20
fft.sage
20
fft.sage
@@ -40,3 +40,23 @@ def fft(F, n):
|
||||
ft_inv = ft^-1
|
||||
return w, ft, ft_inv
|
||||
|
||||
|
||||
# Fast polynomial multiplicaton using FFT
|
||||
def poly_mul(fa, fb, F, n):
|
||||
w, ft, ft_inv = fft(F, n)
|
||||
|
||||
# compute evaluation points from polynomials fa & fb at the roots of unity
|
||||
a_evals = []
|
||||
b_evals = []
|
||||
for i in range(n):
|
||||
a_evals.append(fa(w[i]))
|
||||
b_evals.append(fb(w[i]))
|
||||
|
||||
# multiply elements in a_evals by b_evals
|
||||
c_evals = map(operator.mul, a_evals, b_evals)
|
||||
c_evals = vector(c_evals)
|
||||
|
||||
# using FFT, convert the c_evals into fc(x)
|
||||
fc_coef = c_evals*ft_inv
|
||||
fc2=P(fc_coef.list())
|
||||
return fc2, c_evals
|
||||
|
||||
Reference in New Issue
Block a user