Commits

Neil Blakey-Milner committed 5051a26

Implement radians, deg2rad, and degrees.

Fix log2 to only use rpython-available math.log function that uses a single
argument.

Comments (0)

Files changed (4)

pypy/module/micronumpy/__init__.py

         ("multiply", "multiply"),
         ("negative", "negative"),
         ("not_equal", "not_equal"),
+        ("radians", "radians"),
+        ("degrees", "degrees"),
+        ("deg2rad", "radians"),
         ("reciprocal", "reciprocal"),
         ("sign", "sign"),
         ("sin", "sin"),

pypy/module/micronumpy/interp_ufuncs.py

             ("arccosh", "arccosh", 1, {"promote_to_float": True}),
             ("arctanh", "arctanh", 1, {"promote_to_float": True}),
 
+            ("radians", "radians", 1, {"promote_to_float": True}),
+            ("degrees", "degrees", 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}),

pypy/module/micronumpy/test/test_ufuncs.py

         assert math.isnan(sqrt(-1))
         assert math.isnan(sqrt(nan))
 
+    def test_radians(self):
+        import math
+        from _numpypy import radians, array
+        a = array([
+            -181, -180, -179,
+            181, 180, 179,
+            359, 360, 361,
+            400, -1, 0, 1,
+            float('inf'), float('-inf')])
+        b = radians(a)
+        for i in range(len(a)):
+            assert b[i] == math.radians(a[i])
+
+    def test_deg2rad(self):
+        import math
+        from _numpypy import deg2rad, array
+        a = array([
+            -181, -180, -179,
+            181, 180, 179,
+            359, 360, 361,
+            400, -1, 0, 1,
+            float('inf'), float('-inf')])
+        b = deg2rad(a)
+        for i in range(len(a)):
+            assert b[i] == math.radians(a[i])
+
+    def test_degrees(self):
+        import math
+        from _numpypy import degrees, array
+        a = array([
+            -181, -180, -179,
+            181, 180, 179,
+            359, 360, 361,
+            400, -1, 0, 1,
+            float('inf'), float('-inf')])
+        b = degrees(a)
+        for i in range(len(a)):
+            assert b[i] == math.degrees(a[i])
+
     def test_reduce_errors(self):
         from _numpypy import sin, add
 

pypy/module/micronumpy/types.py

 from pypy.rpython.lltypesystem import lltype, rffi
 from pypy.rlib.rstruct.runpack import runpack
 
+degToRad = math.pi / 180.0
+log2 = math.log(2)
 
 def simple_unary_op(func):
     specialize.argtype(1)(func)
     @simple_binary_op
     def floordiv(self, v1, v2):
         try:
-            return v1 // v2
+            return math.floor(v1 / v2)
         except ZeroDivisionError:
             if v1 == v2 == 0.0:
                 return rfloat.NAN
         return rfloat.isinf(v)
 
     @simple_unary_op
+    def radians(self, v):
+        return v * degToRad
+    deg2rad = radians
+
+    @simple_unary_op
+    def degrees(self, v):
+        return v / degToRad
+
+    @simple_unary_op
     def log(self, v):
         try:
             return math.log(v)
     @simple_unary_op
     def log2(self, v):
         try:
-            return math.log(v, 2)
+            return math.log(v) / log2
         except ValueError:
             if v == 0.0:
                 # CPython raises ValueError here, so we have to check