-
Notifications
You must be signed in to change notification settings - Fork 2
/
_expokitpy.py
173 lines (96 loc) · 4.03 KB
/
_expokitpy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
from ._expokit import dgexpv, dsexpv, zgexpv, zhexpv
import numpy as np
from scipy.sparse.linalg import aslinearoperator,onenormest
__all__ = ["py_dsexpv","py_dgexpv","py_zhexpv","py_zgexpv"]
class ExpokitError(Exception):
pass
messages = {"maximum number of steps reached without convergence":1,
"requested tolerance was too high":2}
def py_dsexpv(v,A,anorm=None,wsp=None,iwsp=None,m=20,t=1.0,tol=0.0,return_work=False):
A = aslinearoperator(A)
v = v.astype(np.float64,casting="safe",copy=False).ravel()
n = v.shape[0]
if A.shape[1] != A.shape[0]:
raise ValueError("Expecting square LinearOperator.")
if A.shape[1] != v.shape[0]:
raise ValueError("Dimension mismatch between LinearOperator and input vector.")
if anorm is None:
anorm=onenormest(A)
if wsp is None:
wsp = np.zeros(7+n*(m+2)+5*(m+2)*(m+2),dtype=np.float64)
if iwsp is None:
iwsp = np.zeros(m+2,dtype=np.int32)
if return_work:
return dict(anorm=anorm,wsp=wsp,iwsp=iwsp,m=m,return_work=False)
u,tol0,iflag0 = dsexpv(m,t,v,tol,anorm,wsp,iwsp,A.matvec,0)
if iflag0 > 0:
raise ExpokitError(messages[iflag])
elif iflag0 < 0:
raise ExpokitError("bad input arguments")
return u
def py_dgexpv(v,A,anorm=None,wsp=None,iwsp=None,m=20,t=1.0,tol=0.0,return_work=False):
A = aslinearoperator(A)
v = v.astype(np.float64,casting="safe",copy=False).ravel()
n = v.shape[0]
if A.shape[1] != A.shape[0]:
raise ValueError("Expecting square LinearOperator.")
if A.shape[1] != v.shape[0]:
raise ValueError("Dimension mismatch between LinearOperator and input vector.")
if anorm is None:
anorm=onenormest(A)
if wsp is None:
wsp = np.zeros(7+n*(m+2)+5*(m+2)*(m+2),dtype=np.float64)
if iwsp is None:
iwsp = np.zeros(m+2,dtype=np.int32)
if return_work:
return dict(anorm=anorm,wsp=wsp,iwsp=iwsp,m=m,return_work=False)
u,tol0,iflag0 = dgexpv(m,t,v,tol,anorm,wsp,iwsp,A.matvec,0)
if iflag0 > 0:
raise ExpokitError(messages[iflag0])
elif iflag0 < 0:
raise ExpokitError("bad input arguments")
return u
def py_zhexpv(v,A,anorm=None,wsp=None,iwsp=None,m=20,t=1.0,tol=0.0,return_work=False):
A = aslinearoperator(A)
v = v.astype(np.complex128,casting="safe",copy=False).ravel()
n = v.shape[0]
if A.shape[1] != A.shape[0]:
raise ValueError("Expecting square LinearOperator.")
if A.shape[1] != v.shape[0]:
raise ValueError("Dimension mismatch between LinearOperator and input vector.")
if anorm is None:
anorm=onenormest(A)
if wsp is None:
wsp = np.zeros(7+n*(m+2)+5*(m+2)*(m+2),dtype=np.complex128)
if iwsp is None:
iwsp = np.zeros(m+2,dtype=np.int32)
if return_work:
return dict(anorm=anorm,wsp=wsp,iwsp=iwsp,m=m,return_work=False)
u,tol0,iflag0 = zhexpv(m,t,v,tol,anorm,wsp,iwsp,A.matvec,0)
if iflag0 > 0:
raise ExpokitError(messages[iflag0])
elif iflag0 < 0:
raise ExpokitError("bad input arguments")
return u
def py_zgexpv(v,A,anorm=None,wsp=None,iwsp=None,m=20,t=1.0,tol=0.0,return_work=False):
A = aslinearoperator(A)
v = v.astype(np.complex128,casting="safe",copy=False).ravel()
n = v.shape[0]
if A.shape[1] != A.shape[0]:
raise ValueError("Expecting square LinearOperator.")
if A.shape[1] != v.shape[0]:
raise ValueError("Dimension mismatch between LinearOperator and input vector.")
if anorm is None:
anorm=onenormest(A)
if wsp is None:
wsp = np.zeros(7+n*(m+2)+5*(m+2)*(m+2),dtype=np.complex128)
if iwsp is None:
iwsp = np.zeros(m+2,dtype=np.int32)
if return_work:
return dict(anorm=anorm,wsp=wsp,iwsp=iwsp,m=m,return_work=False)
u,tol,iflag0 = zgexpv(m,t,v,tol,anorm,wsp,iwsp,A.matvec,0)
if iflag0 > 0:
raise ExpokitError(messages[iflag])
elif iflag0 < 0:
raise ExpokitError("bad input arguments")
return u