Multiplication
Matrices are incredibly powerful for geometric transformations and linear algebra. WGSL supports multiplication of matrices with scalars, vectors, and other matrices using the standard multiplication operator (*).
Depending on the types of the operands, matrix multiplication behaves in different ways:
| Operation | Syntax | Description | Result Type |
|---|---|---|---|
| Scalar Scaling | s * M or M * s |
Multiplies every element of the matrix by the scalar s. |
Same matrix type |
| Vector-Matrix | v * M |
Multiplies row-vector v on the left (behaves as row-vector multiplication). |
Vector |
| Matrix-Vector | M * v |
Multiplies column-vector v on the right (behaves as column-vector multiplication). |
Vector |
| Matrix-Matrix | A * B |
Standard mathematical matrix multiplication. | Matrix |
Scalar-Matrix Multiplication
Multiplying a matrix by a scalar scales every individual element of the matrix independently. The order of operands does not matter (s * M is identical to M * s).
If we have a scalar \(10\) and a \(2 \times 3\) matrix:
Example
const m2x3 = mat2x3f(1.0, 2.0, 3.0, 4.0, 5.0, 6.0);
const scaled_left = 10.0 * m2x3; // mat2x3f
const scaled_right = m2x3 * 10.0; // mat2x3f
Vector-Matrix Multiplication
When a vector v is on the left of a matrix M (v * M), WGSL treats the vector as a row vector (dimensions \(1 \times R\)) and the matrix as having \(R\) rows and \(C\) columns. The multiplication performs a dot product between the vector and each column of the matrix, returning a vector of size \(C\).
For a vector \(v = \begin{pmatrix} 9 & 8 & 7 \end{pmatrix}\) and a \(2 \times 3\) matrix (which has 2 columns of 3 elements—meaning mathematically it has 3 rows and 2 columns in standard row-by-column mathematical notation):
Example
// m2x3 has 2 columns of 3 elements
const m2x3 = mat2x3f(1.0, 2.0, 3.0, 4.0, 5.0, 6.0);
const result: vec2f = vec3f(9.0, 8.0, 7.0) * m2x3; // vec2f(46.0, 118.0)
Matrix-Vector Multiplication
When a vector v is on the right of a matrix M (M * v), WGSL treats the vector as a column vector (dimensions \(C \times 1\)) and the matrix as having \(R\) rows and \(C\) columns. The multiplication returns a vector of size \(R\).
For a \(2 \times 3\) matrix and a column-vector \(v = \begin{pmatrix} 9 \\ 8 \end{pmatrix}\):
Example
const m2x3 = mat2x3f(1.0, 2.0, 3.0, 4.0, 5.0, 6.0);
const result: vec3f = m2x3 * vec2f(9.0, 8.0); // vec3f(41.0, 58.0, 75.0)
Matrix-Matrix Multiplication
Multiplying two matrices A * B performs standard mathematical matrix multiplication.
To multiply matrix A by matrix B:
- The number of columns of A must equal the number of rows of B.
- If
Ais amatCxR(\(C\) columns, \(R\) rows) andBis amatKxC(\(K\) columns, \(C\) rows), the result is amatKxR(\(K\) columns, \(R\) rows).
For example, multiplying a mat2x3 (2 columns, 3 rows) by a mat4x2 (4 columns, 2 rows) is mathematically valid and yields a mat4x3 (4 columns, 3 rows).
Example
const m2x3 = mat2x3f(1.0, 2.0, 3.0, 4.0, 5.0, 6.0); // mat2x3f
const m4x2 = mat4x2f(7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0); // mat4x2f
// Result is mat4x3f
const result: mat4x3f = m2x3 * m4x2;
/* * Copyright ©2026 Michael R. Bernstein. All new modifications licensed under Apache 2.0. * Upstream lineage ©2023 governed by original BSD 3-Clause. See README.md. */ const m2x3 = mat2x3f(1, 2, 3, 4, 5, 6); const mul_s_by_m2x3 : mat2x3f = 10 * m2x3; const mul_m2x3_by_s : mat2x3f = m2x3 * 10; // ╭ ╮ ╭ ╮ // │ 1 4 │ │ 1×9 + 2×8 + 3×7 │ // [9, 8, 7] × │ 2 5 │ = │ 4×9 + 5×8 + 6×7 │ // │ 3 6 │ ╰ ╯ // ╰ ╯ const mul_v3_by_m2x3 : vec2f = vec3(9, 8, 7) * m2x3; // ╭ ╮ ╭ ╮ ╭ ╮ // │ 1 4 │ │ 9 │ │ 1×9 + 4×8 │ // │ 2 5 │ × │ 8 │ = │ 2×9 + 5×8 │ // │ 3 6 │ ╰ ╯ │ 3×9 + 6×8 │ // ╰ ╯ ╰ ╯ const mul_m2x3_by_v2 : vec3f = m2x3 * vec2(9, 8); const mul_m2x3_by_m4x2 : mat4x3f = m2x3 * mat4x2f(7, 8, 9, 10, 11, 12, 13, 14);