The trace of a square matrix is the sum of the numbers running down its main diagonal — top-left to bottom-right. Everything off that diagonal is ignored completely.
Task: write trace(matrix) returning that sum.
matrix is a list of rows and is always square, so it has as many rows as columns.matrix[0][0], matrix[1][1], matrix[2][2], and so on.It looks like an arbitrary thing to add up, and it isn't. The trace equals the sum of the matrix's eigenvalues, which means you can read that total straight off the diagonal without computing a single eigenvalue. It also survives a change of basis untouched, which is why it shows up everywhere from physics to the gradient formulas behind neural networks.