math/mul

Usage

Description

Input

Output

Examples

Multiply two scalars

Multiply two vectors

Multiply two matrices

Multiply a matrix by a scalar (scalar broadcasting)

Multiply a matrix by a column vector (broadcast by columns)

Multiply a row vector by a matrix (broadcast by rows)

Mixed types (float × int)

Complex multiplication

Details

math/mul

Element-wise multiplication

Usage

Y:=math/mul(X1, X2)

Description

Computes the element-wise product 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 multiplied with every element of the array (scalar broadcasting). When multiplying vectors with matrices, broadcasting follows the rules in Details.

Note: math/mul performs Hadamard (element-wise) multiplication, not matrix-matrix linear algebra multiplication.

Input

Argument

Kind

Description

X1

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

First factor. Supports scalars, vectors, and matrices.

X2

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

Second factor. 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 product of X1 * X2. The shape of Y follows broadcasting.

Examples

Multiply two scalars

y:=math/mul(6, 7)--

42

Multiply two vectors

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

Multiply two matrices

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

Multiply a matrix by a scalar (scalar broadcasting)

a:=[
1 3
2 4
]

Multiply a matrix by a column vector (broadcast by columns)

m:=[
1 3
2 4
]
--

2x2

v:=[
10
20
]
--

column vector length 2

Multiply a row vector by a matrix (broadcast by rows)

r:=[
100
200
]
--

row vector length 2

m:=[
1 3
2 4
]
--

2x2

Mixed types (float × int)

y:=math/mul(2.5, 4)--

10.0

Complex multiplication

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

(11 + 2i)

Details

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

Broadcasting rules.

  • Scalar × Array or Array × Scalar: The scalar is multiplied with every element of the array.

  • Matrix × Column Vector (or Vector × Matrix): A length-m column vector can multiply an m×n matrix by multiplying the vector with each column of the matrix.

  • Matrix × Row Vector (or Row Vector × Matrix): A length-n row vector can multiply an m×n matrix by multiplying the vector with each row of the matrix.

Properties. math/mul is commutative and associative on compatible numeric types (ignoring floating-point rounding).

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.