mldivide — Solve A * X = B using MATLAB's left-division operator (\).
X = A \ B (or mldivide(A, B)) solves the left-sided linear system A * X = B. When A is square and nonsingular the solution matches inv(A) * B. Rectangular or rank-deficient matrices are handled via a minimum-norm least-squares solve, mirroring MATLAB's SVD-based semantics.
How does the mldivide function behave in MATLAB / RunMat?
- Scalars divide exactly:
s \ BscalesBby1/s, whileA \ srequiresAto be scalar. - Logical and integer inputs are promoted to double precision before solving.
- Purely real operands return real outputs; any complex operand promotes the computation (and result) to complex arithmetic.
- Inputs must behave like 2-D matrices; trailing singleton dimensions are allowed.
- The number of rows must agree (
size(A, 1) == size(B, 1)), otherwise RunMat raises the MATLAB error"Matrix dimensions must agree." - Underdetermined and overdetermined systems return the minimum-norm least-squares solution.
GPU behavior
When a gpuArray provider is active, RunMat first offers the solve to its mldivide hook. The current WGPU provider downloads the operands to the host, executes the shared SVD-based solver, then uploads the result back to the device so downstream GPU pipelines keep their residency. If no provider is available—or a provider declines the request—RunMat gathers gpuArray inputs to the host, performs the solve there, and returns a host tensor.
GPU residency
No manual residency management is required. If both operands already live on the GPU and the active provider implements mldivide, the solve stays on the device. When the provider falls back to the host (as the current WGPU backend does), RunMat gathers data, executes the same SVD solver used by the CPU implementation, and re-uploads the result so subsequent GPU work continues seamlessly.
Examples of using mldivide in MATLAB / RunMat
Solving a square linear system with backslash
A = [1 2; 3 4];
b = [5; 6];
x = A \ bExpected output:
x =
-4.0000
4.5000Computing a least-squares solution with backslash
A = [1 2; 3 4; 5 6];
b = [7; 8; 9];
x = A \ bExpected output:
x =
-6.0000
6.5000Scaling by a scalar using backslash
s = 2;
B = [2 4 6];
scaled = s \ BExpected output:
scaled = [1 2 3]Solving multiple right-hand sides at once
A = [4 1; 2 3];
B = [1 0; 0 1];
X = A \ BExpected output:
X =
0.3 -0.1
-0.2 0.4Left division with complex matrices
A = [2+i 1; -1 3-2i];
B = [1; 4+i];
X = A \ BExpected output:
X =
-0.0732 - 0.3415i
0.8049 + 0.7561iFAQ
Why must A and B share the number of rows?
Left division solves A * X = B, which requires size(A, 1) == size(B, 1) for matrix multiplication to be defined.
What happens if A is singular or rectangular?
RunMat mirrors MATLAB by computing the minimum-norm least-squares solution using singular-value decomposition—no explicit pseudoinverse is necessary.
Does mldivide work with higher-dimensional arrays?
Inputs must behave like matrices. Trailing singleton dimensions are allowed, but higher-rank arrays should be reshaped before calling mldivide.
How are logical or integer arrays treated?
They are promoted to double precision (true → 1, false → 0) before solving, matching MATLAB semantics.
How does RunMat handle NaN or Inf values?
NaNs and Infs propagate through the least-squares solver exactly as MATLAB does—any slice containing NaNs will typically produce NaNs in the corresponding output entries.
See also
mrdivide, mtimes, svd, gpuArray, gather
Source & Feedback
- Source code: `crates/runmat-runtime/src/builtins/math/linalg/ops/mldivide.rs`
- Found a bug? Open an issue with a minimal reproduction.