[Python] Fastest way to calculate the sum of row-wise "v.T @ v" of a matrix

Discussão em 'Python' iniciado por Stack, Junho 19, 2025.

  1. Stack

    Stack Membro Participativo

    I have a 1000 x 500 matrix. For each row, I want to calculate the dot product between the transpose of the row vector and the row vector itself (which should be a scalar), and sum over all rows. Right now I am using

    import numpy as np

    A = np.random.random((1000, 500))
    res = sum(A.T @ A for i in range(A.shape[0]))


    Since this is the performance bottleneck of my algorithm, I wonder if there are faster ways to do this, peferably a Numpyic solution.

    Continue reading...

Compartilhe esta Página