NumPhp::trace

Sum along diagonals.

Description

float|int NumPhp::trace( array|NdArray $m , int $k = 0 )

Parameters

$m
An array [1, 2, 3] or a NdArray.
$k
Default value is 0. Diagonal offset.

Return Values

A float|int

Examples

Example #1: Calculate default trace

use SciPhp\NumPhp as np;

$m np::arange(09)->reshape(33);

$r np::trace($m);

echo 
"m\n$m""r\n$r";

The above example will output:

m
[[0  1  2]
 [3  4  5]
 [6  7  8]]
r
12

Example #2: Calculate a trace with an offset

use SciPhp\NumPhp as np;

$m np::arange(09)->reshape(33);

$r np::trace($m1);

echo 
"m\n$m""r\n$r";

The above example will output:

m
[[0  1  2]
 [3  4  5]
 [6  7  8]]
r
6

See Also