You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
afgl/tests/test_main.py

31 lines
639 B
Python

import numpy as np
import numpy.linalg as LA
from afgl.util.build_T_matrix import build_T_matrix
from afgl.util.lanczos import lanczos
"""
Todo: better test case
"""
def test_lanczos_return_correct_solution():
N = 1000
M = 999
eigvals = np.random.uniform(10000, 100000, N)
Q, _ = LA.qr(np.random.randn(N, N))
L = Q @ np.diag(eigvals) @ Q.T
s = np.random.randint(1, 10, N)
[V, alp, beta] = lanczos(L, s, M)
T = build_T_matrix(alp, beta)
x = LA.solve(L, s)
e_1 = np.zeros(M)
e_1[0] = 1
y = (LA.inv(T) @ e_1) * LA.norm(s)
x_lanczos = V @ y
assert LA.norm(x - x_lanczos) < 1e-10