Repository navigation
Closed
Description
Inner dots are computed as np.sum(a * b) in many places. Consider replacing it with a @ b. (See also the 3rd item in #169 (comment).)
Below is the output of ChatGPT:
===
- Under the hood:
a @ b(ornp.dot(a, b)) delegates to optimized BLAS libraries that implement fused multiply-add (FMA) operations, eliminating the temporary array and reducing rounding errors compared to separate multiply and sum steps. (https://stackoverflow.com/questions/18092984/numpy-difference-between-dota-b-and-ab-sum) - Performance overhead:
np.sum(a * b)allocates an intermediate array and then sums it, incurring extra memory traffic and Python-level looping; by contrast, BLAS kernels stream data efficiently in optimized C code. (https://stackoverflow.com/questions/75556221/why-is-np-dot-so-much-faster-than-np-sum) - Numerical accuracy: BLAS libraries use hardware FMA instructions to fuse multiply and add into a single rounding operation, improving precision over two separate operations. (https://stackoverflow.com/questions/45200278/numpy-fusing-multiply-and-add-to-avoid-wasting-memory)
- Official recommendation: NumPy’s documentation advises using
np.dot(a, b)or the@operator (vianp.matmul, per PEP 465) for dot products and matrix multiplication. (https://numpy.org/doc/stable/reference/generated/numpy.dot.html, https://numpy.org/doc/stable/reference/generated/numpy.matmul.html, https://peps.python.org/pep-0465/)
===
Reacted by Copilot
Metadata
Metadata
Assignees
Labels
No labels
Activity
jstac commentedon Jun 16, 2025
Thanks @oyamad , this is very helpful -- and interesting.
@mmcky -- would you mind to supervise fixes? Perhaps @oyamad 's students or one of our RAs can take this job?
mmcky commentedon Jun 19, 2025
These are the following instances using a regex pattern
mmcky commentedon Jun 19, 2025
Suggested approach:
hide-inputand add a cell to save the result for comparison with new codecode-cellwith the updated codecode-cellwith an assert to test equality of the former result and the new resultOnce this has passed testing
Example: #467
oyamad commentedon Jun 23, 2025
On
lectures/linear_algebra.md:np.sum(x * y) # Inner product of x and y:This requires some discussion: Does the lecture want to teach element-wise multiplication (
x * y) and power (x ** 2)?If not,
np.sum(x * y)should be replaced withx @ y, andnp.sqrt(np.sum(x**2))withnp.sqrt(x @ x).oyamad commentedon Jun 24, 2025
Or maybe have both "take 1:
np.sum(x * y)" and "take 2:x @ y" and explain that the latter is better?mmcky commentedon Jul 1, 2025
@oyamad thank you. I think this suggestion is great. It is a confusing aspect of
numpyand demonstrating the difference between both seems like a good addition to me.cc: @jstac
mmcky commentedon Jul 1, 2025
@suda-yuga and @UdohNakamura -- thank you for your PR's ❤ -- I have enabled all the automated checks to be run and will organize to review them.
mmcky commentedon Aug 12, 2025
Thanks for opening this @oyamad -- it is a great use of AI and it taught us a lot.
I think this issue can be closed now. I have merged many of the cases pickup up by your team. Thank you!
We have initiated the Lecture improvements project with the global RA team, and we are incorporating some of the AI ideas in that project. The team is also undertaking detailed reviews of this lecture series so will keep an eye out for any further improvements as suggested in this issue.
mmcky commentedon Aug 14, 2025
@copilot can you please review this issue to see if we have caught all cases in this lecture series (
lecturefolder).replacing
np.sum(a * b) witha @ b` as suggested above.