Sum all elements.
float|int|
SciPhp\NdArray
NdArray::sum(
[
int
$axis = null
]
[,
bool
$keepdims = false
])
$axis
$keepdims
A float or an integer.
use SciPhp\NumPhp as np;
$m = np::linspace(1, 9, 9)->reshape(3, 3);
$s = $m->sum();
echo "m\n$m", "s=sum(m)\n$s";
The above example will output:
m [[1 2 3] [4 5 6] [7 8 9]] s=sum(m) 45
use SciPhp\NumPhp as np;
$m = np::linspace(1, 9, 9)->reshape(3, 3);
// Sum over axis 0
$s0 = $m->sum(0);
// Sum over axis 1
$s1 = $m->sum(1);
echo "m\n$m", "s0=$s0", "s1=$s1";
The above example will output:
m [[1 2 3] [4 5 6] [7 8 9]] s0=[12 15 18] s1=[6 15 24]
use SciPhp\NumPhp as np;
$m = np::linspace(1, 9, 9)->reshape(3, 3);
// Sum over axis 0 and keepdims = true
$s0 = $m->sum(0, true);
// Sum over axis 1 and keepdims = true
$s1 = $m->sum(1, true);
echo "m\n$m", "s0=$s0", "s1=$s1";
The above example will output:
m [[1 2 3] [4 5 6] [7 8 9]] s0=[[12 15 18]] s1= [[6 ] [15] [24]]