#!/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)
|