math/sub

Usage

Description

Input

Output

Examples

Subtract two scalars

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)

Row vector minus matrix (broadcast by rows)

Complex subtraction

Details

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

X1

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

Minuend. Supports scalars, vectors, and matrices.

X2

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

Subtrahend. Must be shape-compatible with X1 under broadcasting.

Supported scalar types: 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 result of X1 - X2. The shape of Y follows broadcasting.

Examples

Subtract two scalars

y:=math/sub(10, 3)--

7

Subtract two vectors

x1:=[
10
20
30
]
x2:=[
1
2
3
]

Subtract two matrices

a:=[
10 30
20 40
]
b:=[
7 5
6 4
]

Subtract a scalar from a matrix (matrix - scalar)

a:=[
10 30
20 40
]

Subtract a matrix from a scalar (scalar - matrix)

m:=[
1 3
2 4
]

Matrix minus column vector (broadcast by columns)

m:=[
10 30
20 40
]
--

2x2

v:=[
1
5
]
--

column vector length 2

Row vector minus matrix (broadcast by rows)

r:=[
100
200
]
m:=[
1 3
2 4
]

Complex subtraction

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

(-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-m vector can be subtracted from (or used to subtract) an m×n matrix column-wise.

  • Matrix - Row Vector / Row Vector - Matrix: A length-n vector can be subtracted from (or used to subtract) an m×n matrix 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.