SIAM Workshop Introduction To Python For Mathematicians and Scientists
SIAM Workshop Introduction To Python For Mathematicians and Scientists
SIAM Workshop Introduction To Python For Mathematicians and Scientists
Mike Sussman
[email protected]
http://www.math.pitt.edu/%7esussmanm
1 / 56
Topics
Introduction
Python, the language
Python language specifics
Python basics, Example 1
Functions, flow control, and import, Example 2
Watch out, Alexander!
Classes
A finite element program
Gauß integration, Example 3
BVP by FEM
Shape functions, Example 4
Code, Example 4
FEM code, Example 5
Python language comments
FEniCS
2 / 56
Who am I?
3 / 56
Objectives
4 / 56
References
5 / 56
Getting Python
6 / 56
Topics
Introduction
Python, the language
Python language specifics
Python basics, Example 1
Functions, flow control, and import, Example 2
Watch out, Alexander!
Classes
A finite element program
Gauß integration, Example 3
BVP by FEM
Shape functions, Example 4
Code, Example 4
FEM code, Example 5
Python language comments
FEniCS
7 / 56
What is Python?
8 / 56
Python and modules
9 / 56
Python for scientific use
I numpy
I scipy
I matplotlib.pylab
I sympy
I SAGE
10 / 56
Topics
Introduction
Python, the language
Python language specifics
Python basics, Example 1
Functions, flow control, and import, Example 2
Watch out, Alexander!
Classes
A finite element program
Gauß integration, Example 3
BVP by FEM
Shape functions, Example 4
Code, Example 4
FEM code, Example 5
Python language comments
FEniCS
11 / 56
Running Python
12 / 56
File structure and line syntax
13 / 56
Topics
Introduction
Python, the language
Python language specifics
Python basics, Example 1
Functions, flow control, and import, Example 2
Watch out, Alexander!
Classes
A finite element program
Gauß integration, Example 3
BVP by FEM
Shape functions, Example 4
Code, Example 4
FEM code, Example 5
Python language comments
FEniCS
14 / 56
Python basics
example1.py
1. Debugger
2. x/3, -x/3
3. float(x)/3
4. conjugate(z), abs(w), w*w
5. y0==y1
6. 2**100 (answer is long)
15 / 56
Basic data types
16 / 56
Basic operations
I +, -, *, /
I ** (raise to power)
I % (remainder)
I and, or, not
I >, <, >=, <=, ==, != (logical comparison)
17 / 56
Python array-type data types
18 / 56
Getting help
>>> help(complex)
class complex(object)
| complex(real[, imag]) -> complex number
|
| Create a complex number from a real part and an optional imaginary part.
| This is equivalent to (real + imag*1j) where imag defaults to 0.
|
| Methods defined here:
|
| __abs__(...)
| x.__abs__() <==> abs(x)
|
| __add__(...)
| x.__add__(y) <==> x+y
|
| __div__(...)
| x.__div__(y) <==> x/y
|
| conjugate(...)
| complex.conjugate() -> complex
|
| Return the complex conjugate of its argument. (3-4j).conjugate() == 3+4j
| -----------------------------------------------
| Data descriptors defined here:
|
| imag
| the imaginary part of a complex number
|
| real
| the real part of a complex number
19 / 56
Topics
Introduction
Python, the language
Python language specifics
Python basics, Example 1
Functions, flow control, and import, Example 2
Watch out, Alexander!
Classes
A finite element program
Gauß integration, Example 3
BVP by FEM
Shape functions, Example 4
Code, Example 4
FEM code, Example 5
Python language comments
FEniCS
20 / 56
Functions, flow control, and import
example2.py
1. Debugger
2. i/10
3. n, term, partialSum out of workspace after return!
21 / 56
Functions
22 / 56
Flow control
23 / 56
Importing and naming
24 / 56
Pylab in Spyder
25 / 56
Topics
Introduction
Python, the language
Python language specifics
Python basics, Example 1
Functions, flow control, and import, Example 2
Watch out, Alexander!
Classes
A finite element program
Gauß integration, Example 3
BVP by FEM
Shape functions, Example 4
Code, Example 4
FEM code, Example 5
Python language comments
FEniCS
26 / 56
Subscripts
27 / 56
Subscripts
27 / 56
Subscripts
27 / 56
Subscripts
27 / 56
Subscripts
27 / 56
Equals, Copies, and Deep Copies
>>> import copy as cp
28 / 56
Equals, Copies, and Deep Copies
>>> import copy as cp
>>> x=[1,2]
>>> y=[3,4,x]
>>> z=y
>>> print "x=",x," y=",y," z=",z
x= [1, 2] y= [3, 4, [1, 2]] z= [3, 4, [1, 2]]
28 / 56
Equals, Copies, and Deep Copies
>>> import copy as cp
>>> x=[1,2]
>>> y=[3,4,x]
>>> z=y
>>> print "x=",x," y=",y," z=",z
x= [1, 2] y= [3, 4, [1, 2]] z= [3, 4, [1, 2]]
>>> c=cp.copy(y)
>>> d=cp.deepcopy(y)
>>> print "y=",y," z=",z," c=",c," d=",d
y= [3, 4, [1, 2]] z= [3, 4, [1, 2]] c= [3, 4, [1, 2]] d= [3, 4, [1, 2]]
28 / 56
Equals, Copies, and Deep Copies
>>> import copy as cp
>>> x=[1,2]
>>> y=[3,4,x]
>>> z=y
>>> print "x=",x," y=",y," z=",z
x= [1, 2] y= [3, 4, [1, 2]] z= [3, 4, [1, 2]]
>>> c=cp.copy(y)
>>> d=cp.deepcopy(y)
>>> print "y=",y," z=",z," c=",c," d=",d
y= [3, 4, [1, 2]] z= [3, 4, [1, 2]] c= [3, 4, [1, 2]] d= [3, 4, [1, 2]]
>>> y[0]="*"
>>> print "y=",y," z=",z," c=",c," d=",d
28 / 56
Equals, Copies, and Deep Copies
>>> import copy as cp
>>> x=[1,2]
>>> y=[3,4,x]
>>> z=y
>>> print "x=",x," y=",y," z=",z
x= [1, 2] y= [3, 4, [1, 2]] z= [3, 4, [1, 2]]
>>> c=cp.copy(y)
>>> d=cp.deepcopy(y)
>>> print "y=",y," z=",z," c=",c," d=",d
y= [3, 4, [1, 2]] z= [3, 4, [1, 2]] c= [3, 4, [1, 2]] d= [3, 4, [1, 2]]
>>> y[0]="*"
>>> print "y=",y," z=",z," c=",c," d=",d
y= ["*", 4, [1, 2]] z= ["*", 4, [1, 2]] c= [3, 4, [1, 2]] d= [3, 4, [1, 2]]
28 / 56
Equals, Copies, and Deep Copies
>>> import copy as cp
>>> x=[1,2]
>>> y=[3,4,x]
>>> z=y
>>> print "x=",x," y=",y," z=",z
x= [1, 2] y= [3, 4, [1, 2]] z= [3, 4, [1, 2]]
>>> c=cp.copy(y)
>>> d=cp.deepcopy(y)
>>> print "y=",y," z=",z," c=",c," d=",d
y= [3, 4, [1, 2]] z= [3, 4, [1, 2]] c= [3, 4, [1, 2]] d= [3, 4, [1, 2]]
>>> y[0]="*"
>>> print "y=",y," z=",z," c=",c," d=",d
y= ["*", 4, [1, 2]] z= ["*", 4, [1, 2]] c= [3, 4, [1, 2]] d= [3, 4, [1, 2]]
>>> z[2][0]=9
>>> print "y=",y," z=",z," c=",c," d=",d
28 / 56
Equals, Copies, and Deep Copies
>>> import copy as cp
>>> x=[1,2]
>>> y=[3,4,x]
>>> z=y
>>> print "x=",x," y=",y," z=",z
x= [1, 2] y= [3, 4, [1, 2]] z= [3, 4, [1, 2]]
>>> c=cp.copy(y)
>>> d=cp.deepcopy(y)
>>> print "y=",y," z=",z," c=",c," d=",d
y= [3, 4, [1, 2]] z= [3, 4, [1, 2]] c= [3, 4, [1, 2]] d= [3, 4, [1, 2]]
>>> y[0]="*"
>>> print "y=",y," z=",z," c=",c," d=",d
y= ["*", 4, [1, 2]] z= ["*", 4, [1, 2]] c= [3, 4, [1, 2]] d= [3, 4, [1, 2]]
>>> z[2][0]=9
>>> print "y=",y," z=",z," c=",c," d=",d
y= ["*", 4, [9, 2]] z= ["*", 4, [9, 2]] c= [3, 4, [9, 2]] d= [3, 4, [1, 2]]
28 / 56
Equals, Copies, and Deep Copies
>>> import copy as cp
>>> x=[1,2]
>>> y=[3,4,x]
>>> z=y
>>> print "x=",x," y=",y," z=",z
x= [1, 2] y= [3, 4, [1, 2]] z= [3, 4, [1, 2]]
>>> c=cp.copy(y)
>>> d=cp.deepcopy(y)
>>> print "y=",y," z=",z," c=",c," d=",d
y= [3, 4, [1, 2]] z= [3, 4, [1, 2]] c= [3, 4, [1, 2]] d= [3, 4, [1, 2]]
>>> y[0]="*"
>>> print "y=",y," z=",z," c=",c," d=",d
y= ["*", 4, [1, 2]] z= ["*", 4, [1, 2]] c= [3, 4, [1, 2]] d= [3, 4, [1, 2]]
>>> z[2][0]=9
>>> print "y=",y," z=",z," c=",c," d=",d
y= ["*", 4, [9, 2]] z= ["*", 4, [9, 2]] c= [3, 4, [9, 2]] d= [3, 4, [1, 2]]
>>> c[2][1]=’c’
>>> print "y=",y," z=",z," c=",c," d=",d
28 / 56
Equals, Copies, and Deep Copies
>>> import copy as cp
>>> x=[1,2]
>>> y=[3,4,x]
>>> z=y
>>> print "x=",x," y=",y," z=",z
x= [1, 2] y= [3, 4, [1, 2]] z= [3, 4, [1, 2]]
>>> c=cp.copy(y)
>>> d=cp.deepcopy(y)
>>> print "y=",y," z=",z," c=",c," d=",d
y= [3, 4, [1, 2]] z= [3, 4, [1, 2]] c= [3, 4, [1, 2]] d= [3, 4, [1, 2]]
>>> y[0]="*"
>>> print "y=",y," z=",z," c=",c," d=",d
y= ["*", 4, [1, 2]] z= ["*", 4, [1, 2]] c= [3, 4, [1, 2]] d= [3, 4, [1, 2]]
>>> z[2][0]=9
>>> print "y=",y," z=",z," c=",c," d=",d
y= ["*", 4, [9, 2]] z= ["*", 4, [9, 2]] c= [3, 4, [9, 2]] d= [3, 4, [1, 2]]
>>> c[2][1]=’c’
>>> print "y=",y," z=",z," c=",c," d=",d
y= ["*", 4, [9, c]] z= ["*", 4, [9, c]] c= [3, 4, [9, c]] d= [3, 4, [1, 2]]
>>> x
[9, c]
28 / 56
Equals, Copies, and Deep Copies
>>> import copy as cp
>>> x=[1,2]
>>> y=[3,4,x]
>>> z=y
>>> print "x=",x," y=",y," z=",z
x= [1, 2] y= [3, 4, [1, 2]] z= [3, 4, [1, 2]]
>>> c=cp.copy(y)
>>> d=cp.deepcopy(y)
>>> print "y=",y," z=",z," c=",c," d=",d
y= [3, 4, [1, 2]] z= [3, 4, [1, 2]] c= [3, 4, [1, 2]] d= [3, 4, [1, 2]]
>>> y[0]="*"
>>> print "y=",y," z=",z," c=",c," d=",d
y= ["*", 4, [1, 2]] z= ["*", 4, [1, 2]] c= [3, 4, [1, 2]] d= [3, 4, [1, 2]]
>>> z[2][0]=9
>>> print "y=",y," z=",z," c=",c," d=",d
y= ["*", 4, [9, 2]] z= ["*", 4, [9, 2]] c= [3, 4, [9, 2]] d= [3, 4, [1, 2]]
>>> c[2][1]=’c’
>>> print "y=",y," z=",z," c=",c," d=",d
y= ["*", 4, [9, c]] z= ["*", 4, [9, c]] c= [3, 4, [9, c]] d= [3, 4, [1, 2]]
>>> x
[9, c]
28 / 56
Equals, Copies, and Deep Copies
>>> import copy as cp
>>> x=[1,2]
>>> y=[3,4,x]
>>> z=y
>>> print "x=",x," y=",y," z=",z
x= [1, 2] y= [3, 4, [1, 2]] z= [3, 4, [1, 2]]
>>> c=cp.copy(y)
>>> d=cp.deepcopy(y)
>>> print "y=",y," z=",z," c=",c," d=",d
y= [3, 4, [1, 2]] z= [3, 4, [1, 2]] c= [3, 4, [1, 2]] d= [3, 4, [1, 2]]
>>> y[0]="*"
>>> print "y=",y," z=",z," c=",c," d=",d
y= ["*", 4, [1, 2]] z= ["*", 4, [1, 2]] c= [3, 4, [1, 2]] d= [3, 4, [1, 2]]
>>> z[2][0]=9
>>> print "y=",y," z=",z," c=",c," d=",d
y= ["*", 4, [9, 2]] z= ["*", 4, [9, 2]] c= [3, 4, [9, 2]] d= [3, 4, [1, 2]]
>>> c[2][1]=’c’
>>> print "y=",y," z=",z," c=",c," d=",d
y= ["*", 4, [9, c]] z= ["*", 4, [9, c]] c= [3, 4, [9, c]] d= [3, 4, [1, 2]]
>>> x
[9, c]
28 / 56
Equals, Copies, and Deep Copies
>>> import copy as cp
>>> x=[1,2]
>>> y=[3,4,x]
>>> z=y
>>> print "x=",x," y=",y," z=",z
x= [1, 2] y= [3, 4, [1, 2]] z= [3, 4, [1, 2]]
>>> c=cp.copy(y)
>>> d=cp.deepcopy(y)
>>> print "y=",y," z=",z," c=",c," d=",d
y= [3, 4, [1, 2]] z= [3, 4, [1, 2]] c= [3, 4, [1, 2]] d= [3, 4, [1, 2]]
>>> y[0]="*"
>>> print "y=",y," z=",z," c=",c," d=",d
y= ["*", 4, [1, 2]] z= ["*", 4, [1, 2]] c= [3, 4, [1, 2]] d= [3, 4, [1, 2]]
>>> z[2][0]=9
>>> print "y=",y," z=",z," c=",c," d=",d
y= ["*", 4, [9, 2]] z= ["*", 4, [9, 2]] c= [3, 4, [9, 2]] d= [3, 4, [1, 2]]
>>> c[2][1]=’c’
>>> print "y=",y," z=",z," c=",c," d=",d
y= ["*", 4, [9, c]] z= ["*", 4, [9, c]] c= [3, 4, [9, c]] d= [3, 4, [1, 2]]
>>> x
[9, c]
29 / 56
A Class is a generalized data type
30 / 56
Classes define members’ “attributes”
31 / 56
Examples of attributes
import numpy as np
x=np.array( [0, 0.1, 0.2, 0.4, 0.9, 3.14] )
32 / 56
Operators can be overridden
33 / 56
Topics
Introduction
Python, the language
Python language specifics
Python basics, Example 1
Functions, flow control, and import, Example 2
Watch out, Alexander!
Classes
A finite element program
Gauß integration, Example 3
BVP by FEM
Shape functions, Example 4
Code, Example 4
FEM code, Example 5
Python language comments
FEniCS
34 / 56
Topics
Introduction
Python, the language
Python language specifics
Python basics, Example 1
Functions, flow control, and import, Example 2
Watch out, Alexander!
Classes
A finite element program
Gauß integration, Example 3
BVP by FEM
Shape functions, Example 4
Code, Example 4
FEM code, Example 5
Python language comments
FEniCS
35 / 56
Gauß integration
R1
I Integrate Q = 0
f (ξ)dξ
P3
I Approximate it as Q ≈ i=1 wi f (gi )
I wi and gi come from reference materials.
36 / 56
Example 3
example3.py
I Function of a vector returns a vector
I np.not
I Extensive testing!
I y=0.0*x+1.0 ⇐⇒ y=np.zeros_like(x) ⇐⇒
y=np.zeros( shape(x) )
I append() is a List attribute (function)
37 / 56
Topics
Introduction
Python, the language
Python language specifics
Python basics, Example 1
Functions, flow control, and import, Example 2
Watch out, Alexander!
Classes
A finite element program
Gauß integration, Example 3
BVP by FEM
Shape functions, Example 4
Code, Example 4
FEM code, Example 5
Python language comments
FEniCS
38 / 56
A boundary value problem by the finite element
method
−u 00 + u = f on [0, L]
0
u (0) = 0 = u 0 (L)
39 / 56
FEM formulation
−u 00 + u = f (x) u 0 (0) = u 0 (L) = 0
Multiply through by a function v and integrate
Z L h iL Z L Z L
u 0 (x)v 0 (x)dx + u 0 (x)v (x) + u(x)v (x)dx = f (x)v (x)dx
0 0 0 0
AU = F.
40 / 56
Do integrations elementwise
41 / 56
Topics
Introduction
Python, the language
Python language specifics
Python basics, Example 1
Functions, flow control, and import, Example 2
Watch out, Alexander!
Classes
A finite element program
Gauß integration, Example 3
BVP by FEM
Shape functions, Example 4
Code, Example 4
FEM code, Example 5
Python language comments
FEniCS
42 / 56
What do the shape functions look like?
ξ = (x − k ∆x)/∆x
φ0k (x) = 2.0(ξ − 0.5)(ξ − 1.0),
φ1k (x) = 4.0ξ(1.0 − ξ),
φ2k (x) = 2.0ξ(ξ − 0.5)
I Outside ek , φik = 0
43 / 56
Shape functions are a “partition of unity”
44 / 56
Topics
Introduction
Python, the language
Python language specifics
Python basics, Example 1
Functions, flow control, and import, Example 2
Watch out, Alexander!
Classes
A finite element program
Gauß integration, Example 3
BVP by FEM
Shape functions, Example 4
Code, Example 4
FEM code, Example 5
Python language comments
FEniCS
45 / 56
Example 4
example4.py
I φ21 and φ02 together make φ4
I 2D subscripting: (Amat1[ m, n ])
I plt.plot and plt.hold are like Matlab
I np.linspace is like Matlab
46 / 56
Topics
Introduction
Python, the language
Python language specifics
Python basics, Example 1
Functions, flow control, and import, Example 2
Watch out, Alexander!
Classes
A finite element program
Gauß integration, Example 3
BVP by FEM
Shape functions, Example 4
Code, Example 4
FEM code, Example 5
Python language comments
FEniCS
47 / 56
Example 5
example5.py
I Solves −u 00 + u = f in weak form
R 0 0 R R
u v + uv = fv with
Neumann boundary conditions on [0, 5]
I Two tests: f0 (x) = 1 and f1 (x) = x
I Two exact solutions: u0 (x) = x and
u1 (x) = cosh(5)−1
sinh(5) cosh(x) − sinh(x) + x
48 / 56
Convergence results
Convergence results
N error ratio
5 0.000142449953558
10 8.60661737944e-06 16.55121254702143
Fourth-order
20 5.21196552761e-07 16.51318937903001
40 3.19766785929e-08 16.29927108429344
80 1.97897364593e-09 16.1582134550725
160 1.23080146312e-10 16.0787397905218
convergence is too high, a consequence of “superconvergence.”
49 / 56
Topics
Introduction
Python, the language
Python language specifics
Python basics, Example 1
Functions, flow control, and import, Example 2
Watch out, Alexander!
Classes
A finite element program
Gauß integration, Example 3
BVP by FEM
Shape functions, Example 4
Code, Example 4
FEM code, Example 5
Python language comments
FEniCS
50 / 56
Some differences with C++
I Indentation
I **, and, or
I long
I (=) and copying
I Interpreted vs. compiled
I No private variables
I Programmer must pretend not to see variables starting with __
I No const
I Cannot have two functions with same name
I Allowed in C++ if signatures different
I Automatic garbage collection
I Variable types are implicit
I Constructor syntax
I Extra parameter self
51 / 56
Some versions/variants of Python
52 / 56
Topics
Introduction
Python, the language
Python language specifics
Python basics, Example 1
Functions, flow control, and import, Example 2
Watch out, Alexander!
Classes
A finite element program
Gauß integration, Example 3
BVP by FEM
Shape functions, Example 4
Code, Example 4
FEM code, Example 5
Python language comments
FEniCS
53 / 56
FEniCS
54 / 56
DOLFIN classes
55 / 56
FEniCS example
from dolfin import *
# Compute solution
u = Function(V)
solve(a == L, u, bc)
# Compute solution
u = Function(V)
solve(a == L, u, bc)
# Compute solution
u = Function(V)
solve(a == L, u, bc)
# Compute solution
u = Function(V)
solve(a == L, u, bc)
# Compute solution
u = Function(V)
solve(a == L, u, bc)
# Compute solution
u = Function(V)
solve(a == L, u, bc)
# Compute solution
u = Function(V)
solve(a == L, u, bc)
# Compute solution
u = Function(V)
solve(a == L, u, bc)
# Compute solution
u = Function(V)
solve(a == L, u, bc)
# Compute solution
u = Function(V)
solve(a == L, u, bc)
# Compute solution
u = Function(V)
solve(a == L, u, bc)
# Compute solution
u = Function(V)
solve(a == L, u, bc)
# Compute solution
u = Function(V)
solve(a == L, u, bc)