eig — Compute eigenvalue decompositions in MATLAB and RunMat.
eig(A) computes eigenvalues of square matrix A; eig(A,B) computes generalized eigenvalues for dense square matrix pencils. Additional outputs return right and left eigenvectors under MATLAB and RunMat multi-output semantics.
Syntax
d = eig(A)
d = eig(A, options...)
[V, D] = eig(A)
[V, D] = eig(A, options...)
[V, D, W] = eig(A)
[V, D, W] = eig(A, options...)All supported eig forms
d = eig(A)
d = eig(A, options...)
[V, D] = eig(A)
[V, D] = eig(A, options...)
[V, D, W] = eig(A)
[V, D, W] = eig(A, options...)
d = eig(A, B)
d = eig(A, B, options...)
[V, D] = eig(A, B)
[V, D] = eig(A, B, options...)
[V, D, W] = eig(A, B)
[V, D, W] = eig(A, B, options...)Inputs
A- Type
NumericArray- Required
- Yes
- Description
- Input square matrix.
options- Type
Any- Required
- Variadic
- Description
- Optional selectors (`balance`, `nobalance`, `vector`, `matrix`).
B- Type
NumericArray- Required
- Yes
- Description
- Second square coefficient of A - lambda*B; singular B and infinite or indeterminate generalized eigenvalues are supported.
Returns
d- Type
NumericArray- Description
- Eigenvalues as a column vector.
V- Type
NumericArray- Description
- Right eigenvectors.
D- Type
NumericArray- Description
- Diagonal matrix (or vector when `vector` option is used).
W- Type
NumericArray- Description
- Left eigenvectors.
Returned values from eig depend on how many outputs the caller requests.
Errors
RunMat:eig:InvalidArgument- When
- Option arguments or requested output count are invalid.
- Message
eig currently supports at most three outputs
RunMat:eig:InvalidInput- When
- Input is unsupported or matrix shape is invalid.
- Message
eig: input matrix must be square
RunMat:eig:Internal- When
- Runtime cannot compute or materialize eig outputs.
- Message
eig: internal runtime failure
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 defining the generalized pencil A - lambda*B. Native and portable builds support real or complex pencils with singular B, including infinite and indeterminate eigenvalues.
V contains the right eigenvectors as columns; D is a diagonal matrix of eigenvalues. Standard calls satisfy A*V = V*D; finite generalized eigenpairs satisfy A*v = lambda*B*v. At infinite eigenvalues, use B*v = 0 rather than multiplying by an infinite diagonal entry.
W (three-output form) holds the left eigenvectors as columns, satisfying W' * A = D * W' for standard calls and w' * A = lambda*w' * B for finite generalized eigenpairs. Infinite left eigenpairs satisfy w' * B = 0.
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.
Behavior
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'.
Generalized calls d = eig(A,B), [V,D] = eig(A,B), and [V,D,W] = eig(A,B) solve square, same-size real or complex single or double pencils. Native BLAS/LAPACK builds use ZGGEV; portable builds, including WASM, use the shared generalized complex EVD owner without forming an inverse of B.
The selector 'vector' may be supplied (eig(A,'vector'), eig(A,B,'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 typed-integer coefficient matrices are a named RunMat-only extension. Strict modes reject them before provider access; RunMat mode promotes them to double only after every integer coefficient passes an exact binary64-representability check.
Single host and gpuArray inputs retain single output storage. Complex inputs remain complex throughout the factorisation.
Empty and scalar matrices follow MATLAB’s shape conventions (eig([]) returns [], eig(5) returns 5).
Generalized eigenvalues are projected from homogeneous pairs (alpha,beta): exact zero beta with nonzero alpha gives infinity; exact alpha = beta = 0 gives NaN (an indeterminate eigenvalue). A nonzero denominator is not classified as zero by a tolerance. Infinite or indeterminate entries must not be used in ordinary finite reconstruction products.
Optional balancing keywords ('balance', 'nobalance') are accepted. Balancing defaults to on; true balancing remains an explicit numerical-compatibility gap in the current implementation.
Explicit generalized 'chol' and 'qz' selectors and MATLAB's limited real-symmetric sparse eig form remain explicit implementation gaps rather than advertised completed behavior.
Acceleration and residency
A genuine provider eigensolver may implement the reserved standard eig(A) hook. WGPU uses the canonical contextual host fallback rather than a separate provider-side CPU emulation: it downloads through the input's exact owner, computes the host eigensystem, and restores supported real or complex factors in the source single or double floating class.
If the provider hook cannot serve a call, including generalized or complex problems and calls using 'nobalance' or 'vector', RunMat gathers through the input handle's owning provider, computes with the host implementation, and uploads eligible numeric outputs back through that same owner. Floating-point outputs therefore retain accelerator residency across these transparent fallbacks.
The fusion planner treats eig as an allocation boundary: its result handles are not fused into the decomposition itself, but eligible floating-point outputs remain available to subsequent accelerator work without an explicit gpuArray round trip.
Standard eig(A) uses a genuine provider hook when available under its supported allocation policy. Generalized calls, complex inputs, unsupported options, and finite execution without an admitted provider continuation use the shared host owner and exact-owner restoration; no provider hook is invoked merely to emulate the same host decomposition.
Implementation and validation
eig uses the shared dense complex EVD owner for standard problems. Generalized eig(A,B) uses LAPACK ZGGEV when the native BLAS/LAPACK feature is enabled and a caller-owned portable generalized complex EVD otherwise. Both preserve singular-pencil infinite and indeterminate eigenvalues through the same homogeneous projection. Coefficients, workspace and publication use the selected Runtime allocation and cancellation owners. GPU fallback uses the same host decomposition and exact-owner restoration rather than a separate emulated GPU eigensolver.
CPU-only validated; no dedicated GPU eigensolver
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 / VRetrieving 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 eigenvaluesEigenvalues 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 vectorSolving a Generalized Eigenvalue Problem
A = [2 0; 0 9];
B = [1 0; 0 3];
d = eig(A, B)Expected output:
d = [2; 3]Generalized Eigenvectors
A = [4 1; 0 9];
B = [2 0; 0 3];
[V,D,W] = eig(A, B);
rightResidual = A*V - B*V*D;
leftResidual = W'*A - D*W'*BRunning 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 deviceUsing eig with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how eig changes the result.
Run a small eig example, explain the result, then change one input and compare the output.
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, but true balancing is not implemented yet. This is a documented numerical-compatibility gap.
Are generalized eigenvalue problems supported?⌄
Yes. Native and portable builds support dense real or complex generalized pencils, including singular B. Exact zero homogeneous denominators produce infinite eigenvalues, while a 0/0 pair produces an indeterminate NaN. No inverse of B is required.
How are left eigenvectors normalised?⌄
RunMat scales each standard left eigenvector against its right eigenvector, or each finite generalized left eigenvector against B*v, when the Hermitian pairing is finite and nonzero. Singular, defective, infinite or indeterminate cases cannot universally satisfy W' * V = I or W' * B * V = I; zero or unusable pairings retain the kernel vectors rather than inventing that normalization.
Does eig participate in fusion or auto-offload?⌄
No. Eigenvalue decomposition executes eagerly and terminates fusion. Resident inputs remain available to the builtin for provider dispatch or an explicit owning-provider gather fallback.
How can I continue on the GPU after calling eig today?⌄
When the input provider implements the standard eig(A) hook, validated outputs remain resident. Generalized, complex, 'nobalance', and unsupported-provider paths gather through the input owner, compute on the host, and upload eligible numeric results back to that same owner.
What happens if the eigenvector matrix is singular?⌄
Left eigenvectors are computed by the same decomposition owner, not by inverting the right eigenvector matrix. A singular or defective eigenvector basis need not admit biorthogonal normalization; requesting left vectors does not manufacture such an identity.
What equation does eig solve?⌄
For the standard form [V, D] = eig(A), the outputs satisfy A*V = V*D. Finite generalized eigenpairs satisfy A*v = lambda*B*v, even when B is singular. Infinite eigenpairs use B*v = 0; indeterminate 0/0 entries have no defined finite eigenvalue equation.
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. The current RunMat eigs implementation selects a subset from the dense eig fallback; a true iterative Arnoldi/Lanczos provider path remains future work.
Related Linalg functions
Factor
chol · decomposition · decomposition.ctranspose · decomposition.mldivide · decomposition.mrdivide · decomposition.mtimes · decomposition.rdivide · decomposition.subsref · decomposition.times · decomposition.uminus · decomposition.uplus · eigs · gmres · ichol · ilu · lu · ordeig · ordqz · ordschur · pcg · qr · qz · rsf2csf · schur · svd
Structure
bandwidth · isdiag · ishermitian · issymmetric · istril · istriu · symrcm
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 eig is executed, line by line, in Rust.
- View the source for eig 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.