Download as pdf or txt
Download as pdf or txt
You are on page 1of 58

Kai-Feng Chen

National Taiwan University

PROGRAMMING &
NUMERICAL ANALYSIS2014
Lecture 01: Introduction

OVERVIEW
Actually this is a quasi-laboratory course, since no one can learn
how to do programing by only listening to the lectures and take
notes (and only do the homework once a while!).

PRACTICE is extremely important:

You will never learn the calculus without doing lots of


differential/integral exercises, right?

You are strongly recommended to bring your laptop to this


lecture and practice during the lecture.
(hopefully the battery life of your laptop can run over 3 hours!)

If you do not have a laptop, you are encouraged to work with your
classmate who has laptop during the lecture.

A QUASI-LABORATORY
LECTURE

One will never learn piano without real practice;


simply watching a couple of great performances never works.
3

A QUASI-LABORATORY
LECTURE
I will not just blah-blah throughout the whole 3 hours.
Instead, 1/2~2/3 of the total time will be devoted to an
introductory lecture with slides.

Rest of time will be used for practice/exercise/problem-solving,


just like your laboratory courses!

There will be also some short trial periods during the main
lecture, which allows you to try something easy.

Please also stop me when you run into any difficulties or troubles
throughout the whole lecture.

THE GOAL OF
THIS COURSE
Learn how to solve a problem with computers rather than
a pen and papers.

Learn how to program.


Learn how to utilize the existing tools/functionalities, or build
your own tool.

Learn how to formulate a problem into a simple program that can


give you an answer clearly and quickly.

And have fun with them! (most important!)

WHAT ARE WE GOING


TO DO?
We will learn PYTHON from scratch.

(well, python is probably the easiest computer language to learn within half of
semester and powerful enough to do many things!)

We will discuss how to use python and the numerical/graphical

libraries to solve scientific problems, which could be beneficial to


your own physics (experimental/theoretical) studies in the near
future.

It does not mean you do not need to learn other computer

languages (e.g. C++, fortran, php, etc.) in the future for your own
work. Hopefully you will get some taste of computing in this
semester.

SCIENTIFIC COMPUTING
WITH PYTHON
We will use (some of the) SciPy (pronounced like sign pie)
packages in this lecture:
! !
! !

SciPy library:
fundamental library for scientific computing.

! !
! !

NumPy:
base N-dimensional array package.

! !
! !

Matplotlib:
Comprehensive plot making

You can also consider the IPython as a nice enhanced interactive


console.

More information: http://www.scipy.org


7

OUTLINE
Python, the programming language
The basis
Control flow
Types and data structure
Functions and modules
Input & Output
Classes and others
Numerical analysis with Python
Numerical differential and integration
Random numbers
Show it graphically
Linear algebra
Root finding and minimum finding
Differential equations
Curve fitting and data modeling
8

We will try our best to go


through all of these topics
during this semester!

IF YOU ARE ALREADY


FAMILIAR WITH CODING...
Im pretty sure some of you already well experienced in
programming.

The first part of the course can be too easy for you in this case, and
you can probably learn the later part by yourself without any
difficulties.

If you are in this situation, I would recommend:


Discuss with me and maybe we can do something beyond the
scope (e.g. a more challenging project).
Still come to the class and act as a helper for your classmates
(especially during the exercises period!).

EVALUATION
Homework:
Exercises will be assigned every subject (every 12 weeks).
Please hand back (upload) the code within 1 week.
Quizzes:
Will be assigned during the midterm week (no lecture).
Time limit: 1 week.
Googling the answer is allowed. Discussions are also allowed.
Surely this sounds too relaxed...

10

EVALUATION (II)
From the basic grading toward the final goal:

If you fulfill all of the


minimal requirements
(homework & quizzes,
no delayed hand back)

You have to collect


3 gold coins just like the
super mario game!
11

EVALUATION (III)
How to collect the golden coins:
Be the first 3 people uploading one of the homework .
Be the first 3 people uploading the answer to any one of the

quizzes during/after the midterm week.

Tournament: entering the semi-final round.


Additional project presentation (especially if you are already
familiar with coding).

By default everyone can get B+~A+ easily.


Surely you may still loose the grading if:
1) Delayed/No hand-back of the homework
2) Delayed/No hand-back of the examines

!excluding the first homework,


which is given at the end of the slides.

12

TEXTBOOK &
REFERENCES
For python, most of the information are online. Getting a printed
textbook is not really required. A couple of nice
online books/documents are available:

Python.org tutorial:

http://docs.python.org/2/tutorial/index.html

Think python (slides are based on this book):

http://www.greenteapress.com/thinkpython/html/index.html

A byte of python:

http://swaroopch.com/notes/python/

Caveat: the documents/books may be prepared for


python 2 or python 3. In this lecture we will use
python 2.7 as the default version.
13

TEXTBOOK &
REFERENCES (II)
For SciPy (and NumPy), there are already some document
available on the official website and some online e-books:

Official web document:

http://docs.scipy.org/doc/

NumPy Beginners Guide:

http://it-ebooks.info/book/2847/

SciPy and NumPy book:

http://it-ebooks.info/book/1280/
In principle you can always find the help online, so it is
not really required to have a printed book.

14

TEXTBOOK &
REFERENCES (III)
References for computational physics & algorithms (non-python):
Numerical Recipes:
The Art of Scientific Computing
by William H. Press
3rd Edition (2007)
http://www.nr.com/

An Introduction to
Computational Physics
by Tao Pang
2nd Edition (2006)

Computational Physics:
Problem solving with computers
by Rubin H. Landau et al.
There are still many other
2nd Edition (2007)
computational physics or algorithm
text book can be found on the market.
15

THE ULTIMATE REFERENCE

Whatever, Google tells you everything...

16

GETTING START
If you are using any unix-like operation system, such as Linux or
Mac OSX, usually a python is pre-installed in your system:

You can simply start


a terminal and type
python.

For Windows, in principle you can download the python from the
official download area (please avoid python 3 for now):
http://www.python.org/download/

But wait this is not enough!


17

GETTING START (II)


Since in this lecture we will use SciPy and NumPy, it will be much
easier if you can install all of them together. There are some
integrated package available:

Option #1: Get the Canopy Express (free version):

https://www.enthought.com/store/
Just download it and install. It comes with all the needed
packages already, together with a nice IDE ready to go.

Option #2: Get the Anaconda

http://continuum.io/downloads.html
Also simply download it and install. It requires a little bit more
command line working experience but the support is good.

Basically these two options support Windows, Linux, and Max OSX.
18

GETTING START (III)


If you have installed the Canopy Express, this is what you will get:
Just click the Editor to start
your coding work with the IDE!
You may also want to check the
command line integration:

19

GETTING START (IV)


If you installed the Anaconda, this is what you will see:
The ipython-notebook is
something similar to Matlab
(also available in Canopy).
You should see a similar command
line integration:

If you want an IDE (spyder)


you have to install it first!
20

COMMENTS
In principle you can install all of the required packages (Python

+SciPy+MumPy+Matplotlib) by yourself without the integrated


package like Canopy and Anaconda. But it will take much more
efforts before you can actually work. This is not very
straightforward for beginners.

IDE (integrated development environment) In principle this is


not really a requirement. We will mostly use terminal (command
line) in this course. However, a good IDE can be easier for some
people. You can use it if you like. You can try the one came with
Canopy, or the spyder, or the IDLE (which was developed by the
original python author Guido van Rossum).

21

COMMENTS (CONT.)
Python 2 versus Python 3:

If you check the documents (and the official site) carefully, you
may find there are some issues between python 2 and 3. Basically
python 3 does not have the backward capability with version 2,
and the integration of python 3 has not yet been fully established.
The syntax is slightly different. In this lecture we will use python
2.7 as the default version.
Dont worry about the exact version for now.
The key idea is to learn how to solve a problem
with programming, not the language itself.

22

INTERMISSION
Now its the time to get your working environment ready!
(switch on your laptop now!)

If you already have a python (whatever version/bundle) installed


in your laptop/desktop, you may proceed immediately until we
start to talk about SciPy/NumPy.

It will take a while to install Canopy or Anaconda. You can do it


later today.

If you only have a pad/phone, you can even try some of the online
python interpreter, e.g.:
http://www.compileonline.com/execute_python_online.php
http://repl.it

23

PYTHON:
AN INTRODUCTION
Quote from Wikipedia: Python is a widely used general-purpose,

high-level programming language. Its design philosophy


emphasizes code readability, and its syntax allows programmers to
express concepts in fewer lines of code than would be possible in
languages such as C. The language provides constructs intended to
enable clear programs on both a small and large scale.

The core philosophy (as called The Zen of Python):


Beautiful is better than ugly.
Explicit is better than implicit.
In short:
Simple is better than complex.
simple and beautiful
Complex
is
better
than
complicated.

Readability counts.
24

PYTHON:
AN INTRODUCTION (II)
Python development was started in December 1989.
Python's principal author: Guido van Rossum.
The origin of the name comes from Monty Python (Monty

Python's Flying Circus, a British television comedy show),


not the elongated legless animal.

Monty Python's Flying Circus

Guido van Rossum


25

PYTHON,

THE PROGRAMMING LANGUAGE


Python is a high-level language; others: C/C++, Fortran, Java, etc.
The computers can only run the machine language, which is not
human readable (well, if you are a real human).

The programs written in a high-level language are shorter and


easier to read, and they are portable (not restricted to only one
kind of computer). However, they have to be processed
(translated) before they can run.

26

HELLO WORLD IN
C/C++
Well, the common starting point for any programming language.

#include <stdio.h>
int main()
{
! printf("hello world!\n");
}

helloworld.cc

If you run it in a unix-like system with a gcc compiler:


% gcc helloworld.cc
% ./a.out
hello world!
%

27

HELLO WORLD IN
PYTHON
print 'hello world!'
helloworld.py

Run it in your terminal with python interpreter:


% python helloworld.py
hello world!
%
All you need is just a single line!
The standard filename extension for python is .py.
28

THE COMPILER (& LINKER)


A compiler reads the source code and translates it before the

program starts running; the translated program is the object code


or the executable.

Once a program is compiled, it can be executed it repeatedly


without further translation.

This is what we did for the


C version of hello world.
29

THE INTERPRETER
An interpreter reads a high-level program (the source code) and
executes it.

It processes the program little-by-little at a time, reading lines and


performing computations.

The python version of hello


world program is executed by
the python interpreter.
30

PYTHON,
THE INTERPRETER
Python is considered an interpreted language because Python
programs are executed by an interpreter.

Rapid turn around: no needs of compilation and linking as in C


or C++ for any modification.

Hybrid Python programs are compiled automatically to an

intermediate form called bytecode, which the interpreter then


reads.

This gives Python the development speed of an interpreter

without the performance loss in purely interpreted languages.

However for serious speed limited computations you still need


to use C, or even Fortran.

31

PYTHON,
THE INTERPRETER (II)
There are actually two ways to use the interpreter:
interactive mode and script mode.

Running your program in script mode:


As we did in the previous hello world example: put your
code into a file, process it with the python interpreter.

The Unix/Linux #! trick:


#!/usr/bin/env python
print 'hello world!'

helloworld.py

% chmod +x helloworld.py Make it as an executable file


% ./helloworld.py
hello world!
32

PLAY WITH PYTHON


INTERACTIVELY (I)
Lets try some simple Python commands in the interactive mode:
% python
>>> 2+2
4
>>> # Im a comment. I will not do anything here.
... 2+2 # anything after # is a comment.
4
>>> 6/3
2
>>> (50-5*6)/4
5
Use the python interpreter as a calculator!
>>> 6/-3
The regular precedence is followed:
-2
(,) ** *,/ % +,>>> 2**8 this is exponentiation (28)
256
>>> 10%3 this is modulus
1
33

PLAY WITH PYTHON


INTERACTIVELY (II)
Float point number versus integer:
>>> 8/3
2
>>> 9998/9999 rounding down to integer (floor)
0
>>> 8./3
2.6666666666666665
>>> 8/3.
2.6666666666666665
>>> 8/(6.6/2.2)
2.666666666666667
>>> 8/3+1.0
3.0
>>> round(3.1415927,3) rounding off to 3rd digit
3.142
34

PLAY WITH PYTHON


INTERACTIVELY (III)
You can assign a variable with =:
>>>
>>>
>>>
>>>
900

width = 20
height = 5*9
area = width * height
area

You can assign multiple variables as well:


>>> x = y = z = 1
>>> x
1
>>> x + y + z
3
35

PLAY WITH PYTHON


INTERACTIVELY (IV)
Complex number is also supported. The imaginary part can be
written with a suffix j.

>>> a = 2j
>>> a*a
(-4+0j)
>>> b = complex(3,4) Using the function complex(real, imag)
>>> a+b
(3+6j)
>>> 8j/3j
(2.6666666666666665+0j) Complex numbers are always float.
>>> b.real + b.imag
Exact the real/imaginary part
7.0
with .real and .imag
>>> abs(b) absolute value
5.0
36

FIRST VISIT WITH


STRINGS
Python supports for strings are actually very powerful. See several
examples below:

>>> 'I am a string!'


'I am a string!'
>>> "me too!"
'me too!'
>>> 'anything quoted with two \' is a string.'
"anything quoted with two ' is a string."
>>> "\" also works."
'" also works.'
>>> "pyt" 'hon' Two string literals next to each other
'python'
are automatically concatenated
>>> sentence = 'I am a string variable.'
>>> sentence
'I am a string variable.'
37

FIRST VISIT WITH


STRINGS (II)
More: multiline strings
>>> lines = "line1\nline2\nline3"
>>> print lines
line1
line2
line3
>>> reply = '''I
Use ''' or """ to start/end multiline strings.
... also have
... 3 lines'''
>>> print reply
I
also have
3 lines
38

FIRST VISIT WITH


STRINGS (III)
More: operators +, *, and type conversions
>>> 'oh'+'my'+'god'
'ohmygod'
>>> 'It\'s s'+'o'*20+' delicious!!'
"It's soooooooooooooooooooo delicious!!"
>>> '1234'+'1234'
'12341234'
>>> int('1234')+int('1234')
2468
>>> float('1234')/100
12.34
>>> a = b = 2
>>> str(a)+' plus '+str(b)+' is '+str(a+b)
'2 plus 2 is 4'
39

We will talk more on python strings afterwards.

VALUES AND TYPES


A value is one of the basic things a program works with, like a
letter or a number.

1234 is an integer. 1234 value; integer type.


hello is a string: hello value; string type.

The interpreter can also tell you what type a value has:
>>> type('hello!')
<type 'str'>
>>> type(1234)
<type 'int'>
>>> type(3.14159)
<type 'float'>

40

TYPES & MORE


Remember there are two more numerical types supported:
>>> type(123456789012345L)
<type 'long'>
>>> type(3+4j)
<type 'complex'>

Type casting is easy:


>>> type('1234')
<type 'str'>
>>> type(int('1234'))
<type 'int'>

41

TYPES & MORE (II)


Few more complicated (but very useful) types are also supported.
Will be discussed in a near future lecture.

>>> type(3<4)
<type 'bool'>
>>> type(open(tmpfile,w))
<type 'file'>
>>> type([1,2,3,4,[five,6.0]])
<type 'list'>
>>> type((1,2,three,4))
<type 'tuple'>
>>> type({'name':'Chen','phone':33665153})
<type 'dict'>
>>> type({'a','b',c})
<type 'set'>
42

VARIABLES
A variable is a name that refers to a value.
An assignment statement creates new variables and gives them
values (you have seen this already):

>>> pi = 3.1415926536
>>> last_message = what a beautiful day

Programmers generally choose names for their variables that are


meaningful they document what the variable is used for.

Variable names can be arbitrarily long, can be with both letters and
numbers.

The underscore character _ can appear in a name as well.


43

VARIABLES (CONT.)
The variable names have to start with a letter or _
It is a good idea to begin variable names with a lowercase letter.
Pythons keywords: keywords are used to recognize the structure
of the program, and cannot be used as variable names.

and
as
assert
break
class
continue
def

del
elif
else
except
exec
finally
for

from
global
if
import
in
is
lambda

44

not
or
pass
print
raise
return
try

while
with
yield

INTERMISSION
Now its the time to play with your python interpreter:
As a simple calculator.
Play with the strings.
Examine the python built-in types.
Define your variables and check any possible break-down cases.

45

A STEP TOWARD
PROGRAMMING
A program is a sequence of instructions that specifies how to
perform a mathematical/symbolic computation.

This is a simple example of mathematical computation:


s = t = 1.
for n in range(1,21):
t /= n is equivalent to t = t/n
t /= n
s += t
print "1/0!+1/1!+1/2!+1/3!+...+1/20! =",s
1/0!+1/1!+1/2!+1/3!+...+1/20! = 2.71828182846
46

THE PRINT STATEMENT


As you already seen in the hello world! program, as well as the

previous example, the print statement simply present the output to


the screen (your terminal).

A couple of examples:

>>> print 'show it now!'


show it now!
>>> print 1,2,(10+5j),-0.999,STRING connect w/ comma
1 2 (10+5j) -0.999 STRING
>>> print 1,2,3,4,5
1 2 3 4 5
>>> print str(1)+str(2)+str(3)+str(4)+str(5)
12345
Remark: for python 3, the print becomes a function, you
have to use something like print(hello world!).
47

STRUCTURE OF
A PROGRAM
Input:

get data from the keyboard, a file, or some other device.


Output:
display data on the screen or send data to a file or other device.
Math:
perform basic mathematical operations like addition and
multiplication.
Conditional execution:
check for certain conditions and execute the appropriate code.
Repetition:
perform some action repeatedly, usually with some variation.
Every program youve ever used, no matter how complicated, is
made up of instructions that look pretty much like these.
48

GIVE ME A FLYSWATTER
Programming is error-prone. Programming errors are called bugs
and the process of tracking them down is called debugging.

Three kinds of errors can occur in a program:


Syntax errors
Runtime errors
Semantic errors

49

SYNTAX ERRORS
Syntax refers to the structure of a program and the rules about that
structure.

Python can only execute a program if the syntax is correct;


otherwise, the interpreter displays an error message.

An example:

>>> y = 1 + 4x
File "<stdin>", line 1
y = 1 + 4x
^
SyntaxError: invalid syntax
>>>

50

You probably already see a


couple of syntax errors in
your previous trials.

RUNTIME ERRORS
A runtime error does not appear until after the program has
started running.

These errors are also called exceptions because they usually

indicate that something exceptional (and bad) has happened.

The following ZeroDivisionError only occur when it runs.

>>> a = b = 2
>>> r = ((a + b)/(a - b))**0.5 * (a - b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
>>>

51

SEMANTIC ERRORS
If there is a semantic error in your program, it will run successfully,
but it will not do the right thing.

It does something else.


The problem is that the program you wrote is not the exactly
program you wanted to write.

The meaning of the program (its semantics) is wrong.


Identifying semantic errors can be tricky because it requires you to

work backward by looking at the output of the program and trying


to figure out what it is doing.

(Much) more difficult to debug.

52

SEMANTIC ERRORS
An example:

Just one dot gives you a


completely different answer.

s = t = 1.

s = t = 1
for n in range(1,21):
t /= n
s += t
print "1/0!+1/1!+1/2!+1/3!+...+1/20! =",s
1/0!+1/1!+1/2!+1/3!+...+1/20! = 2
2.71828182846
53

DEBUGGING
It is very difficult to write a bug free program at the first shot.
Programming and debugging can be the same thing.

Programming is the process of debugging a program until it does


exactly what you want.

Having a good programming/coding habit sufficiently helps.


Re-run a buggy program never helps!

54

HANDS-ON SESSION
Up to now we have gone through:
The basic python interface
Variables, types, and operators
Now you should be able to do some useful calculations!
Lets start our first round of
hands-on session now!

55

HANDS-ON SESSION
Practice 1:

Get your working environment ready now, and type in your first
hello world!.

56

HANDS-ON SESSION
Practice 2:

Use the python interpreter as a calculator, and calculate the


following simple math problems:
2
p

Given z = 1 + 3i , whats the real part and imaginary part?


Calculate the magnitude of the total gravitational force that is
acted on point mass A:

4 kg
G = 6.67384 1011 m3kg1s2
5m

3m
A: 5 kg

4m

57

3 kg

HOMEWORK #1
Exercise 1:

Prepare your working environment, run the following program


and take a screenshot (and upload it to the CEIBA system):

print 'Hello!'
print 'My name is XXX.'
print 'My student ID is YYY.'

Replace XXX with your name, YYY with your ID!

58

You might also like