Compute the trace of a square matrix, which is the sum of its main-diagonal entries:
tr(A)=i=1∑NAiiHere, A is an N×N matrix and Aii is its entry in row i and column i. Compute the sum directly from indexed elements and return it as a Python float.
Input: A = [[1, 2], [3, 4]]
Output: 5
Explanation: The main diagonal contains 1 and 4.
Input: A = [[2, -1, 0], [3, 5, 1], [0, 2, -2]]
Output: 5
Input: A = [[42]]
Output: 42
Accumulate A[i, i] for i from zero through A.shape[0] - 1.
Convert the final NumPy scalar with float(total).
Sign in to take notes on this problem
Accepts: array
Compute the trace of a square matrix, which is the sum of its main-diagonal entries:
tr(A)=i=1∑NAiiHere, A is an N×N matrix and Aii is its entry in row i and column i. Compute the sum directly from indexed elements and return it as a Python float.
Input: A = [[1, 2], [3, 4]]
Output: 5
Explanation: The main diagonal contains 1 and 4.
Input: A = [[2, -1, 0], [3, 5, 1], [0, 2, -2]]
Output: 5
Input: A = [[42]]
Output: 42
Accumulate A[i, i] for i from zero through A.shape[0] - 1.
Convert the final NumPy scalar with float(total).
Sign in to take notes on this problem
Accepts: array