math/add

Usage

Description

Input

Output

Examples

Add two scalars

Add two vectors (same length)

Add two matrices (same shape)

Add a scalar to a matrix (scalar broadcasting)

Add a column vector to a matrix (broadcast by columns)

Add a row vector to a matrix (broadcast by rows)

Mixed types (float + int)

Complex addition

Details

math/add

Element-wise addition

Usage

Y:=math/add(X1, X2)

Description

Performs element-wise addition of two numeric inputs. X1 and X2 may be scalars, vectors, or matrices. When one input is a scalar and the other is an array, the scalar is added to every element of the array (scalar broadcasting). When adding a vector to a matrix, the vector may be broadcast across rows or columns as described in Details.

Input

Argument

Kind

Description

X1

int, uint, float, rational, complex, [T], [[T]]

First addend. Supports element-wise addition for scalars, vectors, and matrices.

X2

int, uint, float, rational, complex, [T], [[T]]

Second addend. Must be shape-compatible with X1 under the broadcasting rules.

Supported scalar types (matched and mixed where defined): i8, i16, i32, i64, i128, u8, u16, u32, u64, u128, f32, f64, rational (R64), and complex (C64).

Output

Argument

Kind

Description

Y

matches input

Element-wise sum of X1 and X2. The shape of Y follows broadcasting.

Examples

Add two scalars

y:=math/add(2, 5)

Add two vectors (same length)

x1:=[
1
2
3
]
x2:=[
4
5
6
]

Add two matrices (same shape)

a:=[
1 3
2 4
]
b:=[
5 7
6 8
]

Add a scalar to a matrix (scalar broadcasting)

a:=[
1 3
2 4
]

Add a column vector to a matrix (broadcast by columns)

v:=[
10
20
]
--

length 2 column vector

m:=[
1 3
2 4
]
--

2x2

Add a row vector to a matrix (broadcast by rows)

r:=[
100
200
]
--

row vector of length 2

m:=[
1 3
2 4
]
--

2x2

Mixed types (float + int)

y:=math/add(2.5, 2)--

4.5

Complex addition

y:=math/add(1+2i, 3+-4i)--

(4 - 2i)

Details

Element-wise semantics. Addition is applied per element. Shapes must either match exactly or be compatible via broadcasting.

Broadcasting rules. The function supports these efficient broadcast cases:

  • Scalar + Array: The scalar is added to every element of the array.

  • Matrix + Column Vector (or Vector + Matrix): A length-m column vector can be added to an m×n matrix by adding the vector to each column of the matrix.

  • Matrix + Row Vector (or Row Vector + Matrix): A length-n row vector can be added to an m×n matrix by adding the vector to each row of the matrix.

Commutative & associative. math/add is commutative (X1 + X2 = X2 + X1) and associative where types and shapes allow.

Type support. Implementations exist for signed/unsigned integers, floating-point, rationals, and complex numbers. When types differ, standard numeric promotions apply where defined by the runtime; otherwise a type error is raised.

Errors.

  • Shape mismatch that cannot be resolved by broadcasting.

  • Unsupported type combination.