memoryview

Copy Numpy array to a memoryview

僤鯓⒐⒋嵵緔 提交于 2019-12-04 10:08:56
I have a memoryview on a numpy array and want to copy the content of another numpy array into it by using this memoryview : import numpy as np cimport numpy as np cdef double[:,::1] test = np.array([[0,1],[2,3]], dtype=np.double) test[...] = np.array([[4,5],[6,7]], dtype=np.double) But why is this not possible? It keeps me telling TypeError: only length-1 arrays can be converted to Python scalars Blockquote It works fine if I copy from a memoryview to a memoryview , or from a numpy array to a numpy array, but how to copy from a numpy array to a memoryview ? hpaulj These assignments work: cdef

Cython typed memoryviews: what they really are?

隐身守侯 提交于 2019-12-04 09:07:14
问题 The Cython documentation explains very well what they allow for, how you can declare them, and how to use them. However, it is still not clear to me what they really are. For example, a simple assignment from a numpy array like this: my_arr = np.empty(10, np.int32) cdef int [:] new_arr = my_arr can make the accessing/assignment of my_arr faster. What is it happening behind the scenes? Numpy should already allocate the elements in memory in a contiguous fashion, so what's the deal with

Why does not my program go into infinite loop when array out of bounds occur in C

折月煮酒 提交于 2019-12-04 05:43:33
问题 int main(){ int i; int arr[4]; for(int i=0; i<=4; i++){ arr[i] = 0; } return 0; } I watched a video on youtube of CS107(lecture 13) in which this example is shown and told why the above program will lead to infinite loop by showing memory diagrams. arr[4] goes out of bounds and should lead to an address where i is stored and changing the value of i back to 0 hence leading to infinite loop. But when I tried to run this on my mac using gcc compiler, for loop executed (checked by inserting

How to use Cython typed memoryviews to accept strings from Python?

心不动则不痛 提交于 2019-12-03 16:32:31
问题 How can I write a Cython function that takes a byte string object (a normal string, a bytearray, or another object that follows the buffer protocol) as a typed memoryview? According to the Unicode and Passing Strings Cython tutorial page, the following should work: cpdef object printbuf(unsigned char[:] buf): chars = [chr(x) for x in buf] print repr(''.join(chars)) It does work for bytearrays and other writable buffers: $ python -c 'import test; test.printbuf(bytearray("test\0ing"))' 'test

Buffers and Memoryview Objects explained for the non-C programmer

可紊 提交于 2019-12-03 12:58:15
Python 2.7 has introduced a new API for buffers and memoryview objects . I read the documentation on them and I think I got the basic concept (accessing the internal data of an object in a raw form without copying it, which I suppose means a "faster and less memory-hungry" way to get object data), but to really understand the documentation, the reader should have a knowledge of C that is beyond the one I have. I would be very grateful if somebody would take the time to: explain buffers and memoryview objects in "layman terms" and describe a scenario in which using buffers and memoryview

Cython: Convert memory view to NumPy array

无人久伴 提交于 2019-12-03 10:34:07
How to convert a typed memoryview to an NumPy array in cython? The docs have cimport numpy as np import numpy as np numpy_array = np.asarray(<np.int32_t[:10, :10]> my_pointer) I took this for my case np.asarray(<np.float_t[:, :]> my_memview) Using this the compiler tells me: Can only create cython.array from pointer or array Copying or not is not so decicive. I couldn't find any help on this. You should just be able to use np.asarray directly on the memoryview itself, so something like: np.asarray(my_memview) should work. For example if your cython source file contains this: import numpy as np

Cython: Create memoryview without NumPy array?

空扰寡人 提交于 2019-12-03 03:45:20
Since I found memory-views handy and fast, I try to avoid creating NumPy arrays in cython and work with the views of the given arrays. However, sometimes it cannot be avoided, not to alter an existing array but create a new one. In upper functions this is not noticeable, but in often called subroutines it is. Consider the following function #@cython.profile(False) @cython.boundscheck(False) @cython.wraparound(False) @cython.nonecheck(False) cdef double [:] vec_eq(double [:] v1, int [:] v2, int cond): ''' Function output corresponds to v1[v2 == cond]''' cdef unsigned int n = v1.shape[0] cdef

Why does not my program go into infinite loop when array out of bounds occur in C

岁酱吖の 提交于 2019-12-02 07:20:48
int main(){ int i; int arr[4]; for(int i=0; i<=4; i++){ arr[i] = 0; } return 0; } I watched a video on youtube of CS107(lecture 13) in which this example is shown and told why the above program will lead to infinite loop by showing memory diagrams. arr[4] goes out of bounds and should lead to an address where i is stored and changing the value of i back to 0 hence leading to infinite loop. But when I tried to run this on my mac using gcc compiler, for loop executed (checked by inserting printf) 5 times. i.e for the value of i = 0,1,2,3,4. When there is undefined behavior you can't expect a

Sort memoryview in Cython

帅比萌擦擦* 提交于 2019-12-01 09:19:58
How can I sort a memoryview in-place in Cython? Is there a built-in function that can do it? Right now I have to use a numpy array instead and use numpy 's sort, which is very slow. To follow up on my comment, here are 3 options (numpy and a C and C++ standard library option) from libcpp.algorithm cimport sort from libc.stdlib cimport qsort import numpy as np def sort_numpy(double[:] a, kind): np.asarray(a).sort(kind=kind) # needs to be compiled with C++ def sort_cpp(double[::1] a): # a must be c continuous (enforced with [::1]) sort(&a[0], (&a[0]) + a.shape[0]) # The C version requires a

Sort memoryview in Cython

为君一笑 提交于 2019-12-01 07:31:28
问题 How can I sort a memoryview in-place in Cython? Is there a built-in function that can do it? Right now I have to use a numpy array instead and use numpy 's sort, which is very slow. 回答1: To follow up on my comment, here are 3 options (numpy and a C and C++ standard library option) from libcpp.algorithm cimport sort from libc.stdlib cimport qsort import numpy as np def sort_numpy(double[:] a, kind): np.asarray(a).sort(kind=kind) # needs to be compiled with C++ def sort_cpp(double[::1] a): # a