Another example of Pythonic mathematics, two posts so far at:

http://mathforum.org/kb/message.jspa?messageID=4019537&tstart=0Here's a new edition, better formatting, typo fixed:

========

I've got a gig coming up, so thought I'd do some notes here. It's a Python

class, and I'll be using math topics to motivate problem solving. For

example, there's no factorial built-in = an opportunity to program:

from operator import mul

def factorial(n):

return reduce(mul, range(1,n+1))

>>> factorial(10)

3628800

>>> factorial(100)

93326215443944152681699238856266700490715968264381621468592963

89521759999322991560894146397615651828625369792082722375825118

5210916864000000000000000000000000L

The course starts out with data structures, a foundation on which to build.

Data structures are for storing and later accessing information. In Python,

this might involve pickling, i.e. serialization of objects. Or we might

write out to text files, use a SQL engine, save to the ZODB, or pipe out

through sockets to some unspecified receiver with a TCP/IP address (an IP

number). We might use Twisted for this latter purpose.

On the math side, the data structure we call a dictionary is a collection

type known as a mapping, in that it associates or pairs keys with values. We

may pick our keys and values from the same domain and codomain, in which

case we have a mapping of maybe integers into integers, or floats into

floats, or characters into characters.

This last mapping (characters into characters) is a jumping off point to

simple encryption by means of letter substitution (we treat the space as

just one more letter):

>>> import string

>>> string.uppercase

'ABCDEFGHIJKLMNOPQRSTUVWXYZ\x8a\x8c\x8e\x9f\xc0\xc1\xc2\xc3\xc4\xc5\xc6\

xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xd9\

xda\xdb\xdc\xdd\xde'

>>> myset = string.uppercase[:26]+' '

>>> myset

'ABCDEFGHIJKLMNOPQRSTUVWXYZ '

>>> from random import shuffle

>>> domain = list(myset)

>>> shuffle(myset)

Traceback (most recent call last):

File "<pyshell#15>", line 1, in -toplevel-

shuffle(myset)

File "c:\python24\lib\random.py", line 269, in shuffle

x[i], x[j] = x[j], x[i]

TypeError: object does not support item assignment

>>> shuffle(list(myset))

>>> myset

'ABCDEFGHIJKLMNOPQRSTUVWXYZ '

>>> outputs = domain[:]

>>> shuffle(outputs)

>>> myfunc = dict(zip(domain,outputs))

>>> myfunc

{' ': 'C', 'A': 'R', 'C': 'V', 'B': 'P', 'E': 'N', 'D': 'I', 'G': 'K',

'F' : 'A', 'I': 'Q', 'H': 'Y', 'K': 'X', 'J': 'G', 'M': 'D', 'L': 'O',

'O': 'W', 'N': 'F', 'Q': 'S', 'P': 'J', 'S': ' ', 'R': 'E', 'U': 'B',

'T': 'U', 'W': 'Z', 'V': 'L', 'Y': 'H', 'X': 'M', 'Z': 'T'}

>>> p = 'THE RAIN IN SPAIN STAYS MAINLY IN THE PLAIN'

>>> def encrypt(p, subdict):

e = ''

for c in p:

e = e + subdict[c]

return e

>>> encrypt(p)

Traceback (most recent call last):

File "<pyshell#29>", line 1, in -toplevel-

encrypt(p)

TypeError: encrypt() takes exactly 2 arguments (1 given)

>>> encrypt(p, myfunc)

'UYNCERQFCQFC JRQFC URH CDRQFOHCQFCUYNCJORQF'

Where we're going with this is towards the relations and functions concepts.

Relations pair members in two sets, perhaps the same key (domain element)

with two or more different values (range element), while functions guarantee

a unique and determined value for a given key.

OK for Relation, but not Function:

R(r) --> b

R(r) --> c

OK for Function, and for Relation:

f(r) --> b

f(s) --> b

In addition to defining functions by roster (explicit pairing), we'll be

defining functions according to rules, which is where we transition from

exploring data structures to writing short top-level functions. Or

functions will often be about sequences, e.g. figurate and polyhedral,

Fibonacci, maybe chaotic.[1]

===== ADDENDUM: Note to the Teacher ====== [2]

The Python community is in a sense an ethnicity, in that there's a lot of

shared jargon and concepts. This is true in mathematics as well

(sub-ethnicities within ethnicities, e.g. topologists). Per an earlier

thread here at the Math Forum: all math is ethnomath.

This math/CS hybrid involves mixing heroes from disparate domains. We learn

about Descartes and Fermat, sure, but also about Von Neumann, Dijkstra, Ada

Byron.

http://ei.cs.vt.edu/~history/VonNeumann.htmlhttp://en.wikipedia.org/wiki/Edsger_Dijkstrahttp://www.google.com/search?hl=en&q=Ada+Byron&btnG=Google+SearchWe will also explore the historical beginnings of the World Wide Web: e.g.

Vannevar Bush (MEMEX), Ted Nelson (Xanadu), and Tim Berners-Lee (W3).[3]

http://en.wikipedia.org/wiki/Vannevar_Bushhttp://xanadu.com/http://www.w3.org/People/Berners-Lee/I'm suggesting this as an alternative to precalc/calc, i.e. a discrete math

track in K-12, perhaps leading to a different mix of majors, but still

college prep and still an on ramp into engineering and the sciences for the

most part, although these skills also transfer to art/design majors in other

departments as well.

Kirby

[1] added paragraph for edu-sig edition

[2] this was a separate post, originally.

[3]

http://worldgame.blogspot.com/2004/12/dj-vu.html_______________________________________________

Edu-sig mailing list

[hidden email]
http://mail.python.org/mailman/listinfo/edu-sig