Skip to content

Commit 75ef976

Browse files
authored
move LGMRES demo over from scipy (#50)
* move LGMRES demo over from scipy * remove output and fix typo
1 parent 9b98b64 commit 75ef976

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

ipython/lgmres.ipynb

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "f43700ac",
6+
"metadata": {},
7+
"source": [
8+
"# LGMRES"
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"id": "6d17c3dc",
14+
"metadata": {},
15+
"source": [
16+
"Example showing how LGMRES avoids some problems in the convergence of restarted GMRES."
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": null,
22+
"id": "9f4264f8",
23+
"metadata": {},
24+
"outputs": [],
25+
"source": [
26+
"import scipy.sparse.linalg as la\n",
27+
"import scipy.io as io\n",
28+
"import numpy as np\n",
29+
"import sys\n",
30+
"\n",
31+
"#problem = \"SPARSKIT/drivcav/e05r0100\"\n",
32+
"problem = \"SPARSKIT/drivcav/e05r0200\"\n",
33+
"#problem = \"Harwell-Boeing/sherman/sherman1\"\n",
34+
"#problem = \"misc/hamm/add32\"\n",
35+
"\n",
36+
"mm = np.lib._datasource.Repository('https://math.nist.gov/pub/MatrixMarket2/')\n",
37+
"f = mm.open(f'{problem}.mtx.gz')\n",
38+
"Am = io.mmread(f).tocsr()\n",
39+
"f.close()\n",
40+
"\n",
41+
"f = mm.open(f'{problem}_rhs1.mtx.gz')\n",
42+
"b = np.array(io.mmread(f)).ravel()\n",
43+
"f.close()\n",
44+
"\n",
45+
"bnorm = np.linalg.norm(b)\n",
46+
"count = [0]\n",
47+
"\n",
48+
"\n",
49+
"def matvec(v):\n",
50+
" count[0] += 1\n",
51+
" sys.stderr.write(f\"{count[0]}\\r\")\n",
52+
" return Am@v\n",
53+
"\n",
54+
"\n",
55+
"A = la.LinearOperator(matvec=matvec, shape=Am.shape, dtype=Am.dtype)\n",
56+
"\n",
57+
"M = 100\n",
58+
"\n",
59+
"print(\"MatrixMarket problem %s\" % problem)\n",
60+
"print(f\"Invert {Am.shape[0]} x {Am.shape[1]} matrix; nnz = {Am.nnz}\")\n",
61+
"\n",
62+
"count[0] = 0\n",
63+
"x0, info = la.gmres(A, b, restrt=M, tol=1e-14)\n",
64+
"count_0 = count[0]\n",
65+
"err0 = np.linalg.norm(Am@x0 - b) / bnorm\n",
66+
"print(f\"GMRES({M}): {count_0} matvecs, relative residual: {err0}\")\n",
67+
"if info != 0:\n",
68+
" print(\"Didn't converge\")\n",
69+
"\n",
70+
"count[0] = 0\n",
71+
"x1, info = la.lgmres(A, b, inner_m=M-6*2, outer_k=6, tol=1e-14)\n",
72+
"count_1 = count[0]\n",
73+
"err1 = np.linalg.norm(Am@x1 - b) / bnorm\n",
74+
"print(f\"LGMRES({M - 2*6}, 6) [same memory req.]: {count_1} \"\n",
75+
" f\"matvecs, relative residual: {err1}\")\n",
76+
"if info != 0:\n",
77+
" print(\"Didn't converge\")\n",
78+
"\n",
79+
"count[0] = 0\n",
80+
"x2, info = la.lgmres(A, b, inner_m=M-6, outer_k=6, tol=1e-14)\n",
81+
"count_2 = count[0]\n",
82+
"err2 = np.linalg.norm(Am@x2 - b) / bnorm\n",
83+
"print(f\"LGMRES({M - 6}, 6) [same subspace size]: {count_2} \"\n",
84+
" f\"matvecs, relative residual: {err2}\")\n",
85+
"if info != 0:\n",
86+
" print(\"Didn't converge\")"
87+
]
88+
}
89+
],
90+
"metadata": {
91+
"kernelspec": {
92+
"display_name": "Python 3 (ipykernel)",
93+
"language": "python",
94+
"name": "python3"
95+
},
96+
"language_info": {
97+
"codemirror_mode": {
98+
"name": "ipython",
99+
"version": 3
100+
},
101+
"file_extension": ".py",
102+
"mimetype": "text/x-python",
103+
"name": "python",
104+
"nbconvert_exporter": "python",
105+
"pygments_lexer": "ipython3",
106+
"version": "3.10.5"
107+
}
108+
},
109+
"nbformat": 4,
110+
"nbformat_minor": 5
111+
}

0 commit comments

Comments
 (0)