next up previous contents
Next: Tests of linear algebra Up: Getting started and examples Previous: Getting started   Contents

Tests of basic operations

The file test_basic.py tests most of the basic operations. Results are at 3.2


#!/usr/bin/env python
# (C) 2000 Huaiyu Zhu <hzhu@users.sourceforge.net>.  Licence: GPL
# $Id: test_basic.py,v 1.6 2000/09/13 01:52:36 hzhu Exp $
"""
Test basic operations for Matrix module
"""

from MatPy.Matrix import Matrix, r_range, zeros, ones, eye, rand, norm, mnorm
from MatPy.Matrix import max, min, cumsum, sum, mfunc, solve
from MatPy.mfuncs import expm, logm, sqrtm

import  time
def tic(): global _time; _time = time.time()
def toc(): print "time lapse =",  time.time() - _time

print "-"*40, "basic vector"
a = r_range(-1, 3)
print a, a.__class__
a[2] = 9
assert a.shape == (1,4)
assert a[0,2] == a[2] == 9

print "-"*40, "basic matrix"
b = zeros((2,2))
b [1,0] = 1; b[0,1] = 2
print b
print b.shape,  b[0,1], b[1,0]
print 1 + b + 1
print 2 - b - 2
print 3 * b * 3

print "-"*40, "inverse matrix"
print b.I
print 4 / b
print 4 / b * b

assert norm(b.I * b - eye(2)) < 1e-15
assert norm(b.I - 1/b) < 1e-15

print "-"*40, "arithmatic, norm"
print 1.1 * Matrix(a)*2 + 2 + a

print "-"*40, "special matrices"
c = ones(a.shape) * 2 + zeros(a.shape) + rand(a.shape)
print c

print "-"*40, "transpose, multiplication"
d = -a.T
assert d.shape == (4,1)
assert norm(d.T + a) < 1e-15

print d*c, c*d

print "-"*40, "linear transform"
X = rand((9,4))
y = X * d
print y.T

print "-"*40, "eigenvalues"
C = rand((5,5)) 
print C
print C.eig

print "-"*40, "matrix functions:"
print "-"*40, "identity function"
print C
D = mfunc(lambda x:x, C)
print D
assert  norm(D - C) < 1e-14

print "-"*40, "sqrtm, powm"
A = X.T * X
B = sqrtm(A)
print B
assert norm(B**2 - A) < 4e-14
assert norm(B*B - A) < 2e-14

print "-"*40, "expm, logm"
print expm(C)
assert logm(expm(C)), norm(logm(expm(C)) - C) < 1e-15
		   
print "-"*40, "inverse, eye"
print A.I
assert norm(A.I * A - eye(4)) < 1e-14
print norm(A.I * A - eye(4))

print "-"*40, "solve by inverse"
tic()
A = X.T * X
b = X.T * y
d1 = (A.I * b)
toc()
e = d1 - d
assert norm(e) < 1e-13
assert mnorm(e) < 1e-13
	

print "-"*40, "linear equation"
tic()
d2 = solve(X , y)
toc()
assert norm(d2 - d) < 8e-14
assert norm(d2 - d1) < 8e-14
assert norm(X*d2 - y) < 2e-14


Result obtained with
>>> from MatPy.tests import test_basic


---------------------------------------- basic vector
[-1   0   1   2  ] MatPy.Matrix.Matrix
---------------------------------------- basic matrix
[ 0      2    
  1      0     ]
(2, 2) 2.0 1.0
[ 2      4    
  3      2     ]
[ 0     -2    
 -1      0     ]
[ 0      18   
  9      0     ]
---------------------------------------- inverse matrix
[ 0      1    
  0.5    0     ]
[ 0      4    
  2      0     ]
[ 4      0    
  0      4     ]
---------------------------------------- arithmatic, norm
[-1.2    2      30.8   8.4   ]
---------------------------------------- special matrices
[ 2.83   2.44   2.72   2.73  ]
---------------------------------------- transpose, multiplication
[ 2.83   2.44   2.72   2.73 
  0      0      0      0    
 -25.5  -22    -24.5  -24.5 
 -5.66  -4.88  -5.44  -5.45  ] [-27.1  ]
---------------------------------------- linear transform
[-4.71  -4.01  -5.28  -5.73  -2     -10.1  -2.5   -1.81  -7.76  ]
---------------------------------------- eigenvalues
[ 0.664  0.0771  0.0881  0.755  0.658
  0.301  0.0204  0.408  0.908  0.138
  0.0539  0.641  0.911  0.895  0.908
  0.7    0.223  0.7    0.384  0.597
  0.907  0.807  0.859  0.43   0.869 ]
[ 2.89+0j -0.591+0j  0.715+0j -0.0847+0.38j -0.0847-0.38j ]
---------------------------------------- matrix functions:
---------------------------------------- identity function
[ 0.664  0.0771  0.0881  0.755  0.658
  0.301  0.0204  0.408  0.908  0.138
  0.0539  0.641  0.911  0.895  0.908
  0.7    0.223  0.7    0.384  0.597
  0.907  0.807  0.859  0.43   0.869 ]
[ 0.664  0.0771  0.0881  0.755  0.658
  0.301  0.0204  0.408  0.908  0.138
  0.0539  0.641  0.911  0.895  0.908
  0.7    0.223  0.7    0.384  0.597
  0.907  0.807  0.859  0.43   0.869 ]
---------------------------------------- sqrtm, powm
[ 1.91   0.623  0.616  0.641
  0.623  1.02   0.297  0.263
  0.616  0.297  1.37   0.677
  0.641  0.263  0.677  1.51  ]
---------------------------------------- expm, logm
[ 3.93   1.51   2.33   3.08   3.22 
  1.93   2.15   2.29   2.76   2.17 
  3.17   3.08   5.83   4.73   4.89 
  3.13   2.01   3.47   4.3    3.67 
  4.36   3.28   4.79   4.67   6.14  ]
---------------------------------------- inverse, eye
[ 0.774 -0.718 -0.202 -0.233
 -0.718  1.69  -0.144  0.0645
 -0.202 -0.144  1.17  -0.653
 -0.233  0.0645 -0.653  0.977 ]
1.01899608191e-15
---------------------------------------- solve by inverse
time lapse = 0.0102770328522
---------------------------------------- linear equation
time lapse = 0.00766706466675



Huaiyu Zhu
2002-03-23