What does the eig function do in MATLAB / RunMat?
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').
How does the eig function behave in MATLAB / RunMat?
- Single output
d = eig(A)returns the eigenvalues as ann × 1column vector (values may be complex even whenAis real). - Two outputs
[V,D] = eig(A)return the eigenvectors (columns ofV) and the diagonal eigenvalue matrixD. - Three outputs
[V,D,W] = eig(A)additionally return the left eigenvectorsWsatisfyingW' * 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)returns5). - 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.
eig Function GPU Execution Behaviour
- The WGPU provider implements the reserved
eighook 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. ReapplygpuArrayif you want to continue on the device after such a fallback. - Because
eigis a residency sink, the fusion planner treats it as a barrier and does not attempt to fuse surrounding elementwise work.
Examples of using the eig function in MATLAB / RunMat
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;
recon matches A (up to numerical precision).
Retrieving Left Eigenvectors with Three Outputs
A = [4 2; 1 3];
[V,D,W] = eig(A);
check = W' * A - D * W';
All entries of check are numerically zero.
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);
d equals the diagonal entries [10; -2; 7].
Handling Repeated Eigenvalues
A = [3 1 0; 0 3 0; 0 0 5];
d = eig(A);
d contains the repeated eigenvalue 3 twice.
Using the 'nobalance' Option
A = [1e6 1; 0 1e-6];
d_balanced = eig(A);
d_nobalance = eig(A, 'nobalance');
Both calls return the same eigenvalues in the current implementation; the keyword is accepted for MATLAB compatibility, but it currently forces the CPU fallback even when a GPU provider is active.
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
d is a 2×1 column vector containing the eigenvalues, while V still holds the
corresponding eigenvectors.
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
For matrices whose eigenvalues or eigenvectors require complex storage (or when
'nobalance' is requested) the call falls back to the CPU; reapply gpuArray
if you need to continue on the device.
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.
See Also
svd, qr, lu, chol, gpuArray, gather
Source & Feedback
- Implementation:
crates/runmat-runtime/src/builtins/math/linalg/factor/eig.rs - Feedback: RunMat issue tracker