From 31f9e2d3c5df3adb215723870d3da21e1d973cbe Mon Sep 17 00:00:00 2001 From: Jarrod Millman Date: Fri, 5 Jul 2024 10:46:57 -0700 Subject: [PATCH 1/7] Fix LOBPCG Sakurai typos --- advanced/scipy_sparse/examples/lobpcg_sakurai.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/advanced/scipy_sparse/examples/lobpcg_sakurai.py b/advanced/scipy_sparse/examples/lobpcg_sakurai.py index fc355af75..945246caf 100644 --- a/advanced/scipy_sparse/examples/lobpcg_sakurai.py +++ b/advanced/scipy_sparse/examples/lobpcg_sakurai.py @@ -21,7 +21,7 @@ def sakurai(n): """ A = sp.sparse.eye(n, n) - d0 = np.array(r_[5, 6 * ones(n - 2), 5]) + d0 = np.r_[5, 6 * np.ones(n - 2), 5] d1 = -4 * np.ones(n) d2 = np.ones(n) B = sp.sparse.spdiags([d2, d1, d0, d1, d2], [-2, -1, 0, 1, 2], n, n) @@ -41,13 +41,13 @@ def sakurai(n): # n = 2500 A, B, w_ex = sakurai(n) # Mikota pair -X = np.rand(n, m) +X = np.random.rand(n, m) data = [] -tt = time.clock() +tt = time.process_time() eigs, vecs, resnh = sp.sparse.linalg.lobpcg( A, X, B, tol=1e-6, maxiter=500, retResidualNormsHistory=1 ) -data.append(time.clock() - tt) +data.append(time.process_time() - tt) print("Results by LOBPCG for n=" + str(n)) print() print(eigs) From 12a2f999a1fb48ea22701a969f379b152072197a Mon Sep 17 00:00:00 2001 From: Jarrod Millman Date: Fri, 5 Jul 2024 11:14:17 -0700 Subject: [PATCH 2/7] Apply suggestions from code review Co-authored-by: Stefan van der Walt --- advanced/scipy_sparse/examples/lobpcg_sakurai.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/advanced/scipy_sparse/examples/lobpcg_sakurai.py b/advanced/scipy_sparse/examples/lobpcg_sakurai.py index 945246caf..26553d0b9 100644 --- a/advanced/scipy_sparse/examples/lobpcg_sakurai.py +++ b/advanced/scipy_sparse/examples/lobpcg_sakurai.py @@ -21,7 +21,7 @@ def sakurai(n): """ A = sp.sparse.eye(n, n) - d0 = np.r_[5, 6 * np.ones(n - 2), 5] + d0 = np.hstack([5, 6 * np.ones(n - 2), 5]) d1 = -4 * np.ones(n) d2 = np.ones(n) B = sp.sparse.spdiags([d2, d1, d0, d1, d2], [-2, -1, 0, 1, 2], n, n) @@ -41,13 +41,13 @@ def sakurai(n): # n = 2500 A, B, w_ex = sakurai(n) # Mikota pair -X = np.random.rand(n, m) +X = np.random.random((n, m)) data = [] -tt = time.process_time() +tt = time.time() eigs, vecs, resnh = sp.sparse.linalg.lobpcg( A, X, B, tol=1e-6, maxiter=500, retResidualNormsHistory=1 ) -data.append(time.process_time() - tt) +data.append(time.time() - tt) print("Results by LOBPCG for n=" + str(n)) print() print(eigs) From f47cbcceb5ee93af79810386699c6e036a30aaac Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Fri, 5 Jul 2024 11:31:33 -0700 Subject: [PATCH 3/7] Fix direct solve --- advanced/scipy_sparse/examples/direct_solve.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/advanced/scipy_sparse/examples/direct_solve.py b/advanced/scipy_sparse/examples/direct_solve.py index e342618bf..38de7cf7e 100644 --- a/advanced/scipy_sparse/examples/direct_solve.py +++ b/advanced/scipy_sparse/examples/direct_solve.py @@ -15,7 +15,7 @@ mtx = sp.sparse.lil_array((1000, 1000), dtype=np.float64) mtx[0, :100] = rng.random(100) -mtx[1, 100:200] = mtx[0, :100] +mtx[1, 100:200] = mtx[[0], :100] mtx.setdiag(rng.random(1000)) plt.clf() @@ -27,4 +27,4 @@ x = sp.sparse.linalg.spsolve(mtx, rhs) -print(f"residual: {np.linalg.norm(mtx * x - rhs)!r}") +print(f"residual: {np.linalg.norm(mtx @ x - rhs)!r}") From f91703cfb4dde52638033ec565d21f3db0b0ff7d Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Fri, 5 Jul 2024 11:32:35 -0700 Subject: [PATCH 4/7] Make pyamg example run --- advanced/scipy_sparse/examples/pyamg_with_lobpcg.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/advanced/scipy_sparse/examples/pyamg_with_lobpcg.py b/advanced/scipy_sparse/examples/pyamg_with_lobpcg.py index a7eb4a271..5713a4c29 100644 --- a/advanced/scipy_sparse/examples/pyamg_with_lobpcg.py +++ b/advanced/scipy_sparse/examples/pyamg_with_lobpcg.py @@ -7,6 +7,7 @@ Dirichlet boundary conditions. """ +import numpy as np import scipy as sp import matplotlib.pyplot as plt @@ -21,7 +22,7 @@ ml = smoothed_aggregation_solver(A) # initial approximation to the K eigenvectors -X = sp.rand(A.shape[0], K) +X = np.random.random((A.shape[0], K)) # preconditioner based on ml M = ml.aspreconditioner() From 1e6fc6488e919b63bdf56b598ff8cb56335e123d Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Fri, 5 Jul 2024 12:07:25 -0700 Subject: [PATCH 5/7] Calculate smallest eigenvalues --- advanced/scipy_sparse/examples/lobpcg_sakurai.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced/scipy_sparse/examples/lobpcg_sakurai.py b/advanced/scipy_sparse/examples/lobpcg_sakurai.py index 26553d0b9..00dc08445 100644 --- a/advanced/scipy_sparse/examples/lobpcg_sakurai.py +++ b/advanced/scipy_sparse/examples/lobpcg_sakurai.py @@ -45,7 +45,7 @@ def sakurai(n): data = [] tt = time.time() eigs, vecs, resnh = sp.sparse.linalg.lobpcg( - A, X, B, tol=1e-6, maxiter=500, retResidualNormsHistory=1 + A, X, B, tol=1e-6, largest=False, maxiter=500, retResidualNormsHistory=1, ) data.append(time.time() - tt) print("Results by LOBPCG for n=" + str(n)) From 14d40387f314b0ed910945d17bfc1b4244e16896 Mon Sep 17 00:00:00 2001 From: Jarrod Millman Date: Mon, 8 Jul 2024 13:52:16 -0700 Subject: [PATCH 6/7] Fix formatting --- advanced/scipy_sparse/examples/lobpcg_sakurai.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/advanced/scipy_sparse/examples/lobpcg_sakurai.py b/advanced/scipy_sparse/examples/lobpcg_sakurai.py index 00dc08445..7f7b74cdf 100644 --- a/advanced/scipy_sparse/examples/lobpcg_sakurai.py +++ b/advanced/scipy_sparse/examples/lobpcg_sakurai.py @@ -45,7 +45,13 @@ def sakurai(n): data = [] tt = time.time() eigs, vecs, resnh = sp.sparse.linalg.lobpcg( - A, X, B, tol=1e-6, largest=False, maxiter=500, retResidualNormsHistory=1, + A, + X, + B, + tol=1e-6, + largest=False, + maxiter=500, + retResidualNormsHistory=1, ) data.append(time.time() - tt) print("Results by LOBPCG for n=" + str(n)) From f58617e1a1111e40c05558258fd8272c4ce09ce7 Mon Sep 17 00:00:00 2001 From: Jarrod Millman Date: Mon, 8 Jul 2024 13:59:16 -0700 Subject: [PATCH 7/7] Increase number of iterations to improve accuracy --- advanced/scipy_sparse/examples/lobpcg_sakurai.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced/scipy_sparse/examples/lobpcg_sakurai.py b/advanced/scipy_sparse/examples/lobpcg_sakurai.py index 7f7b74cdf..1ab2618b7 100644 --- a/advanced/scipy_sparse/examples/lobpcg_sakurai.py +++ b/advanced/scipy_sparse/examples/lobpcg_sakurai.py @@ -50,7 +50,7 @@ def sakurai(n): B, tol=1e-6, largest=False, - maxiter=500, + maxiter=2000, retResidualNormsHistory=1, ) data.append(time.time() - tt)