NumPy trace adds the entries on a chosen diagonal rather than returning the diagonal sequence
Reading a matrix diagonal and adding its entries are different operations. One preserves an ordered sequence, while the other produces a total for that selected diagonal. Academia Master Trader explains NumPy trace in English for readers in Hungary. We will begin with a three by three matrix, identify coordinates and then calculate the main trace and two offset traces. The arithmetic uses only the supplied integers. These examples describe array operations, not financial performance, a forecast or additional observations beyond the input.
Record the full input and selected positions
After import numpy as np, define a = np.array([[8,3,6],[2,9,4],[7,5,1]]). Its shape is (3,3), with row and column indices from zero through two. The main diagonal contains eight at (0,0), nine at (1,1) and one at (2,2). These pairs share the same row and column index. None of the other six positions belongs to this main diagonal.
The call np.trace(a) returns 18, because 8 + 9 + 1 = 18. For this two dimensional input, the result is a single sum rather than an array containing three separate diagonal entries. The operation selects the diagonal according to its coordinates and then adds the selected values. Naming both steps makes clear why a value outside the diagonal does not contribute.
Distinguish the trace from the diagonal sequence
For the same input, np.diagonal(a) returns [8,9,1], while np.trace(a) returns 18. The three entry sequence and its total are related, but they are not interchangeable descriptions of an output. The sequence keeps the individual values visible. The sum combines them into one result, which no longer records their original order as separate entries.
Different diagonal sequences can have the same sum. The sequence [7,8,3] also adds to eighteen, but it is not the main diagonal of our input. Knowing the trace alone therefore does not recover every contributing entry. Keep the source matrix or the selected coordinate pairs beside the total when explaining where it came from. The total is a summary of selected values, not a complete description of their arrangement.

Read the figure as selection followed by addition
The figure highlights the main diagonal inside the input matrix and places the sum beside it. Read the highlighted coordinates in increasing row order: (0,0), (1,1) and (2,2). Their values are eight, nine and one. The displayed eighteen is the result of their addition, not a tenth input cell or a replacement value inserted into the original matrix.
The largest supplied entry, nine, contributes because it lies on the selected diagonal. One contributes for exactly the same positional reason. Seven does not contribute to this call because it occupies (2,0), outside the main diagonal. A trace is therefore not the sum of the largest numbers, a row total or a column total. The coordinate selection comes before the arithmetic.
Add an upper offset diagonal
The call np.trace(a,offset=1) returns 7. It adds three at (0,1) and four at (1,2), so 3 + 4 = 7. The column index is one greater than the row index at both positions. This selects the first diagonal above the main diagonal. Only two matching positions fit inside this matrix, so only those two entries contribute.
A positive offset does not add one to the main trace, nor does it move all input values. It chooses a different diagonal before performing the sum. For offset=2, the only contributing position is (0,2), containing six, so the result is six. The matrix dimensions explain why that diagonal has only one entry in this example.
Add a lower offset diagonal
The call np.trace(a,offset=-1) returns 7 as well, this time from two at (1,0) and five at (2,1). The sum is 2 + 5 = 7. This equals the upper offset total numerically, but the contributing coordinates and values are different. Equal results do not establish that the same diagonal was selected.
A rectangular input works too. For b = np.array([[8,3,6],[2,9,4]]), np.trace(b) returns seventeen from eight and nine. Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the shape and offset, list contributing positions and write the addition explicitly. Those checks keep a diagonal total distinct from its source sequence and from sums over other regions.