Qu

trace

Builtin index · Core maths

result = trace(M)

Input: M · Output: result

What it is

The sum of the diagonal of M, a square (or rectangular — only min(rows,cols) diagonal entries are summed) Mat.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = trace(M)
M Matnot documented yet
result scalar or Num
Returns a scalar Num, equal to the sum of the eigenvalues — the cheap way to sanity-check an eigendecomposition.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

A = [4.0, 1.0; 1.0, 3.0]
print("diag={diag(A)}  trace={trace(A)}  cond={cond(A)}")

b = [1.0, 2.0]
r = nnls(A, b)
print("nnls x={r.x}  residual={r.residual}")
output
diag=[4, 3]  trace=7  cond=1.938749
nnls x=[0.090909, 0.636364]  residual=4.965068e-16

from core-math

How it works

trace(A) — the sum of the diagonal.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.