3
import spmatrix, superlu
8
r = numpy.zeros(n, 'd')
11
return math.sqrt(numpy.dot(r, r))
16
return math.sqrt(numpy.dot(t, t))
19
A = spmatrix.ll_mat_sym(n, n)
25
"compute machine epsilon"
27
while (1.0 + eps > 1.0):
31
class EyeTestCase(unittest.TestCase):
34
self.A = speye(self.n).to_csr()
35
self.b = numpy.ones(self.n, 'd')
36
self.x = numpy.zeros(self.n, 'd')
38
def testTrivial(self):
39
luA = superlu.factorize(self.A)
40
luA.solve(self.b, self.x)
41
self.failUnless(residual(self.A, self.x, self.b) == 0.0)
43
luA = superlu.factorize(self.A, diag_pivot_thresh=0.0)
44
luA.solve(self.b, self.x)
45
self.failUnless(residual(self.A, self.x, self.b) == 0.0)
47
luA = superlu.factorize(self.A, relax=20)
48
luA.solve(self.b, self.x)
49
self.failUnless(residual(self.A, self.x, self.b) == 0.0)
51
luA = superlu.factorize(self.A, panel_size=1)
52
luA.solve(self.b, self.x)
53
self.failUnless(residual(self.A, self.x, self.b) == 0.0)
55
for permc_spec in [0, 1, 2, 3]:
56
luA = superlu.factorize(self.A, permc_spec=permc_spec)
57
luA.solve(self.b, self.x)
58
self.failUnless(residual(self.A, self.x, self.b) == 0.0)
60
class Poisson1dTestCase(unittest.TestCase):
63
self.B = poisson.poisson1d(self.n).to_csr()
65
self.b = numpy.zeros(self.n, 'd')
66
self.x = numpy.zeros(self.n, 'd')
67
self.x_exact = numpy.ones(self.n, 'd')
68
self.x_exact /= math.sqrt(self.n)
69
self.B.matvec(self.x_exact, self.b)
71
lmbd_min = 4.0 * math.sin(math.pi/2.0/self.n) ** 2
72
lmbd_max = 4.0 * math.sin((self.n - 1)*math.pi/2.0/self.n) ** 2
73
cond = lmbd_max/lmbd_min
74
self.tol = cond * macheps()
76
def testPoisson1dDefault(self):
77
luA = superlu.factorize(self.B)
78
luA.solve(self.b, self.x)
79
print error(self.x, self.x_exact), self.tol, luA.nnz
80
self.failUnless(error(self.x, self.x_exact) < self.tol)
82
def testPoisson1dNoPivot(self):
83
luA = superlu.factorize(self.B, diag_pivot_thresh=0.0)
84
luA.solve(self.b, self.x)
85
print error(self.x, self.x_exact), self.tol, luA.nnz
86
self.failUnless(error(self.x, self.x_exact) < self.tol)
88
def testPoisson1dRelax(self):
89
luA = superlu.factorize(self.B, relax=20)
90
luA.solve(self.b, self.x)
91
print error(self.x, self.x_exact), self.tol, luA.nnz
92
self.failUnless(error(self.x, self.x_exact) < self.tol)
94
def testPoisson1dSmallPanel(self):
95
luA = superlu.factorize(self.B, panel_size=1)
96
luA.solve(self.b, self.x)
97
print error(self.x, self.x_exact), self.tol, luA.nnz
98
self.failUnless(error(self.x, self.x_exact) < self.tol)
100
def testPoisson1dOrigOrdering(self):
101
luA = superlu.factorize(self.B, permc_spec=0)
102
luA.solve(self.b, self.x)
103
print error(self.x, self.x_exact), self.tol, luA.nnz
104
self.failUnless(error(self.x, self.x_exact) < self.tol)
106
def testPoisson1dMMD_AtimesA(self):
107
luA = superlu.factorize(self.B, permc_spec=1)
108
luA.solve(self.b, self.x)
109
print error(self.x, self.x_exact), self.tol, luA.nnz
110
self.failUnless(error(self.x, self.x_exact) < self.tol)
112
def testPoisson1dMMD_AplusA(self):
113
luA = superlu.factorize(self.B, permc_spec=2)
114
luA.solve(self.b, self.x)
115
print error(self.x, self.x_exact), self.tol, luA.nnz
116
self.failUnless(error(self.x, self.x_exact) < self.tol)
118
def testPoisson1dCOLAMD(self):
119
luA = superlu.factorize(self.B, permc_spec=3)
120
luA.solve(self.b, self.x)
121
print error(self.x, self.x_exact), self.tol, luA.nnz
122
self.failUnless(error(self.x, self.x_exact) < self.tol)
124
class Poisson2dTestCase(unittest.TestCase):
127
self.B = poisson.poisson2d(self.n).to_csr()
129
self.b = numpy.zeros(self.n*self.n, 'd')
130
self.x = numpy.zeros(self.n*self.n, 'd')
131
self.x_exact = numpy.ones(self.n*self.n, 'd')
132
self.x_exact /= math.sqrt(self.n*self.n)
133
self.B.matvec(self.x_exact, self.b)
136
lmbd_min = 4.0/h/h * (math.sin(math.pi*h/2.0) ** 2 +
137
math.sin(math.pi*h/2.0) ** 2)
138
lmbd_max = 4.0/h/h * (math.sin((self.n - 1)*math.pi*h/2.0) ** 2 +
139
math.sin((self.n - 1)*math.pi*h/2.0) ** 2)
140
cond = lmbd_max/lmbd_min
141
self.tol = cond * macheps()
143
def testPoisson2dDefault(self):
144
luA = superlu.factorize(self.B)
145
luA.solve(self.b, self.x)
146
print error(self.x, self.x_exact), self.tol, luA.nnz
147
self.failUnless(error(self.x, self.x_exact) < self.tol)
149
def testPoisson2dNoPivot(self):
150
luA = superlu.factorize(self.B, diag_pivot_thresh=0.0)
151
luA.solve(self.b, self.x)
152
print error(self.x, self.x_exact), self.tol, luA.nnz
153
self.failUnless(error(self.x, self.x_exact) < self.tol)
155
def testPoisson2dRelax(self):
156
luA = superlu.factorize(self.B, relax=20)
157
luA.solve(self.b, self.x)
158
print error(self.x, self.x_exact), self.tol, luA.nnz
159
self.failUnless(error(self.x, self.x_exact) < self.tol)
161
def testPoisson2dSmallPanel(self):
162
luA = superlu.factorize(self.B, panel_size=1)
163
luA.solve(self.b, self.x)
164
print error(self.x, self.x_exact), self.tol, luA.nnz
165
self.failUnless(error(self.x, self.x_exact) < self.tol)
167
def testPoisson2dOrigOrdering(self):
168
luA = superlu.factorize(self.B, permc_spec=0)
169
luA.solve(self.b, self.x)
170
print error(self.x, self.x_exact), self.tol, luA.nnz
171
self.failUnless(error(self.x, self.x_exact) < self.tol)
173
def testPoisson2dMMD_AtimesA(self):
174
luA = superlu.factorize(self.B, permc_spec=1)
175
luA.solve(self.b, self.x)
176
print error(self.x, self.x_exact), self.tol, luA.nnz
177
self.failUnless(error(self.x, self.x_exact) < self.tol)
179
def testPoisson2dMMD_AplusA(self):
180
luA = superlu.factorize(self.B, permc_spec=2)
181
luA.solve(self.b, self.x)
182
print error(self.x, self.x_exact), self.tol, luA.nnz
183
self.failUnless(error(self.x, self.x_exact) < self.tol)
185
def testPoisson2dCOLAMD(self):
186
luA = superlu.factorize(self.B, permc_spec=3)
187
luA.solve(self.b, self.x)
188
print error(self.x, self.x_exact), self.tol, luA.nnz
189
self.failUnless(error(self.x, self.x_exact) < self.tol)
191
if __name__ == '__main__':