I'm new to numpy and I want to calculate the sum of abs of all off-diagonal elements of a numpy array. off-diagonal elements of a matrix are all elements of a matrix except ones that are in the main diagonal of it.
I want to calculate the sum of abs of them so I can implement the Jacobi eigenvalue algorithm of this lecture
so, to compute it, I think this code will work:
import numpy as np
off_diagonal_sum = 0
for i in range(n): # n is the dimension of our square matrix
# mat is our matrix
off_diagonal_sum = off_diagonal_sum + np.sum(np.abs(mat[i, (i + 1):n]))
off_diagonal_sum = off_diagonal_sum + np.sum(np.abs(mat[i, 0:(i - 1)]))
but as I'm new to numpy, I think there should be a simpler and shorter way to compute that. do you have any idea?
thanks in advance.