Author: Taavi Burns <taavi.bu...@gmail.com> Branch: Changeset: r54440:8ae7413e7b32 Date: 2012-04-16 14:33 -0400 http://bitbucket.org/pypy/pypy/changeset/8ae7413e7b32/
Log: Merge in more ufuncs 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 @@ -762,6 +762,8 @@ def test_logaddexp(self): import math + import sys + float_max, float_min = sys.float_info.max, sys.float_info.min from _numpypy import logaddexp # From the numpy documentation @@ -772,7 +774,8 @@ assert logaddexp(0, 0) == math.log(2) assert logaddexp(float('-inf'), 0) == 0 - assert logaddexp(12345678, 12345678) == float('inf') + assert logaddexp(float_max, float_max) == float_max + assert logaddexp(float_min, float_min) == math.log(2) assert math.isnan(logaddexp(float('nan'), 1)) assert math.isnan(logaddexp(1, float('nan'))) @@ -785,6 +788,8 @@ def test_logaddexp2(self): import math + import sys + float_max, float_min = sys.float_info.max, sys.float_info.min from _numpypy import logaddexp2 log2 = math.log(2) @@ -796,7 +801,8 @@ assert logaddexp2(0, 0) == 1 assert logaddexp2(float('-inf'), 0) == 0 - assert logaddexp2(12345678, 12345678) == float('inf') + assert logaddexp2(float_max, float_max) == float_max + assert logaddexp2(float_min, float_min) == 1.0 assert math.isnan(logaddexp2(float('nan'), 1)) assert math.isnan(logaddexp2(1, float('nan'))) 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 @@ -17,6 +17,7 @@ 'render_as_void': True}) degToRad = math.pi / 180.0 log2 = math.log(2) +log2e = 1./log2 def simple_unary_op(func): specialize.argtype(1)(func) @@ -841,45 +842,26 @@ @simple_binary_op def logaddexp(self, v1, v2): - try: - v1e = math.exp(v1) - except OverflowError: - v1e = rfloat.INFINITY - try: - v2e = math.exp(v2) - except OverflowError: - v2e = rfloat.INFINITY + tmp = v1 - v2 + if tmp > 0: + return v1 + rfloat.log1p(math.exp(-tmp)) + elif tmp <= 0: + return v2 + rfloat.log1p(math.exp(tmp)) + else: + return v1 + v2 - v12e = v1e + v2e - try: - return math.log(v12e) - except ValueError: - if v12e == 0.0: - # CPython raises ValueError here, so we have to check - # the value to find the correct numpy return value - return -rfloat.INFINITY - return rfloat.NAN + def npy_log2_1p(self, v): + return log2e * rfloat.log1p(v) @simple_binary_op def logaddexp2(self, v1, v2): - try: - v1e = math.pow(2, v1) - except OverflowError: - v1e = rfloat.INFINITY - try: - v2e = math.pow(2, v2) - except OverflowError: - v2e = rfloat.INFINITY - - v12e = v1e + v2e - try: - return math.log(v12e) / log2 - except ValueError: - if v12e == 0.0: - # CPython raises ValueError here, so we have to check - # the value to find the correct numpy return value - return -rfloat.INFINITY - return rfloat.NAN + tmp = v1 - v2 + if tmp > 0: + return v1 + self.npy_log2_1p(math.pow(2, -tmp)) + if tmp <= 0: + return v2 + self.npy_log2_1p(math.pow(2, tmp)) + else: + return v1 + v2 class NonNativeFloat(NonNativePrimitive, Float): _mixin_ = True _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit