cython

Can I statically link Cython modules into an executable which embeds python?

若如初见. 提交于 2019-12-21 12:11:53
问题 I currently have an executable compiled from C++ that embeds python. The embedded executable runs a python script which load several Cython modules. Both the Cython modules and the executable are linked against a shared library. I want to move the shared library into the executable by statically linking the shared library against the executable. Can I statically link the Cython modules into the executable which embeds python? What is the best way to handle this situation? 回答1: Yes it's

Cython's calculations are incorrect

旧街凉风 提交于 2019-12-21 12:07:33
问题 I implemented the Madhava–Leibniz series to calculate pi in Python, and then in Cython to improve the speed. The Python version: from __future__ import division pi = 0 l = 1 x = True while True: if x: pi += 4/l else: pi -= 4/l x = not x l += 2 print str(pi) The Cython version: cdef float pi = 0.0 cdef float l = 1.0 cdef unsigned short x = True while True: if x: pi += 4.0/l else: pi -= 4.0/l x = not x l += 2 print str(pi) When I stopped the Python version it had correctly calculated pi to 3

Performance comparison Fortran, Numpy,Cython and Numexpr

不想你离开。 提交于 2019-12-21 10:42:44
问题 I have following function: def get_denom(n_comp,qs,x,cp,cs): ''' len(n_comp) = 1 # number of proteins len(cp) = n_comp # protein concentration len(qp) = n_comp # protein capacity len(x) = 3*n_comp + 1 # fit parameters len(cs) = 1 ''' k = x[0:n_comp] sigma = x[n_comp:2*n_comp] z = x[2*n_comp:3*n_comp] a = (sigma + z)*( k*(qs/cs)**(z-1) )*cp denom = np.sum(a) + cs return denom I compare it against a Fortran implementation (My first Fortran function ever): subroutine get_denom (qs,x,cp,cs,n_comp

cython issue: 'bool' is not a type identifier

瘦欲@ 提交于 2019-12-21 06:47:04
问题 I'm desperately trying to expose a std::vector<bool> class member to a Python class. Here is my C++ class: class Test { public: std::vector<bool> test_fail; std::vector<double> test_ok; }; While the access and conversion of test_ok of type double (or int, float, ..) works, it does not for bool ! Here is my Cython class: cdef class pyTest: cdef Test* thisptr cdef public vector[bool] test_fail cdef public vector[double] test_ok cdef __cinit__(self): self.thisptr = new Test() self.test_fail =

Cython --embed flag in setup.py

扶醉桌前 提交于 2019-12-21 04:39:06
问题 I am starting to compile my Python 3 project with Cython, and I would like to know if it's possible to reduce my current compile time workflow to a single instruction. This is my setup.py as of now: from distutils.core import setup from distutils.extension import Extension from Cython.Build import cythonize extensions = [ Extension("v", ["version.py"]), Extension("*", ["lib/*.py"]) ] setup( name = "MyFirst App", ext_modules = cythonize(extensions), ) And this is what I run from shell to

Cython - implementing callbacks

僤鯓⒐⒋嵵緔 提交于 2019-12-21 04:23:19
问题 I have been working with Cython in an attempt to interface with a library written in c++. So far things are going pretty good, and I can effectively use MOST functions within the library. My only problem lies within implementing callbacks. The library has 4 function definitions that look a little something like this: typedef void (*Function1)(const uint16_t *data, unsigned width, unsigned height); void SetCallBack(Function1); So to implement them I figured I would do something like this with

What is causing the 2x slowdown in my Cython implementation of matrix vector multiplication?

跟風遠走 提交于 2019-12-21 04:20:19
问题 I am currently trying to implement basic matrix vector multiplication in Cython (as part of a much larger project to reduce computation) and finding that my code is about 2x slower than Numpy.dot . I am wondering if there is something that I am missing that is resulting in the slowdown. I am writing optimized Cython code, declaring variable types, requiring contiguous arrays, and avoiding cache misses. I even tried having Cython as a wrapper and calling native C code (see below). I'm

Cython - converting pointers to arrays into Python objects

独自空忆成欢 提交于 2019-12-21 03:54:12
问题 Alright, I am so close to finishing this I can taste it. Over the past few week or so, I've been attempting to create a Python extension to interface with a library written in C++ via Cython. With a little help from the guys here and a couple of friends, I have managed to get what feels like 98% of the way there. Only thing remaining is this: I can't for the life of me figure out how to turn a pointer to an array of unsigned shorts into a python object (Preferably a list). A little background

Cython in Ipython: ERROR: Cell magic `%%cython` not found

馋奶兔 提交于 2019-12-21 03:33:24
问题 While using cython in ipython notebook, I see the error below. What's wrong? %load_ext cythonmagic /usr/local/lib/python2.7/dist-packages/IPython/extensions/cythonmagic.py:21: UserWarning: The Cython magic has been moved to the Cython package warnings.warn("""The Cython magic has been moved to the Cython package""") %%cython def fib(int n): cdef int a,b,i for i in range(n): a,b=a+b,b return a ERROR: Cell magic `%%cython` not found. 回答1: What the warning is trying to communicate is that the

How to return new C++ objects in Cython?

主宰稳场 提交于 2019-12-20 23:23:18
问题 I suspect there is an easy answer to this, but I need some help getting started with Cython. I have an existing C++ code base which I want to expose to Python via Cython. For each class I want to expose, I create a Cython cppclass _ClassName and the Python wrapper class ClassName . A minmal example: Object.h CythonMinimal.pyx setup.py content of Object.h : class Object { public: Object clone() { Object o; return o; } }; content of CythonMinimal.pyx : cdef extern from "Object.h": cdef cppclass