cython

Passing a numpy pointer (dtype=np.bool) to C++

て烟熏妆下的殇ゞ 提交于 2019-12-22 03:16:25
问题 I'd like to use a numpy array of type bool in C++ by passing its pointer via Cython. I already know how to do it with other datatypes like uint8. Doing it the same way with boolean it does not work. I am able to compile but there is the following Exception during runtime: Traceback (most recent call last): File "test.py", line 15, in <module> c = r.count(b, 4) File "rect.pyx", line 41, in rect.PyRectangle.count (rect.cpp:1865) def count(self, np.ndarray[bool, ndim=1, mode="c"] array not None,

Passing a numpy pointer (dtype=np.bool) to C++

家住魔仙堡 提交于 2019-12-22 03:16:05
问题 I'd like to use a numpy array of type bool in C++ by passing its pointer via Cython. I already know how to do it with other datatypes like uint8. Doing it the same way with boolean it does not work. I am able to compile but there is the following Exception during runtime: Traceback (most recent call last): File "test.py", line 15, in <module> c = r.count(b, 4) File "rect.pyx", line 41, in rect.PyRectangle.count (rect.cpp:1865) def count(self, np.ndarray[bool, ndim=1, mode="c"] array not None,

How to use a Cython cdef class member method in a native callback

て烟熏妆下的殇ゞ 提交于 2019-12-22 00:41:27
问题 I have a C++ library that is heavily callback driven. The callbacks are registered as std::function instances. A callback registration function might look something like: void register(std::function<void(int)> callback); I can register plain cdef functions by making libcpp.functional.function objects. Say I have this callback: cdef void my_callback(int n): # do something interesting with n I can register it succesfully: cdef function[void(int)]* f_my_callback = new function[void(int)](my

calling Python from C

百般思念 提交于 2019-12-22 00:06:14
问题 I have an C code, and there is some simple file manipulation I would need to do at the beginning for initialization purposes, that would be straightforward with Python. I was wondering if there is a way to do that with calling python from C. I am familiar with calling C from Python, but for doing the opposite I have read that Cython could do but I am very confused on how. The python code has to take in input some strings, make some files manipulation and return some doubles and arrays. 回答1:

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

佐手、 提交于 2019-12-21 20:25:14
说明 尝试翻译 Cython Documentation 以助学习。 水平有限,乐迎指正;文档首页:《 Cython官方文档中文翻译 》 Cython基础 Cython 的根本特性可以总结如下: Cython 是带有 C 数据类型的 Python Cython 就是 Python : Python 的几乎所有代码都是有效的Cython代码。(虽然也有一些局限( Limitations ),但就目前而言还是非常接近的) Cython 编译器将代码转为 C代码 ,从而实现对 Python/C API 的平等调用。 因为变量和参数可声明为 C数据类型 ,故 Cython 能量远不止此。控制 Python值 和 C值 的代码可以自由融合,当需要时则自动转换。 Python 的引用计数维护和错误检查以及全部错误处理功能也都是自动的,其中就包括 try-except 、 try-finally 语句(即便是在处理C数据的过程中) Cython实现Hello World 鉴于 Cython 可以识别任意有效的 python源文件 ,开始时的困难之一就是搞懂如何编译你的扩展。 让我们从标准的 python hello world 开始: print ( "Hello World" ) 将上述代码保存在一个 helloworld.pyx 文件内,然后创建 setup.py 文件

Lambdify works with Python, but throws an exception with Cython

我们两清 提交于 2019-12-21 20:04:59
问题 My website runs this Python script that would be way more optimized if Cython is used. Recently I needed to add Sympy with Lambdify, and this is not going well with Cython. So I stripped the problem to a minimum working example. In the code, I have a dictionary with string keys with values that are lists. I would like to use these keys as variables. In the following simplified example, there's only 1 variable, but generally I need more. Please check the following example: import numpy as np

How to build cython pyx to pyd in anaconda(python3.6) in windows 8.1?

感情迁移 提交于 2019-12-21 19:52:51
问题 I have referred some websites to build pyx to pyd in Windows 8.1.I 'm using Anaconda Distribution with Spyder IDE, I have developed pyx file and unable to build in "Anaconda Command Prompt" Anaconda> python setup.py build --inplace --compiler=mingw32 and tried python setup.py build_ext --inplace --compiler=mingw32 getting following error: File "C:\ProgramData\Anaconda3\lib\distutils\cygwinccompiler.py", line 129 in __init__ if self.ld_version >= "2.10.90": TypeError: '>=' not supported

cx_Freeze fails to include Cython .pyx module

孤者浪人 提交于 2019-12-21 19:21:16
问题 I have a Python application to which I recently added a Cython module. Running it from script with pyximport works fine, but I also need an executable version which I build with cx_Freeze. Trouble is, trying to build it gives me an executable that raises ImportError trying to import the .pyx module. I modified my setup.py like so to see if I could get it to compile the .pyx first so that cx_Freeze could successfully pack it: from cx_Freeze import setup, Executable from Cython.Build import

Cython optimization

醉酒当歌 提交于 2019-12-21 17:52:38
问题 I am writing a rather big simulation in Python and was hoping to get some extra performance from Cython. However, for the code below I don't seem to get all that much, even though it contains a rather large loop. Roughly 100k iterations. Did I make some beginners mistake or is this loop-size simply to small to have a big effect? (In my tests the Cython code was only about 2 times faster). import numpy as np; cimport numpy as np; import math ctypedef np.complex64_t cpl_t cpl = np.complex64 def

Pycharm does not recognize Cython modules located in path

耗尽温柔 提交于 2019-12-21 17:46:08
问题 I have these two lines of code: from libc.stdlib cimport malloc, calloc, realloc, free from optv.tracking_framebuf cimport TargetArray The first one is not highlighted by PyCharm (2016.2.3 professional on Ubuntu 14.04) as unresolved refference but the second line is highlighted in red underline as unresolved refference. My TargetArray class is located in tracking_framebuf.pxd file which is located in /usr/local/lib/python2.7/dist-packages/optv/ along with .c, .pyx, .so files with the same