Skip to content

Commit 3f0e139

Browse files
committed
Minor refactoring
1 parent 356fcb7 commit 3f0e139

File tree

6 files changed

+111
-74
lines changed

6 files changed

+111
-74
lines changed

.github/workflows/formatter.yml

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
name: black formatter
1+
name: Format with Black
22

33
on:
44
push:
55
branches:
66
- main
77
- final_project
8-
9-
pull_request:
10-
branches:
11-
- main
12-
- final_project
8+
workflow_dispatch: # this allows manual trigger from GitHub UI, even though it should not be necessary
139

1410
jobs:
15-
linter:
16-
name: Testing using black formatter
11+
format:
1712
runs-on: ubuntu-latest
1813
steps:
19-
- uses: actions/checkout@v3
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
with:
17+
fetch-depth: 0
2018

2119
- name: Set up Python
22-
uses: actions/setup-python@v3
20+
uses: actions/setup-python@v5
2321
with:
24-
python-version: 3.10.16
25-
26-
- name: Set up Python environment
27-
run: |
28-
python -m venv venv
29-
source venv/bin/activate
22+
python-version: "3.12"
3023

31-
- name: Install dependencies
32-
run: |
33-
source venv/bin/activate
34-
python -m pip install black
35-
36-
- name: Check using Black
24+
- name: Install Black
25+
run: pip install black
26+
27+
- name: Run Black
28+
run: black .
29+
30+
- name: Commit changes
3731
run: |
38-
source venv/bin/activate
39-
python -m black --check .
32+
git config --global user.name "github-actions[bot]"
33+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
34+
git add .
35+
git diff --cached --quiet || git commit -m "Apply Black code formatting"
36+
git push
37+
env:
38+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
39+

src/pyclassify/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@
2626
# QR_cp,
2727
)
2828

29-
from .zero_founder import compute_Psi, secular_solver
29+
from .zero_finder import compute_Psi, secular_solver

src/pyclassify/parallel_tridiag_eigen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from QR_cpp import QR_algorithm
55
from line_profiler import profile, LineProfiler
66
import scipy.sparse as sp
7-
from zero_founder import secular_solver
7+
from zero_finder import secular_solver
88
from line_profiler import LineProfiler
99

1010
profile = LineProfiler()
File renamed without changes.

test/test_eigensolvers.py

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
# QR_cp,
1717
)
1818
from pyclassify.utils import make_symmetric
19-
from pyclassify.zero_founder import compute_Psi, secular_solver
20-
2119

2220
@pytest.fixture(autouse=True)
2321
def set_random_seed():
@@ -117,52 +115,6 @@ def test_EigenSolver(size):
117115
_ = eigensolver.compute_eigenval(diag=np.arange(2), off_diag=np.arange(49))
118116

119117

120-
d_s = [np.array([-0.3, -0.2, 0.1, 0.2, 0.3]), np.array([-0.1, 0.1, 0.2, 0.3, 0.4])]
121-
rho_s = [1.5, -2]
122-
v_s = [np.array([0.3, 0.2, 0.24, 0.34, 1]), np.array([-1, 0.3, 2, 0.4, 2])]
123-
i_s = [0, 1, 2, 3]
124-
125-
126-
@pytest.mark.parametrize("d", d_s)
127-
@pytest.mark.parametrize("rho", rho_s)
128-
@pytest.mark.parametrize("v", v_s)
129-
@pytest.mark.parametrize("i", i_s)
130-
def test_psi_s(rho, d, v, i):
131-
"""
132-
Test to check that the psi_s appearing in the nonlinear solver satisfy the theoretical constraint.
133-
"""
134-
v = v**2
135-
lambda_guess = (d[i + 1] + d[i]) / 2
136-
psi1, psi2, psi1_prime, psi2_prime = compute_Psi(i, v, d, rho)
137-
138-
assert (
139-
psi1_prime(lambda_guess) * psi2_prime(lambda_guess) >= 0
140-
), "Error. The sign of psi1s or psi2s is wrong."
141-
assert psi1(lambda_guess) * rho <= 0, "Error. Inconsistent with the theory."
142-
assert psi2(lambda_guess) * rho >= 0, "Error. Inconsistent with the theory."
143-
144-
145-
@pytest.mark.parametrize("d", d_s)
146-
@pytest.mark.parametrize("rho", rho_s)
147-
@pytest.mark.parametrize("v", v_s)
148-
def test_compute_eigenvalues(rho, d, v):
149-
D = np.diag(d)
150-
rk_1_update = rho * np.outer(v, v)
151-
L = D + rk_1_update
152-
computed_eigs, _, __ = secular_solver(rho, d, v)
153-
print(len(computed_eigs))
154-
print(computed_eigs[1])
155-
print(computed_eigs)
156-
computed_eigs = np.sort(computed_eigs)
157-
eigs, _ = np.linalg.eig(L)
158-
159-
exact_eigs, _ = np.linalg.eig(L)
160-
exact_eigs = np.sort(exact_eigs)
161-
162-
for i in range(len(exact_eigs)):
163-
assert (
164-
np.abs(computed_eigs[i] - exact_eigs[i]) < 1e-8
165-
), "Error. The eigenvalues were not computed correctly."
166118

167119

168120
# @pytest.mark.parametrize("size", sizes)

test/test_zero_finder.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import numpy as np
2+
import scipy.sparse as sp
3+
4+
import scipy
5+
import pytest
6+
from pyclassify import eigenvalues_np, eigenvalues_sp
7+
from pyclassify.zero_finder import compute_Psi, secular_solver
8+
9+
10+
@pytest.fixture(autouse=True)
11+
def set_random_seed():
12+
seed = 1422
13+
np.random.seed(seed)
14+
15+
16+
d_s = [np.array([-0.3, -0.2, 0.1, 0.2, 0.3]), np.array([-0.1, 0.1, 0.2, 0.3, 0.4])]
17+
rho_s = [1.5, -2]
18+
v_s = [np.array([0.3, 0.2, 0.24, 0.34, 1]), np.array([-1, 0.3, 2, 0.4, 2])]
19+
i_s = [0, 1, 2, 3]
20+
21+
22+
@pytest.mark.parametrize("d", d_s)
23+
@pytest.mark.parametrize("rho", rho_s)
24+
@pytest.mark.parametrize("v", v_s)
25+
@pytest.mark.parametrize("i", i_s)
26+
def test_psi_s(rho, d, v, i):
27+
"""
28+
Test to check that the psi_s appearing in the nonlinear solver satisfy the theoretical constraint.
29+
"""
30+
v = v**2
31+
lambda_guess = (d[i + 1] + d[i]) / 2
32+
psi1, psi2, psi1_prime, psi2_prime = compute_Psi(i, v, d, rho)
33+
34+
assert (
35+
psi1_prime(lambda_guess) * psi2_prime(lambda_guess) >= 0
36+
), "Error. The sign of psi1s or psi2s is wrong."
37+
assert psi1(lambda_guess) * rho <= 0, "Error. Inconsistent with the theory."
38+
assert psi2(lambda_guess) * rho >= 0, "Error. Inconsistent with the theory."
39+
40+
41+
42+
@pytest.mark.parametrize("d", d_s)
43+
@pytest.mark.parametrize("rho", rho_s)
44+
@pytest.mark.parametrize("v", v_s)
45+
def test_compute_eigenvalues(rho, d, v):
46+
D = np.diag(d)
47+
rk_1_update = rho * np.outer(v, v)
48+
L = D + rk_1_update
49+
computed_eigs, _, __ = secular_solver(rho, d, v)
50+
print(len(computed_eigs))
51+
print(computed_eigs[1])
52+
print(computed_eigs)
53+
computed_eigs = np.sort(computed_eigs)
54+
eigs, _ = np.linalg.eig(L)
55+
56+
exact_eigs, _ = np.linalg.eig(L)
57+
exact_eigs = np.sort(exact_eigs)
58+
59+
for i in range(len(exact_eigs)):
60+
assert (
61+
np.abs(computed_eigs[i] - exact_eigs[i]) < 1e-8
62+
), "Error. The eigenvalues were not computed correctly."
63+
64+
@pytest.mark.parametrize("d", d_s)
65+
@pytest.mark.parametrize("rho", rho_s)
66+
@pytest.mark.parametrize("v", v_s)
67+
def test_compute_eigenvalues(rho, d, v):
68+
D = np.diag(d)
69+
rk_1_update = rho * np.outer(v, v)
70+
L = D + rk_1_update
71+
computed_eigs, _, __ = secular_solver(rho, d, v)
72+
print(len(computed_eigs))
73+
print(computed_eigs[1])
74+
print(computed_eigs)
75+
computed_eigs = np.sort(computed_eigs)
76+
eigs, _ = np.linalg.eig(L)
77+
78+
exact_eigs, _ = np.linalg.eig(L)
79+
exact_eigs = np.sort(exact_eigs)
80+
81+
for i in range(len(exact_eigs)):
82+
assert (
83+
np.abs(computed_eigs[i] - exact_eigs[i]) < 1e-8
84+
), "Error. The eigenvalues were not computed correctly."
85+

0 commit comments

Comments
 (0)