# trace() Function & Examples

Use the trace() matrix function in Calcul.io. Review its syntax, edit working examples, understand the result, and explore related math functions.

## trace

#### trace(A)

Try it yourself:

```calculio
A = [1, 2, 3; -1, 2, 3; 2, 0, 3]
trace(A)
```

[concat](https://calcul.io/function/concat/index.md)

[det](https://calcul.io/function/det/index.md)

[diag](https://calcul.io/function/diag/index.md)

[identity](https://calcul.io/function/identity/index.md)

[inv](https://calcul.io/function/inv/index.md)

[ones](https://calcul.io/function/ones/index.md)

[range](https://calcul.io/function/range/index.md)

[size](https://calcul.io/function/size/index.md)

[squeeze](https://calcul.io/function/squeeze/index.md)

[subset](https://calcul.io/function/subset/index.md)

[transpose](https://calcul.io/function/transpose/index.md)

[zeros](https://calcul.io/function/zeros/index.md)

---

## trace(): sum a matrix diagonal

**trace(A)** adds the main diagonal entries of a square matrix. For [[a,b],[c,d]], the trace is a+d.

## Why it matters

Trace is invariant under a change of basis and equals the sum of eigenvalues, counted with multiplicity. It appears in linear systems, statistics, and matrix calculus.

## Related matrix tools

Use [det()](https://calcul.io/function/det/index.md) for the determinant, [transpose()](https://calcul.io/function/transpose/index.md) to reflect a matrix, [diag()](https://calcul.io/function/diag/index.md) to work with diagonal entries, and [sum()](https://calcul.io/function/sum/index.md) for general aggregation. [identity()](https://calcul.io/function/identity/index.md) has trace equal to its dimension.

## Caveat

Trace requires a square matrix; check dimensions first with [size()](https://calcul.io/function/size/index.md).

[All functions](https://calcul.io/functions/index.md)
