1

Consider the following example:

cdef test_function():
    cdef:
        double[:] p1 = np.array([3.2, 2.1])
        double[:] p2 = np.array([0.9, 6.])

    return p1-p2

If used, it returns the following error:

Error compiling Cython file:
------------------------------------------------------------
...
cdef test_function():
    cdef:
        double[:] p1 = np.array([3.2, 2.1])
        double[:] p2 = np.array([0.9, 6.])

    return p1-p2
            ^
------------------------------------------------------------

cython_cell_v3.pyx:354:13: Invalid operand types for '-' (double[:]; double[:])

If I am using numpy arrays to initialize the memory view, how can I go about using its functionality? Do I have to somehow do some dereferencing on the memoryviews?

0

1 Answer 1

2

This works:

cpdef test_function():
    cdef:
        double[:] p1 = np.array([3.2, 2.1])
        double[:] p2 = np.array([0.9, 6.])

    # return p1-p2
    cdef int I
    I = p1.shape[0]
    for i in range(I):
        p1[i] -= p2[i]
    return np.asarray(p1)
print "Test _function", test_function()

I iterate on the arrays as though they were 'c' arrays. And without the final np.asarray, it will just display

>>> memview.test_function()
<MemoryView of 'ndarray' at 0xb60e772c>

See also the example in http://docs.cython.org/src/userguide/memoryviews.html#comparison-to-the-old-buffer-support


I tried a different function:

cpdef test_function1(x):
    cdef:
        int i, N = x.shape[0]
        double[:] p1 = x
    for i in range(N):
        p1[i] *= p1[i]
    return np.asarray(p1)*2

x = np.arange(10.)
print "test_function1 return", test_function1(x)
print "x after test_function1", x

As expected, after the function x is x**2. But what the function returns is 2*x**2.

I modify p1 directly, but end up modifying x as well. I think of p1 as a view of x, but one with reduced functionality. np.asarray(p1) gives it a numpy functionality, so I can perform an array * on it and return the result (without further modifying x).

If instead I'd finished the function with:

out = np.asarray(p1)
out *= 2
return out 

I end up modifying the original x as well. out is a numpy view on x. out behaves like an array because it is one, not because of some distant link to x.

Sign up to request clarification or add additional context in comments.

3 Comments

yes; I'm correcting it. I was paying more attention as to whether I needed to create a return array or memory view than to arithmetic operator.
I don't know if I can accept this as an answer simply because I wasn't looking for any solution, but in particular I was wondering how typed memoryviews of numpy arrays could still remember that they were numpy arrays?
I added a function that shows the memoryview is a view with reduced functionality.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.