Sum all elements.
float|int|
SciPhp\NdArray
NumPhp::sum(
array|NdArray
$m
[,
int
$axis = null
]
[,
bool
$keepdims = false
])
$m
$axis
$keepdims
A float or an integer.
use SciPhp\NumPhp as np;
$m = np::linspace(1, 9, 9)->reshape(3, 3);
$s = np::sum($m);
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 = np::sum($m, 0);
// Sum over axis 1
$s1 = np::sum($m, 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 = np::sum($m, 0, true);
// Sum over axis 1 and keepdims = true
$s1 = np::sum($m, 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]]