-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
ENH: expose bit_generator
and random C-API to cython
#15463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
5094692
db01d81
93ec29d
f66326a
6494a44
0e51c6a
a230da9
8c6a68b
ce13211
a111795
74ac4cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -75,7 +75,7 @@ def uint10_uniforms(Py_ssize_t n): | |||||||||
return randoms | ||||||||||
|
||||||||||
# cython example 3 | ||||||||||
def uniforms_ex(Py_ssize_t n, dtype=np.float64): | ||||||||||
def uniforms_ex(bit_generator, Py_ssize_t n, dtype=np.float64): | ||||||||||
""" | ||||||||||
Create an array of `n` uniformly distributed doubles via a "fill" function. | ||||||||||
|
||||||||||
|
@@ -84,6 +84,7 @@ def uniforms_ex(Py_ssize_t n, dtype=np.float64): | |||||||||
|
||||||||||
Parameters | ||||||||||
---------- | ||||||||||
bit_generator: BitGenerator instance | ||||||||||
n: int | ||||||||||
Output vector length | ||||||||||
dtype: {str, dtype}, optional | ||||||||||
|
@@ -95,25 +96,21 @@ def uniforms_ex(Py_ssize_t n, dtype=np.float64): | |||||||||
cdef const char *capsule_name = "BitGenerator" | ||||||||||
cdef np.ndarray randoms | ||||||||||
|
||||||||||
typedict = {'f': np.float32, 'd': np.float64, 'float64': np.float64, | ||||||||||
'float32': np.float32, np.float32: np.float32, | ||||||||||
np.float64: np.float64} | ||||||||||
typ = typedict.get(dtype, None) | ||||||||||
if not typ: | ||||||||||
_dtype = np.dtype(dtype) | ||||||||||
if _dtype.type not in (np.float32, np.float64): | ||||||||||
raise TypeError('Unsupported dtype "%r"' % dtype) | ||||||||||
x = PCG64() | ||||||||||
capsule = x.capsule | ||||||||||
capsule = bit_generator.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) | ||||||||||
randoms = np.empty(n, dtype=dtype) | ||||||||||
if typ is np.float32: | ||||||||||
with x.lock: | ||||||||||
randoms = np.empty(n, dtype=_dtype) | ||||||||||
if _dtype is np.float32: | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
or
Suggested change
As written this is always false. The former is more consistent with line 100. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got be careful here, the latter is correct. The former would have to also check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. However, I think I actually like the EDIT: I guess here the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is meant as a test-run for something to replace the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True
so since the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. using the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still seems to be the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixing |
||||||||||
with bit_generator.lock: | ||||||||||
random_standard_uniform_fill_f(rng, n, <float*>np.PyArray_DATA(randoms)) | ||||||||||
else: | ||||||||||
with x.lock: | ||||||||||
with bit_generator.lock: | ||||||||||
random_standard_uniform_fill(rng, n, <double*>np.PyArray_DATA(randoms)) | ||||||||||
return randoms | ||||||||||
|
||||||||||
mattip marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,7 +54,7 @@ def generate_libraries(ext, build_dir): | |
PCG64_DEFS = [] | ||
# One can force emulated 128-bit arithmetic if one wants. | ||
#PCG64_DEFS += [('PCG_FORCE_EMULATED_128BIT_MATH', '1')] | ||
depends = ['__init__.pxd', 'c_distributions.pxd'] | ||
depends = ['__init__.pxd', 'c_distributions.pxd', '_bit_generator.pxd'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
# npyrandom - a library like npymath | ||
npyrandom_sources = [ | ||
|
Uh oh!
There was an error while loading. Please reload this page.