inv — Compute the inverse of a square matrix with MATLAB-compatible pivoting and GPU fallbacks.
X = inv(A) returns the matrix inverse of a square, full-rank matrix A. The result satisfies A * X = eye(size(A)) within round-off. Scalars behave like 1 ./ A, matching MATLAB semantics.
How inv works in RunMat
- Inputs must be 2-D matrices (trailing singleton dimensions are accepted). Non-square matrices raise the MATLAB error
"inv: input must be a square matrix." - Singular or rank-deficient matrices raise
"inv: matrix is singular to working precision." - Logical and integer inputs are promoted to double precision before inversion.
- Complex inputs are handled in full complex arithmetic.
- Empty matrices return an empty matrix with the same dimensions (e.g.,
inv([])yields[]).
How inv runs on the GPU
When a GPU acceleration provider is active, RunMat forwards the operation to its inv hook. If the provider does not implement a native kernel, RunMat gathers the data to the host, uses the shared CPU implementation, and attempts to re-upload the result so downstream GPU work keeps its residency. The shipping WGPU backend currently follows this gather/compute/upload pattern.
GPU memory and residency
You typically do not need to move data manually. If A already resides on the GPU and the provider implements inv, the computation stays on the device. Providers without a native kernel (including the current WGPU backend) download A, compute the inverse on the host, and re-upload the result, so subsequent GPU code continues to operate on device-resident data. gpuArray remains available for compatibility and for explicitly seeding GPU residency.
Examples
Inverting a 2x2 matrix for solving linear systems
A = [4 -2; 1 3];
X = inv(A)Expected output:
X =
0.3 0.2
-0.1 0.4Checking that inv(A) produces the identity matrix
A = [2 1 0; 0 1 -1; 0 0 3];
X = inv(A);
product = A * XExpected output:
product =
1.0000 0 0
0 1.0000 0
0 0 1.0000Inverting a diagonal matrix with symbolic structure
D = diag([2, 5, 10]);
X = inv(D)Expected output:
X =
0.5000 0 0
0 0.2000 0
0 0 0.1000Computing the inverse of a complex matrix
A = [1+2i 0; 3i 4-1i];
X = inv(A)Expected output:
X =
0.2105 - 0.1053i -0.0158 - 0.1579i
-0.1579 - 0.1184i 0.0526 + 0.2632iUsing inv on a GPU-resident matrix
G = gpuArray([3 1; 0 2]);
invG = inv(G); % stays on the GPU when the provider implements inv
result = gather(invG)Expected output:
result =
0.3333 -0.1667
0 0.5000Handling singular matrices gracefully
A = [1 2; 2 4];
X = inv(A)Expected output:
Error using inv
inv: matrix is singular to working precision.FAQ
Do I need to use inv to solve linear systems?
Prefer mldivide (A \\ b) or linsolve for numerical stability and performance. Use inv only when you explicitly need the inverse matrix.
What error do I get for singular matrices?
RunMat mirrors MATLAB and raises "inv: matrix is singular to working precision." when LU factorisation detects a zero pivot.
Can I invert non-square matrices?
No. inv requires square matrices. Use pinv for pseudoinverses of rectangular matrices.
Does inv support complex numbers?
Yes. Complex matrices are inverted using full complex arithmetic.
What happens with empty matrices?
inv([]) returns [] (an empty matrix) without error.
Does inv preserve GPU residency?
If the acceleration provider exposes an inv hook, the operation stays on the GPU. Otherwise, RunMat gathers, computes on the host, and re-uploads so the caller still receives a GPU tensor.
Related functions to explore
These functions work well alongside inv. Each page has runnable examples you can try in the browser.
pinv, linsolve, mldivide, det, gpuArray, gather, cond, norm, rank, rcond
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how inv works, line by line, in Rust.
- View inv.rs on GitHub
- Learn how the 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 — 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.