[Edu-sig] scientific computing in Python

kirby urner kirby.urner at gmail.com
Fri Apr 30 08:21:56 CEST 2010


My daughter got a hand-me-down GeForce GS8400 from a friend
at school.  That's like a $45 video card, yet is presumably advanced
enough to support Cuda, and therefore PyCuda.  What's that?
http://documen.tician.de/pycuda/

We keep getting back to numpy arrays as a basis for computer
graphics.  Linear algebra with numpy.matrix looks like a good
way to go in a digital math class.

>From Cut-the-Knot, this 9x9 invertible matrix:

>>> import numpy as np

A result of cutting and pasting:

>>> m = """1 1 1 1 1 0 1 0 0
 0 1 0 1 0 1 1 0 1
 1 1 1 0 1 1 1 0 1
 0 1 1 1 0 0 0 1 1
 0 1 0 1 1 1 0 1 0
 1 1 0 0 0 1 1 1 0
 1 0 0 1 1 0 1 1 1
 1 0 1 1 0 1 0 1 0
 0 0 1 0 1 1 1 1 1""".split()

Convert to integers:

>>> mat = np.matrix(np.array([int(x) for x in m]).reshape(9,9))
>>> mat
matrix([[1, 1, 1, 1, 1, 0, 1, 0, 0],
        [0, 1, 0, 1, 0, 1, 1, 0, 1],
        [1, 1, 1, 0, 1, 1, 1, 0, 1],
        [0, 1, 1, 1, 0, 0, 0, 1, 1],
        [0, 1, 0, 1, 1, 1, 0, 1, 0],
        [1, 1, 0, 0, 0, 1, 1, 1, 0],
        [1, 0, 0, 1, 1, 0, 1, 1, 1],
        [1, 0, 1, 1, 0, 1, 0, 1, 0],
        [0, 0, 1, 0, 1, 1, 1, 1, 1]])

Getting the inverse is now as simple as:

>>> print np.round(mat.I, 2)  # that's mat.I with I for Inverse

[[-0.17 -0.23  0.48 -0.07 -0.12  0.11  0.32  0.27 -0.51]
 [ 0.07 -0.07  0.2   0.33  0.2   0.27 -0.2  -0.33 -0.27]
 [ 0.32 -0.12 -0.04  0.2  -0.24 -0.12 -0.36  0.2   0.32]
 [ 0.23  0.37 -0.32 -0.07  0.08 -0.29  0.12  0.27 -0.11]
 [ 0.08 -0.28  0.24 -0.2   0.44 -0.28  0.16 -0.2   0.08]
 [-0.27  0.27  0.2  -0.33  0.2  -0.07 -0.2   0.33  0.07]
 [ 0.48  0.32 -0.56 -0.2  -0.36  0.32 -0.04 -0.2   0.48]
 [-0.11 -0.29 -0.32  0.27  0.08  0.37  0.12 -0.07  0.23]
 [-0.51  0.11  0.48  0.27 -0.12 -0.23  0.32 -0.07 -0.17]]

http://www.cut-the-knot.org/explain_game.shtml

Connecting the dots twixt Pycuda and numpy:
http://documen.tician.de/pycuda/array.html#pycuda.gpuarray.GPUArray

Perry Greenfield has had much to do with getting Python
going at the Space Telescope Science Institute (STScI).
We invited him to give a lightning talk during my class,
but he was stuck in Amsterdam owing to the volcanic
ash problem in Iceland.

http://www.scipy.org/wikis/topical_software/Tutorial

http://stsdas.stsci.edu/perry/pydatatut.pdf
(144 pages by Perry Greenfield and Robert Jedrzejewski
-- about analyzing astronomical imagery in Python with
numpy, pyfits, numdisplay etc.).

Numpy has a lot in common with IDL, an inhouse analysis
language for many on the Hubble project.  In sharing
research data with the wider public however, it's easier if the
files might be worked on by free and open source software.
IDL ain't cheap.

http://www.cfa.harvard.edu/~jbattat/computer/python/science/idl-numpy.html

Speaking of Cuda (used to code for the GPU vs. the CPU), there's
a PyopenCL as well, also by Andreas Klöckner.

http://developer.amd.com/GPU/ATISTREAMSDK/pages/TutorialOpenCL.aspx

Kirby


More information about the Edu-sig mailing list