mldivide — Solve linear systems with left-division in MATLAB and RunMat.
X = A \ B (or mldivide(A, B)) solves A * X = B. Square/nonsingular, rectangular, and rank-deficient solve paths follow MATLAB left-division semantics.
Syntax
X = mldivide(A, B)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | Left coefficient matrix or scalar. |
B | Any | Yes | — | Right-hand side matrix or vector. |
Returns
| Name | Type | Description |
|---|---|---|
X | NumericArray | Solution to A * X = B. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:mldivide:InvalidInput | Inputs are unsupported or incompatible for left division. | mldivide: invalid input |
RunMat:mldivide:Internal | Runtime cannot materialize left-division outputs. | mldivide: internal runtime failure |
How mldivide works
- 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.
Does RunMat run mldivide on the GPU?
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 memory and 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
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.7561iUsing mldivide with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how mldivide changes the result.
Run a small mldivide example, explain the result, then change one input and compare the output.
FAQ
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.
Is there a backslash() function?⌄
No. RunMat documents the matrix left-division operator A \\ B on the mldivide page, matching MATLAB's callable name and operator semantics.
Related Linalg functions
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how mldivide is executed, line by line, in Rust.
- View the source for mldivide in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.