Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Numerical Computing

Similar presentations


Presentation on theme: "Introduction to Numerical Computing"— Presentation transcript:

1 Introduction to Numerical Computing
A first course in computing for STEM students Prof. Joseph Harrington Department of Physics University of Central Florida Spring 2015 © 2015, Joseph Harrington All rights reserved.

2 1: Introduction

3 Notices and Resources We are video-recording all lectures
You will have access to the videos They may be posted publicly Web site: Handouts, lecture notes, videos, etc. Links to all books, resources, etc. online Goal is to do everything for free to students

4 Why STEM? Photographed in Pittsburgh, PA, airport.

5 Concepts Managing project files, planning workflow, keeping a work log
Research skills prep (integrity, reporting, etc.) Programming fundamentals in Python (~1/3 of the course) Debugging, test-driven development, version control Using a command line Shell programming Basic numerical computation Scientific plotting, animation, and video editing Databases System management Doing science/math/engineering problems with a computer More? Need your feedback!

6 Expectations What you can expect to get out of this course
Comfortably use a computer to calculate, visualize, understand, and present Manage your own computer and computing life Learn anything new about computers on your own Work expected in this course 3-6 hours outside class, for real (ask any past student!) Presentations to the class on a few practical computer tasks Developing exercises to contribute to this course in the future

7 Class, People, Credit Video-recording lectures
Introduce course concept and staff Goal is to replace Intro to C with this course Course in development, everything is an experiment! Peer evaluation, need for grading to be automatic in order for class to scale to large-lecture format Student introductions Name, major, year, something about you unrelated to school Computer experience, OS preference and why Restricted registration forms

8 Logging Your Work “We are all scientists” isn't quite true.
Science is not just random trial and error Methodical, thorough Requires keeping and analyzing records We will keep class logs, just like a research log Dated entries for all classroom and work sessions Notes and summaries of what you do Links to online resources Snippets of code, Screen shots and plots Enable you to... Go back later and restart work Figure out what went wrong and fix it Manage a large number of trials Recall your thinking Prove/report your activities to others Never, ever change anything above the current entry!

9 Logging Your Work Purpose of lab log: make a record of what you did for you and others Mostly useful to you and your supervisor Logs are the main item scrutinized by integrity audits Seriousness of lab logs in commercial research, Federal audits Other purposes Online, searchable, can date-match to Used to save you time in the future Work in your lab log, cut and paste Making the log Use your favorite editor Have used text editors in past, but will experiment with Word-style this year Start new entry at bottom with “***** ” and output of ’date’ Leave blank line above and below Never (EVER) change above current date line If tracking time, end with “***** ” and output of ’date’

10 2: Languages and Software

11 Computers and Computation
Computer: powerful, integrated collection of data storage, memory, input/output, and calculation devices Today most interaction is via GUIs GUIs present interface to computer that makes sense for application GUIs very hard to write correctly Generally good for “canned” operations If user is doing new things, GUI is almost impossible Computation, research, exploration are in this category

12 Language Abstraction Stack
Graphical User Interface (GUI) Interpreted languages Text, no details, interactive, shell/IDL/Python Compiled languages Text, all details, C/C++/FORTRAN Assembly language Mnemonic codes for basic operations Binary machine code Hardware Processors, including Central Processing Unit

13 Interpreted Languages
GUI and command-line progs all written in compiled & interpreted languages For computation, use interactive (command prompt) languages that access compiled routines Fast, totally flexible Much easier to learn and use than C Lots of standard analysis methods already written But, must roll up sleeves and learn to program We use Python (edited with IDLE) to manipulate data Emacs to edit shell scripts and other text files, including some data files

14 Why Python? Python is an interactive (i.e., command prompt) language
Accesses fast routines written in compiled languages like C Fast, totally flexible Hides many details: much easier to learn and use than C In science, data are often arranged in arrays or lists Python is a high-level array language:

15 Multiply Arrays: C #include <fitsio.h> /* FITS file I/O package */ #include <memalloc.h> /* memory allocation package */ int main(int argc, char **argv){ double **a; double **b; double **c; char **ahead; char **bhead; int i; int j; int ni; int nj; a = readfits("a.fits", ahead); b = readfits("b.fits", bhead); ni = sxpar(ahead, "NAXIS1"); /* assume same size */ nj = sxpar(ahead, "NAXIS2"); /* better would be to check! */ c = double2Dalloc(nj, ni); for (j = 0; j < nj; j++){ for (i = 0; i < ni; i++){ c[j][i] = a[j][i] * b[j][i]; } /* ....do something with the result.... */

16 Multiply Arrays: Python
import astropy.io.fits as fits a = fits.getdata(’a.fits’) b = fits.getdata(’b.fits’) c = a * b # do something with the result

17 C or Python? Any questions? Python has no declarations
Language takes care of memory management Lots of routines for general numerics and specific sciences It's a real programming language, used widely (unlike IDL, Matlab, Mathematica, etc.) Can do OS interactions, network programming, encryption, databases, fully object-oriented (more than C++), etc. It's free (speech and beer)

18 Python Resources Python help: language
Python.org, Python manual and tutorial Free books (Google titles...) ThinkPython — rank beginner A Byte of Python — intermediate Dive into Python — expert programmer help() within Python Python help: SciPy scipy.org: cookbook, functional list of routines Many tutorials online, some commercial books mailing list SciPy doc wiki: docs.scipy.org

19 Installing Software Class requires certain programs
Python and related packages Unix(-style) command line (“shell”) and utilities Editor, etc. Installs are different on Mac, Linux, Windows In fact, they're mostly pre-installed on Linux and Mac Also need other resources ThinkPython book, for starters

20 Python We want version 3, though version 2 is also fine
Linux comes with Python 2 and 3 Mac comes with Python 2 Windows does not come with Python All require additional packages (Also need to download our book)

21 Command Line Unix command line (“shell”)
Most flexible, most capable command line Not hard to learn if you approach it right Very hard to learn if you don't! Linux is a Unix clone, nothing more to install Mac is Unix under the hood, nothing more to install Windows command line mainly for system use Not used much for actual work Basic principles are a subset of Unix shell Cygwin is an open-source Unix-like shell environment for Windows We will use Cygwin

22 Other Stuff We need an editor: emacs Edits text files, not documents
Extensible with a programming language (elisp), thousands of “modes” Programming and debugging Read and send Browse web Write and compile LaTeX documents Do financial bookkeeping Run shell programs We may need Unix's X window system Might not need it, we'll see (will try without) It works on both Mac and Windows

23 Install Party First collaborative exercise: Make and test install instructions for: Windows: Cygwin Python 3, IDLE 3, numpy, scipy, matplotlib Emacs Mac: Linux: IDLE 3(?), numpy(?), scipy, matplotlib Also, everyone get the free version of ThinkPython

24 3: Unix & Command Lines

25 What is Unix? Began at Bell Labs in late 1960s
Group got a DEC PDP, needed an OS to run a simple game Unix is a play on “multics” (mainframe OS from Honeywell) for a small computer Kernel system calls, common interface for different hardware to run the same programs, solving the problem of porting C programming language What is a shell? A command-line program for running other programs Unix offers options, like more than one shell Allowed for growth, experimentation, development; became dominant OS for high-end computing Mac is Unix already, can now get command-line environment on Windows (Cygwin) Tutorials and the web: ̃wjk/UnixIntro/ index.html The manual, man (what it is and isn't), /usr/share/doc, info

26 Basic Shell Commands Demo: 01shell
Handout: ls, cd, cat, less, mkdir, rmdir, pwd, cp, mv rm is forever emacs: starting, simple edits, help, tutorial, cheat sheet, quitting Dotfiles, ls -a, and ~/.bashrc Aliases: set in .bashrc using emacs alias rm='rm -i' alias mv='mv -i' alias cp='cp -ai' Play with it for the rest of the day, one- on-one help

27 4: Shell Basics Review: ls, cd, cat, less, mkdir, rmdir, pwd, cp, mv, rm Continue in 01shell.sh demo

28 5: More Shell Basics Ensure X and X emacs running on Cygwin (or at least getting a window version of emacs) Check in on comfort level with basic shell commands Look at a directory with both file browser and ls Collect and review HW1 Hand in using zip Go over HW1

29 6: Jobs, Programming Ideas
Organizing files and directories File heirarchy ls –color=auto, ls -R, rm - r, find Combining command-line options: rm -r -i is same as rm -ri Filesystem tour Continue in 01shell.sh demo Job control Pipelines: ls | wc Functions: today(){ date -Idate } Note indentation! Insertions: ls -l | grep `today` Variables: $HOME Loops: for i in a b c; do touch $i done

30 7: Programming Ideas, Scripts
Continue in 01shell.sh demo Reveiw: Pipelines: ls | wc Functions: today(){ date -Idate } Note indentation! Insertions: ls -l | grep `today` Variables: $HOME. Export, set, unset, echo File access permissions: ls -l, chmod, chgrp, chown Loops: for i in a b c; do touch $i done Shell scripts: making a program out of a series of commands

31 8: Python! It's time to program!
We will follow ThinkPython for a while Review chapter 1 It covers the landscape of programming We covered most of this, early in the course Read and work through Chapter 2 It covers interactive use, variables, and operations on numbers and strings Let's all fire up Python!

32 Python Quick Demo Quick plot of a sine function 02pythonintro.py

33 Python Strings and Variables
By tradition, we should start by typing: print(”Hello, world”) Already different: print()is a function, and ”Hello, world” has quotes In the shell, everything is a string Numbers are ASCII text strings ASCII: American Standard Code for Information Interchange, the binary encoding of text characters They are translated to binary by programs that get them as arguments In Python (and most other languges), numbers are different from strings Integers are even different from floating-point numbers Mostly Python tracks this and does the right thing Occasionally, you have to know about it In the shell, if it's not a keyword, function, or program, it's a string Variables need a $ sign In Python, if it's not a keyword, it's an “object”, which includes variables and functions Strings need matched, identical'quotation' ”marks”

34 Python as a Calculator Python as a calculator. Type: 1+1<enter>
I won't tell you to type enter anymore... 3+4*2-5/4 Order of operations applies (3+4)*(2-5)/4 4**4 2**24 2**94 2** Yowza! Python can do infinite-precision arithmetic! (If you're infinitely patient and have infinite RAM) 2.**94 Woah! What's that? Floating-point notation. Integers and floats are different 10.**500 Hmm. Why did it fail? What's the biggest it can do? Experiment. sin(0) Rats! All the “scientific” math functions are in a package called numpy.

35 Numbers in Computers 03pythonbasics.py

36 9: Numbers in Computers HW2 How is text stored in a computer?
ASCII: American Standard Code for Information Interchange How are integers stored? How are real numbers stored? Continue 03pythonbasics.py

37 10: Functions and Arrays Continue 03pythonbasics.py Function calls
Function returns Objects NumPy arrays

38 11: Accessing Arrays Finish 03pythonbasics.py
Shell and Python workshop

39 11: Shell and Python Work Shell and Python workshop

40 Shellphabet Write a shell loop that makes 26 files, each named for and containing a letter of the alphabet Write a simple command that puts a word on the screen, reading the letters from these files Medium: Make the letters appear on the same line Hard: Make the letters appear without spaces (Hint: add 3 characters to the loop. Hint2: the command od -a <file> will tell you exactly what is in each file.)

41 Array Creation and Slicing
Make an array of the even numbers from 0 through 20 Make a new array with the square of every number in the array Make a new array with the first 5 numbers Make a new array with the last 5 numbers Make a new array with the multiples of 4 Set the last 5 numbers to 7 Set the first 5 numbers to np.pi Set the third through sixth numbers to np.e

42 Shell Script to Start Log
Make a new file in your ices directory called logentry Make the file executable. It's an executable shell script. What needs to be on the first line? In the script, make a variable called logfile, which holds the absolute path to a plain-text file OTHER THAN YOUR LOG (for testing). Next, we want to append five asterisks, a space, and the current date to the file referenced in $logfile. What is the difference between appending and redirecting? How do you do each? Which do you want to use here? Finally, we want to open the file with emacs. Bonus: If you want emacs to open the file with the cursor at a certain line, you can run "emacs +<#> <file>" where # is the line number. Can you change the emacs command to make this happen?

43 Multidimensional Array
Here's an example of a 3x3x3 array : [ 1., 2., 3.], [ 1., 2., 3.]], [[ 1., 1., 1.], [ 2., 2., 2.], [ 3., 3., 3.]], [[ 1., 2., 3.], [ 2., 1., 2.], [ 3., 2., 1.]]]) array([[[ 1., 2., 3.], Build a three-dimensional array of the above form, where the first and last arrays are only zeros. Set all three dimensions equal to 5. Import numpy. ‘Allocate’ an empty array, initializing it to either zero or one. Index the array assigning the appropriate values to the appropriate locations. Challenge: do in a loop and declare the max number you are counting to inside the array outside the loop. Print the array to the screen. You will want to do this periodically anyways to check your answers while you are assigning to the array.

44 Formulae in Python Do circumference of circle, Pythagorean, or another commonly known/easy formula Generate random data, perform one of these on the array, and check the result.

45 Array Masking Make an array, x, from 1 - 100.
Create several masks that are true where: x is divisible by 3 x is divisible by 2 x is above and below certain values (12,72) x is a perfect square Print where all the masks are true (just 36)

46 13: HW3, Review HW3

47 14: Control Flow & Functions
Line breaks and statement blocks Control flow Functions Demo: 04flowfunc.py

48 Line Breaks and Blocks You can break a line if there is an open paren, brace, bracket, etc. The line continues until they are all closed You can break a line with a \ at the end of the line A set of statements indented the same is a block They appear in functions, loops, iterations, and conditionals They define what is in those elements

49 Control Flow “Control” flows through a program
It starts at the top of the main program file It goes into functions that are called and returns It can iterate, loop, and choose among branches Loop: while Iteration: for Branch (“conditional”): if

50 Loop A set of statements gets executed as long as a condition is True
The statements are indented the same amount to identify them: x = 0 while x < 22: # x < 22 is the condition print(x) # The red statements loop x = x+1 print(x) # This is not in the loop!

51 Iteration for loops iterate over the elements of a set (list, tuple, numpy array, but not a dict) Each member of the set gets assigned to a variable, in order The statements in the loop get executed once with each setting for i in 1, 2, 33, 444, 5.23, 'a': print(i) # this is in the iteration print(i) # this is not

52 Conditional (“if”) A “conditional” is a branch in the program
If the condition is True, something happens If not, something else (or nothing) happens The keyword is if: if <expression>: Statements to execute if <expression> is True (Optionally followed by...) else: Statements to execute if <expression> is not True Indentation identifies both blocks of statements.

53 Functions Function: a set of statements that can be called by name
In Python, name followed by parentheses containing arguments: y = np.sin(3.278) Functions return a value that substitutes for the function call So, you can compose functions: y = np.sin(np.cos(np.linspace(0 , 2*np.pi, 101))) (We already discussed assigning multiple return values to a tuple) Functions allow you to “box away” complexity, but... Try to think of a “glass brick”, not a “black box”!

54 Writing Functions Start with “def”
Arguments in parentheses, comma- separated Statements indented the same amount: def triple(x): y = 3 * x return y triple(4) 12

55 Local Variables Variables names inside are unrelated to variables outside z = 5 triple(z) 15 It works, even though we used z, not x.

56 Optional Arguments Optional (keyword) arguments have defaults assigned in function definition def myfunc(x, y=3): z = y * x return z

57 Example def triordup(x, y = None, prt1 = False, prt2 = False):
z = 3 * x if prt1: print(x) if prt2: print(y) if y: z = 2 * y return z triordup(3) triordup(x=3) triordup(3, 2) triordup(3, prt1=True) triordup(3, 2, prt2=True) triordup(3, 2, prt1=True) triordup(3, 2, prt2=True, prt1=True)

58 15. Function Docs and Tests
HW 4 Quiz 1 Docstrings Tests

59 Docstrings If the first line in a function contains a string and nothing else, it's a docstring Access with help(<function>) It's stored in <function>.__doc__ There is a format for scientific functions called the NumPy Doc Standard Let's look at it...see example.py

60 Tests There's nothing like a test to find errors!
Reduce your debugging by writing many tests. The examples in our docstring are tests! Run the tests every time you change a function. Write tests for every feature, every argument, etc. Write new tests to exercise any new features you add. Run all the tests often. Write a routine to do this, and to compare the answers to known answers. This saves most of your debugging time!

61 16. Plotting Basics Communicating with plots Plotting elements
Good and bad plotting Visibility/readability Linear vs. log scaling Examples Demo: 04plotting.py

62 Plotting A plot's goal is to communicate something to someone
Part of the goal is to demonstrate something with numerical information For credibility, you must not hide things with cheesy plotting tricks Plots are seen in different media On a personal screen On paper On a screen at the front of a large room Plots are seen by different people Specialists, supervisors, students, the public If the audience doesn't get it, you fail to communicate! Each medium and kind of audience requires a different plot Careers can be made or broken on the quality of plots!

63 Plotting Elements Title Not just “X vs. Y”, but explanatory
In print, the title is the first sentence fragment of the caption. Axes Axis line, major and minor ticks, tick values Axis label, including units Data (points, traces, regions, fields, etc.) Legend (optional, use judgement, don't obstruct the data!)

64 Good Plot, Bad Plot What's biggest: A 15” laptop at 20” from the eyes
A 3” print column at 15” An 8' projection screen at 45'? Angular size matters! The laptop wins. The projection screen loses. For smaller angular sizes: Use larger, thicker letters and line thicknesses Put less on the plot

65 Visibility Young eyes are MUCH better than old eyes
Age 40 starts deterioration There are several kinds of color blindness, esp. in men Red-green ambiguity is the most common; avoid using both Text size is most frequent problem: BIG IS GOOD! Contrast matters Avoid yellow on white, green on blue, gray on black, etc. Avoid two lines or point sets with similar colors Color gamut problems Screen, projector, and printer show same colors differently Reflective (print) colors have less range than luminous (screen) colors

66 Use the Right Scale The point of a plot is to show intervals and mathematical relationships spatially Most plots want a linear scale Log scales show specific mathematical relationships DO NOT use a log scale just to cram a large range onto a small plot! This violates the whole point of plotting! What do log-linear, linear-log, and log- log plots show? (HW!)

67 Good Plot, Bad Plot

68 Good Plot, Bad Plot No title No axis labels No units
Is that the right kind of line?

69 Good Plot, Bad Plot

70 Good Plot, Bad Plot

71 Good Plot, Bad Plot USE BIG LETTERS!!

72 Good Plot, Bad Plot

73 Good Plot, Bad Plot Pixelated (avoid if possible)
Y axis on wrong side (avoid unless reason)

74 Good Plot, Bad Plot

75 Good Plot, Bad Plot Line contrast too low Lines and letters too thin
Text too small for screen

76 Good Plot, Bad Plot Better Text and box could still be larger/thicker
Line could be smoother (if theory, ok for data)

77 Log Plots

80 The World's Worst Plot! Google calls this the world's worst plot. Clearly, it's been cropped, but as is... No title, no x axis, what's that line?

81 17. Images and 3D Plots Log-log plotting: visualizing power laws: 04plotting.py Putting more on a plot, well: Rosling video: 05discreteplots Photographs as data: 06photodata.py Scalar fields Contour plots: 07contours.py False-color images Surface plots

82 Image File Formats Image data can be big!
20 megapixel image takes 60 MB for one image! TIFF (Tagged Image File Format) image format, .tif, .tiff, .TIF Raw (each camera has its own raw format) Compression is key Lossy: tolerate some data loss to get high compression ratio Filters noise, smooths image slightly Real images always have some noise, so lossy is usually fine JPEG (Joint Picture Experts Goup) format, .jpg, .jpeg, .JPG Lossless: no loss, lower compression ratio Does well with large regions of same color, e.g., the white/black background in plots PNG (Portable Network Graphics) format, .png, .PNG

83 Photons and Color Photons have frequency (energy) and quantity (brightness) Objects emit or reflect photons of many frequencies Each frequency has its own brightness Brightness vs. energy is the spectrum of the object Visual systems bin detected photons into energy ranges Compare the intensity in each channel to perceive a color Humans have 3 bins (“cones”) plus a low-light, colorless sensor (“rods”) Most animals are colorblind (1 sensor), but some lobsters have 12 colors! Crude approximation of real world's infinite discrete energies all emitted at once But it works!

84 Photographs as Data Photographs are images of real scenes
Grid image spatially into picture elements (“pixels”) In lit images (monitors and projectors), each pixel has 3 values: R, G, and B How much red, green, and blue (RGB) light to emit Can express RGB on scale of (float) for each color 0 is black, 1 is lamp's maximum red, green, or blue light Usually rescaled as (1 byte integer) to shrink image size (23 bytes to 3 bytes/pixel) Editing programs (GIMP, Photoshop) can use (2 byte integer per color) Printers often use cyan, magenta, yellow, and black (CMYK) 0 is white (blank paper, no ink) 1 is full ink, no paper shows through Since maximum C+M+Y is never really black, add a black ink (K, because B is for “blue”) The color systems RGB and CMYK are called “color gamuts” 06photodata.py

85 18. 3D Plots, Data From Files 3D plotting Reading data from files

86 Seeing 3D With a 2D Retina Reality is in 3D, but our retinas are 2D. We use: Perspective – distant objects are smaller, parallel lines converge Shading – distant objects are dimmer (if lit by our torch...) Motion – move the scene and see objects relative to each other Shadows – mark location on ground, easier to track distance there Parallax – only used within 6 meters (20') Many people don't have it! We do fine... Computer can simulate these 08plot3d.py

87 Data From Files Many ways to encode data in files
Usually want ancillary data that describe main data Origin, dimensions, date acquired, subject, acquisition location and conditions, processing, etc. Note: “data” is the plural of “datum”! ASCII Easiest for people to read Takes many times more space Hard to do things more complicated than columnar tables Can't do lots of columns or it's unreadable (e.g., images) Binary formats – many! HDF – Heirarchical Data Format FITS – Flexible Image Transport System TIFF, JPEG, PNG Formats for specific programs, disciplines, etc. 09datafiles.py

88 19: Creating, Slicing Arrays
Array creation exercises Array slicing exercises

89 Array Creation Make an array of 50 even integers, starting with 10
Make an array of the first 25 odd half- integers (1.5, 3.5, etc). Do this at least 3 ways, one without any math. Make an array with 1000 numbers evenly spaced from 0 through 2π Make an array with 21 numbers evenly scaled in the log from 10 to Hint: help(np.logspace) Challenge: Make an array of 37 log- scaled numbers from 440 through These are the frequencies, in Hz, of the notes from A above middle C through the next three octaves (each octave doubles in frequency). Check your answer here: requencies

90 Array Slicing, 1D Make an array of the even numbers from 0 through 50
Make a new array with the square of every number in the array Make a new array with the first 15 numbers Make a new array with the last 15 numbers Make a new array with the multiples of 4 Set the last 15 numbers to 7 Set the first 15 numbers to np.pi Set the third through sixteenth numbers to np.e

91 Array Slicing, 2D Make an array of 150 zeros Reshape it to (15,10)
Set the odd columns to 1 Set every third row to 3 View column 7 with a variable called x Copy column 8 to a variable called y Plot y vs. x Copy a shape (3,4) rectangle with its lower-left corner at (5,7) to a variable called r Copy a rectangle consisting of every second row starting with row 1 and every third column starting with column 3 and ending before the third-last column to a variable called s

92 20: Broadcasting Broadcasting intro Demo Class exercises

93 Broadcasting Numpy arrays support “broadcasting”
If 2 arrays in an operation have different dimensions And if the dimensions of the smaller array are consistent with dimensions in the larger array Then, the smaller array is repeated along the missing dimensions to do the operation For example, if you have a (20,15) array of 7s times a (15,) array of 9s, the result is a (20,15) array of 63s This is useful and avoids many loops! Demo 10broadcasting.py

94 21: Working on a Computer HW5 Working on a Computer

95 Working on a Computer You can now log, manage files, and program
But, how do you actually use a computer to do a problem? Good habits make faster solutions that you can re-use later Bad habits cause errors and make it hard to get anything done But, short problems don't expose bad habits, and we're not ready for long problems So you'll have to trust me! Use good habits even if they seem unnecessary.

96 Good Habits Log everything, especially at the start. Log how you are thinking about the problem and how it might be solved. Define your problem well What is known (data, facts)? What is asked? Be very specific Who is asking? Is it a general problem, or do you need to present something to someone? What is needed at the end? A plot? A number or numbers? List your assets (data, tools, gear) Outline your solution, relating to problem statement How can you best organize things for this solution, given its size?

97 Example Sally's research project!

98 22: Debugging and Versioning
Debugging is making your code correct (Justin demo) Version control is tracking and managing change (AJ demo)

99 Debugging Debugging is making your code correct Approaches
Code inspection Adding diagnostic prints to trace a variable's values Adding assertions that are true for a working code Unit testing (a unit is a small chunk of code, like a function) Debuggers do this without modifying the code Interactive, even for C/C++/FORTRAN, etc. Justin's debugger demo See also ress.com/category/python-debugger/

100 Version Control Debugging is making your code correct Version control
Recording progress Managing group work on the same files Being able to go back to working code Keeping files neat

101 23. Doing Problems Let's write Python programs to solve some problems
Example physics problem Class work on problems

102 24: Numerical Techniques
There are thousands of numerical techniques you can apply to data It's a field in itself Many books, software libraries We'll introduce a few techniques Give keys to finding and learning more

103 Some Numerical Areas Solving math equations and systems of equations
Interpolating/extrapolating Integrating and differentiating symbolically and numerically Evaluating and approximating special and arbitrary functions Generating random numbers Sorting and selecting data Finding roots (where functions take a specific value, like 0) Optimization (finding minima and maxima of functions) Fourier methods (FFT, Wavelets) Statistics Fitting functions to data

104 More Numerical Areas Finding clusters
Identifying different populations in the same dataset Predicting propagation (cracks, disease, etc.) What else?

105 Applications Predicting the weather
Allocating resources in a political campaign Deploying resources in an epidemic Determining the best search results to display Finding songs for your Pandora channel

106 Getting Started Start with Linear interpolation Function fitting
Both start with some (x,y) data points Interpolation estimates values for points between the data Extrapolation estimates values outside the data range (try not to!) Fitting estimates parameters for a generic function to make it fit the data as well as possible, e.g., y=a + bx + cx2 In this case, a, b, and c are the parameters, given some (x,y) data points

107 Linear Interpolation Example

108 Linear Interpolation Given (xi,yi) data, predict ys at a new set of xs between the existing xs Basically, take the average of the two nearest points, weighted by the distance A weighted average sums the ys times their respective weights and divides that by the sum of the weights The weight for y1 is the distance from the new x to x2 The weight for y2 is the distance from x1 to the new x

109 Homework Problem Start in a new file
Write a function definition with no arguments for a routine called interpolate() Write a docstring saying that it will do linear interpolation, and what the arguments are Write a simple test routine with a trivial test, such as interpolating between points (0,0) and (0,1) at x=0.5 Now fill in the rest of your interpolate() routine and test it If it passes, write some non-trivial tests and see if it passes them Now, write another routine that takes an array of xs, an array of ys (same length), and an array of new xs, and interpolates to find the new ys. Start with a docstring and a test routine, of course. Remember: Each test value must be calculated by hand, without using the routine you're testing!

110 25: Random Sampling Final project Monte Carlo (random) techniques
Gaussian and standard deviation Histograms

111 Final Project Due 30 April Define a problem in your major area
Find some real data online or elsewhere Write code (at least 10 significant functions) to: Read data Do calculation Create plots, tables, etc. Write a lab report on it Problem statement Data Solution Program description Results Present results (10 minutes + 5 min for questions)

112 Probability Basics Probability describes behavior of unpredictable systems Discrete – integer values: coin flips, scores on T/F test Continuous – real values: animal weights or heights, speeds In limit of large N, everything looks continuous Group behavior described by probability (density) function that sums (integrates) to 1 Coin flip: heads: 1/2, tails, 1/2 Die: each number: 1/6 (or 1/N, for an N- sided die) Histogram on a fair die: flat at 1/6 Histogram on sum of 2 fair dice: higher in middle Why? There are more ways to score a 3 on the sum of 2 dice than there are ways to score a 1. We can simulate complicated systems and look at histograms to see overall behavior

113 Mean and Standard Deviation
Mean is just the average: sum items and divide by N (the number of items) Standard deviation is width of the histogram: Subtract mean from each item to get deviation Square the deviations Take their mean, but using N-1 to divide (this is the variance) Take the square root (this is the standard deviation)

114 Histogram Histogram plots how often an event occurs Demo...

115 26: Monte Carlo Final project ideas
Monte Carlo example – How does the standard deviation of the mean improve with more samples? This demo is drawn from the homework solution in another class, so I'm not distributing it online!


Download ppt "Introduction to Numerical Computing"

Similar presentations


Ads by Google