<jupyter_start><jupyter_text>Download this page as a jupyter notebook at [Lab 9-TH](http://54.243.252.9/engr-1330-webroot/8-Labs/Lab09/Lab09-TH.ipynb)  Laboratory 9: Matrices a Blue Pill Approach **LAST NAME, FIRST NAME****R00000000**ENGR 1330 Laboratory 9 - Homework<jupyter_code># Preamble script block to identify host, user, and kernel
import sys
! hostname
! whoami
print(sys.executable)
print(sys.version)
print(sys.version_info)<jupyter_output>atomickitty
sensei
/opt/jupyterhub/bin/python3
3.8.10 (default, Jun  2 2021, 10:49:15) 
[GCC 9.4.0]
sys.version_info(major=3, minor=8, micro=10, releaselevel='final', serial=0)<jupyter_text>Numpy Cheat Sheet(s)Numpy is the core library for scientific computing in Python. It provides a high-performance multidimensional array object, and tools for working with these arrays. The library’s name is short for “Numeric Python” or “Numerical Python”. A pdf file of a summary sheet (you need to download): [https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Numpy_Python_Cheat_Sheet.pdf](https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Numpy_Python_Cheat_Sheet.pdf)or the graphic below (same information):![](https://blog.finxter.com/wp-content/uploads/2019/10/grafik-1-1024x725.png) ___ Exercise 1: Going down to Southpark .... ? __Using the names below:__"Cartman, Kenny, Kyle, Stan, Butters, Wendy, Chef, Mr. Mackey, Randy, Sharon, <NAME>"__A) Create a 3x4 array and print it out B) Sort the array alphabetically by column and print it out C) From the sorted array, slice out a 2x2 array with ('Cartman, Randy, Sharon, Wendy'), and print it out__<jupyter_code>#import numpy
# make an arry of names
# print array
# sort and print sorted array
# slice and print sliced array
names = np.array([['Cartman','Kenny','Kyle','Stan'],['Butters','Wendy','Chef','Mr. Mackey'],['Randy','Sharon','Sheila','Towelie']])
print(names) #Step1
names_sorted = np.sort(names,axis = 0) 
print(names_sorted) #Step2
names_sliced = names_sorted[1:,0:2]
print(names_sliced) #Step3<jupyter_output>[['Cartman' 'Kenny' 'Kyle' 'Stan']
 ['Butters' 'Wendy' 'Chef' 'Mr. Mackey']
 ['Randy' 'Sharon' 'Sheila' 'Towelie']]
[['Butters' 'Kenny' 'Chef' 'Mr. Mackey']
 ['Cartman' 'Sharon' 'Kyle' 'Stan']
 ['Randy' 'Wendy' 'Sheila' 'Towelie']]
[['Cartman' 'Sharon']
 ['Randy' 'Wendy']]<jupyter_text>___ Exercise 2: A Numpy Playground- Step1: Create a 2x5 array with [0,1,2,3,4,5,6,7,8,9] and name it "Array1"- Step2: Extract all the odd numbers of "Array1" and store them in a new array: "Array2"- Step3: In "Array1" replace 0,5, and 9 with 100,500, and 900.- Step4: Add three other odd numbers to "Array2"- Step5: Calculate the average of "Array2"- Step6: Take "Array2" to the power of 5 and print the result.<jupyter_code>import numpy as np         #import numpy
# Step1:
Array1 = np.array([0,1,2,3,4,5,6,7,8,9])
print(Array1)
# Step2:
Array2 = Array1[Array1%2 != 0]
print(Array2)
# Step3:
Array1[0] = 100
Array1[5] = 500
Array1[9] = 900
print(Array1)
# Step4:
Array2 = np.append(Array2,[11,13,15], axis=None)
print(Array2)
# Step5:
print(np.mean(Array2))
# Step6:
print(np.power(Array2,5))<jupyter_output>[0 1 2 3 4 5 6 7 8 9]
[1 3 5 7 9]
[100   1   2   3   4 500   6   7   8 900]
[ 1  3  5  7  9 11 13 15]
8.0
[     1    243   3125  16807  59049 161051 371293 759375]<jupyter_text>___ Exercise 3:  - Step1: Create a 1D Numpy array of all the numbers in your R-number: R | It should have eight numbers- Step2: Create another 1D array by multiplying the previous array by 2- Step3: Create another 1D array by multiplying the first array by 5<jupyter_code>import numpy as np         #import numpy
#Step1:
A1 = np.array([1,2,3,4,5,6,7,8])
print(A1)
#Step2:
A2 = 2*A1
print(A2)
#Step3:
A3 = 5*A1
print(A3)<jupyter_output>[1 2 3 4 5 6 7 8]
[ 2  4  6  8 10 12 14 16]
[ 5 10 15 20 25 30 35 40]<jupyter_text>---You will find this link helpful for the nexttwo exercises: [https://www.codecademy.com/learn/learn-linear-algebra/modules/math-ds-linear-algebra/cheatsheet](https://www.codecademy.com/learn/learn-linear-algebra/modules/math-ds-linear-algebra/cheatsheet) Exercise 4Consider the linear system given by$$\mathbf{A} \cdot \mathbf{x} = \mathbf{B}$$where\begin{gather}\mathbf{A}=\begin{pmatrix}8.17\times10^6 & 0 &-0.5\times10^6\\-1.5\times10^6 & 7.21\times10^6 & 0 \\0 & -1.5\times10^6 & 11.5\times10^6 \\\end{pmatrix}~\mathbf{b}=\begin{pmatrix}2.0\times10^9 \\4.0\times10^9 \\1.0\times10^9 \\\end{pmatrix}\end{gather}Use **numpy** and **linalg** methods to find **x**<jupyter_code>import numpy 
amatrix = [[8.17e6,0,-0.5e6],[-1.5e6,7.21e6,0],[0,-1.5e6,11.5e6]]
A = numpy.array(amatrix)
print(A)
bvector = [2e9,4e9,1e9]
b = numpy.array(bvector)
print(b)
Ainv = numpy.linalg.inv(A)
x = Ainv@b
print(x)
import numpy 
# already read in the arrays
A = numpy.array(amatrix)
Ainv = numpy.linalg.inv(A)
b = numpy.array(bvector)
x = Ainv@b
for i in range(0,len(x)):
    print(x[i])<jupyter_output><empty_output><jupyter_text>--- Exercise 5.Consider the steady-state reactor system with feedback as shown below:A mass balance of the system assuming complete mixing in each reactor (pond, lake, vat, ...) is\begin{gather}\begin{matrix}W_1 = & (Q+\alpha Q + v A_1)  \times C_1 & + 0 \times C_2 &- \alpha Q \times C_3\\W_2 = & -(Q+\alpha Q) \times C_1 & + (Q+\alpha Q + v A_2) \times C_2 & 0 \times C_3 \\W_3 = & 0 \times C_1 & - (Q+\alpha Q)\times C_2 & +(Q+\alpha Q + v A_3) \times C_3\\\end{matrix}\end{gather}where $W_i$ is the loading in kilograms/year , $A_i$ is the reactor surface area in $10^6$ meters squared, $v$ is the settling rate in each reactor in meters/year, $\alpha$ is the recycle fraction, and $Q$ is the volumetric flow rate through the system. The unknown values are the constituient concentrations $C_i$ in each reactor.Decomposing the system of equations above into a Matrix-vector system $\mathbf{A} \cdot \mathbf{C} = \mathbf{W}$, where $\mathbf{A}$ is the coefficient matrix (which we will build using $Q$,$\alpha$, and $v A_i$), $\mathbf{C}$ is the vector of unknown concentrations of the constituients, and $\mathbf{W}$ is the loading vector yields:\begin{gather}\begin{pmatrix}(Q+\alpha Q + v A_1)   & + 0  &- \alpha Q \\-(Q+\alpha Q)  & + (Q+\alpha Q + v A_2)  & 0  \\0 & - (Q+\alpha Q)& +(Q+\alpha Q + v A_3) \\\end{pmatrix}\cdot\begin{pmatrix}C_1 \\C_2 \\C_3 \\\end{pmatrix}~=~\begin{pmatrix}W_1 \\W_2 \\W_3 \\\end{pmatrix}\end{gather}Complete a script that constructs $\mathbf{A}$ given $Q=1\times10^6$ cubic meters/year, $\alpha$ = 0.5, and $v$ = 10 meters/year. Estimate the reactor constituient concentrations in parts per million (milligrams). Now repeat the computation with $\alpha$ = 0.0<jupyter_code>import numpy
# constants and arrays
Q = 1.0e6 # cubic meters/year
alpha = 0.5 # dimensionless
v = 10 # meters/year
Area = numpy.array([0.667,0.571,1.000])
Load = numpy.array([2000.,4000.,1000.])
# get units correct
Load = Load*1e6 #Load in milligrams 
Area = Area*1e6 #Area in million squre meters
# coefficient array
Amat = numpy.array([[Q*(1+alpha)+v*Area[0],0,-alpha*Q],[-Q*(1+alpha),Q*(1+alpha)+v*Area[1],0],[0,-Q*(1+alpha),Q*(1+alpha)+v*Area[2]]])
# invert and solve for c
c = numpy.linalg.inv(Amat)@Load
print(numpy.round(c,0))
import numpy
# constants and arrays
Q = 1.0e6 # cubic meters/year
alpha = 0.0 # dimensionless
v = 10 # meters/year
Area = numpy.array([0.667,0.571,1.000])
Load = numpy.array([2000.,4000.,1000.])
# get units correct
Load = Load*1e6 #Load in milligrams 
Area = Area*1e6 #Area in million squre meters
# coefficient array
Amat = numpy.array([[Q*(1+alpha)+v*Area[0],0,-alpha*Q],[-Q*(1+alpha),Q*(1+alpha)+v*Area[1],0],[0,-Q*(1+alpha),Q*(1+alpha)+v*Area[2]]])
# invert and solve for c
c = numpy.linalg.inv(Amat)@Load
print(numpy.round(c,0))<jupyter_output>[261. 635. 149.]
{'fraction_non_alphanumeric': 0.16096994711724494, 'fraction_numerical': 0.07106926351089901, 'mean_word_length': 4.705665930831493, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 5, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '408094', 'license': 'CC0-1.0', 'n_tokens_mistral': 3270, 'n_tokens_neox': 2935, 'n_words': 843, 'path': '8-Labs/Lab09/src_old/Lab09-TH-WS.ipynb', 'repo_name': 'dustykat/engr-1330-psuedo-course', 'size': '7762'}