unit4: functions numericalanalysis,lunduniversity 2015 · computationalmathematicswithpython unit4:...

33
Computational Mathematics with Python Unit 4: Functions Numerical Analysis, Lund University Lecturers: Claus Führer, Alexandros Sopasakis 2015 Numerical Analysis, Lund University, 2015 1

Upload: others

Post on 26-Jul-2020

25 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Computational Mathematics with PythonUnit 4: Functions

Numerical Analysis, Lund UniversityLecturers: Claus Führer, Alexandros Sopasakis

2015

Numerical Analysis, Lund University, 2015 1

Page 2: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Functions

Numerical Analysis, Lund University, 2015 2

Page 3: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Functions in MathematicsIn Mathematics:A function is written as a map, that uniquelyassigns an element y from the range R to every element x fromthe domain D.

f : x 7→ y.

f is the functionx is its argumenty is its (return) value

There can be several arguments of different type:

Considerf (g, a, b) =

∫ b

ag(x)dx

The arguments are not interchangable. Position matters.

Numerical Analysis, Lund University, 2015 3

Page 4: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Functions in Python

Definition of a function:def f(x1 , x2 , x3):

..... # some computationsy = ... #return y

Evaluation (call) of a function:f(17 , 18 , -2)f([1, 2, 3], {’Tol ’: 1.e-10}, ’ro’)...

Wording:x1, x2, x3 are called function parameters (needed for the definition)17, 18, -2 are called arguments (needed for the evaluation)

Numerical Analysis, Lund University, 2015 4

Page 5: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Passing Arguments

Consider:def subtract (x1 , x2):

return x1 - x2

Passing arguments by position:subtract (1, 2) # returns -1

Position matters.

Passing arguments by keyword:subtract (x2=2, x1=1) # returns -1

Position doesn’t matter.

Numerical Analysis, Lund University, 2015 5

Page 6: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Scope of variables I

Variables defined inside the function are said to belong to thefunction’s scope.They are unknown outside the function.

Definition:def mult2(x):

c = 2.return c*x

Evaluation:mult2(20.) # returns 40c # returns NameError : ’c’

not defined

Numerical Analysis, Lund University, 2015 6

Page 7: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Scope of variables II

Compare:

a is a parameter of thefunctiondef multiply (x, a):

return a*x

a is referenced from outsidethe function’s scope:a = 3def multiply (x):

return a*x

Discuss the (dis-)advantages of these versions!

Numerical Analysis, Lund University, 2015 7

Page 8: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Scope of variables III

Variables with the same name belonging to different scopes aredifferent variables:

a = 3def multiply (x):

a = 17return a*x

multiply (2) # returns 34a # returns (still) 3

Numerical Analysis, Lund University, 2015 8

Page 9: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Local variables

Similarly, immutable variables are copied when calling a function.You cannot change an input variable by changing a local variablelike this:a = 3def f(x):

x = 2return x

print (f(a)) # 2print (a) # 3

However, this can be done for mutable variables like lists,dictionaries, etc.As a principle, never change input variables within a function.

Numerical Analysis, Lund University, 2015 9

Page 10: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Default Argumentsdefault (eng) = standard (sv)

The standard way ...def mult(x, c):

return c*y

a typical cally=mult(2., 13.)# returns 26y=mult(c=13., x=2.)# returns 26

... now with defaultsdef mult(x, c=1.0):

return c*y

possible callsy=mult(2., 13.)# returns 26y=mult(2.)# returns 2

In the definition of the function mandatory parameters mustprecede optional parameters (those with default values).Why?

Numerical Analysis, Lund University, 2015 10

Page 11: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Arguments, Parameters – Summary

Make sure that you understood the differenceI between arguments and parametersI between function definition and function evaluation (call)I between positional arguments and keyword arguments

Numerical Analysis, Lund University, 2015 11

Page 12: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Docstrings

All functions (and everything else) should be documented carefully:

A doctring is the leading comment in a function (or class):

def newton (f, x0):"""Newton ’s method for computing a zero of a functionon input:f ( function ) given function f(x)x0 (float) initial guesson return :y (float) the approximated zero of f"""...

help(newton) in Python or newton? in IPython displays thedocstring as a help text.

Numerical Analysis, Lund University, 2015 12

Page 13: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Computational Mathematics with PythonUnit 5: Linear Algebra

Numerical Analysis, Lund UniversityLecturers: Claus Führer, Alexandros Sopasakis

2015

Numerical Analysis, Lund University, 2015 13

Page 14: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Vectors

Numerical Analysis, Lund University, 2015 14

Page 15: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

The array type

Lists are almost like vectors but the operations on list are not thelinear algebra operation.

DefinitionAn array represents a vector or a matrix in linear algebra. It isoften initialised from a list or another vector. Operations + , * ,/ , - are all elementwise. dot is used for the scalar product.

Numerical Analysis, Lund University, 2015 15

Page 16: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Vector usage

Examplevec = array([1., 3.]) # a vector in the plane2*vec # array ([2., 6.])vec * vec # array ([1., 9.])vec/2 # array ([0.5, 1.5])norm(vec) # normdot(vec , vec) # scalar product

Numerical Analysis, Lund University, 2015 16

Page 17: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Vectors are similar to lists

I Access vectors via their indicesv = array([1., 2., 3.])v[0] # 1.

I The length of a vector is still obtained by the function len.len(v) # 3

I Parts of vectors using slicesv[:2] # array ([1., 2.])

I Replace parts of vectors using slicesv[:2] = [10 , 20]v # array ([10., 20., 3.])

Numerical Analysis, Lund University, 2015 17

Page 18: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Vectors are similar to lists

I Access vectors via their indicesv = array([1., 2., 3.])v[0] # 1.

I The length of a vector is still obtained by the function len.len(v) # 3

I Parts of vectors using slicesv[:2] # array ([1., 2.])

I Replace parts of vectors using slicesv[:2] = [10 , 20]v # array ([10., 20., 3.])

Numerical Analysis, Lund University, 2015 17

Page 19: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Vectors are not lists!Operations are not the same:

I Operations + and * are differentI More operations are defined: - , /I Many functions act elementwise on vectors: exp, sin, sqrt,

etc.I Scalar product with dot

I Vectors have a fixed size: no append methodI Only one type throughout the whole vector

(usually float, complex, int or bool)I Vector slices are views:

v = array([1., 2., 3.])v1 = v[:2] # v is array ([1.,2.])v1[0] = 0. # if v1 is changed ...v # ... v is changed too: array ([0., 2., 3.])

Numerical Analysis, Lund University, 2015 18

Page 20: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

More examplesv1 = array([1., 2., 3.]) # don ’t forget the dots!v2 = array([2, 0, 1.]) # one dot is enoughv1 + v2; v1 / v2; v1 - v2; v1 * v2

3*v13*v1 + 2*v2

dot(v1 , v2) # scalar productcos(v1) # cosine , elementwise

# accessv1[0] # 1.v1[0] = 10

# slicesv1[:2] # array ([10., 2.])v1[:2] = [0, 1] # now v1 == array ([0.,1.,3.])v1[:2] = [1,2,3] # error!

Numerical Analysis, Lund University, 2015 19

Page 21: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Creating Vectors (linspace)

The linspace method is a convenient way to create equallyspaced arrays.xs = linspace (0, 10 , 200) # 200 points between 0 and

10xs[0] # the first point is 0xs[-1] # the last is 10

So for example the plot of the sine function between 0 and 10 willbe obtain by:plot(xs , sin(xs))

Numerical Analysis, Lund University, 2015 20

Page 22: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Creating Vectors (zeros, ones, ...)

Some handy methods to quickly create vectors:zeros zeros(n) creates a vector of size n filled with zerosones ones(n) is the same filled with onesrand rand(n) creates a vector with uniformly distributed random

numbers between 0 and 1empty empty(n) creates an “empty” vector of size n

(try it!)

Numerical Analysis, Lund University, 2015 21

Page 23: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Concatenating Vectors

Since the + operation is redefined we need a means toconcatenate vectors. This is where the command hstack comes tohelp. hstack([v1, v2,..., vn]) concatenates the vectors v1,v2, . . . , vn.

Symplectic permutationWe have a vector of size 2n. We want to permute the first halfwith the second half of the vector with sign change:

(x1, x2, . . . , xn , xn+1, . . . , x2n) 7→ (−xn+1,−xn+2, . . . ,−x2n , x1, . . . , xn)

def symp(v):n = len(v) // 2 # use the integer division //return hstack ([-v[-n:], v[:n]])

Numerical Analysis, Lund University, 2015 22

Page 24: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Vectorized Functions

Note that not all functions may be applied on vectors. For instancethis one:def const(x):

return 1

We will see later how to automatically vectorize a function so thatit works componentwise on vectors.

Numerical Analysis, Lund University, 2015 23

Page 25: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Matrices

Numerical Analysis, Lund University, 2015 24

Page 26: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Matrices as Lists of Lists

DefinitionMatrices are represented by arrays of lists of rows, which are listsas well.

# the identity matrix in 2Did = array([[1., 0.], [0., 1.]])# Python allows this:id = array([[1., 0.],

[0., 1.]])# which is more readable

Numerical Analysis, Lund University, 2015 25

Page 27: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Accessing Matrix Entries

Matrix coefficients are accessed with two indices:M = array([[1., 2.],[3.,4.]])M[0,0] # first row , first column : 1.M[-1,0] # last row , first column : 3.

Numerical Analysis, Lund University, 2015 26

Page 28: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Creating Matrices

Some convenient methods to create matrices are:eye eye(n) is the identity matrix of size n

zeros zeros([n,m]) fills an n ×m matrix with zerosrand rand(n,m) is the same with random values

empty empty([n,m]) same with “empty” values

Numerical Analysis, Lund University, 2015 27

Page 29: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Shape

The shape of a matrix is the tuple of its dimensions. The shape ofan n ×m matrix is (n,m). It is given by the method shape:M = eye(3)M.shape # (3, 3)

V = array([1., 2., 1., 4.])V.shape # (4,) <- tuple with one element

Tip:zeros(A.shape) returns a matrix of the same shape as Acontaining only zeros.rand(*A.shape) does the same but with random values

Recall the difference between the argumentsA.shape and * A.shape

Numerical Analysis, Lund University, 2015 28

Page 30: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Transpose

The transpose of a matrix Aij is a matrix B such that

Bij = Aji

By transposing a matrix you switch the two shape elements.A = ...A.shape # 3,4

B = A.T # A transposeB.shape # 4,3

Note: B is just a “view” of A. Altering B changes A.

Numerical Analysis, Lund University, 2015 29

Page 31: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Matrix vector multiplication

The mathematical concept of reduction:∑j

aijxj

is translated in Python in the function dot:angle = pi/3M = array([[cos(angle), -sin(angle)],

[sin(angle), cos(angle)]])V = array([1., 0.])Y = dot(M, V) # the product M.V

Elementwise vs. matrix multiplicationThe multiplication operator * is always elementwise. It hasnothing to do with the dot operation. A * V is a legal operationwhich will be explained later on.

Numerical Analysis, Lund University, 2015 30

Page 32: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Matrix vector multiplication

The mathematical concept of reduction:∑j

aijxj

is translated in Python in the function dot:angle = pi/3M = array([[cos(angle), -sin(angle)],

[sin(angle), cos(angle)]])V = array([1., 0.])Y = dot(M, V) # the product M.V

Elementwise vs. matrix multiplicationThe multiplication operator * is always elementwise. It hasnothing to do with the dot operation. A * V is a legal operationwhich will be explained later on.

Numerical Analysis, Lund University, 2015 30

Page 33: Unit4: Functions NumericalAnalysis,LundUniversity 2015 · ComputationalMathematicswithPython Unit4: Functions NumericalAnalysis,LundUniversity Lecturers: Claus Führer, Alexandros

Dot product

vector vector s =∑

i xiyi s = dot(x,y)

matrix vector yi =∑

j Aijxj y = dot(A,x)

matrix matrix Cij =∑

k AikBkj C = dot(A,B)

vector matrix yj =∑

i xiAij y = dot(x,A)

Numerical Analysis, Lund University, 2015 31