小编sta*_*icd的帖子

在cython中创建小数组需要花费大量的时间

我正在为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)

python arrays performance numpy cython

7
推荐指数
1
解决办法
1348
查看次数

cython中的空数组:调用PyArray_EMPTY时的段错误

当我尝试运行下面的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)

python arrays numpy cython

5
推荐指数
1
解决办法
772
查看次数

使用python的硬件rng

是否有任何现成的库,以便numpy程序可以使用intel硬件prng(rdrand)来填充随机数的缓冲区?

如果失败了,有人会指出我正确的方向,我可以适应或使用一些C代码(我使用CPython和Cython与numpy,所以最小的包装器就足够了).

我想要的随机生成器是[0,1]之间的均匀随机数.

python random numpy rdrand

5
推荐指数
1
解决办法
1623
查看次数

标签 统计

numpy ×3

python ×3

arrays ×2

cython ×2

performance ×1

random ×1

rdrand ×1