# Matrix trace

`matrix-trace` · version 1.0.0 · Vectors & matrices · free, no key needed

Compute the trace (sum of diagonal entries) of a square finite-number matrix.

**Use when you need to: matrix trace · sum of diagonal entries.**

## Supported

- matrix trace
- sum of diagonal entries

## Not supported

- non-square matrices
- generalized trace over a sub-diagonal

## Behavior

- Input matrix is a square NxN array of finite JSON numbers, N from 1 to 20, magnitude bounded to at most 1000000.
- The result is the left-to-right IEEE-754 double-precision accumulation of matrix[i][i] for every diagonal index i.
- A result of -0 is normalized to 0.

## Input

- `matrix` (array of array of number, required): min items 1; max items 20

## Output

- `result` (number, required)

## Limits

- max dim: 20
- max abs value: 1000000

## Example

Request input:

```json
{
  "matrix": [
    [
      1,
      2
    ],
    [
      3,
      4
    ]
  ]
}
```

Response:

```json
{
  "result": {
    "result": 5
  }
}
```

## How to call it

### MCP

Connect `https://computefirst.net/mcp` ([setup](/docs#connect)), then call `execute` with:

```json
{
  "id": "matrix-trace",
  "version": "1.0.0",
  "input": {
    "matrix": [
      [
        1,
        2
      ],
      [
        3,
        4
      ]
    ]
  }
}
```

### HTTP (no key)

```sh
curl -X POST https://computefirst.net/v1/tools/matrix-trace/versions/1.0.0/execute \
  -H "Content-Type: application/json" \
  -d '{"matrix":[[1,2],[3,4]]}'
```

The machine-readable contract is at [/v1/tools/matrix-trace/versions/1.0.0](/v1/tools/matrix-trace/versions/1.0.0).

### CLI

```sh
node cli.mjs run matrix-trace 1.0.0 --input input.json --base-url https://computefirst.net
```

Get the client at [/clients/cli/](/clients/cli/).

## Related tools

- [Matrix add](/tools/matrix-add): Add two equal-shape finite-number matrices elementwise.
- [Matrix determinant](/tools/matrix-determinant): Compute the determinant of a small square finite-number matrix.
- [Vector outer product](/tools/vector-outer-product): Compute the outer product of two finite-number vectors, producing a matrix.
- [Matrix hadamard](/tools/matrix-hadamard): Multiply two equal-shape finite-number matrices elementwise (Hadamard product).
- [Matrix identity](/tools/matrix-identity): Construct the NxN identity matrix for a given size.
- [Matrix inverse](/tools/matrix-inverse): Compute the inverse of a small square finite-number matrix.
