Skip to main content

Matrix decompositions

warning

Automatic WGSL docs generation publishable on this website is currently waiting on wgpu#6364. In the meantime, refer to the WGSL sources in the wgebra repository.

Matrix decomposition is a family of techniques that aim to represent a matrix as the product of several matrices. Those factors can either allow more efficient operations like inversion or linear system resolution, and might provide some insight regarding intrinsic properties of some data to be analysed (e.g. by observing singular values, eigenvectors, etc.)

info

WGebra implements common matrix decompositions for low-dimensional matrices (currently mat2x2<f32>, mat3x3<f32> and mat4x4<f32>). There is no implementation of these decomposition for large matrices yet.

DecompositionFactorsDerivable shaders
QRQ RQ ~ RWgQR2, WgQR3, WgQR4
LU with partial pivotingP1 L UP^{-1} ~ L ~ UWgLU2, WgLU3, WgLU4
CholeskyL LL ~ L^*WgCholesky2, WgCholesky3, WgCholesky4
Symmetric eigendecompositionU Λ UU ~ \Lambda ~ U^*WgSymmetricEigen2, WgSymmetricEigen3, WgSymmetricEigen4
SVD (2x2 and 3x3 only)U Σ VU ~ \Sigma ~ V^*WgSvd2, WgSvd3

Cholesky

The Cholesky decomposition of a n × n Hermitian Definite Positive (SDP) matrix MM is composed of a n × n lower-triangular matrix LL such that M=LLM = LL^*. Where LL^* designates the conjugate-transpose of LL. If the input matrix is not SDP, such a decomposition does not exist and the matrix method .cholesky(...) returns None. Note that the input matrix is interpreted as a hermitian matrix. Only its lower-triangular part (including the diagonal) is read by the Cholesky decomposition (its strictly upper-triangular is never accessed in memory). See the wikipedia article for further details about the Cholesky decomposition.

Typical uses of the Cholesky decomposition include the inversion of SDP matrices and resolution of SDP linear systems.

Cholesky decomposition of a 3x3 matrix.

#[derive(Shader)]
#[shader(derive(WgCholesky3), src = "your_shader.wgsl")]
struct YourShader;

// Automatically generates a test to check with `cargo test` if `your_shader.wgsl` compiles.
wgcore::test_shader_compilation!(YourShader);

QR

The QR decomposition of a general m × n matrix MM is composed of an m × min(n, m) unitary matrix QQ, and a min(n, m) × m upper triangular matrix (or upper trapezoidal if m<nm < n) RR such that M=QRM = QR. This can be used to compute the inverse or a matrix or for solving linear systems. See also the wikipedia article for further details.

QR decomposition of a 3x3 matrix.

#[derive(Shader)]
#[shader(derive(WgQR3), src = "your_shader.wgsl")]
struct YourShader;

// Automatically generates a test to check with `cargo test` if `your_shader.wgsl` compiles.
wgcore::test_shader_compilation!(YourShader);

LU with partial pivoting

The LU decomposition of a general m × n matrix is composed of a m × min(n, m) lower triangular matrix LL with a diagonal filled with 1, and a min(n, m) × m upper triangular matrix UU such that M=LUM = LU. This decomposition is typically used for solving linear systems, compute determinants, matrix inverse, and matrix rank.

WGebra implements LU decomposition with partial (row) pivoting which computes additionally only one permutation matrix PP such that PM=LUPM = LU.

See also the wikipedia article for further details.

LU decomposition of a 3x3 matrix.

#[derive(Shader)]
#[shader(derive(WgLU3), src = "your_shader.wgsl")]
struct YourShader;

// Automatically generates a test to check with `cargo test` if `your_shader.wgsl` compiles.
wgcore::test_shader_compilation!(YourShader);

Eigendecomposition (symmetric matrix)

The eigendecomposition of a square symmetric matrix MM is composed of an unitary matrix QQ and a real diagonal matrix Λ\Lambda such that M=QΛQM = Q\Lambda Q^*. The columns of QQ are called the eigenvectors of MM and the diagonal elements of Λ\Lambda its eigenvalues.

The matrix QQ and the eigenvalues of the decomposed matrix are respectively accessible as public the fields eigenvectors and eigenvalues of the SymmetricEigen structure.

Eigendecomposition of a 3x3 symmetric matrix.

#[derive(Shader)]
#[shader(derive(WgSymmetricEigen3), src = "your_shader.wgsl")]
struct YourShader;

// Automatically generates a test to check with `cargo test` if `your_shader.wgsl` compiles.
wgcore::test_shader_compilation!(YourShader);

Singular Value Decomposition

The Singular Value Decomposition (SVD) of a rectangular matrix is composed of two orthogonal matrices UU and VV and a diagonal matrix Σ\Sigma with positive (or zero) components. Typical uses of the SVD are the pseudo-inverse, rank computation, and the resolution of least-square problems.

The singular values, left singular vectors, and right singular vectors are respectively stored on the public fields singular_values, u and v_t. Note that v_t represents the adjoint (i.e. conjugate-transpose) of the matrix VV.

SVD decomposition of a 3x3 matrix.

#[derive(Shader)]
#[shader(derive(WgSvd3), src = "your_shader.wgsl")]
struct YourShader;

// Automatically generates a test to check with `cargo test` if `your_shader.wgsl` compiles.
wgcore::test_shader_compilation!(YourShader);