From ab246373e6bc725755858d6728144ed56caf4911 Mon Sep 17 00:00:00 2001 From: Nanoemm Date: Wed, 20 Nov 2024 09:00:17 +0000 Subject: [PATCH 1/2] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index f0a34a553..50a34802b 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -559,6 +559,7 @@ * [Test Knapsack](knapsack/tests/test_knapsack.py) ## Linear Algebra + * [Lanczos-Algorithm](linear_algebra/Lanczos-algorithm.py) * [Gaussian Elimination](linear_algebra/gaussian_elimination.py) * [Jacobi Iteration Method](linear_algebra/jacobi_iteration_method.py) * [Lu Decomposition](linear_algebra/lu_decomposition.py) From d470987ca8b417e435cc6f4d2f4fd2a2351e2fd5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 20 Nov 2024 09:16:12 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- linear_algebra/Lanczos-algorithm.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/linear_algebra/Lanczos-algorithm.py b/linear_algebra/Lanczos-algorithm.py index 4bdba1bce..d8e9ea291 100644 --- a/linear_algebra/Lanczos-algorithm.py +++ b/linear_algebra/Lanczos-algorithm.py @@ -1,20 +1,22 @@ import numpy as np + + def lanczos(A: np.ndarray) -> ([float], [float]): """ - Implements the Lanczos algorithm for a symmetric matrix. + Implements the Lanczos algorithm for a symmetric matrix. - Parameters: - ----------- - matrix : numpy.ndarray - Symmetric matrix of size (n, n). + 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. - """ + 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)) V[:, 0] = np.random.randn(n) @@ -31,4 +33,4 @@ def lanczos(A: np.ndarray) -> ([float], [float]): w -= beta[j - 1] * V[:, j - 1] beta.append(np.linalg.norm(w)) V[:, j + 1] = w / beta[j] - return alpha, beta \ No newline at end of file + return alpha, beta