View all functions

CategoryMath: Linalg & Ops
GPUYes
BLAS/LAPACK

What does the mrdivide function do in MATLAB / RunMat?

X = A / B (or mrdivide(A, B)) solves the right-sided linear system X * B = A. When B is square and nonsingular the solution matches A * inv(B). Rectangular or rank-deficient matrices are handled via a minimum-norm least-squares solve, matching MATLAB's SVD-based semantics.

How does the mrdivide function behave in MATLAB / RunMat?

  • Scalars divide exactly: A / s scales A by 1/s, while s / B requires B to be scalar.
  • Logical and integer inputs are promoted to double precision before solving.
  • Purely real operands produce real outputs; any complex operand promotes the computation (and result) to complex arithmetic.
  • Inputs must be effectively two-dimensional; trailing singleton dimensions are allowed.
  • The number of columns must agree (size(A, 2) == size(B, 2)), otherwise RunMat raises the MATLAB error "Matrix dimensions must agree."
  • Underdetermined and overdetermined systems return the minimum-norm least-squares solution.

mrdivide GPU execution behaviour

When a gpuArray provider is active, RunMat first offers the solve to its mrdivide hook. The WGPU provider currently downloads the operands to the host, executes the same SVD-based solver used by the CPU implementation, then uploads the result back to the device so residency remains transparent. If no provider is available—or the provider declines the request—RunMat gathers any gpuArray inputs to the host, computes the solution, and returns a host tensor.

Examples of using the mrdivide function in MATLAB / RunMat

Solving a square linear system

A = [1 2; 3 4];
B = [5 6; 7 8];
X = A / B;

% Verify the solution
residual = X * B;

Expected output:

X =
     3    -2
     2    -1

residual =
     1     2
     3     4

Computing a least-squares right division

A = [1 2 3];
B = [1 0 1; 0 1 1];
X = A / B;

Expected output:

X = [1 2];

Dividing by a scalar

A = [2 4 6];
scaled = A / 2;

Expected output:

scaled = [1 2 3];

Right division with complex inputs

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

Expected output:

X = -0.1429 - 0.2857i

GPU residency in RunMat (Do I need gpuArray?)

No manual care is required. If both operands already reside on the GPU and the provider supports mrdivide, the solve stays on the device. When the provider falls back to the host (the current WGPU implementation), the runtime seamlessly gathers data, executes the solve, and re-uploads the result to keep downstream GPU pipelines working as expected.

FAQ

Why must A and B share the number of columns?

Right division solves X * B = A; matrix multiplication requires size(A, 2) == size(B, 2).

What happens if B is singular or rectangular?

RunMat matches MATLAB by computing the minimum-norm least-squares solution via singular-value decomposition—no explicit call to pinv is required.

Does mrdivide support higher-dimensional arrays?

No. Inputs must be effectively matrices (trailing singleton dimensions are allowed). Use reshape or (:) to flatten higher-dimensional data before calling mrdivide.

How are logical or integer arrays handled?

They are promoted to double precision (true → 1, false → 0) before solving, matching MATLAB semantics.

How does RunMat handle NaN or Inf values?

They propagate through the least-squares solve in the same way as MATLAB. NaNs in the inputs yield NaNs in the output wherever they influence the solution.

See Also

mtimes, svd, lu, gpuArray, gather

Source & Feedback