Skip to content

Fix complex eigenvectors for size 3 and up - #72

Open
wtn wants to merge 1 commit into
ruby:masterfrom
wtn:vectors
Open

wtn wants to merge 1 commit into
ruby:masterfrom
wtn:vectors

Conversation

@wtn

@wtn wtn commented Nov 16, 2026

Copy link
Copy Markdown

Matrix#eigensystem can return wrong eigenvectors for a matrix of size 3 or more that has complex eigenvalues. The eigenvalues are correct.

m = Matrix[[1.0, 2.0, 3.0], [0.0, 0.0, -1.0], [0.0, 1.0, 0.0]]
e = m.eigensystem
value, vector = e.eigenvalues[1], e.eigenvectors[1]
value          #=> (0.0+1.0i)
m * vector     #=> Vector[(0.0+6.0i), (0.0-1.0i), (-1.0+0.0i)]
vector * value #=> Vector[(-3.0+2.0i), (-0.0-1.0i), (-1.0+0.0i)]

Since a3d4068, the branch for complex eigenvectors in hessenberg_to_real_schur reads the outer variable n, which is -1 after the QR loop. The commit renamed the block parameter of the back-substitution loop from n to k to avoid a -w warning, and the rename covered only the branch for real eigenvectors.

Fix

The branch for complex eigenvectors now uses k. With the example above, m * vector is Vector[(0.5+2.5i), (0.0-1.0i), (-1.0+0.0i)] and equals vector * value.

eigenvector_matrix_inv is computed from the same eigenvectors. Before this change, Matrix[[0.0, -1.0, 2.0], [1.0, 0.0, 3.0], [0.0, 0.0, 5.0]].eigensystem.eigenvector_matrix_inv raises ErrNotRegular; after, v * d * v_inv equals the matrix.

hessenberg_to_real_schur used the outer variable n, which is -1 after
the QR loop, in the branch that computes complex eigenvectors. The
branch for complex eigenvectors now uses the block parameter k, like the
branch for real eigenvectors.

Co-authored-by: Claude Mythos 5.2 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant