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

Passing a function as a class attribute of a Cython class

I'm trying to define a Cython class that accepts a function as one of the class attribute in the __init__ function. I followed this question and tried the following ctypedef int (*f_type)(int, int) cdef class ClassWFunction: cdef f_type f …
cd98
  • 3,442
  • 2
  • 35
  • 51
3
votes
1 answer

Passing C++ abstract class as argument to Cython

If I have an abstract base class and a derived class that inherits from it but I have another class that takes the abstract base class object as an argument, how should I go about wrapping it? class A { public: A(int x, int y); virtual int…
user279545
  • 123
  • 1
  • 8
3
votes
1 answer

Cython - error when trying to access contents of pointer to struct

I have a cdefed class in Cython which looks very similar to this: cdef class AprilTagDetector: cdef capriltag.apriltag_detector_t* _apriltag_detector def __cinit__(self): self._apriltag_detector =…
Liam Marshall
  • 1,353
  • 1
  • 12
  • 21
3
votes
1 answer

List comprehension with cython

I am trying to speed up my Python code with Cython, and so far it is working great. I am having however one single problem: dealing with lists. Using cython -a myscript.pyx, I can see that the only parts of my code that call Python routines are when…
Marlon
  • 47
  • 2
  • 7
3
votes
0 answers

conflict between fortran+iso_c_binding (via ctypes or cython) and matplotlib when reading namelist [only with python Anaconda!!]

[EDIT: the problem only applies with python anaconda, not with standard /usr/bin/python2.7] [FYI: the gist referred to in this post can still be useful for anyone trying to use fortran with ctypes or cython, credit to…
Mahé
  • 445
  • 4
  • 9
3
votes
2 answers

How can I set a PyObject* pointer to None?

I want to use a pointer to change the value of a python object, here is what I tried: This works: def main(): cdef int i cdef int* iptr i = 5 iptr = &i iptr[0] = 1 print(i) # this works, prints 1 and this: cdef class…
Augusto Hack
  • 2,032
  • 18
  • 35
3
votes
0 answers

Windows Cython Python Code then Compile Static Executable

I write a lot of short utility python scripts that eventually make their way to people that are not computer literate and it ends up being quite challenging to get them to install python with the correct modules. I do not want to use something like…
ScottB
  • 31
  • 2
3
votes
1 answer

Wrapping custom type C++ pointer in Cython

What is the best way to wrap a custom type C++ pointer using Cython? For example: import numpy as np cimport numpy as np cdef extern from "A_c.h" cdef cppclass A: A(); void Foo(A* vec); cdef class pyA: cdef A *thisptr …
user279545
  • 123
  • 1
  • 8
3
votes
2 answers

Acessing data buffer of typed memoryview in Cython

I have a function expecting array pointers in Cython, e.g. with the signature cdef void foo(DTYPE_t* x) and a function which receives a typed memoryview from which I would like to call the first function, e.g.: def bar(DTYPE_t[:,::1] X not None): …
loli
  • 395
  • 3
  • 10
3
votes
1 answer

Fortran like array slicing in Cython

Im looking for a concise and efficient way to take multidimensional slices of arrays, perform scalar and matrix arithmetic on on those slices, and then ultimately save the resulting array as a slice in another array. You can do this well in fortran…
Dave Butler
  • 1,646
  • 1
  • 12
  • 18
3
votes
0 answers

Pass Numpy array to C 2D array in pointer to pointer format using Cython

I have C code that I am currently incorporating into Python using Cython. The C code deals with 2D arrays in pointer to pointer format i.e. allocating memory for a 3x3 array is done as follows float **A = (float **)malloc(3*sizeof(float *)); for…
rwolst
  • 12,904
  • 16
  • 54
  • 75
3
votes
1 answer

Calling PyArray_SearchSorted from Cython -- 3 or 4 arguments?

I'm trying to use PyArray_SearchSorted using NumPy's C API from Cython. When call it like PyArray_SearchSorted(values, point, NPY_SEARCHLEFT) I get the GCC error: error: too few arguments to function call, expected 4, have 3. On the other hand, when…
shoyer
  • 9,165
  • 1
  • 37
  • 55
3
votes
1 answer

Cython: how to resolve TypeError: Cannot convert memoryviewslice to numpy.ndarray?

Inside a random_stuff_printer.pyx file, I have a cdef function that looks something like this: cdef np.ndarray[np.float64_t, ndim=4] randomizer(): return np.random.random((4, 4, 4, 4)) Then I have a def function that looks like this, inside the…
bzm3r
  • 3,113
  • 6
  • 34
  • 67
3
votes
1 answer

ValueError: Buffer dtype mismatch, expected 'float' but got 'double'

I compiled a Cython script and run it. When running it I got an error from the following line of code: cdef np.ndarray[float] vola = np.empty(n, dtype=float) Why is that? I want to fill the array vola with float values later on, so I want to…
user3276418
  • 1,777
  • 4
  • 20
  • 29
3
votes
1 answer

Complex valued calculations using Cython

I am trying to do complex number calculations using cython. In the example code I would like to calculate the complex exponential function of a complex number. The problem is that I do not know how to multiply my integer by the imaginary…
laolux
  • 1,445
  • 1
  • 17
  • 28
1 2 3
99
100