Skip to content
Closed
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@
## Linear Algebra
* [Gaussian Elimination](linear_algebra/gaussian_elimination.py)
* [Jacobi Iteration Method](linear_algebra/jacobi_iteration_method.py)
* [Lanczos Algorithm](linear_algebra/lanczos_algorithm.py)
* [Lu Decomposition](linear_algebra/lu_decomposition.py)
* Src
* [Conjugate Gradient](linear_algebra/src/conjugate_gradient.py)
Expand Down
37 changes: 37 additions & 0 deletions linear_algebra/lanczos_algorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import numpy as np


def lanczos(a: np.ndarray) -> tuple[list[float], list[float]]:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file linear_algebra/lanczos_algorithm.py, please provide doctest for the function lanczos

Please provide descriptive name for the parameter: a

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file linear_algebra/lanczos_algorithm.py, please provide doctest for the function lanczos

Please provide descriptive name for the parameter: a

"""
Implements the Lanczos algorithm for a symmetric matrix.

Parameters:
-----------
matrix : numpy.ndarray
Symmetric matrix of size (n, n).

Returns:
--------
alpha : [float]
List of diagonal elements of the resulting tridiagonal matrix.
beta : [float]
List of off-diagonal elements of the resulting tridiagonal matrix.
"""
n = a.shape[0]
v = np.zeros((n, n))
rng = np.random.default_rng()
v[:, 0] = rng.standard_normal(n)
v[:, 0] /= np.linalg.norm(v[:, 0])
alpha: list[float] = []
beta: list[float] = []
for j in range(n):
w = np.dot(a, v[:, j])
alpha.append(np.dot(w, v[:, j]))
if j == n - 1:
break
w -= alpha[j] * v[:, j]
if j > 0:
w -= beta[j - 1] * v[:, j - 1]
beta.append(np.linalg.norm(w))
v[:, j + 1] = w / beta[j]
return alpha, beta