Skip to content

Commit

Permalink
Merge pull request #22 from bashtage/example-fix
Browse files Browse the repository at this point in the history
Example fix
  • Loading branch information
mattip committed May 18, 2019
2 parents 6f70cee + f9e4e2a commit 41f9c9c
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 17 deletions.
6 changes: 1 addition & 5 deletions doc/source/reference/random/extending.rst
Expand Up @@ -80,7 +80,7 @@ removing bounds checks and wrap around, providing array alignment information
from cpython.pycapsule cimport PyCapsule_IsValid, PyCapsule_GetPointer
from numpy.random.common cimport *
from numpy.random.distributions cimport random_gauss_zig
from numpy.random.xoroshiro128 import Xoroshiro128
from numpy.random import Xoroshiro128
@cython.boundscheck(False)
Expand All @@ -93,19 +93,15 @@ removing bounds checks and wrap around, providing array alignment information
x = Xoroshiro128()
capsule = x.capsule
# Optional check that the capsule if from a Basic RNG
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)
for i in range(n):
# Call the function
random_values[i] = random_gauss_zig(rng)
randoms = np.asarray(random_values)
return randoms
The BitGenerator can also be directly accessed using the members of the basic
RNG structure.

Expand Down
6 changes: 4 additions & 2 deletions numpy/random/examples/cython/extending.pyx
@@ -1,13 +1,15 @@
#!/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.randomgen.common cimport bitgen_t
from numpy.random.randomgen import Xoroshiro128
from numpy.random.common cimport bitgen_t
from numpy.random import Xoroshiro128

np.import_array()

Expand Down
8 changes: 5 additions & 3 deletions numpy/random/examples/cython/extending_distributions.pyx
@@ -1,11 +1,13 @@
#!/usr/bin/env python
#cython: language_level=3

import numpy as np
cimport numpy as np
cimport cython
from cpython.pycapsule cimport PyCapsule_IsValid, PyCapsule_GetPointer
from numpy.random.randomgen.common cimport *
from numpy.random.randomgen.distributions cimport random_gauss_zig
from numpy.random.randomgen import Xoroshiro128
from numpy.random.common cimport *
from numpy.random.distributions cimport random_gauss_zig
from numpy.random import Xoroshiro128


@cython.boundscheck(False)
Expand Down
10 changes: 8 additions & 2 deletions numpy/random/examples/cython/setup.py
@@ -1,4 +1,10 @@
# python setup.py build_ext -i
#!/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
Expand All @@ -10,7 +16,7 @@
include_dirs=[np.get_include()])
distributions = Extension("extending_distributions",
sources=['extending_distributions.pyx',
join('..', '..', '..', 'randomgen', 'src',
join('..', '..', 'src',
'distributions', 'distributions.c')],
include_dirs=[np.get_include()])

Expand Down
6 changes: 3 additions & 3 deletions numpy/random/examples/numba/extending_distributions.py
Expand Up @@ -39,7 +39,7 @@
""")
x = Xoroshiro128()
xffi = x.cffi
bit_generator = xffi.bit_generator
bit_generator = xffi.bitgen

random_gauss_zig = lib.random_gauss_zig

Expand All @@ -55,6 +55,6 @@ def normals(n, bit_generator):

# Numba requires a memory address for void *
# Can also get address from x.ctypes.bit_generator.value
brng_address = int(ffi.cast('uintptr_t', bit_generator))
bit_generator_address = int(ffi.cast('uintptr_t', bit_generator))

norm = normalsj(1000, brng_address)
norm = normalsj(1000, bit_generator_address)
4 changes: 2 additions & 2 deletions numpy/random/legacy_distributions.pxd
@@ -1,6 +1,6 @@
#cython: language_level=3

from libc.stdint cimport uint64_t
from libc.stdint cimport int64_t

import numpy as np
cimport numpy as np
Expand Down Expand Up @@ -34,7 +34,7 @@ cdef extern from "distributions-boxmuller.h":
double nonc) nogil
double legacy_wald(aug_bitgen_t *aug_state, double mean, double scale) nogil
double legacy_lognormal(aug_bitgen_t *aug_state, double mean, double sigma) nogil
uint64_t legacy_negative_binomial(aug_bitgen_t *aug_state, double n, double p) nogil
int64_t legacy_negative_binomial(aug_bitgen_t *aug_state, double n, double p) nogil
double legacy_standard_cauchy(aug_bitgen_t *state) nogil
double legacy_beta(aug_bitgen_t *aug_state, double a, double b) nogil
double legacy_f(aug_bitgen_t *aug_state, double dfnum, double dfden) nogil
Expand Down

0 comments on commit 41f9c9c

Please sign in to comment.