Trace by Hand

easynumpy_linalg

Implement trace(X).

Return the trace of a square matrix X (the sum of its diagonal entries). Compute it yourself — np.trace is not allowed — and no for/while loops.

Constraints
  • no scipy, sklearn
  • no trace
  • no for/while loops

Your solution

Edit trace and run the real pytest suite in your browser — no install required. Your code is saved locally.

⌘/Ctrl + ↵

Loading the Python runtime (first run only)…

Hints

Hint 1

np.diag pulls out the diagonal; sum it. (np.einsum('ii->', X) also works.)