RunMat
GitHub

All functions

CategoryMath: Linalg & Factor
Auto GPU

RunMat automatically offloads this function to the GPU when it estimates a speedup, without requiring explicit gpuArray inputs.

Learn more about Auto GPU →
BLAS/LAPACK

This function is backed by optimized BLAS/LAPACK routines (via Apple Accelerate or OpenBLAS) for high-performance linear algebra.

eig — Eigenvalue decomposition with MATLAB-compatible multi-output forms.

eig(A) computes the eigenvalues of a square matrix A. With additional outputs it also returns right and left eigenvectors, matching MATLAB’s multi-output semantics (A*V = V*D and W'*A = D*W').

Syntax

d = eig(A)
[V, D] = eig(A)
[V, D, W] = eig(A)
d = eig(A, B)
[V, D] = eig(A, B)
eig(___, balanceOption)
eig(___, 'vector')
eig(___, 'matrix')
  • A is a square matrix. Logical and integer inputs are promoted to double; complex inputs remain complex throughout the factorization.
  • B is an optional second square matrix that turns the call into the generalized problem A*V = B*V*D. RunMat recognises this syntax but does not yet implement it and raises a descriptive error.
  • V contains the right eigenvectors as columns; D is a diagonal matrix of eigenvalues satisfying A*V = V*D.
  • W (three-output form) holds the left eigenvectors as columns, satisfying W' * A = D * W', and is scaled so that W' * V = I.
  • The trailing selector 'vector' returns the eigenvalues as a column vector in the second output; 'matrix' (the default) returns the diagonal-matrix form.
  • balanceOption is 'balance' (default) or 'nobalance'. The option is accepted for MATLAB compatibility; balancing is currently a no-op.

How eig works

  • Single output d = eig(A) returns the eigenvalues as an n × 1 column vector (values may be complex even when A is real).
  • Two outputs [V,D] = eig(A) return the eigenvectors (columns of V) and the diagonal eigenvalue matrix D.
  • Three outputs [V,D,W] = eig(A) additionally return the left eigenvectors W satisfying W' * A = D * W'.
  • The selector 'vector' may be supplied (eig(A,'vector'), [V,d] = eig(A,'vector')) to request the eigenvalues as a column vector even when two outputs are requested. 'matrix' resets the second output to the diagonal-matrix form.
  • Logical and integer inputs are promoted to double precision. Complex inputs remain complex throughout the factorisation.
  • Empty and scalar matrices follow MATLAB’s shape conventions (eig([]) returns [], eig(5) returns 5).
  • Generalised problems eig(A,B) are not yet implemented; RunMat emits a clear error if you pass the second matrix argument.
  • Optional balancing keywords ('balance', 'nobalance') are accepted. Balancing defaults to on; the current implementation leaves balancing as a no-op while retaining the option for forward compatibility.

How RunMat runs eig on the GPU

The WGPU provider implements the reserved eig hook by downloading the input, running the same CPU decomposition used by the host path, and immediately re-uploading the double-precision eigenvalues, eigenvectors, and diagonal matrix. When the spectrum is real, the outputs therefore remain on the GPU without any user intervention.

If any output requires complex storage or you pass 'nobalance', RunMat automatically falls back to the host implementation and returns CPU-resident arrays. The 'vector' selector also triggers this transparent host fallback today. Reapply gpuArray if you want to continue on the device after such a fallback.

Because eig is a residency sink, the fusion planner treats it as a barrier and does not attempt to fuse surrounding elementwise work.

Examples

Computing Eigenvalues of a 2x2 Matrix

A = [2 1; 0 3];
d = eig(A)

Expected output:

d = [2; 3]

Diagonalizing a Matrix with Two Outputs

A = [0 1; -2 -3];
[V,D] = eig(A);
recon = V * D / V

Retrieving Left Eigenvectors with Three Outputs

A = [4 2; 1 3];
[V,D,W] = eig(A);
check = W' * A - D * W'

Eigenvalues of a Complex-Valued Matrix

A = [1+2i, 2-1i; 0, -3i];
[V,D] = eig(A);
diag(D)      % Complex eigenvalues

Eigenvalues of a Diagonal Matrix

A = diag([10, -2, 7]);
d = eig(A)

Handling Repeated Eigenvalues

A = [3 1 0; 0 3 0; 0 0 5];
d = eig(A)

Using the 'nobalance' Option

A = [1e6 1; 0 1e-6];
d_balanced = eig(A);
d_nobalance = eig(A, 'nobalance')

Returning Eigenvalues as a Vector with Two Outputs

A = [0 1; -2 -3];
[V,d] = eig(A, 'vector');
size(d)    % 2 x 1 column vector

Running eig on a gpuArray

G = gpuArray(randn(128));
d = eig(G);          % Real spectra stay on the GPU when the provider implements eig
isa(d, 'gpuArray')   % logical 1 when the provider kept the result on device

How RunMat validates eig

eig calls the production LAPACK routines dgeev and zgeev via the lapack Rust crate, which uses Apple Accelerate on macOS and OpenBLAS on Linux, the same libraries NumPy, SciPy, and MATLAB's own eigensolvers rely on. CPU correctness is exercised by the in-module tests linked below; GPU residency is validated at the host-fallback boundary described above rather than via a separate GPU eigensolver.

See Correctness & Trust for the full methodology and coverage table.

FAQ

What shapes does eig support?

eig requires a square matrix. Scalars are treated as 1×1 matrices, and empty inputs return empty outputs. Non-square inputs raise an error, matching MATLAB.

Do I always get complex outputs?

Eigenvalues and eigenvectors are returned as complex arrays when necessary. If all imaginary parts are numerically zero, RunMat returns real doubles for convenience, mirroring MATLAB behaviour.

How do I obtain the eigenvalues as a vector when requesting eigenvectors?

Pass the 'vector' selector. For example, [V,d] = eig(A,'vector') returns a column vector d and the right eigenvectors in V. Use 'matrix' (or omit the selector) when you prefer the diagonal-matrix form.

What about the optional balancing keywords?

'balance' (default) and 'nobalance' are accepted. The current release treats balancing as a no-op; the option remains so future releases can introduce a true balancing implementation without breaking user code.

Are generalised eigenvalue problems supported?

Not yet. Calling eig(A,B) raises a descriptive error. This builtin focuses on the standard eigenvalue problem; the generalised form will be added in a future update.

How are left eigenvectors normalised?

RunMat scales left eigenvectors so that W' * V = I (bi-orthonormal columns), matching MATLAB’s conventions. When a provider supplies the GPU implementation the same normalisation is expected.

Does eig participate in fusion or auto-offload?

No. Eigenvalue decomposition executes eagerly and acts as a residency sink. The fusion planner will gather any GPU-resident inputs before factorisation.

How can I continue on the GPU after calling eig today?

When the active provider implements the eig hook (the WGPU backend does for real spectra), the outputs remain on the GPU automatically. If the decomposition falls back to the CPU—because the spectrum is complex or 'nobalance' was requested—call gpuArray on whichever results you need on the device.

What happens if the eigenvector matrix is singular?

When the right eigenvectors form a singular matrix, RunMat falls back to computing left eigenvectors from the conjugate-transposed problem. If that fails, requesting the third output raises an error, matching MATLAB’s failure behaviour.

What equation does eig solve?

For the standard form [V, D] = eig(A), the outputs satisfy A*V = V*D, where the columns of V are right eigenvectors and D is a diagonal matrix of eigenvalues. The generalized form A*V = B*V*D (called as eig(A, B) in MATLAB) is recognised but not yet implemented in RunMat and raises a descriptive error.

How do I get just the eigenvalues versus eigenvalues and eigenvectors?

Call d = eig(A) with a single output to get a column vector of eigenvalues only. Use [V, D] = eig(A) to also get the eigenvectors, or [V, D, W] = eig(A) to additionally get the left eigenvectors in W.

Are the eigenvalues returned by eig sorted?

No. Unlike svd, which returns singular values in descending order, eig returns eigenvalues in whatever order the underlying decomposition produces. Sort them yourself when you need ordering, for example [d, idx] = sort(diag(D)); D = diag(d); V = V(:, idx);.

When should I use eig versus eigs?

Use eig for dense matrices when you want the full spectrum. Use eigs for sparse or very large matrices when you only need a few eigenvalues (for example the k largest or smallest), since it uses iterative Arnoldi/Lanczos methods instead of a full factorization.

Factor

chol · lu · qr · svd

Solve

cond · det · inv · linsolve · norm · pinv · rank · rcond

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how eig works, line by line, in Rust.

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.

Getting started · Benchmarks · Pricing

Try RunMat for free

Write code or describe what you want to compute. The sandbox is free, no account required.