19
19
python tests/test_basic.py [<level>]
23
from Numeric import arange, add, array, dot, zeros, identity
23
from numpy import arange, add, array, dot, zeros, identity, conjugate, transpose
26
from scipy_test.testing import *
26
from numpy.testing import *
28
28
from linalg import solve,inv,det,lstsq, toeplitz, hankel, tri, triu, tril
29
29
from linalg import pinv, pinv2, solve_banded
52
52
for b in ([[1,0,0,0],[0,0,0,1],[0,1,0,0],[0,1,0,0]],
53
53
[[2,1],[-30,4],[2,3],[1,3]]):
54
54
x = solve_banded((l,u),ab,b)
55
assert_array_almost_equal(Numeric.matrixmultiply(a,x),b)
55
assert_array_almost_equal(numpy.dot(a,x),b)
57
57
class test_solve(ScipyTestCase):
59
59
def check_20Feb04_bug(self):
60
60
a = [[1,1],[1.0,0]] # ok
61
61
x0 = solve(a,[1,0j])
62
assert_array_almost_equal(Numeric.matrixmultiply(a,x0),[1,0])
62
assert_array_almost_equal(numpy.dot(a,x0),[1,0])
64
64
a = [[1,1],[1.2,0]] # gives failure with clapack.zgesv(..,rowmajor=0)
67
assert_array_almost_equal(Numeric.matrixmultiply(a,x0),[1,0])
67
assert_array_almost_equal(numpy.dot(a,x0),[1,0])
69
69
def check_simple(self):
70
70
a = [[1,20],[-30,4]]
71
71
for b in ([[1,0],[0,1]],[1,0],
74
assert_array_almost_equal(Numeric.matrixmultiply(a,x),b)
74
assert_array_almost_equal(numpy.dot(a,x),b)
76
76
def check_simple_sym(self):
78
78
for lower in [0,1]:
79
79
for b in ([[1,0],[0,1]],[1,0]):
80
80
x = solve(a,b,sym_pos=1,lower=lower)
81
assert_array_almost_equal(Numeric.matrixmultiply(a,x),b)
81
assert_array_almost_equal(numpy.dot(a,x),b)
83
83
def check_simple_sym_complex(self):
89
89
x = solve(a,b,sym_pos=1)
90
assert_array_almost_equal(Numeric.matrixmultiply(a,x),b)
90
assert_array_almost_equal(numpy.dot(a,x),b)
92
92
def check_simple_complex(self):
93
93
a = array([[5,2],[2j,4]],'D')
101
assert_array_almost_equal(Numeric.matrixmultiply(a,x),b)
101
assert_array_almost_equal(numpy.dot(a,x),b)
103
103
def check_nils_20Feb04(self):
105
105
A = random([n,n])+random([n,n])*1j
106
106
X = zeros((n,n),'D')
108
108
R = identity(n)+identity(n)*0j
109
109
for i in arange(0,n):
149
149
for i in range(n):
150
150
a[i,i] = abs(20*(.1+a[i,i]))
151
151
for j in range(i):
152
a[i,j] = Numeric.conjugate(a[j,i])
152
a[i,j] = numpy.conjugate(a[j,i])
153
153
b = random([n])+2j*random([n])
154
154
for i in range(2):
155
155
x = solve(a,b,sym_pos=1)
156
assert_array_almost_equal(Numeric.matrixmultiply(a,x),b)
156
assert_array_almost_equal(numpy.dot(a,x),b)
158
158
def bench_random(self,level=5):
160
Numeric_solve = LinearAlgebra.solve_linear_equations
159
import numpy.linalg as linalg
160
basic_solve = linalg.solve
162
162
print ' Solving system of linear equations'
163
163
print ' =================================='
165
165
print ' | contiguous | non-contiguous '
166
166
print '----------------------------------------------'
167
print ' size | scipy | Numeric | scipy | Numeric'
167
print ' size | scipy | basic | scipy | basic '
169
169
for size,repeat in [(20,1000),(100,150),(500,2),(1000,1)][:-1]:
171
171
print '%5s' % size,
172
172
sys.stdout.flush()
174
174
a = random([size,size])
175
175
# larger diagonal ensures non-singularity:
176
176
for i in range(size): a[i,i] = 10*(.1+a[i,i])
179
179
print '| %6.2f ' % self.measure('solve(a,b)',repeat),
180
180
sys.stdout.flush()
182
print '| %6.2f ' % self.measure('Numeric_solve(a,b)',repeat),
182
print '| %6.2f ' % self.measure('basic_solve(a,b)',repeat),
183
183
sys.stdout.flush()
185
185
a = a[-1::-1,-1::-1] # turn into a non-contiguous array
186
assert not a.iscontiguous()
186
assert not a.flags['CONTIGUOUS']
188
188
print '| %6.2f ' % self.measure('solve(a,b)',repeat),
189
189
sys.stdout.flush()
191
print '| %6.2f ' % self.measure('Numeric_solve(a,b)',repeat),
191
print '| %6.2f ' % self.measure('basic_solve(a,b)',repeat),
192
192
sys.stdout.flush()
194
194
print ' (secs for %s calls)' % (repeat)
198
198
def check_simple(self):
199
199
a = [[1,2],[3,4]]
201
assert_array_almost_equal(Numeric.matrixmultiply(a,a_inv),
201
assert_array_almost_equal(numpy.dot(a,a_inv),
203
203
a = [[1,2,3],[4,5,6],[7,8,10]]
205
assert_array_almost_equal(Numeric.matrixmultiply(a,a_inv),
205
assert_array_almost_equal(numpy.dot(a,a_inv),
206
206
[[1,0,0],[0,1,0],[0,0,1]])
208
208
def check_random(self):
211
211
a = random([n,n])
212
212
for i in range(n): a[i,i] = 20*(.1+a[i,i])
214
assert_array_almost_equal(Numeric.matrixmultiply(a,a_inv),
214
assert_array_almost_equal(numpy.dot(a,a_inv),
216
216
def check_simple_complex(self):
217
217
a = [[1,2],[3,4j]]
219
assert_array_almost_equal(Numeric.matrixmultiply(a,a_inv),
219
assert_array_almost_equal(numpy.dot(a,a_inv),
222
222
def check_random_complex(self):
225
225
a = random([n,n])+2j*random([n,n])
226
226
for i in range(n): a[i,i] = 20*(.1+a[i,i])
228
assert_array_almost_equal(Numeric.matrixmultiply(a,a_inv),
228
assert_array_almost_equal(numpy.dot(a,a_inv),
231
231
def bench_random(self,level=5):
233
Numeric_inv = LinearAlgebra.inverse
232
import numpy.linalg as linalg
233
basic_inv = linalg.inv
235
235
print ' Finding matrix inverse'
236
236
print ' =================================='
237
237
print ' | contiguous | non-contiguous '
238
238
print '----------------------------------------------'
239
print ' size | scipy | Numeric | scipy | Numeric'
239
print ' size | scipy | basic | scipy | basic'
241
241
for size,repeat in [(20,1000),(100,150),(500,2),(1000,1)][:-1]:
243
243
print '%5s' % size,
244
244
sys.stdout.flush()
246
246
a = random([size,size])
247
247
# large diagonal ensures non-singularity:
248
248
for i in range(size): a[i,i] = 10*(.1+a[i,i])
250
250
print '| %6.2f ' % self.measure('inv(a)',repeat),
251
251
sys.stdout.flush()
253
print '| %6.2f ' % self.measure('Numeric_inv(a)',repeat),
253
print '| %6.2f ' % self.measure('basic_inv(a)',repeat),
254
254
sys.stdout.flush()
256
256
a = a[-1::-1,-1::-1] # turn into a non-contiguous array
257
assert not a.iscontiguous()
257
assert not a.flags['CONTIGUOUS']
259
259
print '| %6.2f ' % self.measure('inv(a)',repeat),
260
260
sys.stdout.flush()
262
print '| %6.2f ' % self.measure('Numeric_inv(a)',repeat),
262
print '| %6.2f ' % self.measure('basic_inv(a)',repeat),
263
263
sys.stdout.flush()
265
265
print ' (secs for %s calls)' % (repeat)
278
278
assert_almost_equal(a_det,-6+4j)
280
280
def check_random(self):
282
Numeric_det = LinearAlgebra.determinant
281
import numpy.linalg as linalg
282
basic_det = linalg.det
284
284
for i in range(4):
285
285
a = random([n,n])
288
288
assert_almost_equal(d1,d2)
290
290
def check_random_complex(self):
292
Numeric_det = LinearAlgebra.determinant
291
import numpy.linalg as linalg
292
basic_det = linalg.det
294
294
for i in range(4):
295
295
a = random([n,n]) + 2j*random([n,n])
298
298
assert_almost_equal(d1,d2)
300
300
def bench_random(self,level=5):
302
Numeric_det = LinearAlgebra.determinant
301
import numpy.linalg as linalg
302
basic_det = linalg.det
304
304
print ' Finding matrix determinant'
305
305
print ' =================================='
306
306
print ' | contiguous | non-contiguous '
307
307
print '----------------------------------------------'
308
print ' size | scipy | Numeric | scipy | Numeric'
308
print ' size | scipy | basic | scipy | basic '
310
310
for size,repeat in [(20,1000),(100,150),(500,2),(1000,1)][:-1]:
312
312
print '%5s' % size,
313
313
sys.stdout.flush()
315
315
a = random([size,size])
317
317
print '| %6.2f ' % self.measure('det(a)',repeat),
318
318
sys.stdout.flush()
320
print '| %6.2f ' % self.measure('Numeric_det(a)',repeat),
320
print '| %6.2f ' % self.measure('basic_det(a)',repeat),
321
321
sys.stdout.flush()
323
323
a = a[-1::-1,-1::-1] # turn into a non-contiguous array
324
assert not a.iscontiguous()
324
assert not a.flags['CONTIGUOUS']
326
326
print '| %6.2f ' % self.measure('det(a)',repeat),
327
327
sys.stdout.flush()
329
print '| %6.2f ' % self.measure('Numeric_det(a)',repeat),
329
print '| %6.2f ' % self.measure('basic_det(a)',repeat),
330
330
sys.stdout.flush()
332
332
print ' (secs for %s calls)' % (repeat)
335
def direct_lstsq(a,b):
336
a1 = Numeric.matrixmultiply(Numeric.transpose(a),a)
337
b1 = Numeric.matrixmultiply(Numeric.transpose(a),b)
335
def direct_lstsq(a,b,cmplx=0):
340
343
class test_lstsq(ScipyTestCase):
342
344
def check_random_overdet_large(self):
343
345
#bug report: Nils Wagner
353
355
for b in ([[1,0],[0,1]],[1,0],
354
356
[[2,1],[-30,4]]):
355
357
x = lstsq(a,b)[0]
356
assert_array_almost_equal(Numeric.matrixmultiply(a,x),b)
358
assert_array_almost_equal(numpy.dot(a,x),b)
358
360
def check_simple_overdet(self):
359
361
a = [[1,2],[4,5],[3,4]]
368
370
x,res,r,s = lstsq(a,b)
369
371
#XXX: need independent check
370
assert_array_almost_equal(x,[[-0.05555556],[0.11111111],[0.27777778]])
372
assert_array_almost_equal(x,[[-0.05555556],
373
[0.11111111],[0.27777778]])
372
375
def check_random_exact(self):
406
409
a = random([n,m]) + 1j * random([n,m])
407
for i in range(m): a[i,i] = 20*(.1+a[i,i])
411
a[i,i] = 20*(.1+a[i,i])
408
412
for i in range(2):
409
413
b = random([n,3])
410
414
x,res,r,s = lstsq(a,b)
411
415
assert r==m,'unexpected efficient rank'
412
416
#XXX: check definition of res
413
assert_array_almost_equal(x,direct_lstsq(a,b),1e-3)
414
#XXX: tolerance 1e-3 is quite large, investigate the reason
417
assert_array_almost_equal(x,direct_lstsq(a,b,1))
416
419
class test_tri(unittest.TestCase):
417
420
def check_basic(self):
422
assert_equal(tri(4,typecode='f'),array([[1,0,0,0],
425
assert_equal(tri(4,dtype='f'),array([[1,0,0,0],
426
429
def check_diag(self):
427
430
assert_equal(tri(4,k=1),array([[1,1,0,0],
431
434
assert_equal(tri(4,k=-1),array([[0,0,0,0],
440
443
assert_equal(tri(3,4),array([[1,0,0,0],
443
446
def check_diag2d(self):
444
447
assert_equal(tri(3,4,k=2),array([[1,1,1,0],
499
502
assert_array_equal(y,[[1,2,3],[2,1,2],[3,2,1]])
500
503
y = toeplitz([1,2,3],[1,4,5])
501
504
assert_array_equal(y,[[1,4,5],[2,1,4],[3,2,1]])
503
506
class test_hankel(unittest.TestCase):
504
507
def check_basic(self):
505
508
y = hankel([1,2,3])