NumPy trace adds diagonal entries instead of returning a diagonal vector
A diagonal can be described as a sequence of positions or as a sum of the values at those positions. NumPy trace performs the sum. Academia Master Trader presents this English lesson for readers in Kuwait, using constructed arrays to separate diagonal extraction, diagonal addition and the choice of axes in a higher dimensional array.
Start with the main diagonal of a matrix
After import numpy as np, define m = np.array([[4,7,10],[13,16,19],[22,25,28]]). The call np.trace(m) returns 48, because the main diagonal contains four, sixteen and twenty eight.
This result is a single sum, not the vector [4,16,28]. The distinction matters whenever a later calculation expects individual entries. Trace combines those entries through addition. It does not preserve them as a separate sequence, and it does not add every value in the matrix.

An offset selects a different diagonal
The call np.trace(m,offset=1) returns 26, from seven plus nineteen. Positive offsets select diagonals above the main diagonal. The call np.trace(m,offset=-1) returns 38, from thirteen plus twenty five, on the diagonal below it.
Only the selected diagonal contributes to each sum. The offset does not select a triangular region. In particular, using offset one does not include the main diagonal or every entry above it. Keep the word diagonal separate from triangle when describing the operation.
Rectangular input still has a usable diagonal
Define r = np.array([[2,5,8],[11,14,17]]), with shape (2,3). Its main diagonal contains two and fourteen, so np.trace(r) returns 16. A square input is not required.
The selected diagonal ends when one of the participating axes runs out of positions. An offset can therefore change the number of entries being added. Record the array shape and the offset alongside the sum so that the result remains interpretable.
Higher dimensional input needs an explicit axis choice
Define b = np.arange(1,9).reshape(2,2,2). By default, np.trace(b) uses axes zero and one, returning [8,10]. It does not automatically choose the final two axes.
For a stack of matrices stored along the leading axis, write np.trace(b,axis1=1,axis2=2). This returns [5,13], the main diagonal sum of each matrix in that stack. Both calls are valid, but their axis choices describe different groups of positions. The output shape removes the two participating axes and keeps the others.
The returned shape is useful evidence about which axes were combined. In this example one axis remains, so the answer has two entries. Check the axis numbers against the arrangement of the data before calling the function. A correct sum over the wrong pair of axes still answers the wrong question.
State the arithmetic and the data type
The optional dtype controls the output and accumulator type. NumPy's default handling can promote small integer inputs to the default integer precision. Record a deliberate dtype choice when the accumulation type matters to your calculation, rather than treating the input label as a complete description of the sum.
Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Write down the selected diagonal, its participating axes and the returned shape before using a trace result in another calculation.