View all functions

CategoryMath: Linalg & Solve
GPUYes
BLAS/LAPACK

What does the inv function do in MATLAB / RunMat?

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 does the inv function behave in MATLAB / 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 []).

inv GPU execution behaviour

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.

Examples of using the inv function in MATLAB / RunMat

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.4

Checking that inv(A) produces the identity matrix

A = [2 1 0; 0 1 -1; 0 0 3];
X = inv(A);
product = A * X;

Expected output:

product =
    1.0000         0         0
         0    1.0000         0
         0         0    1.0000

Inverting 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.1000

Computing 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.2632i

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.5000

Handling singular matrices gracefully

A = [1 2; 2 4];
X = inv(A);

Expected output:

Error using inv
inv: matrix is singular to working precision.

GPU residency in RunMat (Do I need gpuArray?)

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.

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.

See Also

pinv, linsolve, mldivide, det, gpuArray, gather