next up previous contents
Next: Tests of matrix functions Up: Getting started and examples Previous: Tests of gplot module   Contents

Tests of elementwise functions

The file test_efuncs.py tests the elementwise functions. Results are at 3.11


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

from MatPy.Matrix import r_range, rand, norm
from MatPy.efuncs import *
import sys

if __name__ == "__main__":	waittime = None
else:						waittime = 1

# 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

A = rand((3,3))
checkequal(sqrt(A).__tpow__(2),  A, 1e-14)

checkequal(exp(A)+exp(-A),  2 * cosh(A), 1e-14)
checkequal(exp(A)-exp(-A),  2 * sinh(A), 1e-14)
checkequal(sinh(A), cosh(A).__tmul__(tanh(A)), 1e-14)
checkequal(cosh(A).__tpow__(2), 1 + sinh(A).__tpow__(2), 2e-14)

checkequal(1j*sin(A),  sinh(A*1j), 1e-14)
checkequal(cos(A),  cosh(A*1j), 1e-14)
checkequal(sin(A),  cos(A).__tmul__(tan(A)), 1e-14)
checkequal(cos(A).__tpow__(2), 1 -  sin(A).__tpow__(2), 2e-14)

checkequal(cos(acos(A)), A, 1e-14)
checkequal(sin(asin(A)), A, 1e-14)
checkequal(tan(atan(A)), A, 1e-14)


# Plot graphics
i = 0
def test(g, __name__, funcs, x):
	"For each f in funcs, draw a line with f(args)"
	global i
	ys = map(lambda f,x=x:f(x), funcs)
	names = map(lambda f:f.__name__, funcs)

	if not len(funcs) == len(names): raise ValueError

	g.plot(ys, names=names, xs=[x]*len(funcs))
	g.hardcopy("efunc_%s.ps" %i)
	i = i + 1
	wait(waittime)
	
#------------------------------------------------------------------
print "All of these tests are shown in graphics"

from MatPy.gplot import Gplot, wait
g = Gplot()
x = r_range(100)/10.

#--------------------------------------------
g.title("Positive range functions")
test(g, __name__,
	 (log, log10),
	 (x+.1)/3)

#--------------------------------------------
g.title("Miscellenious functions")
test(g, __name__,
	 (abs, ceil, floor),
	 x-5)
"sign does not work?"

#--------------------------------------------
g.title("Trigonometric functions")
g.axis(yrange=(-3,3))
test(g, __name__,
	 (sin, cos, tan, atan),
	 (x-5)*2)
g.axis()

#--------------------------------------------
g.title("Exponential and related functions")
test(g, __name__,
	 (exp, sinh, cosh, tanh, asinh),
	 (x-5)/3)

#--------------------------------------------
g.title("Gamma and related special functions")
test(g, __name__,
	 (lgam, psi, rgam),
	 x+.1)

"""
abs,
ceil,
floor,
sign,
sqrt,
sinc,
exp,
log,
log10,
sinh,
cosh,
tanh,
asinh,
sin,
cos,
tan,
asin,
acos,
atan,
gam,
lgam,
igam,
igamc(x, a):
igami(y, a):
beta(a, b):
lbeta(a, b):
ibeta(x, a, b):
ibetai(y, a, b):
psi,
rgam,
"""


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


All of these tests are shown in graphics



Huaiyu Zhu
2002-03-23