[pypy-commit] pypy numpy-random: implement a few simple methods in numpy.random.

timo_jbo noreply at buildbot.pypy.org
Sat Sep 24 20:31:21 CEST 2011


Author: Timo Paulssen <timonator at perpetuum-immobile.de>
Branch: numpy-random
Changeset: r47583:a6ab5e7ba103
Date: 2011-09-22 20:05 +0200
http://bitbucket.org/pypy/pypy/changeset/a6ab5e7ba103/

Log:	implement a few simple methods in numpy.random.

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
@@ -1,5 +1,21 @@
 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'
@@ -51,3 +67,5 @@
         'average': 'app_numpy.average',
         'mean': 'app_numpy.mean',
     }
+
+    submodules = {"random": RandomModule}
diff --git a/pypy/module/micronumpy/app_random.py b/pypy/module/micronumpy/app_random.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/micronumpy/app_random.py
@@ -0,0 +1,31 @@
+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)
+
diff --git a/pypy/module/micronumpy/test/test_random.py b/pypy/module/micronumpy/test/test_random.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/micronumpy/test/test_random.py
@@ -0,0 +1,60 @@
+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]


More information about the pypy-commit mailing list