2

I am trying to use cython to work on and output (numpy) array. I have read this post which explains how to do it using cpython array. For example:

from cpython.array import array, clone
def int[:] foo1d(int m):
    cdef int[:] mv;
    cdef array template = array('i', [])
    mv = clone(template, m, True)
    return mv

However I am wondering how to do this for multidimensional typed memoryview. For example, I can do this with numpy:

import numpy as np
cimport numpy as np

cdef int[:,:] foo2d(int m, int n):
    cdef int[:,:] mv;
    mv = np.zeros((m, n), dtype=int)
    return mv

But I couldn't find how to do this with cpython.array. I appreciate any help and suggestions.

Dongyao Li
  • 21
  • 1
  • Python's array.arrays are one-dimensional (numpy's array are ndarrays, i.e. multidimensional) and thus not support more than one dimension. You could however use Cython's arrays: https://cython.readthedocs.io/en/latest/src/userguide/memoryviews.html#cython-arrays – ead May 26 '20 at 08:23
  • Got it. Thanks @ead ! – Dongyao Li May 26 '20 at 17:18

0 Answers0