Let\'s say I want to implement Numpy\'s
x[:] += 1
in Cython. I could write
@cython.boundscheck(False)
@cython.wraparoundcheck(F
With numpy.ravel
numpy.ravel(a, order='C')
Return a contiguous flattened array.
A 1-D array, containing the elements of the input, is returned. A copy is made only if needed.
@cython.boundscheck(False)
@cython.wraparound(False)
def add1_ravel(np.ndarray xs):
cdef unsigned long i
cdef double[::1] aview
narray = xs.ravel()
aview = narray
for i in range(aview.shape[0]):
aview[i] += 1
# return xs if the memory is shared
if not narray.flags['OWNDATA'] or np.may_share_memory(xs, narray):
return xs
# otherwise new array reshaped
shape = tuple(xs.shape[i] for i in range(xs.ndim))
return narray.reshape(shape)