Taavi Burns avatar Taavi Burns committed 7244bbc Merge

Merge in more ufuncs

Comments (0)

Files changed (4)

pypy/module/micronumpy/__init__.py

         ("fmod", "fmod"),
         ("floor", "floor"),
         ("ceil", "ceil"),
+        ("trunc", "trunc"),
         ("greater", "greater"),
         ("greater_equal", "greater_equal"),
         ("less", "less"),
         ('bitwise_or', 'bitwise_or'),
         ('bitwise_xor', 'bitwise_xor'),
         ('bitwise_not', 'invert'),
+        ('left_shift', 'left_shift'),
+        ('right_shift', 'right_shift'),
         ('invert', 'invert'),
         ('isnan', 'isnan'),
         ('isinf', 'isinf'),

pypy/module/micronumpy/interp_ufuncs.py

             ("fmod", "fmod", 2, {"promote_to_float": True}),
             ("floor", "floor", 1, {"promote_to_float": True}),
             ("ceil", "ceil", 1, {"promote_to_float": True}),
+            ("trunc", "trunc", 1, {"promote_to_float": True}),
             ("exp", "exp", 1, {"promote_to_float": True}),
             ("exp2", "exp2", 1, {"promote_to_float": True}),
             ("expm1", "expm1", 1, {"promote_to_float": True}),

pypy/module/micronumpy/test/test_ufuncs.py

         for i in range(3):
             assert c[i] == a[i] - b[i]
 
-    def test_floorceil(self):
-        from _numpypy import array, floor, ceil
+    def test_floorceiltrunc(self):
+        from _numpypy import array, floor, ceil, trunc
         import math
-        reference = [-2.0, -2.0, -1.0, 0.0, 1.0, 1.0, 0]
-        a = array([-1.4, -1.5, -1.0, 0.0, 1.0, 1.4, 0.5])
-        b = floor(a)
-        for i in range(5):
-            assert b[i] == reference[i]
-        reference = [-1.0, -1.0, -1.0, 0.0, 1.0, 2.0, 1.0]
-        a = array([-1.4, -1.5, -1.0, 0.0, 1.0, 1.4, 0.5])
-        b = ceil(a)
-        assert (reference == b).all()
-        inf = float("inf")
-        data = [1.5, 2.9999, -1.999, inf]
-        results = [math.floor(x) for x in data]
-        assert (floor(data) == results).all()
-        results = [math.ceil(x) for x in data]
-        assert (ceil(data) == results).all()
+        ninf, inf = float("-inf"), float("inf")
+        a = array([ninf, -1.4, -1.5, -1.0, 0.0, 1.0, 1.4, 0.5, inf])
+        assert ([ninf, -2.0, -2.0, -1.0, 0.0, 1.0, 1.0, 0.0, inf] == floor(a)).all()
+        assert ([ninf, -1.0, -1.0, -1.0, 0.0, 1.0, 2.0, 1.0, inf] == ceil(a)).all()
+        assert ([ninf, -1.0, -1.0, -1.0, 0.0, 1.0, 1.0, 0.0, inf] == trunc(a)).all()
+        assert all([math.isnan(f(float("nan"))) for f in floor, ceil, trunc])
+        assert all([math.copysign(1, f(float("nan"))) == 1 for f in floor, ceil, trunc])
+        assert all([math.copysign(1, f(float("-nan"))) == -1 for f in floor, ceil, trunc])
 
     def test_copysign(self):
         from _numpypy import array, copysign
         assert (bitwise_not(a) == ~a).all()
         assert (invert(a) == ~a).all()
 
+    def test_shift(self):
+        from _numpypy import left_shift, right_shift
+        import sys
+
+        assert (left_shift([5, 1], [2, 31]) == [20, 2**31]).all()
+        assert (right_shift(10, range(5)) == [10, 5, 2, 1, 0]).all()
+
     def test_comparisons(self):
         import operator
         from _numpypy import equal, not_equal, less, less_equal, greater, greater_equal

pypy/module/micronumpy/types.py

         return math.ceil(v)
 
     @simple_unary_op
+    def trunc(self, v):
+        if v < 0:
+            return math.ceil(v)
+        else:
+            return math.floor(v)
+
+    @simple_unary_op
     def exp(self, v):
         try:
             return math.exp(v)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.