[pypy-commit] pypy numpy-exp: (michaelh): Implemented sign and reciprocal ufuncs.
alex_gaynor
noreply at buildbot.pypy.org
Tue May 17 00:40:04 CEST 2011
Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: numpy-exp
Changeset: r44225:d1aa42609816
Date: 2011-05-16 17:49 -0500
http://bitbucket.org/pypy/pypy/changeset/d1aa42609816/
Log: (michaelh): Implemented sign and reciprocal ufuncs.
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
@@ -11,9 +11,11 @@
# ufuncs
'absolute': 'interp_ufuncs.absolute',
+ 'maximum': 'interp_ufuncs.maximum',
+ 'minimum': 'interp_ufuncs.minimum',
'negative': 'interp_ufuncs.negative',
- 'minimum': 'interp_ufuncs.minimum',
- 'maximum': 'interp_ufuncs.maximum',
+ 'reciprocal': 'interp_ufuncs.reciprocal',
+ 'sign': 'interp_ufuncs.sign',
}
appleveldefs = {}
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
@@ -1,5 +1,6 @@
from pypy.interpreter.gateway import unwrap_spec
from pypy.module.micronumpy.interp_numarray import BaseArray, Call1, Call2, Signature
+from pypy.rlib import rfloat
from pypy.tool.sourcetools import func_with_new_name
@@ -24,17 +25,29 @@
return func_with_new_name(impl, "%s_dispatcher" % func.__name__)
@ufunc
-def negative(value):
- return -value
-
- at ufunc
def absolute(value):
return abs(value)
@ufunc2
+def maximum(lvalue, rvalue):
+ return max(lvalue, rvalue)
+
+ at ufunc2
def minimum(lvalue, rvalue):
return min(lvalue, rvalue)
- at ufunc2
-def maximum(lvalue, rvalue):
- return max(lvalue, rvalue)
+ at ufunc
+def negative(value):
+ return -value
+
+ at ufunc
+def reciprocal(value):
+ if value == 0.0:
+ return rfloat.copysign(rfloat.INFINITY, value)
+ return 1.0 / value
+
+ at ufunc
+def sign(value):
+ if value == 0.0:
+ return 0.0
+ return rfloat.copysign(1.0, value)
\ No newline at end of file
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
@@ -40,3 +40,21 @@
c = maximum(a, b)
for i in range(3):
assert c[i] == max(a[i], b[i])
+
+ def test_sign(self):
+ from numpy import array, sign
+
+ reference = [-1.0, 0.0, 0.0, 1.0]
+ a = array([-5.0, -0.0, 0.0, 6.0])
+ b = sign(a)
+ for i in range(4):
+ assert b[i] == reference[i]
+
+ def test_reciporocal(self):
+ from numpy import array, reciprocal
+
+ reference = [-0.2, float("inf"), float("-inf"), 2.0]
+ a = array([-5.0, 0.0, -0.0, 0.5])
+ b = reciprocal(a)
+ for i in range(4):
+ assert b[i] == reference[i]
\ No newline at end of file
More information about the pypy-commit
mailing list