Codebook, wrong output in shape of an array

Hi, I have the following error in Codebook Codercise F.1.4:
Error: operands could not be broadcast together with shapes (8,) (4,)

I have run it in my jupyter notebook and I have shape (8,)
Best,
Adin

I am running this code

def fft_multiplication(poly_a, poly_b):
“”"Returns the result of multiplying two polynomials

Args:
    poly_a (array[complex]): 1-D array of coefficients 
    poly_b (array[complex]): 1-D array of coefficients 

Returns: 
    array[complex]: complex coefficients of the product
        of the polynomials
"""
##################
# YOUR CODE HERE #
################## 

# Calculate the number of values required
value= len(poly_a)+ len(poly_b)+ 1
# Figure out the nearest power of 2
n= nearest_power_of_2(value)
# Pad zeros to the polynomial
poly_a= np.pad(poly_a, (0,n-len(poly_a)))
poly_b= np.pad(poly_b, (0,n-len(poly_b)))
# Convert the polynomials to value representation 
value_a= coefficients_to_values(poly_a)
value_b= coefficients_to_values(poly_b)
# Multiply
#value_c= np.zeros((n,),dtype=complex)
#for index in range(n):
#    value_c[index]= value_a[index] * value_b[index]
value_c= np.multiply(value_a,value_b)
#print(value_c)
# Convert back to coefficient representation
return values_to_coefficients(value_c)

This post helped me

1 Like

Glad to know you figured it out! Yes, it’s a bit sneaky :slight_smile:

1 Like
##################
# YOUR CODE HERE #
################## 

# Calculate the number of values required
n=max(len(poly_a), len(poly_b))+1
# Figure out the nearest power of 2
np2=nearest_power_of_2(n)

# Pad zeros to the polynomial
a= np.pad(poly_a, (0, np2-len(poly_a)), 'constant')
b= np.pad(poly_b, (0, np2-len(poly_b)), 'constant')
# Convert the polynomials to value representation 
c=coefficients_to_values(a)
d=coefficients_to_values(b)


# Multiply
e=np.multiply(value_a,value_b)

# Convert back to coefficient representation

return values_to_coefficients(e)