我正在为numpy编写一个新的随机数生成器,当我遇到这个非常奇怪的行为时,根据任意分布生成随机数:
这是test.pyx
#cython: boundscheck=False
#cython: wraparound=False
import numpy as np
cimport numpy as np
cimport cython
def BareBones(np.ndarray[double, ndim=1] a,np.ndarray[double, ndim=1] u,r):
return u
def UntypedWithLoop(a,u,r):
cdef int i,j=0
for i in range(u.shape[0]):
j+=i
return u,j
def BSReplacement(np.ndarray[double, ndim=1] a, np.ndarray[double, ndim=1] u):
cdef np.ndarray[np.int_t, ndim=1] r=np.empty(u.shape[0],dtype=int)
cdef int i,j=0
for i in range(u.shape[0]):
j=i
return r
Run Code Online (Sandbox Code Playgroud)
setup.py
from distutils.core import setup
from Cython.Build import cythonize
setup(name = "simple cython func",ext_modules = cythonize('test.pyx'),)
Run Code Online (Sandbox Code Playgroud)
分析代码
#!/usr/bin/python
from __future__ import division
import …
Run Code Online (Sandbox Code Playgroud) 当我尝试运行下面的cython代码生成一个空数组时,就会出现段错误.
有没有办法在python中生成空的numpy数组而不调用np.empty()
?
cdef np.npy_intp *dims = [3]
cdef np.ndarray[np.int_t, ndim=1] result = np.PyArray_EMPTY(1, dims,
np.NPY_INTP, 0)
Run Code Online (Sandbox Code Playgroud) 是否有任何现成的库,以便numpy程序可以使用intel硬件prng(rdrand)来填充随机数的缓冲区?
如果失败了,有人会指出我正确的方向,我可以适应或使用一些C代码(我使用CPython和Cython与numpy,所以最小的包装器就足够了).
我想要的随机生成器是[0,1]之间的均匀随机数.