我正在尝试在 python 中为两个相同次数的多项式创建一个Karatsuba 算法,它应该返回结果系数的数组。
def karatsuba(a, b):
n = len(a)
aHigh = []
aLow = []
bHigh = []
bLow = []
c = []
for i in range(n//2):
aLow.append(a[i])
bLow.append(b[i])
for i in range(n//2):
aHigh.append(a[n//2+i])
bHigh.append(b[n//2+i])
tA = []
tB = []
for i in range(n//2):
tA.append(aLow[i] + aHigh[i])
tB.append(bLow[i] + bHigh[i])
cMid = karatsuba(tA, tB)
cLow = karatsuba(aLow, bLow)
cHigh = karatsuba(aHigh, bHigh)
for i in range(n-1):
c.append(cLow[i])
c[n-1] = 0
for i in range(n-1):
c.append(cHigh[i])
for i in range(n-1):
c[n//2 + i] += cMid[i] - (cLow[i] + cHigh[i])
return c
我在cMid = karatsuba(tA, tB)
. 有人有意见吗?我会非常感激!