Lists and Loops 1 Lists in Python defining lists lists as queues and stacks inserting and removing membership and ordering lists 2 Loops in Python for and while loops the composite trapezoidal rule MCS 507 Lecture 4 Mathematical, Statistical and Scientific Software Jan Verschelde, 5 September 2012 Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 1 / 29
Lists and Loops 1 Lists in Python defining lists lists as queues and stacks inserting and removing membership and ordering lists 2 Loops in Python for and while loops the composite trapezoidal rule Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 2 / 29
lists via range With range we make a list of consecutive integers: = range(5,11) >>> type(l) <type list > [5, 6, 7, 8, 9, 10] Note: range(a,b) terminates at b - 1. The default start of range is 0: >> range(5) [0, 1, 2, 3, 4] Instead of 1 as default step, use 3: >>> range(4,4+5*3,3) [4, 7, 10, 13, 16] Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 3 / 29
selecting and assigning = range(5,10) >>> len(l) 5 >>> K = L; K [5, 6, 7, 8, 9] We can assign lists as any variable. Indexing starts at 0: [2] 7 [2] = 17 Assigning to L also changes K: [5, 6, 17, 8, 9] >>> K [5, 6, 17, 8, 9] Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 4 / 29
taking slices of lists Generating 7 integers starting at 4 with step size 5: = range(4,4+7*5,5) [4, 9, 14, 19, 24, 29, 34] A slice: [2:5] [14, 19, 24] The first and last 3 numbers are selected as [:3] [4, 9, 14] [-3:] [24, 29, 34] Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 5 / 29
Lists and Loops 1 Lists in Python defining lists lists as queues and stacks inserting and removing membership and ordering lists 2 Loops in Python for and while loops the composite trapezoidal rule Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 6 / 29
implementing a queue In a queue with first-come first-served protocol, we append items to the end and pop the front. = [].append( a ) [ a ].append( b ); L [ a, b ] >>> first = L.pop(0) >>> first a [ b ] Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 7 / 29
implementing a stack In a stack (e.g.: a pile of books), we push to the front and also pop the front. = [].insert(0, a ); L [ a ].insert(0, b ); L [ b, a ] >>> top = L.pop(0) >>> top b [ a ] Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 8 / 29
Lists and Loops 1 Lists in Python defining lists lists as queues and stacks inserting and removing membership and ordering lists 2 Loops in Python for and while loops the composite trapezoidal rule Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 9 / 29
the insert method The first argument of insert is the index of the new element in the list. After L.insert(k,i) we have: L[k] == i. = range(0,5); L [0, 1, 2, 3, 4].insert(2, a ) [0, 1, a, 2, 3, 4].insert(len(L), b ) [0, 1, a, 2, 3, 4, b ].insert(1000, c ); L [0, 1, a, 2, 3, 4, b, c ] Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 10 / 29
popping and removing Observe the difference between pop and remove: = range(0,5); L [0, 1, 2, 3, 4].remove(0) [1, 2, 3, 4].pop(0) 1 Also their arguments have different type:.pop(2) 4 [2, 3].remove(3); L [2] Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 11 / 29
deleting elements >>> x = a ; y = b ; L = [x,y] [ a, b ] >>> del L[0] [ b ] >>> x a Although the first element a of the list is deleted, we can still refer to that a via the name x Difference with remove? The del applies to any variable. Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 12 / 29
memory locations addresses and values >>> x = 3 >>> id(x) 33568624 The machine view: 33568624 address of x 3 value of x In Python, the name x refers to the object 3. x name reference 3 object Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 13 / 29
visualizing lists from scitools.lumpy import Lumpy lumpy = Lumpy() lumpy.make_reference() L0 = [1, 4, 3] L1 = L0 L2 = L1[:-1] L1[0] = 100 lumpy.object_diagram() Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 14 / 29
Lists and Loops 1 Lists in Python defining lists lists as queues and stacks inserting and removing membership and ordering lists 2 Loops in Python for and while loops the composite trapezoidal rule Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 15 / 29
testing membership The in operator returns True or False: = range(5,10); L [5, 6, 7, 8, 9] >>> 6 in L True >>> 3 in L False If e in L, then L[L.index(e)] == e:.index(6) 1 [1] 6 Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 16 / 29
ordering lists = range(1,11) >>> import random >>> random.shuffle(l) [3, 6, 9, 10, 7, 8, 1, 4, 2, 5] >>> max(l); min(l) 10 1.reverse() [5, 2, 4, 1, 8, 7, 10, 9, 6, 3].sort() [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 17 / 29
Lists and Loops 1 Lists in Python defining lists lists as queues and stacks inserting and removing membership and ordering lists 2 Loops in Python for and while loops the composite trapezoidal rule Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 18 / 29
for loops Computing the sum of all elements in a list L: e L e. s = 0 for e in L: s = s + e print s The indentation is important! To see how s evolves: s = 0 for e in L: s = s + e print s The second code fragment prints as many times as len(l), while the first code prints only once. Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 19 / 29
while loops The for loop is appropriate when the number of steps is fixed and known in advance. A while loop is more flexible. s = 0 for e in L: s = s + e is equivalent to s = 0; i = 0 while (i < len(l)): s = s + L[i] i = i + 1 The while loop continues as long as the condition is True. Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 20 / 29
Lists and Loops 1 Lists in Python defining lists lists as queues and stacks inserting and removing membership and ordering lists 2 Loops in Python for and while loops the composite trapezoidal rule Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 21 / 29
the composite trapezoidal rule To approximate the integral of a function f(x) over [a, b], the trapezoidal rule is b a f(x)dx 1 (f(a) + f(b))(b a). 2 Geometrically, we approximate the area under f(x) for x [a, b] by the area of a trapezium, with base [a, b] and heights f(a) and f(b). Dividing [a, b] into n intervals of length h = (b a)/n, denote x k = a + kh, x 0 = a, x n = b, the composite trapezoidal rule is b a f(x)dx h n 1 2 (f(a) + f(b)) + h f(a + kh). k=1 Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 22 / 29
evaluating functions At the command prompt $: $ python testevalfun.py give a function in x : exp(x)*sin(x) give a value for x : 1 formula exp(x)*sin(x) at 1 is 2.28735528718 The script testevalfun.py uses StringFunction of the module scitools. Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 23 / 29
the script testevalfun.py # L-4 MCS 507 Wed 5 Sep 2012 : testevalfun.py from scitools.stringfunction \ import StringFunction formula = raw_input( give a function in x : ) f = StringFunction(formula) v = input( give a value for x : ) y = f(v) print formula, formula, at, v, is, y Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 24 / 29
running comptraprule.py $ python comptraprule.py give a function in x : exp(x)*sin(x) what is left end of [a,b]? 0 what is right end of [a,b]? 2 give number of evaluations : 100 approximate integral : 5.26408379811 $ python comptraprule.py give a function in x : exp(x)*sin(x) what is left end of [a,b]? 0 what is right end of [a,b]? 2 give number of evaluations : 1000 approximate integral : 5.38346879116 $ python comptraprule.py give a function in x : exp(x)*sin(x) what is left end of [a,b]? 0 what is right end of [a,b]? 2 give number of evaluations : 10000 approximate integral : 5.39554739369 Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 25 / 29
the script comptraprule.py from scitools.stringfunction \ import StringFunction formula = raw_input( give a function in x : ) f = StringFunction(formula) a = input( what is left end of [a,b]? ) b = input( what is right end of [a,b]? ) n = input( give number of evaluations : ) h = (b-a)/float(n) s = (f(a) + f(b))/2 for i in range(1,n): s = s + f(a+i*h) s = s*h print approximate integral :, s Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 26 / 29
using scipy Romberg integration extrapolates on the composite trapezoidal rule, available in the module integrate of the package scipy. >>> from scipy.integrate import romberg >>> from math import exp, sin >>> romberg(lambda x: exp(x)*sin(x),0,2) 5.3968910090331912 With lambda we quickly define a function. Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 27 / 29
Summary + Exercises We started chapter 2 of the text book. Exercises: 1 Type L = [3, 9]; K = L; K[1] = 6 in an interactive Python session. Sketch (or use Lumpy) the relations between K and L. 2 Write a script that prompts the user for a string definition of a function f (in x), the end points of an interval [a, b] and the number n of equidistant samples in [a, b]. The script prints the maximum and minimum value of f at the n equidistant samples in [a, b]. 3 Replace the for loop in comptraprule.py by an equivalent while loop. Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 28 / 29
more exercises 4 Write a script that asks the user ( for ) two integers n and k. n n! The script prints the value of = k k!(n k)!. 5 Modify comptraprule.py so that instead of n the script prompts the user for a tolerance ǫ (e.g.: 0.001). Starting with n = 2, the number of evaluations n doubles in every step till the difference between two consecutive approximations for the integral < ǫ. The first homework collection is on Monday 10 September, at 10AM. Bring to class your answers to exercise 3 of Lecture 1; exercises 1, 2 of Lecture 2; and exercises 1, 3 of Lecture 3. Scientific Software (MCS 507) Lists and Loops 5 Sep 2012 29 / 29