inv — Compute inverses of square matrices in MATLAB and RunMat.
X = inv(A) returns the matrix inverse of square, full-rank matrix A. Scalar inputs behave like 1 ./ A, and decomposition/pivoting behavior follows MATLAB semantics.
Syntax
X = inv(A)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | Input square matrix. |
Returns
| Name | Type | Description |
|---|---|---|
X | NumericArray | Inverse of A. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:inv:InvalidInput | Input shape/type is unsupported or matrix is singular for inversion. | inv: invalid input |
RunMat:inv:Internal | Runtime fails while executing inversion or fallback/upload paths. | inv: internal runtime failure |
How inv works
- 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[]).
Does RunMat run inv 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.Using inv with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how inv changes the result.
Run a small inv example, explain the result, then change one input and compare the output.
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 Linalg functions
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how inv is executed, line by line, in Rust.
- View the source for inv 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.