cython

Cython using gmp arithmetic

夙愿已清 提交于 2021-01-28 09:15:27
问题 I'm trying to implement a simple code in cython using Jupyter notebook (I use python 2) and using gmp arithmetic in order to handle very large integers. I'm not a gmp/cython expert. My question is : how do I print the value a in the function fib(). The following code returns {}. As fas as I can understand it has to do with stdout. For instance I tried gmp_printf and it didn't work. %%cython --link-args=-lgmp cdef extern from "gmp.h": ctypedef struct mpz_t: pass cdef void mpz_init(mpz_t) cdef

Return a 2D Cython pointer to Python array

橙三吉。 提交于 2021-01-28 01:32:39
问题 I am currently passing from Cython to C the following pointer of a pointer: #convert the input Python 2D array to a memory view cdef double[:,:] a_cython= np.asarray(a,order="C") #define a pointer of a pointer with dimensions of a cdef double** point_to_a = <double **>malloc(N * sizeof(double*)) #initialize the pointer if not point_to_a: raise MemoryError #try: for i in range(N): point_to_a[i] = &a_cython[i, 0] #pass this double pointer to a C function logistic_sigmoid(&point_to_a[0], N,M)

Is there a way to improve speed of parsing date for large file? [duplicate]

不想你离开。 提交于 2021-01-27 18:54:20
问题 This question already has answers here : Fast conversion of timestamps for duration calculation (2 answers) Closed 3 years ago . I am reading a big csv file which has about 1B rows. I ran into a issue with parsing the date. Python is slow in the processing. a single line in the file looks like the following, '20170427,20:52:01.510,ABC,USD/MXN,1,OFFER,19.04274,9000000,9@15@8653948257753368229,0.0\n' if I only look through the data, it takes 1 minute. t0 = datetime.datetime.now() i = 0 with

How to store a boolean mask as an attribute of a Cython class?

眉间皱痕 提交于 2021-01-27 13:34:27
问题 I failed to save a boolean mask as an attribute of a Cython class. In the real code I need this mask to perform tasks more efficiently. Here it follows a sample code: core.pyx import numpy as np cimport numpy as np cdef class MyClass(): cdef public np.uint8_t[:] mask # uint8 has the same data structure of a boolean array cdef public np.float64_t[:] data def __init__(self, size): self.data = np.random.rand(size).astype(np.float64) self.mask = np.zeros(size, np.uint8) script.py import numpy as

How to store a boolean mask as an attribute of a Cython class?

╄→尐↘猪︶ㄣ 提交于 2021-01-27 13:26:33
问题 I failed to save a boolean mask as an attribute of a Cython class. In the real code I need this mask to perform tasks more efficiently. Here it follows a sample code: core.pyx import numpy as np cimport numpy as np cdef class MyClass(): cdef public np.uint8_t[:] mask # uint8 has the same data structure of a boolean array cdef public np.float64_t[:] data def __init__(self, size): self.data = np.random.rand(size).astype(np.float64) self.mask = np.zeros(size, np.uint8) script.py import numpy as

Call cdef function by name in Cython

梦想的初衷 提交于 2021-01-27 12:42:28
问题 I have a bunch of cdef functions in Cython, that are called by a def function in a pyx file, e.g.: cdef inline void myfunc_c(...): (...) return def wrapper(...): myfunc_c(...) return This works well. But to simplify not having to have a python wrapper for each cdef function, I was trying to index the cdef functions by name, either by assigning them to a dictionary or something like: def wrapper(operation): if operation == 'my_func': func = myfunc_c func(...) return But this doesn't work.

Initialise Cython Memoryview efficiently

北慕城南 提交于 2021-01-27 07:22:28
问题 I'm currently setting my MemoryView s in my Cython pyx file as follows: @cython.boundscheck(False) cdef int[:] fill_memview(): # This happens inside a big loop so needs to be fast cdef int[:] x = np.empty(10) for i in range(10): x[i] = i return x cdef stupid_loop(): for i in range(10000): fill_memview() When I compile the pyx file with cython -a foo.pyx the line cdef int[:] x = np.empty(10) shows in the resulting annotated html file in dark yellow (meaning it has lots of Python calls slowing

Cython: why is size_t faster than int?

孤街浪徒 提交于 2021-01-27 06:09:10
问题 Changing certain Cython variables from type int to type size_t can significantly reduce some functions times (~30%), but I do not understand why. For example: cimport numpy as cnp import numpy as np def sum_int(cnp.int64_t[::1] A): cdef unsigned long s = 0 cdef int k for k in xrange(A.shape[0]): s += A[k] return s def sum_size_t(cnp.int64_t[::1] A): cdef unsigned long s = 0 cdef size_t k for k in xrange(A.shape[0]): s += A[k] return s a = np.array(range(1000000)) And the timing results: In

Convert C++ vector to numpy array in Cython without copying [duplicate]

雨燕双飞 提交于 2021-01-27 04:45:49
问题 This question already has answers here : Passing C++ vector to Numpy through Cython without copying and taking care of memory management automatically (2 answers) Closed last year . There is a C++ function that returns a vector of floats. How to convert this vector to NumPy array without copying? Now I'm doing this: cdef np.ndarray arr = np.ascontiguousarray(cpp_vector, dtype=np.float) return arr but this works very slow (assume copying occurs) on large vectors. 回答1: Casting the vector to

How to wrap C structs in Cython for use in Python?

天大地大妈咪最大 提交于 2021-01-27 04:07:33
问题 For a bit of learning experience, I'm trying to wrap a few parts of SDL (1.2.14) in Cython in an extension for Python 3.2. I am having a problem figuring out how to wrap C structs straight into Python, being able to access its attributes directly like: struct_name.attribute For example, I want to take the struct SDL_Surface: typedef struct SDL_Rect { Uint32 flags SDL_PixelFormat * format int w, h Uint16 pitch void * pixels SDL_Rect clip_rect int refcount } SDL_Rect; And be able to use it like