-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/luk036/ellalgo
- Loading branch information
Showing
7 changed files
with
140 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -173,3 +173,6 @@ MANIFEST | |
.venv*/ | ||
.conda*/ | ||
.python-version | ||
|
||
# my stuffs | ||
.hypothesis |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import numpy as np | ||
|
||
""" | ||
Power method for finding the largest eigenvalue of a square matrix | ||
""" | ||
|
||
|
||
class Options: | ||
def __init__(self, max_iters, tolerance): | ||
self.max_iters = max_iters | ||
self.tolerance = tolerance | ||
|
||
|
||
def norm_l1(x): | ||
return np.sum(np.abs(x)) | ||
|
||
|
||
def power_iteration(A, x, options): | ||
"""Power iteration method | ||
x: assuming not zero. | ||
""" | ||
x /= np.sqrt(np.sum(x**2)) | ||
for niter in range(options.max_iters): | ||
x1 = x | ||
x = A @ x1 | ||
x /= np.sqrt(np.sum(x**2)) | ||
if norm_l1(x - x1) <= options.tolerance or norm_l1(x + x1) <= options.tolerance: | ||
return x, x @ (A @ x), niter | ||
return x, x @ (A @ x), options.max_iters | ||
|
||
|
||
def power_iteration4(A, x, options): | ||
"""Power iteration method | ||
x: assuming not zero. | ||
""" | ||
x /= norm_l1(x) | ||
for niter in range(options.max_iters): | ||
x1 = x | ||
x = A @ x1 | ||
x /= norm_l1(x) | ||
if norm_l1(x - x1) <= options.tolerance or norm_l1(x + x1) <= options.tolerance: | ||
x /= np.sqrt(np.sum(x**2)) | ||
return x, x @ (A @ x), niter | ||
x /= np.sqrt(np.sum(x**2)) | ||
return x, x @ (A @ x), options.max_iters | ||
|
||
|
||
def power_iteration2(A, x, options): | ||
"""Power iteration method | ||
x: assuming not zero. | ||
""" | ||
x /= np.sqrt(np.sum(x**2)) | ||
new = A @ x | ||
ld = x @ new | ||
for niter in range(options.max_iters): | ||
ld1 = ld | ||
x[:] = new[:] | ||
x /= np.sqrt(np.sum(x**2)) | ||
new = A @ x | ||
ld = x @ new | ||
if abs(ld1 - ld) <= options.tolerance: | ||
return x, ld, niter | ||
return x, ld, options.max_iters | ||
|
||
|
||
def power_iteration3(A, x, options): | ||
"""Power iteration method | ||
x: assuming not zero. | ||
""" | ||
new = A @ x | ||
dot = x @ x | ||
ld = (x @ new) / dot | ||
for niter in range(options.max_iters): | ||
ld1 = ld | ||
x[:] = new[:] | ||
dot = x @ x | ||
if dot >= 1e150: | ||
x /= np.sqrt(np.sum(x**2)) | ||
new = A @ x | ||
ld = x @ new | ||
if abs(ld1 - ld) <= options.tolerance: | ||
return x, ld, niter | ||
else: | ||
new = A @ x | ||
ld = (x @ new) / dot | ||
if abs(ld1 - ld) <= options.tolerance: | ||
x /= np.sqrt(np.sum(x**2)) | ||
return x, ld, niter | ||
x /= np.sqrt(np.sum(x**2)) | ||
return x, ld, options.max_iters | ||
|
||
|
||
# Test data | ||
A = np.array([[3.7, -3.6, 0.7], [-3.6, 4.3, -2.8], [0.7, -2.8, 5.4]]) | ||
options = Options(max_iters=2000, tolerance=1e-7) | ||
|
||
x = np.array([0.3, 0.5, 0.4]) | ||
x1, ld, niter = power_iteration(A, x, options) | ||
print(x1) | ||
print(ld) | ||
|
||
x = np.array([0.3, 0.5, 0.4]) | ||
x4, ld, niter = power_iteration4(A, x, options) | ||
print(x4) | ||
print(ld) | ||
|
||
options.tolerance = 1e-14 | ||
|
||
x = np.array([0.3, 0.5, 0.4]) | ||
x2, ld, niter = power_iteration2(A, x, options) | ||
print(x2) | ||
print(ld) | ||
|
||
x = np.array([0.3, 0.5, 0.4]) | ||
x3, ld, niter = power_iteration3(A, x, options) | ||
print(x3) | ||
print(ld) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
PlatformWithVersion=Python | ||
PlatformWithVersion=Python | ||
BuildCommands=conda env create --file environment.yml --prefix ./venv --quiet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
""" | ||
Dummy conftest.py for ellalgo. | ||
Dummy conftest.py for ellalgo. | ||
If you don't know what this is for, just leave it empty. | ||
Read more about conftest.py under: | ||
- https://docs.pytest.org/en/stable/fixture.html | ||
- https://docs.pytest.org/en/stable/writing_plugins.html | ||
If you don't know what this is for, just leave it empty. | ||
Read more about conftest.py under: | ||
- https://docs.pytest.org/en/stable/fixture.html | ||
- https://docs.pytest.org/en/stable/writing_plugins.html | ||
""" | ||
|
||
# import pytest |