我完成了一项任务,但在第5和第6部分有点卡住了。它基本上是这样的:使用FFT乘以两个二进制数。我想知道是否有人可以提供帮助。
# The binary numbers and their product
a_bin = 0b100100100100
b_bin = 0b111000111000
c_bin = a_bin * b_bin
print('The product of a and b is', c_bin)
# (i) The coefficients of the polynomials A and B
Acoeff = [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1]
Bcoeff = [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1]
# (ii) the value representations of A and B
Aval = np.fft(Acoeff, 32)
Bval = np.fft(Bcoeff, 32)
# (iii) The value representation of C
Cval = []
for i in range(len(Aval)):
Cval.append(Aval[i] * Bval[i])
print(Cval)
# (iv) The coefficients of the polynomial C
Ccoeff = np.ifft(Cval)
# we'll get rid of the imaginary parts, which are just numerical errors
for i, c in enumerate(Ccoeff):
Ccoeff[i] = int(round(c.real))
# (v) calculate the product by evaluating the polynomial at 2, i.e., C(2)
# (You may need to take the real part at the end if there is a small imaginary component)
prod = 0
print('Using the FFT the product of a and b is', int(round(prod.real)))
# (vi) write code to calculate the binary digits of c directly from the coefficients of C, Ccoeff.
# hint: You can use (q,r) = divmod(x, 2) to find the quotient and remainder of x when divided by 2