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 |
|---|---|---|
|
| First addend. Supports element-wise addition for scalars, vectors, and matrices. |
|
| Second addend. Must be shape-compatible with |
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 |
|---|---|---|
| matches input | Element-wise sum of |
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)
length 2 column vector
m:=[2x2
Add a row vector to a matrix (broadcast by rows)
row vector of length 2
m:=[2x2
Mixed types (float + int)
4.5
Complex addition
(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-
mcolumn vector can be added to anm×nmatrix by adding the vector to each column of the matrix.Matrix + Row Vector (or Row Vector + Matrix): A length-
nrow vector can be added to anm×nmatrix 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.