NdArray::add

Add two matrices.

Description

SciPhp\NdArray NdArray::add( array|NdArray $n )

Parameters

$n
An array [1, 2, 3] or a NdArray.

Return Values

The new SciPhp\NdArray

Examples

Example #1: Addition of 2 square matrices

use SciPhp\NumPhp as np;

$m np::linspace(199)->reshape(33);
$n np::linspace(199)->reshape(33);

$a $m->add($n);

echo 
"m=n\n$m""a=m + n\n$a";

The above example will output:

m=n
[[1  2  3]
 [4  5  6]
 [7  8  9]]
a=m+n
[[2   4   6 ]
 [8   10  12]
 [14  16  18]]

Example #2: Addition of a vector and a matrix

use SciPhp\NumPhp as np;

$m np::linspace(166)->reshape(23);
$n np::linspace(133);

$a $m->add($n);

echo 
"m\n$m""n\n$n""a=m+n\n$a";

The above example will output:

m
[[1  2  3]
 [4  5  6]]
n
[1  2  3]
a=m+n
[[2  4  6]
 [5  7  9]]

Example #3: Addition of lambda and matrix

use SciPhp\NumPhp as np;

$m np::linspace(166)->reshape(23);

$a $m->add(5);

echo 
"m\n$m""a=m.5\n$a";

The above example will output:

m
[[1  2  3]
 [4  5  6]]
a=m+5
[[6   7   8 ]
 [9   10  11]]

See Also