cython

Cython : exposing C++ classes with nested typedef (s)

别等时光非礼了梦想. 提交于 2020-01-02 09:28:17
问题 According to this question/answer in stackoverflow, it is not possible to directly rewrite C++ nested typedefs in cython. I have such a problem and I don't know which is the right/optimal way to proceed. Let me be more specific with an example. Below, you can find the content of two C++ files (one header.h and one .cpp) and of two corresponding cython files (one .pxd and one .pyx). In the C++ header file called cpp_graph.h you can see nested typedef declarations; for example, that

How can I compile a C function into a numpy ufunc and load it dynamically?

浪子不回头ぞ 提交于 2020-01-02 08:44:35
问题 I have some Python code that automatically generates a C function. This function takes some doubles as input and returns a double, calling various functions from the C standard library along the way. One of the things I would like to do with this is compile it into a numpy ufunc and load it into the running Python process. I just want the function to run element-wise on its input numpy arrays, like numpy's minimum for example, at reasonable speed. I was surprised that I couldn't find clear

Cython: 'PyxImporter' object has no attribute 'find_spec'

北慕城南 提交于 2020-01-02 07:42:02
问题 I'm trying to integrate a Cython module into my project and I'm having trouble getting it to compile correctly. I have traced my problem to this minimal example: Say I have two files a.py and b.pyx located in the same directory, if I then do the following in a.py : import pyximport; pyximport.install() import b Then everything works fine, b.pyx is compiled and imported successfully. But if I instead do this in a.py , assuming that a.py and b.pyx are located in dir1/dir2 : import os import sys

Cython: 'PyxImporter' object has no attribute 'find_spec'

喜夏-厌秋 提交于 2020-01-02 07:41:04
问题 I'm trying to integrate a Cython module into my project and I'm having trouble getting it to compile correctly. I have traced my problem to this minimal example: Say I have two files a.py and b.pyx located in the same directory, if I then do the following in a.py : import pyximport; pyximport.install() import b Then everything works fine, b.pyx is compiled and imported successfully. But if I instead do this in a.py , assuming that a.py and b.pyx are located in dir1/dir2 : import os import sys

Wrapping custom type C++ pointer in Cython

孤街浪徒 提交于 2020-01-02 07:30:31
问题 What is the best way to wrap a custom type C++ pointer using Cython? For example: import numpy as np cimport numpy as np cdef extern from "A_c.h" cdef cppclass A: A(); void Foo(A* vec); cdef class pyA: cdef A *thisptr def ___cinit___(self): self.thisptr = new A() def __dealloc___(self): del self.thisptr How should I use cython to wrap Foo? I have tried the following but I have gotten assertion errors from Buffer.py or the error that A is not a base type of memoryview slice def Foo(self, np

How do I pass a pointer to a c function in Cython?

寵の児 提交于 2020-01-02 01:04:39
问题 I'm trying call qsort in Cython with a custom compare function but I don't understand how to pass the function reference. First, I have a struct: cdef struct Pair: int i,j float h The compare function sorts by h : cdef int compare(const_void *a, const_void *b): cdef float v = ((<Pair*>a)).h-((<Pair*>b)).h if v < 0: return -1 if v > 0: return 1 return 0 This is the part I'm having trouble with: cdef Pair[5] pa for i in range(5): pa[i].i = i; pa[i].j = i*2; pa[i].h = i*.5; qsort(pa,5,sizeof

cython error compiling with print function parameters

房东的猫 提交于 2020-01-02 00:49:50
问题 when use cython to create helloworld.c from helloworld.pyx , this error occured: error compiling Cython file: ------------------------------------------------------------ ... print('hello world',end='') ^ ------------------------------------------------------------ p21.pyx:1:23: Expected ')', found '=' my command to create helloworld.c is: cython3 --embed p21.pyx 回答1: It looks like cython treats all prints as python 2 statements by default. In order to use the python 3 print function you need

Cython and constructors of classes

六月ゝ 毕业季﹏ 提交于 2020-01-01 17:27:06
问题 I have a problem with Cython usage of default constructors. My C++ class Node is the following Node.h class Node { public: Node() { std::cerr << "calling no arg constructor" << std::endl; w=0.0; d=0.0; } Node(double val, double val2); { std::cerr << "calling 2 args constructor" << std::endl; this->w=val; this->d=val2; } private: double d,w; } is wrapped in Cython as follows cdef extern from "Node.h": cdef cppclass Node: Node() except + Node(double val1, double val2) except + double d double w

Should I define my Cython function using def, cdef, or cpdef for optimal performance?

那年仲夏 提交于 2020-01-01 10:53:07
问题 How can I know whether to use def, cdef or cpdef when defining a Cython function, assuming I want optimal performance? 回答1: If you want optimal performance, you should know that as mentioned in this answer to a related question: Once the function has been called there is no difference in the speed that the code inside a cdef and a def function runs at. So for optimal Cython performance you should always statically type all arguments and variables , and intuitively you would then be tempted to

Cython Memoryviews — From Array of Structs?

China☆狼群 提交于 2020-01-01 09:45:10
问题 I'd like to quickly fill with as few copies as possible a long array of structs that I'm receiving incrementally from C. If my struct is only primary data types, like the following: cdef packed struct oh_hi: int lucky char unlucky Then the following works fine: DEF MAXPOWER = 1000000 cdef oh_hi * hi2u = <oh_hi *>malloc(sizeof(oh_hi)*MAXPOWER) cdef oh_hi [:] hi2me = <oh_hi[:MAXPOWER]> hi2u But once I change my struct to hold a character array: cdef packed struct oh_hi: int lucky char unlucky