Author: Maciej Fijalkowski <fij...@gmail.com> Branch: Changeset: r53363:d5856ea5175f Date: 2012-03-12 14:52 -0700 http://bitbucket.org/pypy/pypy/changeset/d5856ea5175f/
Log: merge diff --git a/pypy/module/micronumpy/__init__.py b/pypy/module/micronumpy/__init__.py --- a/pypy/module/micronumpy/__init__.py +++ b/pypy/module/micronumpy/__init__.py @@ -107,6 +107,10 @@ ('logical_xor', 'logical_xor'), ('logical_not', 'logical_not'), ('logical_or', 'logical_or'), + ('log', 'log'), + ('log2', 'log2'), + ('log10', 'log10'), + ('log1p', 'log1p'), ]: interpleveldefs[exposed] = "interp_ufuncs.get(space).%s" % impl diff --git a/pypy/module/micronumpy/interp_boxes.py b/pypy/module/micronumpy/interp_boxes.py --- a/pypy/module/micronumpy/interp_boxes.py +++ b/pypy/module/micronumpy/interp_boxes.py @@ -80,6 +80,7 @@ descr_mul = _binop_impl("multiply") descr_div = _binop_impl("divide") descr_truediv = _binop_impl("true_divide") + descr_floordiv = _binop_impl("floor_divide") descr_mod = _binop_impl("mod") descr_pow = _binop_impl("power") descr_lshift = _binop_impl("left_shift") @@ -100,6 +101,7 @@ descr_rmul = _binop_right_impl("multiply") descr_rdiv = _binop_right_impl("divide") descr_rtruediv = _binop_right_impl("true_divide") + descr_rfloordiv = _binop_right_impl("floor_divide") descr_rmod = _binop_right_impl("mod") descr_rpow = _binop_right_impl("power") descr_rlshift = _binop_right_impl("left_shift") @@ -208,6 +210,7 @@ __mul__ = interp2app(W_GenericBox.descr_mul), __div__ = interp2app(W_GenericBox.descr_div), __truediv__ = interp2app(W_GenericBox.descr_truediv), + __floordiv__ = interp2app(W_GenericBox.descr_floordiv), __mod__ = interp2app(W_GenericBox.descr_mod), __divmod__ = interp2app(W_GenericBox.descr_divmod), __pow__ = interp2app(W_GenericBox.descr_pow), @@ -222,6 +225,7 @@ __rmul__ = interp2app(W_GenericBox.descr_rmul), __rdiv__ = interp2app(W_GenericBox.descr_rdiv), __rtruediv__ = interp2app(W_GenericBox.descr_rtruediv), + __rfloordiv__ = interp2app(W_GenericBox.descr_rfloordiv), __rmod__ = interp2app(W_GenericBox.descr_rmod), __rdivmod__ = interp2app(W_GenericBox.descr_rdivmod), __rpow__ = interp2app(W_GenericBox.descr_rpow), diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py --- a/pypy/module/micronumpy/interp_numarray.py +++ b/pypy/module/micronumpy/interp_numarray.py @@ -102,6 +102,7 @@ descr_mul = _binop_impl("multiply") descr_div = _binop_impl("divide") descr_truediv = _binop_impl("true_divide") + descr_floordiv = _binop_impl("floor_divide") descr_mod = _binop_impl("mod") descr_pow = _binop_impl("power") descr_lshift = _binop_impl("left_shift") @@ -136,6 +137,7 @@ descr_rmul = _binop_right_impl("multiply") descr_rdiv = _binop_right_impl("divide") descr_rtruediv = _binop_right_impl("true_divide") + descr_rfloordiv = _binop_right_impl("floor_divide") descr_rmod = _binop_right_impl("mod") descr_rpow = _binop_right_impl("power") descr_rlshift = _binop_right_impl("left_shift") @@ -1250,6 +1252,7 @@ __mul__ = interp2app(BaseArray.descr_mul), __div__ = interp2app(BaseArray.descr_div), __truediv__ = interp2app(BaseArray.descr_truediv), + __floordiv__ = interp2app(BaseArray.descr_floordiv), __mod__ = interp2app(BaseArray.descr_mod), __divmod__ = interp2app(BaseArray.descr_divmod), __pow__ = interp2app(BaseArray.descr_pow), @@ -1264,6 +1267,7 @@ __rmul__ = interp2app(BaseArray.descr_rmul), __rdiv__ = interp2app(BaseArray.descr_rdiv), __rtruediv__ = interp2app(BaseArray.descr_rtruediv), + __rfloordiv__ = interp2app(BaseArray.descr_rfloordiv), __rmod__ = interp2app(BaseArray.descr_rmod), __rdivmod__ = interp2app(BaseArray.descr_rdivmod), __rpow__ = interp2app(BaseArray.descr_rpow), diff --git a/pypy/module/micronumpy/interp_ufuncs.py b/pypy/module/micronumpy/interp_ufuncs.py --- a/pypy/module/micronumpy/interp_ufuncs.py +++ b/pypy/module/micronumpy/interp_ufuncs.py @@ -388,6 +388,7 @@ "int_only": True}), ("bitwise_xor", "bitwise_xor", 2, {"int_only": True}), ("invert", "invert", 1, {"int_only": True}), + ("floor_divide", "floordiv", 2, {"promote_bools": True}), ("divide", "div", 2, {"promote_bools": True}), ("true_divide", "div", 2, {"promote_to_float": True}), ("mod", "mod", 2, {"promote_bools": True}), @@ -441,6 +442,11 @@ ("arcsinh", "arcsinh", 1, {"promote_to_float": True}), ("arccosh", "arccosh", 1, {"promote_to_float": True}), ("arctanh", "arctanh", 1, {"promote_to_float": True}), + + ("log", "log", 1, {"promote_to_float": True}), + ("log2", "log2", 1, {"promote_to_float": True}), + ("log10", "log10", 1, {"promote_to_float": True}), + ("log1p", "log1p", 1, {"promote_to_float": True}), ]: self.add_ufunc(space, *ufunc_def) diff --git a/pypy/module/micronumpy/test/test_numarray.py b/pypy/module/micronumpy/test/test_numarray.py --- a/pypy/module/micronumpy/test/test_numarray.py +++ b/pypy/module/micronumpy/test/test_numarray.py @@ -625,6 +625,56 @@ for i in range(5): assert b[i] == i / 5.0 + def test_floordiv(self): + from math import isnan + from _numpypy import array, dtype + + a = array(range(1, 6)) + b = a // a + assert (b == [1, 1, 1, 1, 1]).all() + + a = array(range(1, 6), dtype=bool) + b = a // a + assert b.dtype is dtype("int8") + assert (b == [1, 1, 1, 1, 1]).all() + + a = array([-1, 0, 1]) + b = array([0, 0, 0]) + c = a // b + assert (c == [0, 0, 0]).all() + + a = array([-1.0, 0.0, 1.0]) + b = array([0.0, 0.0, 0.0]) + c = a // b + assert c[0] == float('-inf') + assert isnan(c[1]) + assert c[2] == float('inf') + + b = array([-0.0, -0.0, -0.0]) + c = a // b + assert c[0] == float('inf') + assert isnan(c[1]) + assert c[2] == float('-inf') + + def test_floordiv_other(self): + from _numpypy import array + a = array(range(5)) + b = array([2, 2, 2, 2, 2], float) + c = a // b + assert (c == [0, 0, 1, 1, 2]).all() + + def test_rfloordiv(self): + from _numpypy import array + a = array(range(1, 6)) + b = 3 // a + assert (b == [3, 1, 1, 0, 0]).all() + + def test_floordiv_constant(self): + from _numpypy import array + a = array(range(5)) + b = a // 2 + assert (b == [0, 0, 1, 1, 2]).all() + def test_truediv(self): from operator import truediv from _numpypy import arange 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 @@ -481,3 +481,26 @@ assert (logical_xor([True, False, True, False], [1, 2, 0, 0]) == [False, True, True, False]).all() assert (logical_not([True, False]) == [False, True]).all() + + def test_logn(self): + import math + from _numpypy import log, log2, log10 + + for log_func, base in [(log, math.e), (log2, 2), (log10, 10)]: + for v in [float('-nan'), float('-inf'), -1, float('nan')]: + assert math.isnan(log_func(v)) + for v in [-0.0, 0.0]: + assert log_func(v) == float("-inf") + assert log_func(float('inf')) == float('inf') + assert (log_func([1, base]) == [0, 1]).all() + + def test_log1p(self): + import math + from _numpypy import log1p + + for v in [float('-nan'), float('-inf'), -2, float('nan')]: + assert math.isnan(log1p(v)) + for v in [-1]: + assert log1p(v) == float("-inf") + assert log1p(float('inf')) == float('inf') + assert (log1p([0, 1e-50, math.e - 1]) == [0, 1e-50, 1]).all() 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 @@ -280,6 +280,12 @@ return v1 / v2 @simple_binary_op + def floordiv(self, v1, v2): + if v2 == 0: + return 0 + return v1 // v2 + + @simple_binary_op def mod(self, v1, v2): return v1 % v2 @@ -418,6 +424,15 @@ return rfloat.copysign(rfloat.INFINITY, v1 * v2) @simple_binary_op + def floordiv(self, v1, v2): + try: + return v1 // v2 + except ZeroDivisionError: + if v1 == v2 == 0.0: + return rfloat.NAN + return rfloat.copysign(rfloat.INFINITY, v1 * v2) + + @simple_binary_op def mod(self, v1, v2): return math.fmod(v1, v2) @@ -533,6 +548,48 @@ def isinf(self, v): return rfloat.isinf(v) + @simple_unary_op + def log(self, v): + try: + return math.log(v) + except ValueError: + if v == 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 + + @simple_unary_op + def log2(self, v): + try: + return math.log(v, 2) + except ValueError: + if v == 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 + + @simple_unary_op + def log10(self, v): + try: + return math.log10(v) + except ValueError: + if v == 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 + + @simple_unary_op + def log1p(self, v): + try: + return rfloat.log1p(v) + except OverflowError: + return -rfloat.INFINITY + except ValueError: + return rfloat.NAN + class Float32(BaseType, Float): T = rffi.FLOAT _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit