Skip to content

np.sum(a * b) versus a @ b #463

Closed

Description

@oyamad

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:

===

===

Activity

  1. jstac commented on Jun 16, 2025

    @jstac
    Contributor

    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?

  2. mmcky commented on Jun 19, 2025

    @mmcky
    Contributor

    These are the following instances using a regex pattern

    • lectures/aiyagari.md: return np.sum(asset_probs * am.a_vals) [aiyagari] MAINT: transfer np.sum(a * b) to a @ b #467
    • lectures/career.md: self.F_mean = np.sum(self.θ * self.F_probs)
    • lectures/career.md: self.G_mean = np.sum(self.ϵ * self.G_probs)
    • lectures/lake_model.md: β * γ * np.sum(np.maximum(U, V) * p_vec)
    • lectures/lake_model.md: w = np.sum(V * p_vec * (w_vec - τ > w_bar)) / np.sum(p_vec * (w_vec -
    • lectures/linear_algebra.md:np.sum(x * y) # Inner product of x and y
    • lectures/mccall_model_with_separation.md: d_new = np.sum(np.maximum(v, u(c) + β * d) * q)
    • lectures/mccall_model.md: reject = c + β * np.sum(v * q)
    • lectures/mccall_model.md: return (1 - β) * (c + β * np.sum(v * q))
    • lectures/mccall_model.md: h = np.sum(w * q) / (1 - β)
    • lectures/mccall_model.md: h_next = c + β * np.sum(s * q)
    • lectures/mccall_q.md: reject = c + β * np.sum(v * q)
    • lectures/mle.md: return np.sum(y * np.log(μ) - μ - np.log(factorial(y)))
    • lectures/mle.md: return np.sum(y * np.log(μ) + (1 - y) * np.log(1 - μ))
    • lectures/mle.md: return np.sum((X.T * y * ϕ / μ - X.T * (1 - y) * ϕ / (1 - μ)),
    • lectures/multivariate_normal.md:Σθ_hat_arr_C = np.array([np.sum(C[n, i+1:n+1] ** 2) for i in range(n)])
    • lectures/opt_transport.md:total_cost = np.sum(X * C)
    • lectures/prob_matrix.md:mean = np.sum(Benford_pmf * k)
    • lectures/prob_matrix.md:var = np.sum([(k-mean)**2 * Benford_pmf])
  3. mmcky commented on Jun 19, 2025

    @mmcky
    Contributor

    Suggested approach:

    1. change the current code-cell to hide-input and add a cell to save the result for comparison with new code
    2. add a new code-cell with the updated code
    3. include another code-cell with an assert to test equality of the former result and the new result
    4. ask co-pilot to provide a review (optional)

    Once this has passed testing

    1. Ask for review of the PR, and/or remove the original and comparison code

    Example: #467

  4. oyamad commented on Jun 23, 2025

    @oyamad
    MemberAuthor

    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 with x @ y, and np.sqrt(np.sum(x**2)) with np.sqrt(x @ x).

  5. oyamad commented on Jun 24, 2025

    @oyamad
    MemberAuthor

    Or maybe have both "take 1: np.sum(x * y)" and "take 2: x @ y" and explain that the latter is better?

  6. mmcky commented on Jul 1, 2025

    @mmcky
    Contributor

    Or maybe have both "take 1: np.sum(x * y)" and "take 2: x @ y" and explain that the latter is better?

    @oyamad thank you. I think this suggestion is great. It is a confusing aspect of numpy and demonstrating the difference between both seems like a good addition to me.

    cc: @jstac

  7. mmcky commented on Jul 1, 2025

    @mmcky
    Contributor

    @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.

  8. mmcky commented on Aug 12, 2025

    @mmcky
    Contributor

    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.

  9. mmcky commented on Aug 14, 2025

    @mmcky
    Contributor

    @copilot can you please review this issue to see if we have caught all cases in this lecture series (lecture folder).

    replacing np.sum(a * b) with a @ b` as suggested above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions