Author: Taavi Burns <[email protected]>
Branch: numpy-ufuncs3
Changeset: r54299:c644bd3dfefe
Date: 2012-04-12 01:27 -0400
http://bitbucket.org/pypy/pypy/changeset/c644bd3dfefe/
Log: Replaced logaddexp(2) functions with the numpy implementations.
Tests pass better now!
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
@@ -764,6 +764,7 @@
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
@@ -774,8 +775,8 @@
assert logaddexp(0, 0) == math.log(2)
assert logaddexp(float('-inf'), 0) == 0
- assert logaddexp(sys.float_info.max, sys.float_info.max) ==
sys.float_info.max
- assert logaddexp(sys.float_info.min, sys.float_info.min) == math.log(2)
+ 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')))
@@ -789,6 +790,7 @@
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)
@@ -800,8 +802,8 @@
assert logaddexp2(0, 0) == 1
assert logaddexp2(float('-inf'), 0) == 0
- assert logaddexp2(sys.float_info.max, sys.float_info.max) ==
sys.float_info.max
- assert logaddexp2(sys.float_info.min, sys.float_info.min) == 1.0
+ 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
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit