Timo Paulssen avatar Timo Paulssen committed a6ab5e7

implement a few simple methods in numpy.random.

Comments (0)

Files changed (3)

pypy/module/micronumpy/__init__.py

 from pypy.interpreter.mixedmodule import MixedModule
 
+class RandomModule(MixedModule):
+    applevel_name = "random"
+
+    interpleveldefs = {}
+
+    appleveldefs = {}
+
+    for a in [
+        "get_state",
+        "set_state",
+        "seed",
+        "rand",
+        "randn",
+        "standard_normal"]:
+
+        appleveldefs[a] = "app_random.%s" % a
 
 class Module(MixedModule):
     applevel_name = 'numpy'
         'average': 'app_numpy.average',
         'mean': 'app_numpy.mean',
     }
+
+    submodules = {"random": RandomModule}

pypy/module/micronumpy/app_random.py

+from __future__ import absolute_import
+
+from numpy import array
+import random
+
+_random = random.Random()
+
+def get_state():
+    return _random.getstate()
+
+def set_state(state):
+    _random.setstate(state)
+
+def seed(seed):
+    _random.seed(seed)
+
+def rand(*shape):
+    assert len(shape) == 1
+
+    return array(_random.random() for x in range(shape[0]))
+
+def randn(*shape):
+    if len(shape) == 0:
+        return _random.gauss(0, 1)
+    assert len(shape) == 1
+
+    return array(_random.gauss(0, 1) for x in range(shape[0]))
+
+def standard_normal(size=None):
+    return randn(*size)
+

pypy/module/micronumpy/test/test_random.py

+from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest
+
+class AppTestRandom(BaseNumpyAppTest):
+    def test_rand_single(self):
+        from numpy.random import rand
+        from numpy import array, dtype
+
+        single = rand(1)
+        assert isinstance(single, array)
+        assert single.dtype is dtype(float)
+        assert single.shape == (1,)
+        assert single[0] >= 0
+        assert single[0] < 1
+
+    def test_rand_multiple(self):
+        from numpy.random import rand
+        from numpy import dtype
+
+        multi = rand(5)
+
+        assert multi.shape == (5,)
+        assert min(multi) >= 0
+        assert max(multi) < 1
+        assert multi.dtype is dtype(float)
+
+    def test_randn_single(self):
+        from numpy.random import randn
+
+        single = randn()
+
+        assert isinstance(single, float)
+
+    def test_randn_multiple(self):
+        from numpy.random import randn
+
+        multi = randn(6)
+
+        assert multi.shape == (6,)
+
+    def test_state(self):
+        from numpy.random import set_state, get_state, randn
+
+        state = get_state()
+        number = randn()
+        other_number = randn()
+
+        set_state(state)
+        assert randn() == number
+        assert randn() == other_number
+
+    def test_seed(self):
+        from numpy.random import seed, rand
+
+        seed(9001)
+        number = rand(1)[0]
+        other_number = rand(1)[0]
+
+        seed(9001)
+        assert number == rand(1)[0]
+        assert other_number == rand(1)[0]
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.