Python Numpy (1) : Intro To Multi-Dimensional Array & Numerical Linear Algebra
Python Numpy (1) : Intro To Multi-Dimensional Array & Numerical Linear Algebra
Python Numpy (1) : Intro To Multi-Dimensional Array & Numerical Linear Algebra
Harry Lee
January 29, 2018
CEE 696
Table of contents
1. Introduction
2. Linear Algebra
1
Introduction
From the last lecture
import numpy as np
ibound = np.ones((NLAY,NROW,NCOL),dtype=np.int32)
We have used numpy package and its array objects for MODFLOW
model setup. Let’s dig into them.
2
Why Numpy?
3
Have you used MATLAB or R?
4
Next slides..
• Array creation
• Array access/slicing
• Array operations
5
Numpy Example
import numpy as np
Figure 1: http://pages.physics.cornell.edu/~myers/teaching/
ComputationalMethods/python/arrays.html
7
Numpy Array (1) - Creation
x = np.array([1.0, 2.0])
print(x.dtype) # datatype = float64
9
Numpy Array (3) - Modification
a = np.array([[1,2],[3,4]])
b = np.array(a) # create a new array
c = a # referencing
print(a)
print(b)
print(c)
a[0,0] = 10
print(a)
print(b)
print(c) # this is easy.. wait, what?
10
Reference/Shallow Copy vs. Deep Copy
x = np.array([1, 2, 3])
y = x
z = np.copy(x)
x[0] = 10
print(id(x),x)
print(id(y),y)
print(id(z),z)
11
Numpy Array (4) - Slice Notation CON’T
a[start:end]
a[start:end:step]
Make sure the [:end] value represents the first value that is not in
the selected slice.
# create an array
a = np.array([1,2,3,4,5,6,7,8,9,10])
a[:] # a copy of the whole array
a[0:10] # = a[0:] = a[:10] = a[:] = a[::]
a[0:10:2] # = a[:10:2] = a[::2]
# create an array
a = np.array([[1,2,3], [4,5,6], [7,8,9]])
b = a[:2, 1:3]
# This is IMPORTANT!!
print(a)
b[0, 0] = 10 # b[0, 0] from a[0, 1]
print(a) # print it.. wait, what?
13
Numpy Array (6) - Slice Notation CON’T
Make sure the dimension of your array is consistent with what you
thought!
14
Numpy Array (7) - Element Access
print(a)
15
Numpy Array (8) - Element Access
print(idx)
# in a single statement
print(a[a > 2])
16
Arrays (6) - Operations (1)
x = np.array([[1,2],[3,4]])
y = np.array([[5,6],[7,8]])
print(x+y)
print(np.add(x, y))
print(x-y)
print(np.subtract(x, y))
17
print(np.sqrt(x))
Arrays (6) - operations (2)
x = np.array([[1,2],[3,4]])
y = np.array([[5,6],[7,8]])
v = np.array([9,10])
w = np.array([11, 12])
# Matrix-vector product
print(x.dot(v))
print(np.dot(x, v))
# Matrix-matrix product
print(x.dot(y))
18
print(np.dot(x, y))
Linear Algebra
Solution to linear system
Ax = b
A is n by n matrix
b is n × 1 vector
x is n × 1 vector to solve
19
MODFLOW - Numerical Modeling (1)
Figure 2: cell (i,j,k) configuration for mass balance equation (from Fig. 2-2
Harbaugh [2005]) 20
MODFLOW - Numerical Modeling (2)
Figure 3: Flow into cell i,j,k from cell i,j-1,k (from Fig. 2-3 Harbaugh [2005])
∑
Combining with mass balance equation Qi = 0 (for steady stead)
for every cell will lead to the system of linear equations
Aϕ = f 21
numpy.linalg.inv
# solution of Ah = f
h = np.linalg.solve(A,f)
23
Connection to Quadratic Function Optimization
1
f(x) = xT Ax − bxT
2
24