math/sub
Element-wise subtraction
Usage
Y:=math/sub(X1, X2)Description
Computes the element-wise difference of two numeric inputs. X1 is the minuend and X2 is the subtrahend; the result is X1 - X2. Inputs may be scalars, vectors, or matrices. When one input is a scalar and the other is an array, the scalar is broadcast across the array (scalar broadcasting). Vector–matrix row/column operations follow the rules in Details.
Input
Argument | Kind | Description |
|---|---|---|
|
| Minuend. Supports scalars, vectors, and matrices. |
|
| Subtrahend. Must be shape-compatible with |
Supported scalar types: 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 result of |
Examples
Subtract two scalars
7
Subtract two vectors
Subtract two matrices
Subtract a scalar from a matrix (matrix - scalar)
Subtract a matrix from a scalar (scalar - matrix)
Matrix minus column vector (broadcast by columns)
2x2
v:=[column vector length 2
Row vector minus matrix (broadcast by rows)
Complex subtraction
(-2 + 6i)
Details
Element-wise semantics. Subtraction is performed per element. Shapes must either match or be compatible under broadcasting.
Broadcasting rules.
Array - Scalar / Scalar - Array: The scalar is broadcast across every element of the array.
Matrix - Column Vector / Column Vector - Matrix: A length-
mvector can be subtracted from (or used to subtract) anm×nmatrix column-wise.Matrix - Row Vector / Row Vector - Matrix: A length-
nvector can be subtracted from (or used to subtract) anm×nmatrix row-wise.
Non-commutative. math/sub is not commutative: math/sub(X1, X2) ≠ math/sub(X2, X1) in general. Parentheses and operand order matter.
Type support. Implementations exist for signed/unsigned integers, floating-point, rationals, and complex numbers. When types differ, standard numeric promotions apply where supported.
Errors.
Shape mismatch that cannot be resolved by broadcasting.
Unsupported type combination.