Skip to content

Commit

Permalink
unit tests for Lanzco's method
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenhky committed Sep 2, 2022
1 parent 9798fc5 commit 03b5a2b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions mogu/spectral/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

from .dft import slowDFT, slowInvDFT
from .fft import FFT, invFFT
from .lanzcos import lanzcos
33 changes: 31 additions & 2 deletions test/test_lanzcos.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@

import unittest

import numpy as np

from mogu.spectral import lanzcos


class test_lanzcos(unittest.TestCase):
def test_dft(self):
pass
def test_four(self):
tol = 1e-8
A = np.array(
[
[1., 1., 2., -1.],
[1., 2., -3., 0.],
[2., -3., -1., 0.],
[-1., 0., 0., 0.5]
]
)
eigval1, eigvec1 = np.linalg.eig(A)
eigval2, eigvec2 = lanzcos(A, A.shape[0])

assert len(eigval2) == A.shape[0] + 1

eig1dict = {}
for i in range(A.shape[0]):
for j in range(len(eigval2)):
if abs(eigval1[i] - eigval2[j]) < tol:
eig1dict[i] = j
break
assert len(set(eig1dict.values())) == len(eigval1)

for i in range(A.shape[0]):
j = eig1dict[i]
assert np.abs(np.linalg.norm(eigvec2[:, j]) - 1.) < tol
assert np.all(np.abs(A @ eigvec2[:, j] / eigval2[j] - eigvec2[:, j]) < tol)


if __name__ == '__main__':
Expand Down

0 comments on commit 03b5a2b

Please sign in to comment.