mldivide — Solve linear systems with matrix left division.
mldivide(A, B) is the callable form of A \ B and solves A * X = B for scalar, square, rectangular, rank-deficient, complex, and supported provider-resident inputs.
Syntax
X = mldivide(A, B)Inputs
A- Type
Any- Required
- Yes
- Description
- Left numeric, logical, character, complex, or provider-resident operand.
B- Type
Any- Required
- Yes
- Description
- Right numeric, logical, character, complex, or provider-resident operand.
Returns
C- Type
NumericArray- Description
- Matrix-arithmetic result.
Errors
RunMat:mldivide:InvalidInput- When
- Inputs are unsupported, are not matrices, or have incompatible dimensions.
- Message
mldivide: unsupported operand types
RunMat:mldivide:Internal- When
- The solver or acceleration provider cannot complete the operation correctly.
- Message
mldivide: internal runtime failure
Left division
X = A \ B and mldivide(A, B) solve A * X = B. Dense square, rectangular, and rank-deficient inputs use a minimum-norm least-squares solve.
Both inputs must behave as matrices, with trailing singleton dimensions allowed. Nonscalar inputs must have the same number of rows.
Classes and scalar forms
A real pair returns a real floating result; a complex operand promotes the solve to complex arithmetic. Logical matrices are converted to double.
When A is scalar, A \ B scales B by 1/A. Fixed-width integers are supported only in this scalar-left form, where the integer class is preserved and division follows fixed-width rules. Integer matrix solving is rejected.
Sparse systems
On the host, nonsingular square CSC systems use direct triangular substitution when applicable and sparse LU otherwise. Rectangular and numerically singular CSC systems use column-pivoted Householder QR, followed by a complete orthogonal decomposition when needed, to return a minimum-norm least-squares solution. Zero-order systems preserve the solution shape. These routes borrow the coefficient matrix instead of converting it to dense storage. A dense right-hand side with the same real or complex single/double class preserves that class in the result; multiple right-hand sides share one factorization. Mixed-class sparse systems remain unsupported. Nonfinite input or a nonfinite numerical result returns an error.
Sparse factor fill, solve scratch, and final results support finite managed host allocation. Fill is admitted as it grows; a sparse input can still require substantial fill and may exceed the available budget. Triangular solves write directly into admitted result storage. QR uses native-precision factors and results, with scaled double-precision norm reductions. Numerical rank uses native machine epsilon times the larger matrix dimension times the initial largest column norm; an exactly zero matrix has rank zero. A second decomposition retains that selected rank rather than applying another tolerance. This is RunMat's minimum-norm and numerical-rank policy, not a promise of identical mixed-edge or rank decisions to MATLAB. Dense rectangular and rank-deficient inputs retain the minimum-norm SVD route.
Acceleration
A resident solve is offered to the exact provider that owns its inputs. Mixed-residency operands are uploaded only when their class and precision can be represented by that owner.
A typed unsupported-operation response permits host fallback. Provider failures, aliased outputs, wrong shapes, wrong types, and wrong owners are reported rather than treated as an unsupported optimization.
Examples
Solve a square system
A = [1 2; 3 4];
b = [5; 6];
x = A \ bExpected output:
x = [-4; 4.5]Solve an overdetermined system
A = [1 2; 3 4; 5 6];
b = [7; 8; 9];
x = A \ bExpected output:
x = [-6; 6.5]Scale by a scalar on the left
s = 2;
B = [2 4 6];
X = s \ BExpected output:
X = [1 2 3]Solve multiple right-hand sides
A = [4 1; 2 3];
B = eye(2);
X = A \ BExpected output:
X = [0.3 -0.1; -0.2 0.4]Solve a sparse system with two right-hand sides
A = sparse([0 2; 1 3]);
B = [10 -2; 19 -1];
X = A \ BExpected output:
X = [4 2; 5 -1]Solve a tall sparse least-squares system
A = sparse([1 0; 0 1; 1 1]);
b = [2; 3; 5];
x = A \ bExpected output:
x = [2; 3]Solve a wide sparse system with minimum norm
A = sparse([1 0 1; 0 1 1]);
b = [1; 1];
x = A \ bExpected output:
x = [1/3; 1/3; 2/3]Solve a rank-deficient sparse system
A = sparse([1 2; 2 4]);
b = [5; 10];
x = A \ bExpected output:
x = [1; 2]Solve a complex system
A = [2+i 1; -1 3-2i];
B = [1; 4+i];
X = A \ B;
R = A * XExpected output:
R agrees with BUsing 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 have the same number of rows?⌄
The equation A * X = B requires each right-hand side to have one entry for every row of A.
What happens for singular or rectangular A?⌄
Dense inputs use a minimum-norm singular-value decomposition solve. Same-class host sparse inputs use rank-revealing QR and a complete orthogonal decomposition for minimum-norm least squares. Numerical rank follows the documented native-precision tolerance. MATLAB backslash does not generally promise the same minimum-norm choice for rectangular systems.
Are higher-dimensional arrays supported?⌄
Inputs must be effective matrices. Trailing singleton dimensions are allowed; reshape other arrays before solving.
How are logical and integer arrays treated?⌄
Logical matrices become double. Integer values are accepted only for scalar-left division and preserve the integer class.
Is backslash a callable alias?⌄
No. Use mldivide(A, B) or the A \ B operator.
Related Linalg functions
Ops
cross · ctranspose · dot · mpower · mrdivide · mtimes · pagemtimes · pagetranspose · trace · transpose
Structure
bandwidth · isdiag · ishermitian · issymmetric · istril · istriu · symrcm
Factor
chol · decomposition · decomposition.ctranspose · decomposition.mldivide · decomposition.mrdivide · decomposition.mtimes · decomposition.rdivide · decomposition.subsref · decomposition.times · decomposition.uminus · decomposition.uplus · eig · eigs · gmres · ichol · ilu · lu · ordeig · ordqz · ordschur · pcg · qr · qz · rsf2csf · schur · svd
Source you can read
Every RunMat function's source is public. 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 a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/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.