Last active
June 9, 2017 14:40
-
-
Save Thuener/ab2870bff4473f5ea46cfc6bfa44c2b0 to your computer and use it in GitHub Desktop.
Polynomial multiplication Karatsuba
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function MP3(p1::Vector{Int64},p2::Vector{Int64}) | |
if length(p1) == 1 || length(p2) == 1 | |
return([p1[1]*p2[1]]) | |
end | |
n = length(p1) | |
n_div2 = floor(Int64,n/2) | |
A = p1[1:n_div2] | |
B = p1[(n_div2+1):(n)] | |
C = p2[1:n_div2] | |
D = p2[(n_div2+1):(n)] | |
AC = MP3(A,C) | |
BD = MP3(B,D) | |
AC_BD = MP3(A+B,C+D) | |
MP = Array(Int64,2*n-1) | |
for i = 1:2*n-1 | |
if i <= n_div2 | |
MP[i] = AC[i] | |
elseif i > n_div2 && i < n_div2+n | |
i2 = i - n_div2 | |
aux =0 | |
if i <= n-1 | |
aux = AC[i] | |
elseif i > n | |
aux = BD[i-n] | |
end | |
MP[i] = aux+(AC_BD[i2] -AC[i2] - BD[i2]) | |
else | |
MP[i] = BD[i-n] | |
end | |
end | |
return MP | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment