-
Notifications
You must be signed in to change notification settings - Fork 24
/
test_umfpack.py
241 lines (184 loc) · 7.47 KB
/
test_umfpack.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
""" Test functions for UMFPACK wrappers
"""
from __future__ import division, print_function, absolute_import
import random
import unittest
import warnings
from numpy.testing import assert_array_almost_equal
from scipy.sparse import csc_matrix, linalg, spdiags, SparseEfficiencyWarning
import numpy as np
import scikits.umfpack as um
_is_32bit_platform = np.intp(0).itemsize < 8
# Force int64 index dtype even when indices fit into int32.
def _to_int64(x):
y = csc_matrix(x).copy()
y.indptr = y.indptr.astype(np.int64)
y.indices = y.indices.astype(np.int64)
return y
class _DeprecationAccept(unittest.TestCase):
def setUp(self):
self.mgr = warnings.catch_warnings()
self.mgr.__enter__()
warnings.simplefilter('ignore', SparseEfficiencyWarning)
def tearDown(self):
self.mgr.__exit__()
class TestScipySolvers(_DeprecationAccept):
"""Tests inverting a sparse linear system"""
def test_solve_complex_umfpack(self):
# Solve with UMFPACK: double precision complex
linalg.use_solver(useUmfpack=True)
a = self.a.astype('D')
b = self.b
x = linalg.spsolve(a, b)
assert_array_almost_equal(a*x, b)
@unittest.skipIf(_is_32bit_platform, reason="requires 64 bit platform")
def test_solve_complex_long_umfpack(self):
# Solve with UMFPACK: double precision complex, long indices
linalg.use_solver(useUmfpack=True)
a = _to_int64(self.a.astype('D'))
b = self.b
x = linalg.spsolve(a, b)
assert_array_almost_equal(a*x, b)
def test_solve_umfpack(self):
# Solve with UMFPACK: double precision
linalg.use_solver(useUmfpack=True)
a = self.a.astype('d')
b = self.b
x = linalg.spsolve(a, b)
assert_array_almost_equal(a*x, b)
@unittest.skipIf(_is_32bit_platform, reason="requires 64 bit platform")
def test_solve_long_umfpack(self):
# Solve with UMFPACK: double precision
linalg.use_solver(useUmfpack=True)
a = _to_int64(self.a.astype('d'))
b = self.b
x = linalg.spsolve(a, b)
assert_array_almost_equal(a*x, b)
def test_solve_sparse_rhs(self):
# Solve with UMFPACK: double precision, sparse rhs
linalg.use_solver(useUmfpack=True)
a = self.a.astype('d')
b = csc_matrix(self.b).T
x = linalg.spsolve(a, b)
assert_array_almost_equal(a*x, self.b)
def test_factorized_umfpack(self):
# Prefactorize (with UMFPACK) matrix for solving with multiple rhs
linalg.use_solver(useUmfpack=True)
a = self.a.astype('d')
solve = linalg.factorized(a)
x1 = solve(self.b)
assert_array_almost_equal(a*x1, self.b)
x2 = solve(self.b2)
assert_array_almost_equal(a*x2, self.b2)
@unittest.skipIf(_is_32bit_platform, reason="requires 64 bit platform")
def test_factorized_long_umfpack(self):
# Prefactorize (with UMFPACK) matrix for solving with multiple rhs
linalg.use_solver(useUmfpack=True)
a = _to_int64(self.a.astype('d'))
solve = linalg.factorized(a)
x1 = solve(self.b)
assert_array_almost_equal(a*x1, self.b)
x2 = solve(self.b2)
assert_array_almost_equal(a*x2, self.b2)
def test_factorized_without_umfpack(self):
# Prefactorize matrix for solving with multiple rhs
linalg.use_solver(useUmfpack=False)
a = self.a.astype('d')
solve = linalg.factorized(a)
x1 = solve(self.b)
assert_array_almost_equal(a*x1, self.b)
x2 = solve(self.b2)
assert_array_almost_equal(a*x2, self.b2)
def setUp(self):
self.a = spdiags([[1, 2, 3, 4, 5], [6, 5, 8, 9, 10]], [0, 1], 5, 5)
self.a2 = _to_int64(self.a)
self.b = np.array([1, 2, 3, 4, 5], dtype=np.float64)
self.b2 = np.array([5, 4, 3, 2, 1], dtype=np.float64)
_DeprecationAccept.setUp(self)
class TestFactorization(_DeprecationAccept):
"""Tests factorizing a sparse linear system"""
def test_complex_lu(self):
# Getting factors of complex matrix
umfpack = um.UmfpackContext("zi")
for A in self.complex_matrices:
umfpack.numeric(A)
(L,U,P,Q,R,do_recip) = umfpack.lu(A)
L = L.todense()
U = U.todense()
A = A.todense()
if not do_recip:
R = 1.0/R
R = np.matrix(np.diag(R))
P = np.eye(A.shape[0])[P,:]
Q = np.eye(A.shape[1])[:,Q]
assert_array_almost_equal(P*R*A*Q,L*U)
@unittest.skipIf(_is_32bit_platform, reason="requires 64 bit platform")
def test_complex_int64_lu(self):
# Getting factors of complex matrix with long indices
umfpack = um.UmfpackContext("zl")
for A in self.complex_int64_matrices:
umfpack.numeric(A)
(L,U,P,Q,R,do_recip) = umfpack.lu(A)
L = L.todense()
U = U.todense()
A = A.todense()
if not do_recip:
R = 1.0/R
R = np.matrix(np.diag(R))
P = np.eye(A.shape[0])[P,:]
Q = np.eye(A.shape[1])[:,Q]
assert_array_almost_equal(P*R*A*Q,L*U)
def test_real_lu(self):
# Getting factors of real matrix
umfpack = um.UmfpackContext("di")
for A in self.real_matrices:
umfpack.numeric(A)
(L,U,P,Q,R,do_recip) = umfpack.lu(A)
L = L.todense()
U = U.todense()
A = A.todense()
if not do_recip:
R = 1.0/R
R = np.matrix(np.diag(R))
P = np.eye(A.shape[0])[P,:]
Q = np.eye(A.shape[1])[:,Q]
assert_array_almost_equal(P*R*A*Q,L*U)
@unittest.skipIf(_is_32bit_platform, reason="requires 64 bit platform")
def test_real_int64_lu(self):
# Getting factors of real matrix with long indices
umfpack = um.UmfpackContext("dl")
for A in self.real_int64_matrices:
umfpack.numeric(A)
(L,U,P,Q,R,do_recip) = umfpack.lu(A)
L = L.todense()
U = U.todense()
A = A.todense()
if not do_recip:
R = 1.0/R
R = np.matrix(np.diag(R))
P = np.eye(A.shape[0])[P,:]
Q = np.eye(A.shape[1])[:,Q]
assert_array_almost_equal(P*R*A*Q,L*U)
def setUp(self):
random.seed(0) # make tests repeatable
real_matrices = []
real_matrices.append(spdiags([[1, 2, 3, 4, 5], [6, 5, 8, 9, 10]],
[0, 1], 5, 5))
real_matrices.append(spdiags([[1, 2, 3, 4, 5], [6, 5, 8, 9, 10]],
[0, 1], 4, 5))
real_matrices.append(spdiags([[1, 2, 3, 4, 5], [6, 5, 8, 9, 10]],
[0, 2], 5, 5))
real_matrices.append(np.random.rand(3,3))
real_matrices.append(np.random.rand(5,4))
real_matrices.append(np.random.rand(4,5))
self.real_matrices = [csc_matrix(x).astype('d')
for x in real_matrices]
self.complex_matrices = [x.astype(np.complex128)
for x in self.real_matrices]
self.real_int64_matrices = [_to_int64(x)
for x in self.real_matrices]
self.complex_int64_matrices = [_to_int64(x)
for x in self.complex_matrices]
_DeprecationAccept.setUp(self)
if __name__ == "__main__":
unittest.main()