cython

vectorization of looping on an array from cython

烈酒焚心 提交于 2019-12-24 13:21:39
问题 Consider the following example of doing an inplace-add on a Cython memoryview: #cython: boundscheck=False, wraparound=False, initializedcheck=False, nonecheck=False, cdivision=True from libc.stdlib cimport malloc, free from libc.stdio cimport printf cimport numpy as np import numpy as np cdef extern from "time.h": int clock() cdef void inplace_add(double[::1] a, double[::1] b): cdef int i for i in range(a.shape[0]): a[i] += b[i] cdef void inplace_addlocal(double[::1] a, double[::1] b): cdef

How to speed up monte carlo simulation in python

人盡茶涼 提交于 2019-12-24 13:03:25
问题 I have written a poker simulator that calculates the probability to win in texas holdem by running a simulation of games (i.e. monte carlo simulation). Currently it runs around 10000 simulations in 10 seconds which in general is good enough. However, there are apps on the iPhone that are running around 100x faster. I'm wondering whether there's anything I can do to speed up the program significantly. I have already replaced strings with lists and found many ways to speed up the program by

Undefined symbol importerror when attempting Cython C++ example

隐身守侯 提交于 2019-12-24 11:54:05
问题 Edit: I've now tried the same code on a macbook and it works fine. So, the problem is confined to my Linux Mint 17.2 system with anaconda python 2.7, cython 0.23.4, and gcc 4.8. The macbook is using anaconda python 2.7, cython 0.22.1, and Apple LLVM version 6.1.0. Original post: I'm attempting to get the Cython c++ example working from here: http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html As there is some room for interpretation, my exact code is pasted below. It's all in a

Cython官方文档中文翻译:教程

安稳与你 提交于 2019-12-24 10:31:05
说明 尝试翻译 Cython Documentation 以助学习。 水平有限,乐迎指正;文档首页:《 Cython官方文档中文翻译 》 本文是目录,每条目录有两部分内容:英文+中文(仅已翻译的有); 其英文附有原来链接;中文附有中文文章链接。 Basic Tutorial >>><<< 初级教程 The Basics of Cython >>><<< Cython基础 Cython Hello World >>><<< Cython实现Hello World Fibonacci Fun >>><<< 斐波那契(Fibonacci)的乐趣 Primes >>><<< 质数 Primes with C++ >>><<< 用C++计算质数 Language Details >>><<< 语言细节 Calling C functions >>><<< 调用C函数 Dynamic linking >>><<< 动态链接 External declarations >>><<< 其他声明 Naming parameters >>><<< 命名参数 Using C libraries Defining external declarations Writing a wrapper class Memory management Compiling and linking Extension

How do I read a C char array into a python bytearray with cython?

时光总嘲笑我的痴心妄想 提交于 2019-12-24 09:17:34
问题 I have an array with bytes and its size: cdef char *bp cdef size_t size How do I read the array into a Python bytearray (or another appropriate structure that can easily be pickled)? 回答1: Three reasonably straightforward ways to do it: Use the appropriate C API function as I suggested in the comments: from cpython.bytes cimport PyBytes_FromStringAndSize output = PyBytes_FromStringAndSize(bp,size) This makes a copy, which may be an issue with a sufficiently large string. For Python 2 the

Is an explicit NUL-byte necessary at the end of a bytearray for cython to be able to convert it to a null-terminated C-string

孤人 提交于 2019-12-24 08:57:14
问题 When converting a bytearray -object (or a bytes -object for that matter) to a C-string, the cython-documentation recommends to use the following: cdef char * cstr = py_bytearray there is no overhead, as cstr is pointing to the buffer of the bytearray -object. However, C-strings are null-terminated and thus in order to be able to pass cstr to a C-function it must also be null-terminated. The cython-documentation doesn't provide any information, whether the resulting C-strings are null

python setuptools: how can I install package with cython submodules?

走远了吗. 提交于 2019-12-24 07:39:06
问题 I have a python package named pytools. It contains a cython-based submodule nms. When I install the root package pytools with sudo python -H setup.py , the root package seems to be installed properly. But the installation didn't copy compiled nms.so to /usr/local/lib/python2.7/dist-packages/pytools/nms/ . And When I import pytools in ipython, an error encountered: ImportError: cannot import name nms If I manually copy the pytools/nms/nms.so to /usr/local/lib/python2.7/dist-packages/pytools

Cython, OS X and link errors

冷暖自知 提交于 2019-12-24 07:19:09
问题 I've followed a simple tutorial on Cython with the following steps: Make a simple python file, testme.py: print( "Hello there!!" ) Create a c file from that using cython: cython -a testme.py Compile the resulting testme.c file with gcc: gcc -Os -I /usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/include/python2.7 -o testme testme.c -lpthread -lm -lutil -ldl The result is quite a few lines like these: Undefined symbols for architecture x86_64: "_PyCode_New",

Using scipy.integrate.ode with cython

北战南征 提交于 2019-12-24 06:59:13
问题 I'm currently trying to use my already written python code with Cython in the hope of a better performance. However, I encounter problems at a point I use scipy.interpolate.ode: TypeError: f() takes exactly 3 positional arguments (2 given) This can be reproduced with the following code: import scipy.integrate as inte import scipy.interpolate import numpy as np class model(object): def __init__(self): self.r = np.arange(10) #a variable self.val = np.arange(10,20) #some values already

The Cython module is not an IPython extension

て烟熏妆下的殇ゞ 提交于 2019-12-24 06:48:26
问题 When trying to load Cython extension from my Jupiter notebook %load_ext Cython I get the below message: The Cython module is not an IPython extension I did not have this problem a couple of weeks ago, when I wrote a Cython extension on my nb. I have tried to upgrade ipython and cython both from my regular environment (I am using a Mac running 10.11) and from Anaconda, with no success. Any tips? Thanks a lot! 回答1: Cython is not an IPython extension, so %load_ext won't work on it. You can still