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

Why can I compile as C but not as C++ with Cython on Mac OS X

I am trying to figure out how to use C/C++ code in python using Cython. I can get the following example working as C code: sum.h: #ifndef MY_SUM_H_ #define MY_SUM_H_ int my_sum(int a, int b); #endif sum.c: int my_sum(int a, int b){ return a +…
Jens Renders
  • 496
  • 1
  • 4
  • 17
3
votes
0 answers

How to write functions for computing matrix inverse and solve in cython using lapack, without python objects?

I am trying to write Cython functions to compute inverses and obtain solutions to linear systems. I know this is what Numpy does. But I need use these functions in another loop I'm writing in Cython. And so, I don't want to use Python objects in the…
jtorca
  • 1,531
  • 2
  • 17
  • 31
3
votes
1 answer

Cython: prange is repeating not parallelizing

I have the following simple Cython function for a parallel reduction: # cython: boundscheck = False # cython: initializedcheck = False # cython: wraparound = False # cython: cdivision = True # cython: language_level = 3 from cython.parallel import…
aschein
  • 41
  • 3
3
votes
0 answers

torch NameError: name '_C' is not defined

I'm getting the following error when trying to import torch in Jupyter: --------------------------------------------------------------------------- NameError Traceback (most recent call last)
Zahra
  • 6,798
  • 9
  • 51
  • 76
3
votes
1 answer

Cython: understanding a typed memoryview with a indirect_contignuous memory layout

I want to understand more about Cython's awesome typed-memoryviews and the memory layout indirect_contiguous. According to the documentation indirect_contiguous is used when "the list of pointers is contiguous". There's also an example usage: #…
brain4711
  • 123
  • 9
3
votes
2 answers

indexing Cython memoryview using memoryview of ints

Using Cython, I try to do this: cpdef myFun(double[:] array): cdef int[:] sortIndices = np.argsort(array, kind='mergesort') array = array[sortIndices] The compiler complains: Invalid index for memoryview specified, type int[:] How do I…
PDiracDelta
  • 2,348
  • 5
  • 21
  • 43
3
votes
1 answer

Pickling Cython decorated function results in PicklingError

I have the following code: def decorator(func): @functools.wraps(func) def other_func(): print('other func') return other_func @decorator def func(): pass If I try to pickle func everything works. However if I compile the…
stefano1
  • 161
  • 10
3
votes
2 answers

specify *.pyd output path?

In this post, the answer is for the output path of .c files. I want to know how can I output all the .pyd file to a specific directory (by default, it mingles with the source code), for example, the directory pyd/ in the source code.
an offer can't refuse
  • 4,245
  • 5
  • 30
  • 50
3
votes
1 answer

How to prevent a __Pyx_MemoryView_Len(__pyx_v_a) Python Interaction in Cython

I have a simple Cython function, which gets the length of a memoryview: cdef int get_length(int[:] a): return len(a) I am compiling the compiling the code with the annotate=True directive, to let me see where Cython has some Python…
Ginger
  • 8,320
  • 12
  • 56
  • 99
3
votes
1 answer

Does Cython compile imported modules as part of the binary?

I'm just now reading into cython and I'm wondering if cython compiles imported modules as part of the executable of if you still need to have the modules installed on the target machine to run the cython binary.
Steven Lutz
  • 467
  • 1
  • 6
  • 16
3
votes
3 answers

Optimizing my Cython/Numpy code? Only a 30% performance gain so far

Is there anything I've forgotten to do here in order to speed things up a bit? I'm trying to implement an algorithm described in a book called Tuning Timbre Spectrum Scale. Also---if all else fails, is there a way for me to just write this part of…
Chironex
  • 415
  • 6
  • 15
3
votes
1 answer

NumPy: how to left join arrays with duplicates

To use Cython, I need to convert df1.merge(df2, how='left') (using Pandas) to plain NumPy, while I found numpy.lib.recfunctions.join_by(key, r1, r2, jointype='leftouter') doesn't support any duplicates along key. Is there any way to solve it?
Naive
  • 475
  • 1
  • 6
  • 14
3
votes
1 answer

Missing numpy attribute when using Cython

I have a very simply cython module called empty_test.pyx: cimport numpy as cnp cpdef return_empty(): return cnp.empty(0, dtype=np.int32) When I try to run return_empty I get this error: empty_test.pyx:5:14: cimported module has no attribute…
Ginger
  • 8,320
  • 12
  • 56
  • 99
3
votes
3 answers

Link Cython-wrapped C functions against BLAS from NumPy

I want to use inside a Cython extension some C functions defined in .c files that uses BLAS subroutines, e.g. cfile.c double ddot(int *N, double *DX, int *INCX, double *DY, int *INCY); double call_ddot(double* a, double* b, int n){ int one =…
anymous.asker
  • 1,179
  • 9
  • 14
3
votes
1 answer

Multiple inheritance of cython cdef classes

I have some classes implemented as cdef class in cython. In client python code, I would like to compose the classes with multiple inheritance, but I'm getting a type error. Here is a minimal reproducible example: In [1]: %load_ext cython In [2]:…
Stephen
  • 2,613
  • 1
  • 24
  • 42