Overview:
-
The trace of a matrix is defined as the sum of the elements along with its main diagonal.
-
The trace of a matrix can be calculated for off-diagonals as well. The off-diagonals are diagonals above or below the main diagonal.
-
The NumPy function from the linalg module provides the trace() function that calculates and returns the trace for a given matrix for the specified diagonal. The off-diagonals are specified using an offset to the function trace().
Example:
# Example Python program that finds the trace of a matrix # Create a rectangular matrix of order 3x4 # Sum along the main diagonal # Sum of the off-diagonal whose offset is 1 # Sum of the off-diagonal whose offset is -1 |
Output:
Rectangular matrix: |