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

What is the correct way to call scipy.linalg.cython_blas.daxpy?

I want to do in place vector addition in cython, so I'm using scipy.linalg.cython_blas.daxpy . My syntax is: daxpy(&n_samples, &tmp, &Q[0, ii], &inc, &G[0], &inc) But in scikit-learn code, I read axpy(n_samples, -w[ii], &X_data[ii * n_samples], 1,…
P. Camilleri
  • 12,664
  • 7
  • 41
  • 76
3
votes
1 answer

Cython prange fails with Fatal Python error: PyThreadState_Get: no current thread

i am trying to parallelize a loop with prange: cdef fun(double [::1] someData)) nogil: #direct call to a external c function C_function(&someData[0]) #.... def parallelEvaluate(np.ndarray[np.double_t] largerArray): #.... cdef…
mneuner
  • 433
  • 5
  • 25
3
votes
2 answers

Verifying compatibility in compiling extension types, and using them with cdef

Standing questions: Why do other errors in Cython compilation point to the specific line of error, while this doesn't? Prior to update: Due to difficulty in compiling extension types, as referenced in the 'won't compile' link below, it was thought…
ballade4op52
  • 2,142
  • 5
  • 27
  • 42
3
votes
2 answers

Using the line-profiler (in ipython) on compiled Cython code

I read the answer to this question How to profile cython functions line-by-line, but I can't seem to get it to work with my setup. I have a cumsum.pyx file: # cython: profile=True # cython: linetrace=True # cython: binding=True DEF CYTHON_TRACE =…
user357269
  • 1,835
  • 14
  • 40
3
votes
0 answers

How to use bitset(c++) in cython

I am writing a python code that needs an efficient data structure to hold MANY bits. I think the container, bitset in C++, perfectly fits my need. Note I know there is a class bitarray in python that can provide the same functions. However, I really…
3
votes
0 answers

Cython - Passing variable number of arguments from Python to C ellipsis

I am attempting to use Cython to wrap an external C library which has several functions which use the ellipsis (...) as part of the signature. The code here provides a partial solution to the problem. The relevant portion for my question is cdef…
PaxRomana99
  • 564
  • 1
  • 5
  • 12
3
votes
1 answer

Any way to get "interfaces" with Cython?

My application needs to have several Cython cdef classes that inherit from a single base class, but yet still implement many interfaces. These interfaces would be used to do isinstance() checks on the classes to make sure they conform to certain…
Timothy Baldridge
  • 10,455
  • 1
  • 44
  • 80
3
votes
1 answer

Casting a bytearray containing 11-bit integers to an array of 16-bit integers

I have a bytes object or bytearray object representing a packed stream of 11-bit integers. (Edit: Stream is 11-bit big-endian integers without padding.) Is there a reasonably efficient way of copying this to a stream of 16-bit integers? Or any other…
ARF
  • 7,420
  • 8
  • 45
  • 72
3
votes
1 answer

Identify C function in C code generated using Cython

I have been trying to understand how cython really works. Very first, I have written test.pyx file containing, import numpy as np a=2;b=3; np.sum(a,b) np.subtract(a,b) I wrap this code in setup.py file, from distutils.core import setup from…
Sulabh Tiwari
  • 307
  • 2
  • 6
  • 21
3
votes
1 answer

cython create string with nogil

I would like to create a c++ string in a nogil function in a file that would be cimported via pxd. If I define, string output = "" or string output = string("blah"), this uses the python interpreter. Is there a way to define the a string so that…
Michael WS
  • 2,450
  • 4
  • 24
  • 46
3
votes
3 answers

Cython Gibbs sampler slightly slower than numpy one

I have implemented a Gibbs sampler to generate textured images. According to the beta parameters (array of shape(4)), we can generate various textures. Here is my initial function using Numpy: def gibbs_sampler(img_label, betas, burnin,…
floflo29
  • 2,261
  • 2
  • 22
  • 45
3
votes
1 answer

How to debug Cython in an IDE

I am trying to debug Cython code that wraps a c++ class, and the error I am hunting is somewhere in the C++ code. It would be awfully convenient if I could somehow debug as if it were written in one language, i.e. if there's an error in the C++…
fbence
  • 2,025
  • 2
  • 19
  • 42
3
votes
1 answer

specifying string types in cython code

I'm doing some experimentation with cython and I came across some unexpected behavior: In [1]: %load_ext cython In [2]: %%cython ...: cdef class foo(object): ...: cdef public char* val ...: def __init__(self, char* val): ...: …
Stephen
  • 2,613
  • 1
  • 24
  • 42
3
votes
2 answers

Speed-up cython code

I have code that is working in python and want to use cython to speed up the calculation. The function that I've copied is in a .pyx file and gets called from my python code. V, C, train, I_k are 2-d numpy arrays and lambda_u, user, hidden are ints.…
саша
  • 521
  • 5
  • 20
3
votes
3 answers

High performance all-to-all comparison of vectors in Python

First to tell about the background: several methods for comparison between clusterings relies on so called pair counting. We have two vectors of flat clusterings a and b over the same n entities. At pair counting for all possible pairs of entities…
deeenes
  • 4,148
  • 5
  • 43
  • 59