TwoDArrays
TwoDArrays
Two-Dimensional Arrays
Topics
Motivation
The numpy Module
Subscripting
functions and 2d Arrays
Visualizing
38 18 82 77
83 53 12 10
This is row 1.
Rows and Columns
12 17 49 61
38 18 82 77
83 53 12 10
This is column 2.
Entries
12 17 49 61
38 18 82 77
83 53 12 10
Nodes
4 and 6
Are
connected
Captures the connectivity in
a network
Where Do They Come From
An m-by-n array
of pixels.
12 17 49 61
38 18 82 77
83 53 12 10
A = [[12,17,49,61],[38,18,82,77],[83,53,12,10]]
A list of lists.
Accessing Entries
12 17 49 61
38 18 82 77 A[1][2]
83 53 12 10
A = [[12,17,49,61],[38,18,82,77],[83,53,12,10]]
Accessing Entries
12 17 49 61
38 18 82 77 A[2][1]
83 53 12 10
A = [[12,17,49,61],[38,18,82,77],[83,53,12,10]]
Setting Up 2D Arrays
Here is a function that returns a reference to
an m-by-n array of zeros:
def zeros(m,n):
v = []
for k in range(n):
v.append(0.0)
A = []
for k in range(m):
A.append(v)
return A
Python is Awkward
Note how the row and column dimensions are passed to zeros
Accessing an Entry
>>> A = zeros((3,2))
>>> A[2,1] = 10
>>> A
array([[ 0., 0.],
[ 0., 0.],
[ 0., 10.]])
>>> A = array([[1,2,3],[4,5,6]])
>>> A
array([[1, 2, 3],
[4, 5, 6]])
1 2 3
A
3x3
2 4 6 times
table
3 6 9
Nested Loops and 2D Arrays
A = array((3,3))
for i in range(3):
for j in range(3):
A[i,j] = (i+1)*(j+1)
for i in range(3):
A[i,0] = (i+1)*(0+1)
A[i,1] = (i+1)*(1+1)
A[i,2] = (i+1)*(2+1)
Equivalent!
Understanding 2D Array Set-Up
for i in range(3):
A[i,0] = (i+1)*(0+1)
A[i,1] = (i+1)*(1+1)
A[i,2] = (i+1)*(2+1)
1 2 3 Row 0 is
set up when
i=0
Understanding 2D Array Set-Up
for i in range(3):
A[i,0] = (i+1)*(0+1)
A[i,1] = (i+1)*(1+1)
A[i,2] = (i+1)*(2+1)
1 2 3 Row 1 is
set up when
2 4 6
i=1
Understanding 2D Array Set-Up
for i in range(3):
A[i,0] = (i+1)*(0+1)
A[i,1] = (i+1)*(1+1)
A[i,2] = (i+1)*(2+1)
1 2 3 Row 2 is
set up when
2 4 6
i=2
4 6 9
Extended Example
10 36 22 15 62
C ---> 12 35 20 12 66
13 37 21 16 59
10 36 22 15 62 It costs
$12 for
C ---> 12 35 20 12 66 factory 1
to make
product 3
13 37 21 16 59
38 5 99 34 42
I ---> 82 19 83 12 42
51 29 21 56 87
38 5 99 34 42 Factory 1
can sell up
I ---> 82 19 83 12 42 to 83 units
of product 2.
51 29 21 56 87
PO ---> 1 0 12 29 5
The customer
wishes to
PO ---> 1 0 12 29 5 purchase 29
product 3 units
10 36 22
I --> A 2-by-3
array.
12 35 20
I = array([[10,36,22],[12,35,20]])
Computing Row and Column
Dimension Using shape
Suppose: Useful in functions
and methods with 2D
10 36 22 array arguments
I -->
12 35 20
(m,n) is a tuple
(m,n) = I.shape m: 2 n: 3
shape is an attribute of the array class
Finding the Location of the
Smallest Value Using argmin
>>> x>y
array([ True, True, True], dtype=bool)
>>> all(x>y)
True
>>> x>z
array([ True, False, True], dtype=bool)
>>> any(x>z)
True
inf
A special float that behaves like infinity
>>> x = inf
>>> 1/x
0
>>> x+1
Inf
>>> inf > 9999999999999
True
Now Lets Develop the Class
Company
10 36 22 30 40 50
I --> C -->
12 35 20 60 70 80
10 36 22 30 40 50
I --> C -->
12 35 20 60 70 80
10 36 22 30 40 50
I --> C -->
12 35 20 60 70 80
10 36 22 30 40 50
I --> C -->
12 35 20 60 70 80
10 36 22 30 40 50
I --> C -->
12 35 20 60 70 80
10 36 22 30 40 50
I --> C -->
12 35 20 60 70 80
10 36 22 30 40 50
I --> C -->
12 35 20 60 70 80
C ---> 12 35 20 12 66
13 37 21 16 59
PO ---> 1 0 12 29 5
For factory 0:
C ---> 12 35 20 12 66 j = 0
13 37 21 16 59
PO ---> 1 0 12 29 5
s = 0;
For
for j in range(5):
factory 0:
s = += C[0,j] * PO[j]
10 36 22 15 62
C ---> 12 35 20 12 66 j = 1
13 37 21 16 59
PO ---> 1 0 12 29 5
s = 0
For
for j in range(5):
factory 0:
s = += C[0,j] * PO[j]
10 36 22 15 62
C ---> 12 35 20 12 66 j = 2
13 37 21 16 59
PO ---> 1 0 12 29 5
s = 0;
0
For for j
j=1:5
in range(5):
factory 0: s = s+=+C[0,j]
C(1,j)**PO[j]
PO(j)
10 36 22 15 62
C ---> 12 35 20 12 66 j = 3
13 37 21 16 59
PO ---> 1 0 12 29 5
s = 0;
0
For for j
j=1:5
in range(5):
factory 0: s = s+=+C[0,j]
C(1,j)**PO[j]
PO(j)
10 36 22 15 62
C ---> 12 35 20 12 66 j = 4
13 37 21 16 59
PO ---> 1 0 12 29 5
s = 0;
0
For for j
j=1:5
in range(5):
factory 0: s = s+=+C[0,j]
C(1,j)**PO[j]
PO(j)
10 36 22 15 62
C ---> 12 35 20 12 66
13 37 21 16 59
PO ---> 1 0 12 29 5
s = 0;
0
For for j
j=1:5
in range(5):
factory 1: ss==+=
s +C[1,j]
C(2,j)*PO(j)
* PO[j]
10 36 22 15 62
C ---> 12 35 20 12 66
13 37 21 16 59
PO ---> 1 0 12 29 5
For s = 0
factory k: for j in range(5):
s = += C[k,j] * PO[j]
To Answer Q1 We Have
def Order(self,PO):
Returns an m-by-1 array that
houses how much it costs
each factory to fill the PO.
10 36 22 15 62 1019
self.C --> 12 35 20 12 66 930
13 37 21 16 59 1040
PO --> 1 0 12 29 5
Returns [1019,930,1040]
Implementation
def Order(self,PO):
C = self.C
(m,n) = C.shape
theCosts = zeros((m,1))
for k in range(m):
for j in range(n):
theCosts[k] += C[k,j]*PO[j]
return theCosts
Using Order
Assume that the following are initialized:
I the Inventory array
C the Cost array
PO the purchase order array
>>> A = Company(I,C)
>>> x = A.Order(PO)
>>> kMin = x.argmin()
>>> xMin = x[kMin]
38 5 99 34 42 Yes
I --> 82 19 83 12 42 No
51 29 21 56 87 Yes
PO --> 1 0 12 29 5
38 5 99 34 42 Yes
I --> 82 19 83 12 42 No
51 29 21 56 87 Yes
PO --> 1 0 12 29 5
38 5 99 34 42 Yes
I --> 82 19 83 12 42 No
51 29 21 56 87 Yes
PO --> 1 0 12 29 5
38 5 99 34 42 Yes
I --> 82 19 83 12 42 No
51 29 21 56 87 Yes
PO --> 1 0 12 29 5
38 5 99 34 42 Yes
I --> 82 19 83 12 42 No
51 29 21 56 87 Yes
PO --> 1 0 12 29 5
def CanDo(self,PO):
""" Return the indices of those
factories with sufficient
inventory.
>>> A = Company(I,C)
>>> kVals = A.CanDo(PO)
>>> A = Company(I,C)
>>> kVals = A.CanDo(PO)
theCosts = Order(PO)
Who = CanDo(P0)
if len(Who)==0:
return None
else:
Who Can Fill the Purchase Order
Most Cheaply?
38 5 99 34 42 Yes 1019
I --> 82 19 83 12 42 No
51 29 21 56 87 Yes 1040
PO --> 1 0 12 29 5
>>> A = Company(I,C)
>>> (kMin,costMin) = A.Cheapest(PO)
38 5 99 34 42 Yes 1019
I --> 82 19 83 12 42 No
51 29 21 56 87 Yes 1040
PO --> 1 0 12 29 5
Before
Updating Inventory
37 5 87 5 37
I --> 82 19 83 12 42
51 29 21 56 87
PO --> 1 0 12 29 5
After
Method for Updating
the Inventory Array
After Processing a PO
def UpDate(self,k,PO):
n = len(PO)
for j in range(n):
# Reduce the inventory of product j
self.I[k,j] = self.I[k,j] - PO[j]
# Decrease the total value
self.TV = self.TV - self.C[k,j]*P0[j]