summaryrefslogtreecommitdiff
path: root/numpy/random/_examples/cython
diff options
context:
space:
mode:
authorKevin Sheppard <kevin.k.sheppard@gmail.com>2019-11-21 11:10:16 +0000
committerKevin Sheppard <kevin.k.sheppard@gmail.com>2019-11-21 11:10:16 +0000
commit7548073e58e89b414ca999d666c19189291dc1e5 (patch)
treed0d77d3bd569fc5c1374c3c306c6d26342acc100 /numpy/random/_examples/cython
parentd428127183d46b2fbd99afefa4670642addf8d6e (diff)
downloadnumpy-7548073e58e89b414ca999d666c19189291dc1e5.tar.gz
DOC: Clean up examples of low-level random access
Test examples on Linux and Windows Correct bug in Cython example Improve building instructions for numba examples
Diffstat (limited to 'numpy/random/_examples/cython')
-rw-r--r--numpy/random/_examples/cython/extending.pyx2
-rw-r--r--numpy/random/_examples/cython/extending_distributions.pyx23
-rw-r--r--numpy/random/_examples/cython/setup.py11
3 files changed, 23 insertions, 13 deletions
diff --git a/numpy/random/_examples/cython/extending.pyx b/numpy/random/_examples/cython/extending.pyx
index 2a866648d..d12c0b919 100644
--- a/numpy/random/_examples/cython/extending.pyx
+++ b/numpy/random/_examples/cython/extending.pyx
@@ -39,7 +39,7 @@ def uniform_mean(Py_ssize_t n):
return randoms.mean()
-# This function is declated nogil so it can be used without the GIL below
+# This function is declared nogil so it can be used without the GIL below
cdef uint32_t bounded_uint(uint32_t lb, uint32_t ub, bitgen_t *rng) nogil:
cdef uint32_t mask, delta, val
mask = delta = ub - lb
diff --git a/numpy/random/_examples/cython/extending_distributions.pyx b/numpy/random/_examples/cython/extending_distributions.pyx
index d17da45c1..3f342c475 100644
--- a/numpy/random/_examples/cython/extending_distributions.pyx
+++ b/numpy/random/_examples/cython/extending_distributions.pyx
@@ -7,14 +7,17 @@ import numpy as np
cimport numpy as np
cimport cython
from cpython.pycapsule cimport PyCapsule_IsValid, PyCapsule_GetPointer
+from libc.stdint cimport uint16_t, uint64_t
from numpy.random._bit_generator cimport bitgen_t
+
from numpy.random import PCG64
@cython.boundscheck(False)
@cython.wraparound(False)
def uniforms(Py_ssize_t n):
- """ Create an array of `n` uniformly distributed doubles.
+ """
+ Create an array of `n` uniformly distributed doubles.
A 'real' distribution would want to process the values into
some non-uniform distribution
"""
@@ -40,24 +43,32 @@ def uniforms(Py_ssize_t n):
return randoms
# cython example 2
-
@cython.boundscheck(False)
@cython.wraparound(False)
-def uint16_uniforms(Py_ssize_t n):
+def uint10_uniforms(Py_ssize_t n):
+ """Uniform 10 bit integers stored as 16-bit unsigned integers"""
cdef Py_ssize_t i
cdef bitgen_t *rng
cdef const char *capsule_name = "BitGenerator"
- cdef double[::1] random_values
+ cdef uint16_t[::1] random_values
+ cdef int bits_remaining
+ cdef int width = 10
+ cdef uint64_t buff, mask = 0x3FF
x = PCG64()
capsule = x.capsule
if not PyCapsule_IsValid(capsule, capsule_name):
raise ValueError("Invalid pointer to anon_func_state")
rng = <bitgen_t *> PyCapsule_GetPointer(capsule, capsule_name)
- random_values = np.empty(n, dtype='uint32')
+ random_values = np.empty(n, dtype='uint16')
# Best practice is to release GIL and acquire the lock
+ bits_remaining = 0
with x.lock, nogil:
for i in range(n):
- random_values[i] = rng.next_uint32(rng.state)
+ if bits_remaining < width:
+ buff = rng.next_uint64(rng.state)
+ random_values[i] = buff & mask
+ buff >>= width
+
randoms = np.asarray(random_values)
return randoms
diff --git a/numpy/random/_examples/cython/setup.py b/numpy/random/_examples/cython/setup.py
index 315527a2d..19f045fc0 100644
--- a/numpy/random/_examples/cython/setup.py
+++ b/numpy/random/_examples/cython/setup.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
"""
-Build the demos
+Build the Cython demonstrations of low-level access to NumPy random
Usage: python setup.py build_ext -i
"""
@@ -11,18 +11,17 @@ from Cython.Build import cythonize
from setuptools.extension import Extension
from os.path import join, abspath, dirname
-curpath = abspath(dirname(__file__))
+path = abspath(dirname(__file__))
extending = Extension("extending",
- sources=[join(curpath, 'extending.pyx')],
+ sources=[join(path, 'extending.pyx')],
include_dirs=[
np.get_include(),
- join(curpath, '..', '..')
+ join(path, '..', '..')
],
)
distributions = Extension("extending_distributions",
- sources=[join(curpath, 'extending_distributions.pyx'),
- ],
+ sources=[join(path, 'extending_distributions.pyx')],
include_dirs=[np.get_include()])
extensions = [extending, distributions]