def convolve_poly_split(A, B, wf=dft.omega2, error=0): lenA, lenB = len(A), len(B) if lenA != lenB: raise Exception('Coefficient arrays must be of the same length') ## 1. pad A and B with zeros until their lengths are an integral ## power of 2 padded_A = dft.pad_with_zeros(A) padded_B = dft.pad_with_zeros(B) ## 2. pad A and B more so that their length is 2*n, where n is ## the length of padded_A. padded_A = pad_with_zeros_until_required_length(padded_A, 2 * len(padded_A)) padded_B = pad_with_zeros_until_required_length(padded_B, 2 * len(padded_A)) ## 3. compute dft of padded A and B fft_A = dft.recursive_fft(padded_A, wf=wf, error=error) fft_B = dft.recursive_fft(padded_B, wf=wf, error=error) ## 4. compute pointwise multiplication of A and B C = [] for a, b in zip(fft_A, fft_B): C.append(a * b) ## 5. compute inverse dft of A*B and split the output into ## real and imaginary numbers. reals, imags = dft.recursive_inverse_fft_split(C, wf=wf, error=error) ## 6. the degree of A and B is lenA-1. the degree of A*B is ## 2*(lenA-1) = 2*lenA - 2. return reals[0:2 * lenA - 1], imags[0:2 * lenA - 1]
def convolve_poly_split(A, B, wf=dft.omega2, error=0): lenA, lenB = len(A), len(B) if lenA != lenB: raise Exception('Coefficient arrays must be of the same length') ## 1. pad A and B with zeros until their lengths are an integral ## power of 2 padded_A = dft.pad_with_zeros(A) padded_B = dft.pad_with_zeros(B) ## 2. pad A and B more so that their length is 2*n, where n is ## the length of padded_A. padded_A = pad_with_zeros_until_required_length(padded_A, 2*len(padded_A)) padded_B = pad_with_zeros_until_required_length(padded_B, 2*len(padded_A)) ## 3. compute dft of padded A and B fft_A = dft.recursive_fft(padded_A, wf=wf, error=error) fft_B = dft.recursive_fft(padded_B, wf=wf, error=error) ## 4. compute pointwise multiplication of A and B C = [] for a, b in zip(fft_A, fft_B): C.append(a*b) ## 5. compute inverse dft of A*B and split the output into ## real and imaginary numbers. reals, imags = dft.recursive_inverse_fft_split(C, wf=wf, error=error) ## 6. the degree of A and B is lenA-1. the degree of A*B is ## 2*(lenA-1) = 2*lenA - 2. return reals[0:2*lenA-1], imags[0:2*lenA-1]
def convolve_poly(A, B, wf=dft.omega2, error=0): ## 1. ensure that coefficient arrays A and B are of the same length lenA, lenB = len(A), len(B) if lenA != lenB: raise Exception('Coefficient arrays must be of the same length') ## 2. pad A and B until their lengths are an integral power of 2 padded_A = dft.pad_with_zeros(A) padded_B = dft.pad_with_zeros(B) ## 3. pad A and B more so that their lengths are 2n, where n ## is the length of padded A padded_A = pad_with_zeros_until_required_length(A, 2 * len(padded_A)) padded_B = pad_with_zeros_until_required_length(B, 2 * len(padded_A)) ## 4. compute dft of padded A and B fft_A = dft.recursive_fft(padded_A, wf=wf, error=error) fft_B = dft.recursive_fft(padded_B, wf=wf, error=error) ## 5. compute pointwise multiplication of A and B C = [] for a, b in zip(fft_A, fft_B): C.append(a * b) ## 6. compute inverse dft and return the first 2*lenA-2 elements. return dft.recursive_inverse_fft(C, wf=wf, error=error)[0:2 * lenA - 1]
def convolve_poly(A, B, wf=dft.omega2, error=0): ## 1. ensure that coefficient arrays A and B are of the same length lenA, lenB = len(A), len(B) if lenA != lenB: raise Exception('Coefficient arrays must be of the same length') ## 2. pad A and B until their lengths are an integral power of 2 padded_A = dft.pad_with_zeros(A) padded_B = dft.pad_with_zeros(B) ## 3. pad A and B more so that their lengths are 2n, where n ## is the length of padded A padded_A = pad_with_zeros_until_required_length(A, 2*len(padded_A)) padded_B = pad_with_zeros_until_required_length(B, 2*len(padded_A)) ## 4. compute dft of padded A and B fft_A = dft.recursive_fft(padded_A, wf=wf, error=error) fft_B = dft.recursive_fft(padded_B, wf=wf, error=error) ## 5. compute pointwise multiplication of A and B C = [] for a, b in zip(fft_A, fft_B): C.append(a*b) ## 6. compute inverse dft and return the first 2*lenA-2 elements. return dft.recursive_inverse_fft(C, wf=wf, error=error)[0:2*lenA-1]