Author: mattip <[email protected]>
Branch: numpypy-complex2
Changeset: r57491:92efd35420dd
Date: 2012-09-23 21:07 +0200
http://bitbucket.org/pypy/pypy/changeset/92efd35420dd/
Log: test, fix expm1
diff --git a/pypy/module/micronumpy/test/test_ufuncs.py
b/pypy/module/micronumpy/test/test_ufuncs.py
--- a/pypy/module/micronumpy/test/test_ufuncs.py
+++ b/pypy/module/micronumpy/test/test_ufuncs.py
@@ -496,11 +496,9 @@
cmpl(nan, 5.), cmpl(5., nan), cmpl(nan, nan),
]
b = exp2(array(a,dtype=c))
- got_err = False
for i in range(len(a)):
try:
res = self.c_pow((2,0), (a[i].real, a[i].imag))
- print a[i],'=>',res
if a[i].imag == 0. and math.copysign(1., a[i].imag)<0:
res = (res[0], -0.)
elif a[i].imag == 0.:
@@ -511,24 +509,22 @@
res = (nan, nan)
msg = 'result of 2**%r(%r) got %r expected %r\n ' % \
(c,a[i], b[i], res)
- try:
- # cast untranslated boxed results to float,
- # does no harm when translated
- t1 = float(res[0])
- t2 = float(b[i].real)
- self.rAlmostEqual(t1, t2, rel_err=rel_err, msg=msg)
- t1 = float(res[1])
- t2 = float(b[i].imag)
- self.rAlmostEqual(t1, t2, rel_err=rel_err, msg=msg)
- except AssertionError as e:
- print e.message
- got_err = True
- if got_err:
- raise AssertionError('Errors were printed to stdout')
+ # cast untranslated boxed results to float,
+ # does no harm when translated
+ t1 = float(res[0])
+ t2 = float(b[i].real)
+ self.rAlmostEqual(t1, t2, rel_err=rel_err, msg=msg)
+ t1 = float(res[1])
+ t2 = float(b[i].imag)
+ self.rAlmostEqual(t1, t2, rel_err=rel_err, msg=msg)
def test_expm1(self):
- import math
- from _numpypy import array, expm1
+ import math, cmath
+ from _numpypy import array, expm1, complex128, complex64
+ inf = float('inf')
+ ninf = -float('inf')
+ nan = float('nan')
+ cmpl = complex
a = array([-5.0, -0.0, 0.0, 12345678.0, float("inf"),
-float('inf'), -12343424.0])
@@ -542,6 +538,47 @@
assert expm1(1e-50) == 1e-50
+ for c,rel_err in ((complex128, 5e-323), (complex64, 1e-7)):
+ a = [cmpl(-5., 0), cmpl(-5., -5.), cmpl(-5., 5.),
+ cmpl(0., -5.), cmpl(0., 0.), cmpl(0., 5.),
+ cmpl(-0., -5.), cmpl(-0., 0.), cmpl(-0., 5.),
+ cmpl(-0., -0.), cmpl(inf, 0.), cmpl(inf, 5.),
+ cmpl(inf, -0.), cmpl(ninf, 0.), cmpl(ninf, 5.),
+ cmpl(ninf, -0.), cmpl(ninf, inf), cmpl(inf, inf),
+ cmpl(ninf, ninf), cmpl(5., inf), cmpl(5., ninf),
+ cmpl(nan, 5.), cmpl(5., nan), cmpl(nan, nan),
+ ]
+ b = expm1(array(a,dtype=c))
+ got_err = False
+ for i in range(len(a)):
+ try:
+ res = cmath.exp(a[i]) - 1.
+ if a[i].imag == 0. and math.copysign(1., a[i].imag)<0:
+ res = cmpl(res.real, -0.)
+ elif a[i].imag == 0.:
+ res = cmpl(res.real, 0.)
+ except OverflowError:
+ res = cmpl(inf, nan)
+ except ValueError:
+ res = cmpl(nan, nan)
+ msg = 'result of expm1(%r(%r)) got %r expected %r\n ' % \
+ (c,a[i], b[i], res)
+ try:
+ # cast untranslated boxed results to float,
+ # does no harm when translated
+ t1 = float(res.real)
+ t2 = float(b[i].real)
+ self.rAlmostEqual(t1, t2, rel_err=rel_err, msg=msg)
+ t1 = float(res.imag)
+ t2 = float(b[i].imag)
+ self.rAlmostEqual(t1, t2, rel_err=rel_err, msg=msg)
+ except AssertionError as e:
+ print e.message
+ got_err = True
+ if got_err:
+ raise AssertionError('Errors were printed to stdout')
+
+
def test_sin(self):
import math
from _numpypy import array, sin
@@ -1201,7 +1238,7 @@
assert False, 'untested: ' + \
'numpy.real. numpy.imag' + \
- 'exp2, expm1, ' + \
+ 'expm1, ' + \
'log2, log1p, ' + \
'logaddexp, npy_log2_1p, logaddexp2'
diff --git a/pypy/module/micronumpy/types.py b/pypy/module/micronumpy/types.py
--- a/pypy/module/micronumpy/types.py
+++ b/pypy/module/micronumpy/types.py
@@ -1268,10 +1268,24 @@
@complex_unary_op
def expm1(self, v):
+ #Duplicate exp() so in the future it will be easier
+ # to implement seterr
+ if math.isinf(v[1]):
+ if math.isinf(v[0]):
+ if v[0]<0:
+ return -1., 0.
+ return rfloat.NAN, rfloat.NAN
+ elif (isfinite(v[0]) or \
+ (math.isinf(v[0]) and v[0] > 0)):
+ return rfloat.NAN, rfloat.NAN
try:
- return rfloat.expm1(v)
+ res = rcomplex.c_exp(*v)
+ res = (res[0]-1, res[1])
+ return res
except OverflowError:
- return rfloat.INFINITY
+ if v[1]==0:
+ return rfloat.INFINITY, 0.0
+ return rfloat.INFINITY, rfloat.NAN
@complex_unary_op
def sin(self, v):
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit