mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-26 10:58:40 +00:00
Modified file name to be lowercase and replaced - for _
Changed argument name A to a Changed variable name V to v Added specifications for beta and alpha Changed np.random.randn
This commit is contained in:
parent
f38f1a7c83
commit
03a37ebaca
@ -1,5 +1,5 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
def lanczos(A: np.ndarray) -> ([float], [float]):
|
def lanczos(a: np.ndarray) -> tuple[list[float], list[float]]:
|
||||||
"""
|
"""
|
||||||
Implements the Lanczos algorithm for a symmetric matrix.
|
Implements the Lanczos algorithm for a symmetric matrix.
|
||||||
|
|
||||||
@ -15,20 +15,21 @@ def lanczos(A: np.ndarray) -> ([float], [float]):
|
|||||||
beta : [float]
|
beta : [float]
|
||||||
List of off-diagonal elements of the resulting tridiagonal matrix.
|
List of off-diagonal elements of the resulting tridiagonal matrix.
|
||||||
"""
|
"""
|
||||||
n = A.shape[0]
|
n = a.shape[0]
|
||||||
V = np.zeros((n, n))
|
v = np.zeros((n, n))
|
||||||
V[:, 0] = np.random.randn(n)
|
rng = np.random.default_rng()
|
||||||
V[:, 0] /= np.linalg.norm(V[:, 0])
|
v[:, 0] = rng.standard_normal(n)
|
||||||
alpha = []
|
v[:, 0] /= np.linalg.norm(v[:, 0])
|
||||||
beta = []
|
alpha : list[float] = []
|
||||||
|
beta : list[float] = []
|
||||||
for j in range(n):
|
for j in range(n):
|
||||||
w = np.dot(A, V[:, j])
|
w = np.dot(a, v[:, j])
|
||||||
alpha.append(np.dot(w, V[:, j]))
|
alpha.append(np.dot(w, v[:, j]))
|
||||||
if j == n - 1:
|
if j == n - 1:
|
||||||
break
|
break
|
||||||
w -= alpha[j] * V[:, j]
|
w -= alpha[j] * v[:, j]
|
||||||
if j > 0:
|
if j > 0:
|
||||||
w -= beta[j - 1] * V[:, j - 1]
|
w -= beta[j - 1] * v[:, j - 1]
|
||||||
beta.append(np.linalg.norm(w))
|
beta.append(np.linalg.norm(w))
|
||||||
V[:, j + 1] = w / beta[j]
|
v[:, j + 1] = w / beta[j]
|
||||||
return alpha, beta
|
return alpha, beta
|
Loading…
x
Reference in New Issue
Block a user