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
A- Type
Any- Required
- Yes
- Description
- Input square matrix.
Returns
X- Type
NumericArray- Description
- Inverse of A.
Errors
RunMat:inv:InvalidInput- When
- Input shape/type is unsupported or matrix is singular for inversion.
- Message
inv: invalid input
RunMat:inv:Internal- When
- Runtime fails while executing inversion or fallback/upload paths.
- Message
inv: internal runtime failure
Behavior
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 []).
Acceleration and residency
Uses the acceleration provider's inv hook when available; the default WGPU backend gathers to the host, computes the inverse, and re-uploads the result to preserve residency.
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.
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.
Implementation and validation
Original GPU declarations: precisions f32, f64; elementwise false; reduction false; broadcasting none.
Original fusion declarations: elementwise false; reduction false; max inputs 1; constants uniform.
Examples
Inverting a 2x2 matrix for solving linear systems
A = [4 -2; 1 3];
X = inv(A)Expected output:
X = [3/14, 1/7; -1/14, 2/7]Checking 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.2000-0.4000i, 0; -0.2471-0.2118i, 0.2353+0.0588i]Using 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
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
Ops
cross · ctranspose · dot · mldivide · mpower · mrdivide · mtimes · pagemtimes · pagetranspose · trace · transpose
Source you can read
Every RunMat function's source is public. 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 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.