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
3 answers

Pass c function as argument to python function in cython

I have the following code which works: %%cython cdef int add(int a, int b): return a+b cdef int mult(int a, int b): return a*b ctypedef int (*function_type)(int a, int b) cdef int lambda_c(function_type func, int c, int d): …
Ivan Mishalkin
  • 1,049
  • 9
  • 25
3
votes
1 answer

Cython: How to make an array of functions with different signatures

In Cython I would like to store an array of functions, but the functions don't all have the same signature. Specifically, some have two parameters and some three. I defined the following: ctypedef long (*func2param)(long param1, long…
Bruce Nielson
  • 753
  • 8
  • 23
3
votes
4 answers

(memory-)efficient operations between arbitrary columns of numpy array

I have a large 2D numpy array. I would like to be able to efficiently run row-wise operations on subsets of the columns, without copying the data. In what follows, a = np.arange(1000000).reshape(1000, 10000) and columns = np.arange(1, 1000, 2). For…
Pietro Battiston
  • 7,930
  • 3
  • 42
  • 45
3
votes
1 answer

Transfer a c++ object between cython and pybind11 using python capsules

I have two c++ libraries that expose python API but using two different frameworks(pybind11 and cython). I need to transfer an object between them (both ways) using python capsules. Since cython and pybind11 use the python capsules different ways,…
tomdol
  • 381
  • 3
  • 14
3
votes
1 answer

Correct way to check if object is decimal

For int objects one may use cimported functions from cpython like so: %%cython from decimal import Decimal from cpython cimport PyLong_Check val_decimal = Decimal(1) print(PyLong_Check(val_decimal)) False But it seems like there is no…
Xronx
  • 1,160
  • 7
  • 23
3
votes
2 answers

(Cython) Make the modulo argument optional in __pow__

I am trying to define some built-in arithmetic operations for a cdef class in a Cython file. At first I tried making the __pow__ function like so: def __pow__(self, other, modulo=None): pass But received the following error message when…
Will Da Silva
  • 6,386
  • 2
  • 27
  • 52
3
votes
1 answer

Is it possible to override a cdef method with def? Override method won't execute

I am implementing a class MyBinaryTissueClassifier that has superclass TissueClassifier (which I do not have the ability to change). I am attempting to override one of the methods in TissueClassifier in MyBinaryTissueClassifier but my implementation…
3
votes
1 answer

Does Cython container not release memory?

When I run the following code, I expect that once foo() has been executed, the memory used by it (basically to create m) would be released. However, that is not the case. To release this memory I need to restart the IPython console. %%cython #…
Manish Goel
  • 843
  • 1
  • 8
  • 21
3
votes
0 answers

What is the easiest way to parallelize a recursive code in Cython

Consider a recursive code in Cython of the following generic form: cpdef function(list L1, list L2): global R cdef int i,n #... cdef list LL1,LL2 #... # ... # core of the code # ... n= #... for i in range(n): …
3
votes
1 answer

Alternatives to using nested functions in PySpark mapPartitions when using Cython?

I have a row-wise operation I wish to perform on my dataframe which takes in some fixed variables as parameters. The only way I know how to do this is with the use of nested functions. I'm trying to use Cython to compile a portion of my code, then…
absolutelydevastated
  • 1,657
  • 1
  • 11
  • 28
3
votes
1 answer

How to optimize speed to calculate the mean along the Z axis in a 3D array? Cython vs Numpy

I am trying to speed up the calculation of the mean values along the Z axis in a 3d array. I read the documentation of cython to add types, memory views and so on, to accomplish this task. However, when I compare both: the function based on numpy…
Roger Almengor
  • 442
  • 5
  • 16
3
votes
1 answer

Function pointer in cython extension type

I am writing a cython module that provides several extension types that use optimized cdef functions. Several of these extension types (about 10, each containing about 200 lines of code) have exactly the same structure but don't call the same cdef…
Ipse Lium
  • 970
  • 1
  • 6
  • 19
3
votes
1 answer

type object 'pmdarima.arima._arima.array' has no attribute '__reduce_cython__' I am getting this error. Anyone knows how to solve this in python

type object 'pmdarima.arima._arima.array' has no attribute 'reduce_cython' I am getting the above error. Please anyone knows how to solve it.
3
votes
0 answers

How to solve or suppress wall of warnings using any pystan code

When I run any pystan code, the output is what I expect, but I get a wall of warnings. I've tried updating pystan and cython, as these are mentioned in the wall of warnings. My pystan is now version 2.17.1 and cython 0.29.2. I'm running…
Femkemilene
  • 183
  • 2
  • 15
3
votes
0 answers

Improving performance of Cython function for faster file read

I'm trying to read an ascii file(text-based) of a particular format. I have done some line-profiling and post of the time is being used in looping. I'm trying if the code inside the loop could be improved in performance. Things I've tried Faster…
Fenil
  • 396
  • 1
  • 5
  • 16