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

ImportError: No module named 'Cython' when installing Cython on mac for darkflow

I'm trying to install darkflow on my mac and I've already downloaded cython extensions. I'm trying to use this command on terminal: python3 setup.py build_ext --inplace Traceback (most recent call last): File "setup.py", line 3, in
Kartman19
  • 31
  • 1
  • 2
3
votes
1 answer

Cython self defined data types ndarray

I've created a dtype for my np.ndarrays: particle_t = np.dtype([ ('position', float, 2), ('momentum', float, 2), ('velocity', float, 2), ('force', float, 2), ('charge', int, 1), ]) According to the official examples one can…
3
votes
1 answer

Cython returning a double complex to a float complex causes the expression not to be in pure C

I have a problem trying to use complex64_t in Cython. Here is my simple cython example. cimport numpy as cnp cdef extern from "complex.h": double complex cexp(double complex) cpdef example(): cdef float b = 2.0 cdef cnp.complex64_t…
rtclark
  • 555
  • 4
  • 16
3
votes
1 answer

Duplicate enum member names in Cython - redeclaration error?

Cython doesn't seem to allow me to reuse enum member names. I have the following enums that I'm trying to cythonize: from enum import Enum class Fruit(Enum): UNKNOWN = 0 APPLE = 1 ORANGE = 2 class Animal(Enum): UNKNOWN = 0 …
Katie
  • 918
  • 1
  • 5
  • 18
3
votes
1 answer

Can not find libraries in C code

I am trying to compile a module using cython, but the compiler can not find include libraries in an external c file (I use cmath as example, but other libraries that I want such as cstdio, cstdint, cstring have the same issue) A minimal example is…
Jonathan Lindgren
  • 1,192
  • 3
  • 14
  • 31
3
votes
1 answer

Estimating determinant using LAPACK wrapper for LU decomposition in Cython

I define the function that calculates the determinant of a matrix here. But sometimes I get the wrong sign. I modeled my function from this answer. from scipy.linalg.cython_lapack cimport dgetrf cpdef double det_c(double[:, ::1] A, double[:, ::1]…
jtorca
  • 1,531
  • 2
  • 17
  • 31
3
votes
1 answer

Inlining a cdef method from a cdef class from another cython package

I have cython a class which looks like this: cdef class Cls: cdef func1(self): pass If I use this class in another library, will I be able to inline func1 which is a class method? Or should I find a way around it (by creating a func…
The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
3
votes
5 answers

Convert String to 64bit integer mapping characters to custom two-bit values mapping

I am trying to map a string of characters (A, T, C, G) into a 64 bit integer where each letter is represented as two bits using this mapping: mapping = {'A': 0b00, 'C': 0b01, 'G': 0b10, 'T': 0b11} The "sequence" string will not be longer than 28…
3
votes
2 answers

Fastest way to sort in Python (no cython)

I have a problem where I've to sort a very big array(shape - 7900000X4X4) with a custom function. I used sorted but it took more than 1 hour to sort. My code was something like this. def compare(x,y): print('DD '+str(x[0])) …
Ayush Garg
  • 33
  • 3
3
votes
1 answer

How to make cython function accept float or double array input?

Suppose I have the following (MCVE...) cython function cimport cython from scipy.linalg.cython_blas cimport dnrm2 cpdef double func(int n, double[:] x): cdef int inc = 1 return dnrm2(&n, &x[0], &inc) Then, I cannot call it on a np.float32…
P. Camilleri
  • 12,664
  • 7
  • 41
  • 76
3
votes
1 answer

Runtime generation and compilation of Cython functions

A brief version Is there an easy way to compile a Cython function in runtime given the code of the function as a string? In details I have a parametrized subroutine e.g. cdef algo(x, params) the algorithm performs a relatively small number of…
Maxim
  • 61
  • 5
3
votes
1 answer

How to build gdb under pyenv environment

I'm trying to build gdb with python 2.7.12 installed by pyenv. Python install command was pyenv install 2.7.12 -k Then I executed commands like below to build gdb. wget ftp://sourceware.org/pub/gdb/releases/gdb-8.1.tar.gz tar xvf gdb-8.1.tar.gz cd…
fx-kirin
  • 1,906
  • 1
  • 20
  • 33
3
votes
1 answer

Cython: How to declare default values in pxd files

I have a function I'd like to abstract into an importable module: cdef list generate_random_vectors(int num_vectors, int length, float scale=1): cdef list return_list np.random.seed() return_list = [] for n in range(num_vectors): …
David R
  • 994
  • 1
  • 11
  • 27
3
votes
2 answers

How to access the asyncio / uvloop loop from C

I'm completely new to python, but have an async python app using uvloop which uses a C api module I created which also needs access to the async loop. 1) asyncio does not yet have a c-api for this? Any hacks to get an event loop usable in C? Is…
Craftables
  • 236
  • 1
  • 8
3
votes
1 answer

cProfile has no attribute runctx

I am trying to learn my way around Cython, and I am following the official documentation. Recently, i tried to do the tutorial provided in "http://docs.cython.org/en/latest/src/tutorial/profiling_tutorial.html". The objective here is to profile a…
Rafael Marques
  • 1,335
  • 4
  • 22
  • 35