# Numpy Trace: A Guide to Calculating Trace Using Numpy in Python Matrix is fun! Not the movie, but the one from mathematics. After having a decent understanding of the properties of matrices, one can very well tap into their potential & deploy them over a wide range of applications. Data encryption and password generation to name a few!

This article will help you to understand one of the many properties of matrices – the matrix trace. The trace aids in calculating the sum of the diagonal elements in any given matrix. Let’s get started by importing the numpy library using the below code.

```import numpy as np
```

We shall further explore the trace( ) function through each of the following sections.

• Syntax of trace( ) function
• Calculating Trace for Two-Dimensional Arrays
• Calculating Trace for Three-Dimensional Arrays
• Using Offset for Calculating Trace

## Syntax of trace( ) function

The trace( ) function sums up all the elements in the diagonal of any given N-dimensional array, but things just don’t end there. It also happens to be packed with a handful of features such as those given within the syntax below,

```numpy.trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None)
```

where,

• a – N-dimensional array for which the trace is to be calculated
• offset – an optional construct used to offset the trace calculation from the main diagonal. Set to zero (0) by default, one can also use ‘+1’ or ‘-1’ to offset the main diagonal positively or negatively respectively
• axis1 – an optional construct used to specify the first axis of the two-dimensional sub-arrays from which the diagonal is to be taken. Set to zero (0) by default.
• axis2 – an optional construct used to specify the second axis of the two-dimensional sub-arrays from which the diagonal is to be taken. Set to one (1) by default.
• dtype – an optional construct set to none by default, but could be used to specify the data type which is being used
• out – an optional construct set to none by default, but could be used to store the result

## Calculating Trace for Two-Dimensional Arrays

After importing the numpy library let us find the trace of a two-dimensional array using the code given below.

```ar1 = np.array([[12, 5],
[5, 9]], dtype = int)
np.trace(ar1)
```

Once the above code is run, the following computation happens in the back end for calculating the trace of the matrix given in the form of an array.

• The first element of the first row from which the diagonal starts is added with the second element of the second row where the diagonal ends.
• The sum of the two elements (i.e) 12+9 = 21 is then returned as the trace of the given input array.

The final answer deduced in the last step as stated above can also be seen as the result in the below image when the code is run.

## Calculating Trace for Three-Dimensional Arrays

The same technique can also be deployed while calculating the trace of a three-dimensional array such as the one given below.

```ar2 = np.array([[2.1, 5.5, 8.8], [10.2, 7.3, 3.5], [4.7, 9.8, 6.3]], dtype=float)
np.trace(ar2, dtype=float)
```

Since the data type has been specified the diagonal elements are added (2.1+7.3+6.3 = 15.7) & the result with the decimal value has been returned as the trace for the given input array.

## Using Offset for Calculating Trace

Trace shall be calculated for the same array used in the above section, but with the diagonal being offset by ‘+1’.

```ar2 = np.array([[2.1, 5.5, 8.8], [10.2, 7.3, 3.5], [4.7, 9.8, 6.3]], dtype=float)
np.trace(ar2, offset=+1, dtype=float)
```