1 |
|
2 |
|
3 | # Function trace
|
4 |
|
5 | Calculate the trace of a matrix: the sum of the elements on the main
|
6 | diagonal of a square matrix.
|
7 |
|
8 |
|
9 | ## Syntax
|
10 |
|
11 | ```js
|
12 | math.trace(x)
|
13 | ```
|
14 |
|
15 | ### Parameters
|
16 |
|
17 | Parameter | Type | Description
|
18 | --------- | ---- | -----------
|
19 | `x` | Array | Matrix | A matrix
|
20 |
|
21 | ### Returns
|
22 |
|
23 | Type | Description
|
24 | ---- | -----------
|
25 | number | The trace of `x`
|
26 |
|
27 |
|
28 | ## Examples
|
29 |
|
30 | ```js
|
31 | math.trace([[1, 2], [3, 4]]) // returns 5
|
32 |
|
33 | const A = [
|
34 | [1, 2, 3],
|
35 | [-1, 2, 3],
|
36 | [2, 0, 3]
|
37 | ]
|
38 | math.trace(A) // returns 6
|
39 | ```
|
40 |
|
41 |
|
42 | ## See also
|
43 |
|
44 | [diag](diag.md)
|