Author: Alex Gaynor <alex.gay...@gmail.com> Branch: Changeset: r44980:98d973ece18a Date: 2011-06-16 15:36 -0700 http://bitbucket.org/pypy/pypy/changeset/98d973ece18a/
Log: numpy ufuncs now work on individual items, as well as elementwise 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 @@ -8,22 +8,24 @@ def ufunc(func): signature = Signature() - @unwrap_spec(array=BaseArray) - def impl(space, array): - w_res = Call1(func, array, array.signature.transition(signature)) - array.invalidates.append(w_res) - return w_res + def impl(space, w_obj): + if isinstance(w_obj, BaseArray): + w_res = Call1(func, w_obj, w_obj.signature.transition(signature)) + w_obj.invalidates.append(w_res) + return w_res + return space.wrap(func(space.float_w(w_obj))) return func_with_new_name(impl, "%s_dispatcher" % func.__name__) def ufunc2(func): signature = Signature() - @unwrap_spec(larray=BaseArray, rarray=BaseArray) - def impl(space, larray, rarray): - new_sig = larray.signature.transition(signature).transition(rarray.signature) - w_res = Call2(func, larray, rarray, new_sig) - larray.invalidates.append(w_res) - rarray.invalidates.append(w_res) - return w_res + def impl(space, w_lhs, w_rhs): + if isinstance(w_lhs, BaseArray) and isinstance(w_rhs, BaseArray): + new_sig = w_lhs.signature.transition(signature).transition(w_rhs.signature) + w_res = Call2(func, w_lhs, w_rhs, new_sig) + w_lhs.invalidates.append(w_res) + w_rhs.invalidates.append(w_res) + return w_res + return space.wrap(func(space.float_w(w_lhs), space.float_w(w_rhs))) return func_with_new_name(impl, "%s_dispatcher" % func.__name__) @ufunc 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 @@ -3,6 +3,13 @@ class AppTestUfuncs(BaseNumpyAppTest): + def test_single_item(self): + from numpy import negative, sign, minimum + + assert negative(5.0) == -5.0 + assert sign(-0.0) == 0.0 + assert minimum(2.0, 3.0) == 2.0 + def test_negative(self): from numpy import array, negative _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit