ChEn-3170: Computational Methods in Chemical Engineering Fall 2018 UMass Lowell; Prof. V. F. de Almeida 16Sep2018
'''Python packages are accessed with an import directive as such:'''
import numpy as np # import the package and create the alias: np
'''Element-by-element addition or subtraction'''
vec1 = np.array( np.random.random(5) )
print('vec1 =',vec1)
vec2 = np.array(np.random.random(5))
print('vec2 =',vec2)
result = vec1 + vec2 # element-by-element sum
print('addition =',result)
result = vec1 - vec2 # element-by-element subtraction
print('difference =',result)
vec1 = [0.4948492 0.50804512 0.49464907 0.15373364 0.13291299] vec2 = [0.21566749 0.91287678 0.80807924 0.16844383 0.23493782] addition = [0.71051669 1.4209219 1.30272831 0.32217747 0.36785081] difference = [ 0.27918171 -0.40483166 -0.31343017 -0.01471019 -0.10202484]
'''Element-by-element product or division'''
vec1 = np.array(np.random.random(5))
print('vec1 =',vec1)
vec2 = np.array(np.random.random(5))
print('vec2 =',vec2)
result = vec1 * vec2 # element-by-element product
print('product =',result)
result = vec1 / vec2 # element-by-element division
print('division =',result)
vec1 = [0.43200833 0.6674497 0.25652916 0.14406066 0.18762794] vec2 = [0.96027621 0.58917668 0.08877367 0.45240347 0.08928542] product = [0.41484732 0.3932458 0.02277303 0.06517354 0.01675244] division = [0.44987923 1.13285152 2.88969881 0.31843403 2.10143998]
'''Vector scalar product'''
vec1 = np.array(np.random.random(5))
print('vec1 =',vec1)
vec2 = np.array(np.random.random(5))
print('vec2 =',vec2)
result = np.dot(vec1, vec2) # scalar or dot product
print('scalar product =',result)
vec1 = [0.88753958 0.14292897 0.88250603 0.08425018 0.79054425] vec2 = [0.1707121 0.84016215 0.23226843 0.20964275 0.90130206] scalar product = 1.2067571404399116
'''Scaling of a vector'''
vec = np.array(np.random.random(5))
print('vec =',vec)
factor = 0.345
scaled = factor * vec # scaling of vec element-by-element product
print('scaled =', scaled) # assigned to new variable `scaled`
vec *= factor # in-place scaling
print('vec =',vec)
vec = [0.94205399 0.26144985 0.83272946 0.5677138 0.32675787] scaled = [0.32500863 0.0902002 0.28729166 0.19586126 0.11273147] vec = [0.32500863 0.0902002 0.28729166 0.19586126 0.11273147]
'''Mathematical Operations on a Vector'''
vec = np.array(np.random.random(5))
print('vec =',vec)
log_vec = np.log(vec) # natural log element-by-element
print('log(vec) =',log_vec)
exp_vec = np.exp(log_vec) # exponential
print('exp(vec) =',exp_vec)
sin_vec = np.sin(vec) # sine
print('sin(vec) =',sin_vec)
vec_cubed = vec**3 # powers
print('vec^3 =',vec_cubed)
vec_mean = vec.mean() # arithmetic mean
print('mean(vec) =',vec_mean)
vec_std = vec.std() # standard deviation
print('std(vec) =',vec_std)
vec = [0.94374382 0.99443512 0.28660622 0.28017179 0.87161722] log(vec) = [-0.05790052 -0.00558043 -1.24964606 -1.27235234 -0.13740492] exp(vec) = [0.94374382 0.99443512 0.28660622 0.28017179 0.87161722] sin(vec) = [0.8097605 0.83845125 0.28269852 0.27652074 0.76537076] vec^3 = [0.8405477 0.98339808 0.02354273 0.02199243 0.66218205] mean(vec) = 0.6753148332340315 std(vec) = 0.32238452752106733
'''Element-by-element addition or subtraction'''
mat1 = np.random.random( (3,3) )
print('mat1 =\n',mat1)
mat2 = np.random.random( (3,3) )
print('mat2 =\n',mat2)
result = mat1 + mat2 # element-by-element sum
print('addition =\n',result)
result = mat1 - mat2 # element-by-element subtraction
print('difference =\n',result)
mat1 = [[0.91511137 0.43938518 0.25735646] [0.05320647 0.97188316 0.40480159] [0.51208713 0.49529661 0.84443821]] mat2 = [[0.95402571 0.85511215 0.2022494 ] [0.98150039 0.54533474 0.88917533] [0.86584316 0.62102913 0.93636154]] addition = [[1.86913708 1.29449733 0.45960586] [1.03470687 1.5172179 1.29397692] [1.37793029 1.11632574 1.78079975]] difference = [[-0.03891434 -0.41572697 0.05510706] [-0.92829392 0.42654842 -0.48437374] [-0.35375603 -0.12573252 -0.09192333]]
'''Element-by-element product or division'''
mat1 = np.random.random((3,3))
print('mat1 =\n',mat1)
mat2 = np.random.random((3,3))
print('mat2 =\n',mat2)
result = mat1 * mat2 # element-by-element product
print('product =\n',result)
result = mat1 / mat2 # element-by-element division (cross your fingers)
print('division =\n',result)
mat1 = [[0.69162913 0.640721 0.35747977] [0.08853372 0.64461986 0.54428793] [0.44229097 0.32215725 0.2833583 ]] mat2 = [[0.96206606 0.26864935 0.23756847] [0.72656987 0.18806798 0.60116479] [0.47796772 0.82998389 0.46257312]] product = [[0.66539292 0.17212928 0.08492592] [0.06432593 0.12123236 0.32720674] [0.2114008 0.26738533 0.13107393]] division = [[0.71889984 2.38497132 1.50474414] [0.12185163 3.42758962 0.9053889 ] [0.92535741 0.3881488 0.61256975]]
'''Produce Noise on a Matrix Image'''
from matplotlib import pyplot as plt # import the pyplot function of the matplotlib package
plt.rcParams['figure.figsize'] = [20, 4] # extend the figure size on screen output
# copy URL of image in the images/ course repository;
# click on the image link, use the Download buttom to create this URL
block = plt.imread('https://raw.githubusercontent.com/dpploy/chen-3170/master/notebooks/images/glacier.png',format='png')
plt.figure(1)
plt.imshow( block )
plt.title('Matrix Reloaded',fontsize=14)
print('mtrx shape =',block.shape) # inspect the array shape
mtrx shape = (300, 400, 3)
'''Use Matrix Multiplication'''
shape = block.shape[0:2] # use the shape to automate noise_mtrx generation
noise_mtrx = np.random.random(shape) # generate random matrix
block_noise = block[:,:,2] * noise_mtrx # apply noise to the blue channel
plt.figure(2)
plt.imshow(block_noise)
plt.title('Noisy Matrix',fontsize=14)
Text(0.5,1,'Noisy Matrix')
'''Matrix Scaling (matrix product or division by a scalar)'''
mat1 = np.random.random((3,3))
print('mat1 =\n',mat1)
factor = 3.21
result = factor * mat1 # scaling of mat1 element-by-element; product with factor
print('product =\n',result)
mat1 = [[0.59976811 0.38594959 0.24106681] [0.80506064 0.92111181 0.81179824] [0.41518648 0.7054152 0.75050306]] product = [[1.92525562 1.2388982 0.77382447] [2.58424465 2.95676892 2.60587235] [1.33274859 2.26438279 2.40911482]]
'''Matrix Scaling of an Image'''
color_channel = np.copy(block[:,:,0]) # copy the red channel
color_channel /= color_channel.max() # scale to gray, 0-255 values
color_channel *= 255
gray_channel = color_channel.astype(int) # truncate all float data type to int
plt.figure(3)
plt.imshow(gray_channel,cmap='gray')
plt.title('Matrix Gray Scaling',fontsize=14)
Text(0.5,1,'Matrix Gray Scaling')
'''Other Mathematical Operations on a Matrix'''
mtrx = np.copy(block[:,:,0])
plt.figure(4)
plt.imshow(mtrx)
plt.title('Original',fontsize=14)
log_mtrx = np.log(mtrx + .001) # natural log element-by-element
plt.figure(5)
plt.imshow(log_mtrx)
plt.title('Log Transform',fontsize=14)
exp_mtrx = np.exp(log_mtrx) # exponential
plt.figure(6)
plt.imshow(exp_mtrx)
plt.title('Exp of Log Transform',fontsize=14)
sin_mtrx = np.sin( mtrx + np.pi/2 ) # sine
plt.figure(7)
plt.imshow(sin_mtrx)
plt.title('Sine Transform',fontsize=14)
mtrx_cubed = mtrx**3 # powers
plt.figure(8)
plt.imshow(mtrx_cubed)
plt.title('Cube Transform',fontsize=14)
mtrx_mean = mtrx.mean() # arithmetic mean
print('mean(mtrx) =',mtrx_mean)
mtrx_std = mtrx.std() # standard deviation
print('std(mtrx) =',mtrx_std)
mean(mtrx) = 0.38594216 std(mtrx) = 0.37984067
'''Matrix Transposition'''
'''clockwise rotation followed by horizontal right to left flip'''
mtrx = np.random.random((5,7))
np.set_printoptions(precision=3) # one way to control printing of numpy arrays
print('mtrx =\n',mtrx)
mtrx_T = mtrx.transpose() # transpose of a mtrx: M[i,j] -> M[j,i]
print('mtrx^T =\n',mtrx_T)
mtrx = [[0.694 0.185 0.28 0.504 0.616 0.68 0.623] [0.394 0.967 0.295 0.065 0.614 0.681 0.321] [0.893 0.906 0.23 0.597 0.36 0.283 0.896] [0.529 0.795 0.647 0.911 0.999 0.015 0.271] [0.498 0.86 0.088 0.457 0.884 0.213 0.623]] mtrx^T = [[0.694 0.394 0.893 0.529 0.498] [0.185 0.967 0.906 0.795 0.86 ] [0.28 0.295 0.23 0.647 0.088] [0.504 0.065 0.597 0.911 0.457] [0.616 0.614 0.36 0.999 0.884] [0.68 0.681 0.283 0.015 0.213] [0.623 0.321 0.896 0.271 0.623]]
'''Matrix Transposition'''
'''example of adding a transformed matrix to another transform transposed'''
'''note: to add a matrix to its transpose, a matrix must be square'''
n_rows = block.shape[0]
n_columns = n_rows
mtrx = np.copy(block[:n_rows,:n_columns,0]) # select a square block
sin_mtrx = np.sin( mtrx + np.pi/2 ) # sine
sin_mtrx /= sin_mtrx.max()
plt.figure(9)
plt.imshow(sin_mtrx)
plt.title('Sine Transform',fontsize=14)
mtrx_cubed = mtrx**3 # powers
plt.figure(10)
plt.imshow(mtrx_cubed)
plt.title('Cube Transform',fontsize=14)
plt.figure(11)
plt.imshow( sin_mtrx + mtrx_cubed.transpose() ) # sine + cubed transposed
plt.title('Sine + Cube Transpose Transform',fontsize=14)
Text(0.5,1,'Sine + Cube Transpose Transform')