Introduction to Mathematical and Scientific Programming
MATH 1MP3

Winter 2015
Functions
the most important tool for structuring programs
allows modularity
basic definition: def function_name(args): plus indented code block
when function is called, go to the function, with the arguments, run code until you
hit return() (return None
Introduction to Mathematical and Scientific Programming
MATH 1MP3

Winter 2015
The Euclidean algorithm
definition and history
oldest known algorithm (Euclid, c. 300 BCE) (algorithm from alKhwrizm, c.
780850 CE)
find greatest common divisor of two integers: gcd(a,b) is the largest
integer c such that c divides both a and b evenly.
Introduction to Mathematical and Scientific Programming
MATH 1MP3

Winter 2015
Tuples
simple; nonmutable version of lists
faster, safer
can be expressed as x, y, z (or (x,y,z), probably clearer)
empty tuple: ()
tuple with one element: (x,)
can do many of the same things as with lists
x = (1,4,"a",3)
print(x[1]) # indexing
print(x[2
Introduction to Mathematical and Scientific Programming
MATH 1MP3

Winter 2015
Logistics
Homework #4 is due at midnight (i.e. 11:59:59 PM) on Weds February 11.
You must submit your homework to the Dropbox on Avenue to Learn as a single text
(.py) file containing two function definitions (int_to_roman and logist_map, as specified
bel
Introduction to Mathematical and Scientific Programming
MATH 1MP3

Winter 2015
import numpy.random as npr
from project_answers import *
# YAHTZEE
# test that choice() and randint() give same results
npr.seed(1001)
sides=6
p1 = tuple(npr.choice(range(1,sides+1),size=5)
npr.seed(1001)
p2 = tuple(npr.randint(1,sides+1,size=5)
assert(p1
Introduction to Mathematical and Scientific Programming
MATH 1MP3

Winter 2015
Basics
need to define symbols first
from sympy import *
# not usually recommended but OK here
x, y = symbols('x','y') # names don't *have* to match but should usually
# or
x = Symbol('x')
print(x)
print(y)
print(x+x+x) # surprise!
# x
# y
# 3*x
then we ca
Introduction to Mathematical and Scientific Programming
MATH 1MP3

Winter 2015
Conditionals and flow control
Conditionals: Do something if something else is true
Flow control: Go to different places in the code: especially, repeat calculations
Everything we need for interesting programs (the rest is commentary)
Technically we can co
Introduction to Mathematical and Scientific Programming
MATH 1MP3

Winter 2015
Errors
Types of errors
syntax errors vs. logic errors
failure modes:
o
obvious failure
Python crashes
machine crashes
o
program stops with an error partway through: bad mandelbrot #0
program never stops (infinite loop)
wrong answer
always
occasionally
Nex
Introduction to Mathematical and Scientific Programming
MATH 1MP3

Winter 2015
Corrections
Get the shape of an array via a.shape, not a.shape() (shape is an attribute, not
a method, of an array)
one correct way to check whether an index fits into a list/tuple/string etc. is
def index_ok(i,x):
n = len(x)
return(n <= i < n)
numpy arr
Introduction to Mathematical and Scientific Programming
MATH 1MP3

Winter 2015
Installation
If youre lucky (but probably not), you can install numpy in the usual way
open PyCharm
go to Preferences/Project: untitled/Project Interpreter
if you dont see numpy in the list of available modules, click on the + in the lower
left corner of
Introduction to Mathematical and Scientific Programming
MATH 1MP3

Winter 2015
Basic setup
create a figure: fig=plt.figure()
can include figure size figsize=(w,h), background/edge color, resolution (dpi=dots
per inch)
add a subplot (or axes): ax = fig.add_subplot(1,1,1) (rows,columns,which plot)
now can show or save the figure: fig.
Introduction to Mathematical and Scientific Programming
MATH 1MP3

Winter 2015
Instructor: Ben Bolker
o
o
http:/www.math.mcmaster.ca/bolker
o
bolker@mcmaster.ca: please include 1mp3 in Subject:
HH 314 (sometimes LSB 336); office hours TBA
TA: Jake Szamosi
o
teaching@jcszamosi.ca: please allow one full business day for response
Grad
Introduction to Mathematical and Scientific Programming
MATH 1MP3

Winter 2015
pandas stands for panel data system. Its a convenient and powerful system for
handling large, complicated data sets.
Download US measles data from Project Tycho.
read_csv reads a CSV file as a data frame; it automatically interprets the first
row as headi