More pythonic math

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

More pythonic math

Kirby Urner

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

Here'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)
 >>> factorial(100)

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
 >>> myset = string.uppercase[:26]+' '
 >>> myset
 >>> from random import shuffle
 >>> domain = list(myset)
 >>> shuffle(myset)

 Traceback (most recent call last):
 File "<pyshell#15>", line 1, in -toplevel-
 File "c:\python24\lib\", line 269, in shuffle
 x[i], x[j] = x[j], x[i]
 TypeError: object does not support item assignment
 >>> shuffle(list(myset))
 >>> myset
 >>> 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'}
 >>> 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-
 TypeError: encrypt() takes exactly 2 arguments (1 given)
 >>> encrypt(p, myfunc)

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

We 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]

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.


[1] added paragraph for edu-sig edition
[2] this was a separate post, originally.

Edu-sig mailing list
[hidden email]