computational mathematics with python · computationalmathematicswithpython unit5(cont):...
TRANSCRIPT
Computational Mathematics with PythonUnit 5 (cont): More on arrays, more on functions
Numerical Analysis, Lund UniversityLecturer: Claus Führer
2015
Numerical Analysis, Lund University, 2015 1
Solving a Linear SystemIf A is a matrix and b is a vector you solve the linear equation
A · x = b
using solve which has the syntax x = solve(A,b).
ExampleWe want to solve {
x1 + 2x2 = 13x1 + 4x2 = 4
from scipy. linalg import solve
A = array([[1., 2.],[3., 4.]])
b = array([1., 4.])x = solve(A,b)dot(A, x) # should be almost b
Numerical Analysis, Lund University, 2015 2
Slices
Slices are similar to that of lists and vectors except that there arenow two dimensions.
M[i,:] a vector filled by the row i of MM[:,j] a vector filled by the column j of M
M[2:4,:] slice 2 : 4 on the rows onlyM[2:4,1:4] slice on rows and columns
Omitting a dimensionIf you omit an index or a slice, Scipy assumes you are taking rowsonly.
M[3] is the third row of MM[1:3] is a matrix with the second and third rows of M .
Numerical Analysis, Lund University, 2015 3
Altering a Matrix
You may alter a matrix using slices or direct access.I M [ 2,3] = 2.I M [ 2, : ] = < a vector>I M [ 1 : 3, : ] = < a matrix>I M [ 1 : 4,2:5 ] = < a matrix>
The matrices and vectors above must have the right size to “fit” inthe matrix M .
Numerical Analysis, Lund University, 2015 4
Some technical terms
Rank The rank of an array is the number of indices used to identifyan element. A matrix has rank 2, a vector has rank 1, a scalarhas rank 0.Don’t confound this term with the rank used in LinearAlgebra.
Shape The shape is a tuple with the dimensions of the array: A 2× 3matrix is represented in Python by an array with shape (2, 3).
Length The number of elements in an array. An array with shape(2, 3) has length 6. An array with shape (2, ) (representing avector) has length 2.Don’t confound this term with the term length used in LinearAlgebra.
Numerical Analysis, Lund University, 2015 5
Rank of matrix slices
When slicing the rank of the result object is as follows:
access rank kindindex,index 0 scalarslice,index 1 vectorslice,slice 2 matrix
Example
0 1 2 3
4 5 6 7
8 9 10 11
0 1 2 3
4 5 6 7
8 9 10 11
0 1 2 3
4 5 6 7
8 9 10 11
0 1 2 3
4 5 6 7
8 9 10 11
0 1 2 3
4 5 6 7
8 9 10 11
access shape rank kindM [ : 2,1: - 1 ] (2, 2) 2 matrixM [ 1, : ] 4 1 vectorM [ 1,1] ∅ 0 scalarM [ 1 : 2, : ] (1, 4) 2 matrixM [ 1 : 2,1:2 ] (1, 1) 2 matrix
Numerical Analysis, Lund University, 2015 6
Reshaping
From a given tensor (vector or matrix) one may obtain anothertensor by reshaping.
Numerical Analysis, Lund University, 2015 7
Reshaping Example
A = arange (6)
A. reshape (1,6)
A. reshape (6,1)
A. reshape (2,3)
A. reshape (3,2)
0 1 2 3 4 5
0 1 2 3 4 5
0
1
2
3
4
5
0 1 2
3 4 5
0 1
2 3
4 5
Numerical Analysis, Lund University, 2015 8
Reshaping Trick
Note that Python can guess one of the new dimensions. Just givea negative integer for the dimension to be guessed:A = arange (12) # a vector of length 12
A. reshape (1,-1) # row matrixA. reshape (-1,1) # column matrix
A. reshape (3,-1) # 3,4 matrixA. reshape (-1,4) # same
Numerical Analysis, Lund University, 2015 9
Building Matrices
I Piling vectorsI Stacking vectorsI Stacking column matrices
The universal method to build matrices is concatenate. Thisfunction is called by several convenient functions
I hstack to stack matrices horizontallyI vstack to stack matrices verticallyI column_stack to stack vectors in columns
Numerical Analysis, Lund University, 2015 10
Stacking Vectors
v1 = array([1,2])v2 = array([3,4])
vstack ([v1 ,v2])
column_stack ([v1 ,v2])
1 2
3 4
1 3
2 4
Numerical Analysis, Lund University, 2015 11
sum, max, min
You may perform a number ofoperations on arrays, either onthe whole array, orcolumn-wise or row-wise. Themost common are
I maxI minI sum
Example
1 2 3 4
5 6 7 8
A.sum ()
36
1 2 3 4
5 6 7 8
A.sum(axis=0)
The result is a vector
6 8 10 12
1 2 3 4
5 6 7 8
A.sum(axis=1)
The result is a vector
10 26
Numerical Analysis, Lund University, 2015 12
Boolean Arrays
Numerical Analysis, Lund University, 2015 13
Boolean Arrays
One may use Boolean arrays to create a “template” for modifyinganother array:
B = array([[True , False],[False , True]]) # the template array
M = array([[2, 3],[1, 4]]) # the other array
M[B] = 0 # using the templateM # [[0, 3], [1, 0]]M[B] = 10 , 20M # [[10 , 3], [1, 20]]
Numerical Analysis, Lund University, 2015 14
Creating Boolean Arrays
It might be just as tedious to create the boolean array by handthan to change the array directly. There are however manymethods to create Boolean arrays.Any logical operator will create a Boolean array instead of aBoolean.M = array([[2, 3], [1, 4]])M > 2 # array ([[ False , True], [False , True ]])M == 0 # array ([[ False , False], [False , False ]])N = array([[2, 3], [0, 0]])M == N # array ([[ True , True], [False , False ]])...
This allows the elegant syntax:M[M>2] = 0# all the elements > 2 are replaced by 0
Numerical Analysis, Lund University, 2015 15
Comparing Arrays
Note that because array comparison create Boolean arrays, onecannot compare arrays directly.The solution is to use the methods all and any:A = array([[1,2],[3,4]])B = array([[1,2],[3,3]])A == B # creates array ([[ True , True], [True , False ]])(A == B).all () # False(A != B).any () # True
Numerical Analysis, Lund University, 2015 16
Boolean Operations
For the same reason as before you cannot use and, or nor not onBoolean arrays! Use the following replacement operators instead:
logic operator replacement for Bool arraysA and B A & BA or B A | Bnot A - A
a = array([True , True , False , False])b = array([True , False , True , False])
a and b # error!a & b # array ([True , False , False , False ])a | b # array ([True , Trues , True , False ])-a # array ([ False , False , True , True ])
Numerical Analysis, Lund University, 2015 17
Universal Functions
Numerical Analysis, Lund University, 2015 18
Universal Functions
DefinitionA universal function (or ufunc) is a function that operates onarrays in an element-by-element fashion. That is, a ufunc is a“vectorized” wrapper for a function that takes a fixed number ofscalar inputs and produces a fixed number of scalar outputs.
Examples:from scipy import *sincosexp
Numerical Analysis, Lund University, 2015 19
Vectorized Functions
Non-universal functions can be wrapped to behave like universalfuntions.This is done by the command vectorize.# A non - universal functiondef heaviside (x):
if x >= 0:return 1.
else:return 0.
heaviside ( linspace (-1,1,100)) # returns an error
vheaviside = vectorize ( heaviside ) # a new functionvheaviside ( linspace (-1,1,100)) # does the job
Numerical Analysis, Lund University, 2015 20
Vectorized Functions (Cont.)
xvals= linspace (-1,1,100)plot(xvals , vectorize ( heaviside )(xvals))axis([-1.5,1.5,-0.5,1.5])
Numerical Analysis, Lund University, 2015 21
More on Functions
Numerical Analysis, Lund University, 2015 22
Unpacking Arguments
Positional arguments remind us of listsKeyword arguments remind us of dictionaries
data = [[1,2], [3,4]]style = {’linewidth ’:3, ’marker ’:’o’, ’color ’:’green ’}
Star operators unpack these to form a valid parameter listplot(*data , ** style)
∗ unpacks a list to positional arguments∗∗ unpacks dictionaries to keyword arguments
Numerical Analysis, Lund University, 2015 23
Passing (tunneling) argumentsAlso in the definition of functions you might find these constructs.This is often used to pass arguments through a functiondef outer(f, x, *args , ** keywords ):
return f(x, *args , ** keywords )
def inner(x, y, z, u):print(y, z)print(u)return x ** 2
A callL=[1, 2]D={’u’:15}outer(inner , 3, *L, ** D)
Equivalently:outer(inner , 3, 1, 2, u=15)
Note, the function outer cannot know how many arguments itneeds to provide a full parameter list to the “inner” function f.
Numerical Analysis, Lund University, 2015 24
Return
The return statement returns a single object!
def my_func (x):return 1,2,3,4,5,6
What is the object that is returned here? Which type does it have?(see Unit 3)
Statements after the return statement are ignored:
def my_func (x):return 1,2,3z = 25 # ignored
def my_func (x):if x > 0:
return 1else:
return -1z = 25 # ignored
Numerical Analysis, Lund University, 2015 25
No Return
A function without a return statement returns None:
def my_func (x):z = 2*x
a = my_func (10.)
type(a) # <type ’NoneType ’>a == None # true
Numerical Analysis, Lund University, 2015 26
Functions are objects
Functions are objects, they can be deleted, reassigned, copied ...
def square (x):""" Return the square of ‘x‘"""return x ** 2
square (4) # 16sq = square # now sq is the same as squaresq(4) # 16del square # ‘square ‘ doesn ’t exist anymoreprint ( newton (sq , .2)) # passing as argument
Numerical Analysis, Lund University, 2015 27
Partial ApplicationPartial application = closures
In mathematics we often “freeze” a parameter of a function:
fω(x) = f (x, ω) = sin(ωx)
In Python there are many possibilities to do this, here is one ...def make_sine (omega):
def f(x):return sin(omega*x)
return f
fomega = make_sin (omega)
In order to understand this solution, recallI the scope of a variable and references to variables out of scopeI functions are objects
Numerical Analysis, Lund University, 2015 28
Anonymous Functions: the lambda keyword
With λ-functions one has a handy tool for making one-linefunction definitions:f = lambda x: 3.*x ** 2 + 2.*x + 0.5f(3) # returns 33.5g = lambda x,y: 3.*x-2.*yg(1,1) # returns 1.0
Example for a common application, compute∫ 1
0 x2 + 5 dx:
import scipy. integrate as sisi.quad( lambda x: x ** 2+5,0,1)
Numerical Analysis, Lund University, 2015 29
Partial Applications and λ
fω(x) = f (x, ω) = sin(ωx)
.... now simply becomes
omega = 3.fomega = lambda x: sin(omega*x)fomega (1.) # returns 0.14112 ...
Numerical Analysis, Lund University, 2015 30