import numpy as np from scipy.fftpack import fft, ifft defnextPower2(L): return np.power(2,np.ceil(np.log2(L))).astype(int) defint2Array(n, size): res = np.zeros(nextPower2(size), dtype = np.int8) i = 0 while n > 0: n, res[i] = divmod(n, 10) i += 1 return res defarray2Int(arr): return np.dot(np.around(arr,0),10**np.arange(len(arr)))
1 2 3 4 5 6 7 8 9 10 11 12 13 14
a = fft(int2Array(34, 4)) b = fft(int2Array(77, 4)) c = a * b d = ifft(c) e = array2Int(d) a,b,c,d,e # (array([7.-0.j, 4.-3.j, 1.-0.j, 4.+3.j]), # array([14.-0.j, 7.-7.j, 0.-0.j, 7.+7.j]), # array([98. -0.j, 7.-49.j, 0. -0.j, 7.+49.j]), # array([28.+0.j, 49.+0.j, 21.-0.j, 0.+0.j]), # (2618+0j)) array2Int(ifft(fft(int2Array(457, 6))*fft(int2Array(756, 6)))) # (345492+0j)