Carnegie Mellon Worcester Polytechnic Institute Numpy pylab matplotlib
Carnegie Mellon Worcester Polytechnic Institute Numpy, pylab, matplotlib (follow-up) Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction to Computer Science, 2 nd edition, by John Zelle and copyright notes by Prof. George Heineman of Worcester Polytechnic Institute) CS-1004, A-Term 2014 Numpy, pylab, matplotlib 1
Carnegie Mellon Worcester Polytechnic Institute Follow-up from Prof. Heineman ¢ Lecture notes from Friday are here: – § http: //www. tinyurl. com/guest-1004 § Link also on course website (under Lecture Notes) § Useful stuff at end of notes, beyond what he covered in lecture ¢ Read especially, § 1. 7, § 1. 8 § Try the “Debugging Challenge” § 1. 8 § Questions on quizzes like this CS-2301, D-Term 2014 Introduction 2
Carnegie Mellon Worcester Polytechnic Institute Follow-up from Prof. Heineman (continued) ¢ Pylab § Another way of accessing matplotlib § I. e. , a. py script: – from matplotlib. pylab import * import matplotlib. pylab __doc__ = matplotlib. pylab. __doc__ ¢ matplotlib. pylab is a longer script § Containing # provide the recommended moduleabbrevs in # the pylab namespace import matplotlib. pyplot as plt import numpy as np import numpy. ma as ma ¢ Is anyone still having trouble installing matplotlib? CS-2301, D-Term 2014 Introduction 3
Carnegie Mellon Worcester Polytechnic Institute Follow-up from Prof. Heineman — numpy ¢ ¢ Principal class is ‘array’ Somewhat like lists but, § Multiple dimensions § Different way of indexing 2 nd and subsequent dimensions § More efficient internally ¢ Tutorial: http: //wiki. scipy. org/Tentative_Num. Py_Tutorial § Check it out § Much will be over your head now § More useful after end of course CS-2301, D-Term 2014 Introduction 4
Carnegie Mellon Worcester Polytechnic Institute Numpy and arrays — Example L M N P = = [1, 2, 3, 4, 5] [] [] [] for x in L: M. append(x*x) N. append(sqrt(x)) P. append(2**x) Q = [L, M, N, P] Q 1 = numpy. array(Q) ¢ With Python lists Q[2][3] ¢ With numpy arrays Q 1[2, 3] CS-2301, D-Term 2014 Introduction 5
Carnegie Mellon Worcester Polytechnic Institute Questions? CS-2301, D-Term 2014 Introduction 6
Carnegie Mellon Worcester Polytechnic Institute Follow-up from Prof. Heineman — “Compile often!” ¢ ¢ What does this mean? § Especially since Python is an interpretive language? Example: – typing code into Python shell § Errors show up (almost) immediately Example: – typing into a module § When do errors show up? What does “Run module” menu command do? § Resets shell § (Virtually) “types” content of module into shell § As if you typed it yourself! § Picks up some errors … § … as interpreter ¢ But some errors cannot be caught until you actually call the function! CS-2301, D-Term 2014 Introduction 7
Carnegie Mellon Worcester Polytechnic Institute Additional advice — “Test often” ¢ ¢ Example 1 — Homework #2 Q 3 asks you to print out information for each term § Why? § Answer: – so you can see that it is working correctly! § How many had it working the first time you tried it? ¢ ¢ Q 6 asks you to use the function you developed in Q 3 100 times (or more) § What about all those lines of debug output? Options: – § Comment out the print lines § Copy Q 3 function to Q 6 function with new name § Remove or comment out print lines § Write a separate test program or function CS-2301, D-Term 2014 Introduction 8
Carnegie Mellon Worcester Polytechnic Institute Example — Pong ¢ ¢ Separate into two parts § Physics of the motion § Display in window Physics part: – § Motion in each dimension is independent of other dimension § Function: – move 1 D(pos, vel, u. Bound, l. Bound) § Returns new positions, accounting for bouncing off edges ¢ Testing physics part: – § Set up some (artificial) parameters § In a loop, call move 1 D(), get and print new position, use for next call § Examine manually CS-2301, D-Term 2014 Introduction 9
Carnegie Mellon Worcester Polytechnic Institute Questions? CS-2301, D-Term 2014 Introduction 10
- Slides: 10