diff options
author | mattip <matti.picus@gmail.com> | 2019-11-19 22:23:00 -0800 |
---|---|---|
committer | mattip <matti.picus@gmail.com> | 2019-11-19 22:23:00 -0800 |
commit | 521ea22552297345680c426c0204b17b644e6bd0 (patch) | |
tree | 883cc4a161230acc71692c71ff5fc69a98ffed35 /numpy/random/examples/cython | |
parent | 7d75171c4e6fbb2ed0aebda10908a6a0b01d0c7e (diff) | |
download | numpy-521ea22552297345680c426c0204b17b644e6bd0.tar.gz |
MAINT: move numpy/random/examples -> numpy/random/_examples
Diffstat (limited to 'numpy/random/examples/cython')
-rw-r--r-- | numpy/random/examples/cython/extending.pyx | 78 | ||||
-rw-r--r-- | numpy/random/examples/cython/extending_distributions.pyx | 63 | ||||
-rw-r--r-- | numpy/random/examples/cython/setup.py | 32 |
3 files changed, 0 insertions, 173 deletions
diff --git a/numpy/random/examples/cython/extending.pyx b/numpy/random/examples/cython/extending.pyx deleted file mode 100644 index 2a866648d..000000000 --- a/numpy/random/examples/cython/extending.pyx +++ /dev/null @@ -1,78 +0,0 @@ -#!/usr/bin/env python -#cython: language_level=3 - -from libc.stdint cimport uint32_t -from cpython.pycapsule cimport PyCapsule_IsValid, PyCapsule_GetPointer - -import numpy as np -cimport numpy as np -cimport cython - -from numpy.random._bit_generator cimport bitgen_t -from numpy.random import PCG64 - -np.import_array() - - -@cython.boundscheck(False) -@cython.wraparound(False) -def uniform_mean(Py_ssize_t n): - cdef Py_ssize_t i - cdef bitgen_t *rng - cdef const char *capsule_name = "BitGenerator" - cdef double[::1] random_values - cdef np.ndarray randoms - - 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) - # Best practice is to acquire the lock whenever generating random values. - # This prevents other threads from modifying the state. Acquiring the lock - # is only necessary if if the GIL is also released, as in this example. - with x.lock, nogil: - for i in range(n): - random_values[i] = rng.next_double(rng.state) - randoms = np.asarray(random_values) - return randoms.mean() - - -# This function is declated 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 - mask |= mask >> 1 - mask |= mask >> 2 - mask |= mask >> 4 - mask |= mask >> 8 - mask |= mask >> 16 - - val = rng.next_uint32(rng.state) & mask - while val > delta: - val = rng.next_uint32(rng.state) & mask - - return lb + val - - -@cython.boundscheck(False) -@cython.wraparound(False) -def bounded_uints(uint32_t lb, uint32_t ub, Py_ssize_t n): - cdef Py_ssize_t i - cdef bitgen_t *rng - cdef uint32_t[::1] out - cdef const char *capsule_name = "BitGenerator" - - x = PCG64() - out = np.empty(n, dtype=np.uint32) - 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) - - with x.lock, nogil: - for i in range(n): - out[i] = bounded_uint(lb, ub, rng) - return np.asarray(out) diff --git a/numpy/random/examples/cython/extending_distributions.pyx b/numpy/random/examples/cython/extending_distributions.pyx deleted file mode 100644 index d17da45c1..000000000 --- a/numpy/random/examples/cython/extending_distributions.pyx +++ /dev/null @@ -1,63 +0,0 @@ -#!/usr/bin/env python -#cython: language_level=3 -""" -This file shows how the to use a BitGenerator to create a distribution. -""" -import numpy as np -cimport numpy as np -cimport cython -from cpython.pycapsule cimport PyCapsule_IsValid, PyCapsule_GetPointer -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. - A 'real' distribution would want to process the values into - some non-uniform distribution - """ - cdef Py_ssize_t i - cdef bitgen_t *rng - cdef const char *capsule_name = "BitGenerator" - cdef double[::1] random_values - - x = PCG64() - capsule = x.capsule - # Optional check that the capsule if from a BitGenerator - if not PyCapsule_IsValid(capsule, capsule_name): - raise ValueError("Invalid pointer to anon_func_state") - # Cast the pointer - rng = <bitgen_t *> PyCapsule_GetPointer(capsule, capsule_name) - random_values = np.empty(n, dtype='float64') - with x.lock, nogil: - for i in range(n): - # Call the function - random_values[i] = rng.next_double(rng.state) - randoms = np.asarray(random_values) - - return randoms - -# cython example 2 - -@cython.boundscheck(False) -@cython.wraparound(False) -def uint16_uniforms(Py_ssize_t n): - cdef Py_ssize_t i - cdef bitgen_t *rng - cdef const char *capsule_name = "BitGenerator" - cdef double[::1] random_values - - 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') - # Best practice is to release GIL and acquire the lock - with x.lock, nogil: - for i in range(n): - random_values[i] = rng.next_uint32(rng.state) - randoms = np.asarray(random_values) - return randoms diff --git a/numpy/random/examples/cython/setup.py b/numpy/random/examples/cython/setup.py deleted file mode 100644 index 315527a2d..000000000 --- a/numpy/random/examples/cython/setup.py +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env python3 -""" -Build the demos - -Usage: python setup.py build_ext -i -""" - -import numpy as np -from distutils.core import setup -from Cython.Build import cythonize -from setuptools.extension import Extension -from os.path import join, abspath, dirname - -curpath = abspath(dirname(__file__)) - -extending = Extension("extending", - sources=[join(curpath, 'extending.pyx')], - include_dirs=[ - np.get_include(), - join(curpath, '..', '..') - ], - ) -distributions = Extension("extending_distributions", - sources=[join(curpath, 'extending_distributions.pyx'), - ], - include_dirs=[np.get_include()]) - -extensions = [extending, distributions] - -setup( - ext_modules=cythonize(extensions) -) |