Intro to
Programming
with Python

Fall 2012
course
navigation

Nov 27

I've put more here that we can do today. We'll pick and choose and/or continue Thursday.
Options ------- 1. Final project questions? 2. Course evaluations (Tue or Thu) 3. Big term review. 4. Examples: 4a) scientific python 4b) Tron google competition (aichallenge.org , winter 2010) 4c) traveling salesman problem

1

Final project questions? Presentations one week from today; due a week from Friday. Discuss status.

2

Course evaluations.

3

Review: walk through everything we've done this semester ... with your help.

4

Python examples :

scientific python

Discuss numpy, matplotlib, etc; look at some examples.
Fast manipulations of big collections of numbers, including plots, transforms, programming.
Similar tools: MatLab, IDL, GDL
Numpy has several classes for collections of numbers:

While many people use python for serious numerical work, it is unfortunately true that getting all the pieces installed isn't always simple. For one thing, the current packages are a merge of several others, including numpy and scipy; for another, many of the libraries depend on C and Fortran compilers that don't always install easily. (Apple, for example, is now using their own C compiler llvm , which doesn't always behave the same way that gcc - what everyone else uses - behaves.)
One effort to build an "easy to install all-in-one" package is this one, which I'll use a demo here.
$ python >>> from pylab import * # all matplotlib.pyplot & numpy names # plot of sin(x)/x >>> dx = 0.01 >>> x = arange(dx, 4*pi, dx) # array range >>> y = sin(x)/x # element by element >>> plot(x, y) # plot of a histogram of randoms >>> randoms = randn(1e4) # 10,000 gaussian randoms (mean=0, variance=1) >>> hist(randoms, 100) # graph 'em in 100 bins # statistics >>> mean(randoms) # near 0 >>> std(y) # standard deviation (near 1) # manipulating an array >>> x.ndim 1 >>> x.shape (1256,) >>> x[3:4] array([ 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1 ]) # matrix work # *** note that numpy's, nm[i,j] is python's m[i][j] ; i=row, j=column >>> m = matrix([[1,2,3], [1,0,1], [0,2,-1]]) >>> m matrix([[1, 2, 3], [1, 0, 1], [0, 2, -1]]) >>> m[0,:] matrix([[1, 2, 3]]) >>> m[: , 0] # remember that a[:] is all elements matrix([[1], [1], [0]]) >>> m * m # matrix multiplication matrix([[ 3, 8, 2], [ 1, 4, 2], [ 2, -2, 3]]) >>> m[0:2, 0:2] matrix([[1, 2], [1, 0]]) >>> inv(m) matrix([[-0.33333333, 1.33333333, 0.33333333], [ 0.16666667, -0.16666667, 0.33333333], [ 0.33333333, -0.33333333, -0.33333333]]) >>>

Tron robots

from a previous year's programming workshop
Browse through a bit of the code:
Run an example from notes on my laptop at /Users/mahoney/academics/term/2010-01-spring/programming_workshop/tron/

traveling salesman problem

Some code I wrote while looking at a classic CS problem with a plan student several years ago (attached).
http://cs.marlboro.edu/ courses/ fall2012/python/ notes/ Nov_27
last modified Tuesday November 27 2012 10:20 am EST

attachments [paper clip]

     name last modified size
[DIR]traveling_salesman/ Nov 26 2012 9:23 pm -