Skip to content

fix: keep power iteration relative error nonnegative - #15465

Open
pralav-25 wants to merge 1 commit into
TheAlgorithms:masterfrom
pralav-25:fix/power-iteration-negative-eigenvalue
Open

pralav-25 wants to merge 1 commit into
TheAlgorithms:masterfrom
pralav-25:fix/power-iteration-negative-eigenvalue

Conversation

@pralav-25

@pralav-25 pralav-25 commented Sep 29, 2026 •

Copy link
Copy Markdown

Describe your change

Power iteration can stop after its first iteration when the Rayleigh quotient is negative. The relative-error denominator currently uses the signed quotient, so a negative estimate produces a negative error that immediately satisfies the positive tolerance.

For example, power_iteration(np.diag([-4.0, 2.0]), np.ones(2)) returns an eigenvalue of -2.8 with a residual norm of 2.4. Dividing by the magnitude of the quotient keeps the relative error nonnegative and allows convergence to the dominant eigenvalue, -4.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- This PR changes code only, following the repository's request to keep code and doctest changes separate.
  • Documentation change?

Reproduction and verification:

import numpy as np
from linear_algebra.src.power_iteration import power_iteration

matrix = np.diag([-4.0, 2.0])
value, vector = power_iteration(matrix, np.ones(2))
assert np.isclose(value, -4.0, rtol=0, atol=1e-9)
assert np.linalg.norm(matrix @ vector - value * vector) < 1e-5

Validation:

  • All 5 existing doctests pass, including the real and Hermitian checks in test_power_iteration().
  • Six additional local regression cases (three negative-dominant real/Hermitian matrices, each with flat and column vectors) fail before the change and pass afterward against numpy.linalg.eigvalsh and eigenvector residuals.
  • ruff check and ruff format --check pass for the changed file.
  • The repository's full pre-commit run --all-files check passes.
  • Local numerical checks used Python 3.12 and NumPy 2.3.5. The full Python 3.15 repository test suite was not run locally.

Upstream CI: Ruff, type checking, documentation build, and pre-commit checks passed. The test run reports 3,428 passed and one failure in the untouched web_programming/crypto_price_tracker.py doctest: CoinGecko's live API returned HTTP 403. The power-iteration tests passed in that run.

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values retain their Python type hints.
  • All existing doctests pass.
  • No new algorithm or Python file is introduced.

@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files tests are failing Do not merge until tests pass labels Sep 29, 2026

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

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files tests are failing Do not merge until tests pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants