🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.trace()

AI & DATA SCIENCE // np-trace

np.trace() returns the sum of the elements along a matrix's main diagonal.

Syntax

np.trace(arr, offset=0)

Deep Dive Course

For a square matrix, the trace is simply the sum of the elements where the row index equals the column index — the same diagonal that np.eye() fills with ones. For a non-square matrix, trace() still works, summing along the diagonal up to the shorter dimension. The offset parameter lets you sum a diagonal above (positive offset) or below (negative offset) the main one instead. The trace is a useful summary statistic in linear algebra — for example, it equals the sum of a matrix's eigenvalues, a fact used in various numerical algorithms.

1Understanding np.trace()

For a square matrix, the trace is simply the sum of the elements where the row index equals the column index — the same diagonal that np.eye() fills with ones. For a non-square matrix, trace() still works, summing along the diagonal up to the shorter dimension. The offset parameter lets you sum a diagonal above (positive offset) or below (negative offset) the main one instead. The trace is a useful summary statistic in linear algebra — for example, it equals the sum of a matrix's eigenvalues, a fact used in various numerical algorithms.

💡

The trace equals the sum of a matrix's eigenvalues — a useful mental shortcut and sanity check when working with eigenvalue-related computations, since you can verify np.trace(A) roughly matches the sum of np.linalg.eigvals(A).

editor.html
import numpy as np

matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(np.trace(matrix))
localhost:3000

2Practical Example

Here is a real-world application of np.trace() showing how it is used in production NumPy code.

editor.html
import numpy as np

matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(np.trace(matrix, offset=1))
localhost:3000

3Best Practices

Follow these guidelines when working with np.trace():

1. Use np.trace() directly instead of manually extracting the diagonal with np.diagonal() and summing it, for clarity

2. Use the offset parameter when you specifically need a diagonal other than the main one, rather than manually indexing

3. Remember trace() works on non-square matrices too, summing only up to the length of the shorter dimension

⚠️

Tip: The trace equals the sum of a matrix's eigenvalues — a useful mental shortcut and sanity check when working with eigenvalue-related computations, since you can verify np.trace(A) roughly matches the sum of np.linalg.eigvals(A).

editor.html
import numpy as np

matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(np.trace(matrix))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(np.trace(matrix))
Example 02Advanced Example
import numpy as np

matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(np.trace(matrix, offset=1))

Best Practices

  • Use np.trace() directly instead of manually extracting the diagonal with np.diagonal() and summing it, for clarity
  • Use the offset parameter when you specifically need a diagonal other than the main one, rather than manually indexing
  • Remember trace() works on non-square matrices too, summing only up to the length of the shorter dimension

Interview Question

Why is np.trace(matrix) mathematically equal to the sum of that matrix's eigenvalues?

Hint: This is a specific, well-known linear algebra theorem, not a coincidence.

This is a standard result from linear algebra: for any square matrix, the trace, the sum of its main diagonal elements, always equals the sum of its eigenvalues, counted with multiplicity, regardless of what those eigenvalues actually are, since they can even be complex. This relationship holds because both the trace and the sum of eigenvalues can be derived from the coefficients of the matrix's characteristic polynomial, which is invariant regardless of the basis used to represent the matrix.

Exercises

MediumPractice using np.trace() in a real scenario.
View Solution
import numpy as np

matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(np.trace(matrix))

Frequently Asked Questions

Why is np.trace(matrix) mathematically equal to the sum of that matrix's eigenvalues?

This is a standard result from linear algebra: for any square matrix, the trace, the sum of its main diagonal elements, always equals the sum of its eigenvalues, counted with multiplicity, regardless of what those eigenvalues actually are, since they can even be complex. This relationship holds because both the trace and the sum of eigenvalues can be derived from the coefficients of the matrix's characteristic polynomial, which is invariant regardless of the basis used to represent the matrix.

Related Functions

np-linalg-eignp-linalg-detndarray-t