ITADN

Puzzle 11: conv_spec does not implement convolution

#35OpenBrandonConder 创建于 2024-06-21
B
BrandonCondercommented
The conv_spec function does not implement convolution: ```python def conv_spec(a, b): out = np.zeros(*a.shape) len = b.shape[0] for i in range(a.shape[0]): out[i] = sum([a[i + j] * b[j] for j in range(len) if i + j < a.shape[0]]) return out ``` One of the inputs should be index-reversed (see [wikipedia](https://en.wikipedia.org/wiki/Convolution#Discrete_convolution)) We can also confirm the error using numpy.convolve ```python np.convolve(a, b, mode='same') ``` Test 1 output: ```python array([ 0, 1, 4, 7, 10, 13]) ``` which conflicts with the conv_spec output: ```python Spec : [ 5. 8. 11. 14. 5. 0.] ``` Test 2 output: ```python array([ 0, 1, 4, 10, 16, 22, 28, 34, 40, 46, 52, 58, 64, 70, 76]) ``` which also conflicts with the conv_spec output: ```python Spec : [14. 20. 26. 32. 38. 44. 50. 56. 62. 68. 74. 80. 41. 14. 0.] ```
1 条评论