将 Numpy 数组复制到内存视图

Copy Numpy array to a memoryview

我在 numpy 数组上有一个 memoryview,我想使用 memoryview:[=22= 将另一个 numpy 数组的内容复制到其中]

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)

但为什么这不可能呢?它让我告诉

TypeError: only length-1 arrays can be converted to Python scalars Blockquote

如果我从 memoryview 复制到 memoryview,或者从 numpy 数组复制到 numpy 数组,它工作正常,但是如何从numpy 数组到 memoryview?

这些作业有效:

cdef double[:,::1] test2d = np.array([[0,1],[2,3],[4,5]], dtype=np.double)
cdef double[:,::1] temp = np.array([[4,5],[6,7]], dtype=np.double)
test2d[...] = 4
test2d[:,1] = np.array([5],dtype=np.double)
test2d[1:,:] = temp
print np.asarray(test2d)

正在显示

[[ 4.  5.]
 [ 4.  5.]
 [ 6.  7.]]

我在 添加了一个在缩进上下文中使用此内存视图 'buffer' 方法的答案。

cpdef int testfunc1c(np.ndarray[np.float_t, ndim=2] A,
                    double [:,:] BView) except -1:
    cdef double[:,:] CView
    if np.isnan(A).any():
        return -1
    else:
        CView = la.inv(A)
        BView[...] = CView
        return 1

它没有执行其他发帖者想要的无副本缓冲区分配,但它仍然是一个高效的 memoryview 副本。