next up previous contents
Next: Tests of tensor operations Up: Getting started and examples Previous: Tests of elementwise functions   Contents

Tests of matrix functions

The file test_mfuncs.py tests the matrix functions. Results are at 3.12


#!/usr/bin/env python
# (C) 2000 Huaiyu Zhu <hzhu@users.sourceforge.net>.	 Licence: GPL
# $Id: test_mfuncs.py,v 1.4 2001/08/29 09:17:28 hzhu Exp $
"""
Test mfuncs module 
"""

from MatPy.Matrix import rand, norm, eye
from MatPy.mfuncs import *
import sys

# Print out values
A = rand((3,3))
print A

for f in [sqrtm, expm, logm, sinhm, coshm, tanhm, sinm,
		  cosm, tanm, asinm, acosm, atanm]:
	print f(A)

# Check consistency
def checkequal(x, y, r):
	try:
		assert norm(x-y) <= (norm(x)+norm(y)) *   r
	except:
		print sys.exc_info()
		print "x =", x
		print "y =", y
		raise

checkequal(sqrtm(A) ** 2, A, 1e-14)

checkequal(expm(A)+expm(-A), 2 * coshm(A), 1e-14)
checkequal(expm(A)-expm(-A), 2 * sinhm(A), 1e-14)
checkequal(sinhm(A), coshm(A)*tanhm(A), 1e-14)
#print "-"*40
#print coshm(A)**2
#print sinhm(A)**2
#print norm(coshm(A)**2 - sinhm(A)**2 - eye(3))
#print "-"*40
checkequal(coshm(A)**2 - sinhm(A)**2, eye(3), 2e-14)

checkequal(1j*sinm(A), sinhm(A*1j), 1e-14)
checkequal(cosm(A), coshm(A*1j), 1e-14)
checkequal(sinm(A), cosm(A)*tanm(A), 4e-14)
checkequal(cosm(A)**2 + sinm(A)**2, eye(3), 2e-14)

checkequal(cosm(acosm(A)), A, 1e-14)
checkequal(sinm(asinm(A)), A, 1e-14)
checkequal(tanm(atanm(A)), A, 1e-14)


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


[ 0.828  0.139  0.5  
  0.6    0.367  0.73 
  0.12   0.85   0.14  ]
[ 0.875-0.0572j  0.0442+0.149j  0.308-0.135j
  0.36-0.118j  0.538+0.307j  0.467-0.278j
  0.0271+0.18j  0.576-0.469j  0.386+0.424j ]
[ 2.52   0.644  1.02 
  1.31   2.04   1.34 
  0.654  1.28   1.68  ]
[-0.518-0.267j  0.459+0.696j  0.387-0.63j
  0.548-0.549j -0.424+1.43j  0.413-1.3j
  0.492+0.836j  0.207-2.18j -0.494+1.98j ]
[ 1.03   0.293  0.671
  0.831  0.55   0.934
  0.282  1      0.277 ]
[ 1.48   0.351  0.351
  0.481  1.49   0.402
  0.372  0.271  1.4   ]
[ 0.617 -0.00855  0.327
  0.369  0.188  0.52 
 -0.0342  0.682  0.0104 ]
[ 0.66   0.0146  0.36 
  0.412  0.218  0.563
 -0.0104  0.721  0.0299 ]
[ 0.645 -0.247 -0.243
 -0.334  0.638 -0.277
 -0.262 -0.183  0.695 ]
[ 2.82   1.74   2.19 
  2.9    2.24   2.72 
  1.81   2.28   1.58  ]
[ 0.891-0.33j  0.175-0.271j  0.549-0.283j
  0.665-0.385j  0.413-0.316j  0.792-0.33j
  0.157-0.286j  0.904-0.234j  0.171-0.245j ]
[ 0.68+0.33j -0.175+0.271j -0.549+0.283j
 -0.665+0.385j  1.16+0.316j -0.792+0.33j
 -0.157+0.286j -0.904+0.234j  1.4+0.245j ]
[ 0.643  0.0117  0.349
  0.398  0.212  0.546
 -0.0128  0.701  0.0286 ]



Huaiyu Zhu
2002-03-23