Skip to content

Commit

Permalink
generating tridiagonal matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenhky committed Aug 27, 2022
1 parent 9a61ef0 commit 4758842
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
26 changes: 26 additions & 0 deletions mogu/spectral/lanzcos.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@

# implementing Lanzco's algorithm

import numpy as np


def lanzcos(M, k):
assert M.shape[0] == M.shape[1]
assert (M == M.T).all()
d = M.shape[1]
assert k <= d

a = np.zeros(k+1)
b = np.zeros(k)

v0 = np.random.uniform(size=d)
v0 /= np.linalg.norm(v0)

a[0] = v0.T @ M @ v0
v1 = M @ v0 - a[0]*v0
v1 /= np.linalg.norm(v1)

for i in range(1, k+1):
a[i] = v1.T @ M @ v1
b[i-1] = v0.T @ M @ v1
v2 = M @ v1 - a[i]*v1 - b[i-1]*v0
v2 /= np.linalg.norm(v2)

v0, v1 = v1, v2
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Cython>=0.29.0
numpy>=1.18.0
scipy>=1.6.0
numpy>=1.22.0
scipy>=1.9.0
numba>=0.53.0
networkx>=2.0
graphflow>=0.1.1
Expand Down
11 changes: 11 additions & 0 deletions test/test_lanzcos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

import unittest


class test_lanzcos(unittest.TestCase):
def test_dft(self):
pass


if __name__ == '__main__':
unittest.main()

0 comments on commit 4758842

Please sign in to comment.