RunMat
GitHub

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 mldivide works in RunMat

  • Scalars divide exactly: s \ B scales B by 1/s, while A \ s requires A to 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.

How mldivide runs 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 \ b

Expected output:

x =
    -4.0000
     4.5000

Computing a least-squares solution with backslash

A = [1 2; 3 4; 5 6];
b = [7; 8; 9];
x = A \ b

Expected output:

x =
    -6.0000
     6.5000

Scaling by a scalar using backslash

s = 2;
B = [2 4 6];
scaled = s \ B

Expected output:

scaled = [1 2 3]

Solving multiple right-hand sides at once

A = [4 1; 2 3];
B = [1 0; 0 1];
X = A \ B

Expected output:

X =
     0.3   -0.1
    -0.2    0.4

Left division with complex matrices

A = [2+i 1; -1 3-2i];
B = [1; 4+i];
X = A \ B

Expected output:

X =
   -0.0732 - 0.3415i
    0.8049 + 0.7561i

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.

These functions work well alongside mldivide. Each page has runnable examples you can try in the browser.

mrdivide, mtimes, svd, gpuArray, gather, ctranspose, dot, mpower, trace, transpose

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how mldivide works, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.

  • Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
  • Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
  • A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.

Getting started · Benchmarks · Pricing

Try RunMat — free, no sign-up

Start running MATLAB code immediately in your browser.