Questions tagged [cython]

Cython is a superset of the Python language for quickly generating Python C extensions.

Cython is a superset of the Python language for quickly generating Python C/C++ extensions. Cython is a pidgin language of Python and C/C++. Unlike pure Python, Cython code is not directly interpreted by the Python interpreter, but is instead used to generate C/C++ code. The generated C/C++ code can then be compiled into a C/C++-extension, which then can be imported by Python code.

Cython syntax was originally based on Python 2, with added type declarations à la C/C++, however, its syntax now supports both Python 2 and 3 language features. Additionally, Cython is capable of generating C/C++ extension code compatible with either Python 2 or Python 3. Its syntax now allows the use of advanced C++ constructs such as template and stl container. Finally, thanks to static typing, Cython code generally executes much faster than Python code.

Cython is freely available under the open source Apache License.

The latest release of Cython is 3.0 alpha 5 (released 2020-05-19). Cython is available from the PyPI package index repository.

5220 questions
3
votes
1 answer

Fastest way to expose C strings to NumPy?

I'm working on converting some old text logs to a usable format in Python. The files are huge, so I'm writing my own C extensions to run through the files as quickly as possible and parse out the relevant fields with regular expressions. My…
Max
  • 301
  • 1
  • 9
3
votes
3 answers

Does Cython offer any reasonably easy and efficient way to iterate Numpy arrays as if they were flat?

Let's say I want to implement Numpy's x[:] += 1 in Cython. I could write @cython.boundscheck(False) @cython.wraparoundcheck(False) def add1(np.ndarray[np.float32_t, ndim=1] x): cdef unsigned long i for i in range(len(x)): x[i] +=…
MWB
  • 11,740
  • 6
  • 46
  • 91
3
votes
5 answers

Global name 'col2im_6d_cython' is not defined, CS231n

I'm following CS231n and met a problem when doing assignment2: ConvolutionalNetworks: global name 'col2im_6d_cython' is not defined. I think the problem was due to a failure in importing functions from im2col_cython.pyx, which used cython. I've…
Mufei Li
  • 129
  • 1
  • 4
3
votes
1 answer

How do I cast a 2D list to a void pointer and back

I'm trying to write two Cython functions to wrap external functions. The functions are the inverse of each another; one accepts a string, and returns a struct with two fields: a void pointer to a 2D array (the second dimension is always two…
urschrei
  • 25,123
  • 12
  • 43
  • 84
3
votes
1 answer

How do I declare an enum in a cpp class in cython

I am trying to wrap a c++ library using cython. The c++ header file say MyFile.h declares a class like this: class MyClass { public: enum MyEnum{ TYPE0 = 0, TYPE1 = 1, TYPE2 = 2, }; MyClass(MyEnum…
3
votes
0 answers

using numpy array of arrays in Cython parallel with nogil

I want to assign numpy array of arrays (array with elements as array) inside Cython parallel loop with nogil. I use the dtype as an object to be able to assign the numpy array as an array element. It works in Cython without parallelizing. But by…
Haleh
  • 31
  • 3
3
votes
1 answer

Wrapping a C++ struct in Cython

I am trying to Cython-wrap a dll written in C++ with the following header file: #define EXTERN_DLL_EXPORT extern "C" __declspec(dllexport) struct cmplx64 { float re; float im; }; EXTERN_DLL_EXPORT int foo(cmplx64 *arr, int arr_sz); The…
Trekkie
  • 964
  • 1
  • 9
  • 32
3
votes
1 answer

AES-NI intrinsics in Cython?

Is there a way to use AES-NI instructions within Cython code? Closest I could find is how someone accessed SIMD instructions: https://groups.google.com/forum/#!msg/cython-users/nTnyI7A6sMc/a6_GnOOsLuQJ AES-NI in Python thread was not…
ArekBulski
  • 4,520
  • 4
  • 39
  • 61
3
votes
2 answers

Cython conditional compile based on external value

I try to conditionally compile (or generate) to c code from a Cython pxd. I read that I can DEF to define aa value and IF to conditionally generate based on its value, but how can I get this value to get from outside of the pxd file? Specifically…
ron
  • 9,262
  • 4
  • 40
  • 73
3
votes
1 answer

How to compile my python code in cython with external python libs like pybrain

I need more perfomance running my neural network, so I thinked that building it with cython will be good idea. I am building my code like this: from distutils.core import setup from Cython.Build import cythonize setup( ext_modules =…
3
votes
1 answer

Make extension using Cython, with a different name than the source file

I know how to make C code from Python using cythonize -o mymodule.c mymodule.py or using the cython command. The resulting C file has to be compiled to an Python extension using gcc or any other C compiler, and I'm also able doing that. If I want it…
linusg
  • 6,289
  • 4
  • 28
  • 78
3
votes
1 answer

PEP-484 Type Annotations with own types

PEP-484 provides semantics for type annotations. These are geared very much towards a) documentation and b) help for IDEs. They are less geared towards code optimization. For example, it is unfortunately not possible to use PEP 484 annotations…
Dan
  • 161
  • 1
  • 4
3
votes
1 answer

Possible bug in Cython generated C++ code

I'm trying to create a Cython wrapper for the __gnu_parallel::sort in the same way as they do in this thread Parallel in-place sort for numpy arrays. This is my simplified code for wrapparallel.pyx: import cython cimport cython cdef extern from…
crunchit
  • 33
  • 4
3
votes
2 answers

Dylib built on CI can't be loaded

I'm building a Rust binary (liblonlat_bng.dylib) on Travis CI, pulling it into a Cython extension (in the same dir as the Cython source .c/.pyx), and testing it, also on Travis CI (in a different repo and build). However, tests of the Python package…
urschrei
  • 25,123
  • 12
  • 43
  • 84
3
votes
1 answer

Using Python objects in C++

I'm writing a code that calculates the images of nonlinear maps using methods from interval analysis, applies a minkowski sum and repeats for an arbitrary number of iterations. I've written a working code in Python, however I would like to be able…
duncster94
  • 570
  • 6
  • 23