Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax issue #1

Open
tharun2011 opened this issue Feb 15, 2016 · 1 comment
Open

Syntax issue #1

tharun2011 opened this issue Feb 15, 2016 · 1 comment

Comments

@tharun2011
Copy link

What does this statement mean ' cdef double[:, ::1] Y = Y_np ' ? Why can't we directly use Y_np?
In line 338, Y_np -= np.mean(Y_np, 0). Shouldn't this be Y instead of Y_np?

@gcr
Copy link
Contributor

gcr commented Feb 15, 2016

Ah, Y_np is a numpy array and Y is a special Typed Memoryview object. That statement defines Y as a new Typed Memoryview that refers to the same data that Y_np refers to. Cython uses Memoryviews to refer to a structured block of memory. They can be backed by Numpy arrays, or anything else that implements the buffer interface (for example, i think you can convert ordinary strings to byte memoryviews)

We do it this way because indexing elements of a Typed Memoryview is much faster than indexing numpy arrays. Memoryviews can be indexed with pointer arithmetic, but Numpy indexing requires the slow Python interpreter to dispatch to Numpy's Array.__get__() Python call, then to the Numpy C implementation, then back to the Python interpreter, then back to the Cython program.

However, Memoryviews don't have defined arithmetic operations. If you want to subtract something from all the elements, you have to write the explicit loop yourself. So, we sometimes use Y_np -= np.mean(...).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants