Saturday, May 12, 2012

pyglet Tile Engine Progress

This is just to let you know that I'm still working on the pyglet tile engine, here and there, and that it's making my brain hurt.

Reading up on some of the more advanced features of Numpy makes my brain hurt in a different way.  Whence comes the mindpain?  This is legal:


>>>import numpy
>>>testarray = numpy.array([17,23,94,81,3,76,12,52,80,35,102,69,52])
>>>boolarray = testarray > 51

Now, boolarray is a parallel array to testarray, containing only booleans.  In each slot in boolarray that matches testarray is True if the condition was true, and False if the condition was false.

What's more, we can now do this:
>>>testarray[boolarray]
array([ 94,  81,  76,  52,  80, 102,  69,  52])

That is, we can index an array with a parallel boolean array.  And the resulting array object isn't actually a separate array, but a view into the array.  If we change an element in this new array, what gets changed is the object in the original array.

Some similar things can be done in stock Python, I gather, using moderate-level Pythonic mojo like list comprehensions, but doing it using Numpy, if I'm understanding this correctly, has the advantage of being optimized.  Access to Numpy arrays benefit from all being the same type of data; behind the scenes, indexes into these arrays are done using C-style pointer arithmetic instead of Python list redirections.  Particularly, performing an action over every element in a Numpy array is supposedly fairly quick compared to the same thing in Python... which might provide a clue as to why I'm researching this feature.


No comments:

Post a Comment