Tutorial 3 - support

ECE Generic - University of Houston

Han Q Le (c) -copyrighted 

Tutorial 3 part 1

Dot product concept and demo

In [2]:
import scipy as sp
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

dot vectors

In [2]:
priceX = [52., 86.4, 22.5, 134]; shareX = [500, 250, 400, 300]
In [3]:
priceX
Out[3]:
[52.0, 86.4, 22.5, 134]
In [3]:
np.dot(priceX, shareX)
Out[3]:
96800.0

bakery example

Note: this will not be the same as the Mathematica example in .nb file because of random data

In [3]:
matp2=np.floor((2*(9*np.random.rand(4,2)+15)+0.5))/2.
In [4]:
matp2
Out[4]:
array([[24. , 17. ],
       [17. , 19.5],
       [15. , 23.5],
       [18.5, 22. ]])
In [5]:
nmat2 = [[63.5, 71.5, 50], [117.25, 143.5, 91]]
wkcost=np.dot(matp2,nmat2)
In [6]:
wkcost
Out[6]:
array([[3517.25 , 4155.5  , 2747.   ],
       [3365.875, 4013.75 , 2624.5  ],
       [3707.875, 4444.75 , 2888.5  ],
       [3754.25 , 4479.75 , 2927.   ]])

cake types and ingredient requirement

In [7]:
ingrmat = [[0., 0.3, 0.1, 0.5, 0.4]
           , [0.75, 0.25, 0.5, 0.2, 0.6]];
prodmat = [[55, 70, 40]
           , [80, 50, 40]
           , [30, 45, 30]
           , [25, 40, 30]
           , [60, 80, 50]];
In [8]:
demandmat=np.dot(ingrmat,prodmat)
In [9]:
demandmat
Out[9]:
array([[ 63.5 ,  71.5 ,  50.  ],
       [117.25, 143.5 ,  91.  ]])

weekly price dot demand for cost

random price here is not the same as the Mathematica tutorial example

In [10]:
wkcost=np.dot(matp2,demandmat);
print(wkcost)
[[3517.25  4155.5   2747.   ]
 [3365.875 4013.75  2624.5  ]
 [3707.875 4444.75  2888.5  ]
 [3754.25  4479.75  2927.   ]]

demo of matrix dot associative property

In [12]:
result1=np.dot(matp2,np.dot(ingrmat,prodmat)) 
result2=np.dot(np.dot(matp2,ingrmat),prodmat)
In [14]:
print(result1)
print(result2)
[[3517.25  4155.5   2747.   ]
 [3365.875 4013.75  2624.5  ]
 [3707.875 4444.75  2888.5  ]
 [3754.25  4479.75  2927.   ]]
[[3517.25  4155.5   2747.   ]
 [3365.875 4013.75  2624.5  ]
 [3707.875 4444.75  2888.5  ]
 [3754.25  4479.75  2927.   ]]

Matrix dot with transpose

In [16]:
mdim=5; ndim=3 ; pdim=4 ; rint=10;
matA=np.floor((np.random.rand(mdim,ndim)-0.5)*2*rint+0.5)
matAT=np.transpose(matA)
matB=np.floor((np.random.rand(ndim,pdim)-0.5)*2*rint+0.5)
matBT=np.transpose(matB)
print(np.transpose(np.dot(matA,matB)))
print(np.dot(matBT,matAT))
[[-57. -21. -11.  -3.   4.]
 [ 18.  14.  14.   6.   7.]
 [-82. 111.  95.  19. 107.]
 [163. -59.  -5.  35. -61.]]
[[-57. -21. -11.  -3.   4.]
 [ 18.  14.  14.   6.   7.]
 [-82. 111.  95.  19. 107.]
 [163. -59.  -5.  35. -61.]]

Inverse matrix and linear algebra solve

In [3]:
from scipy import linalg
In [4]:
purchM=[[4, 2, 4], [3, 3, 9], [9, 0, 3]]
total=[10.6, 17.1, 11.4]
In [5]:
price=linalg.solve(purchM,total)
print(price)
[0.85 1.1  1.25]
In [6]:
purchInv=linalg.inv(purchM)
print(purchInv)
[[ 0.125      -0.08333333  0.08333333]
 [ 1.         -0.33333333 -0.33333333]
 [-0.375       0.25        0.08333333]]
In [7]:
np.dot(purchInv,total)
Out[7]:
array([0.85, 1.1 , 1.25])
In [ ]: