Download (right-click, save target as ...) this page as a jupyterlab notebook from: Lab8
LAST NAME, FIRST NAME
R00000000
ENGR 1330 Laboratory 8 - In-Lab
# Preamble script block to identify host, user, and kernel
import sys
! hostname
! whoami
print(sys.executable)
print(sys.version)
print(sys.version_info)
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)
Is the file A-Inverse.txt indeed the inverse of A-Matrix.txt ?
The treasure map problem was already presented, and we replaced the explicitly defined map lists with a file, allowing for the use of multiple maps. Starting with our original map, but contained in a text file named http://54.243.252.9/engr-1330-webroot/8-Labs/Lab07/treasure1.txt we can read the map using file manipulation methods.
Here is what the file looks like
c1,c2,c3,c4,c5
r1,34,21,32,41,25
r2,14,42,43,14,31
r3,54,45,52,42,23
r4,33,15,51,31,35
r5,21,52,33,13,23
The upper left hand corner appears to be 3 spaces, then the remainder of the first row is column headings, which we dont need. Similarily the second row and beyond, has a column of row labels, then the actual data contents.
Our reading exercise will need to get just the data and ignore (or discard) the rest of the information.
However our search method visited all cells in the grid, and did not use the clues explicitly in the map. Modify the search method to use the clues in the individual cells.
treasuremap = [] # empty list to the map information
treasurefile = open("treasure1.txt","r") # open a read connection
for line in treasurefile:
treasuremap.append([str(n) for n in line.strip().split(",")])
treasurefile.close()
Now we have the map, we can use list delete and slicing to remove un-necessary data
del treasuremap[0] #remove entire first row
for irow in range(len(treasuremap)): #step through remaining rows
del treasuremap[irow][0] #kill leading column each row
Now we can use our treasure map search to complete the example
#####################################
for i in range(0,5,1):
what_to_print =','.join(map(repr, treasuremap[i][:]))
print(what_to_print) # print the map by row
howMany = 25 # set how many moves before we quit
#### Clue Directed Search ####
found = False
# start at (1,1)
rowNow=1
colNow=1
tryCount = 0
while not found:
# get row and column from rowNow and colNow values
row = rowNow
column = colNow
# get maprowval and mapcolval
maprowval = str(treasuremap[row-1][column-1])[0]
mapcolval = str(treasuremap[row-1][column-1])[1]
# test if cell is a treasure cell or not
if int(maprowval) == row and int(mapcolval) == column :
print('Cell ',treasuremap[row-1][column-1], ' contains TREASURE ') # print the result
print('Treasure found after ',tryCount,' cells visited')
found = True
break
pass #comment this line out when have message
else:
print('Cell ',row,column, ' contains no treasure, move to Cell ',treasuremap[row-1][column-1]) # message here for no treasure
rowNow = int(maprowval)
colNow = int(mapcolval)
found = False
pass #comment this line out when have message
tryCount+=1
if tryCount > howMany :
print('No treasure after ',tryCount,' cells visited')
break
'34','21','32','41','25' '14','42','43','14','31' '54','45','52','42','23' '33','15','51','31','35' '21','52','33','13','23' Cell 1 1 contains no treasure, move to Cell 34 Cell 3 4 contains no treasure, move to Cell 42 Cell 4 2 contains no treasure, move to Cell 15 Cell 1 5 contains no treasure, move to Cell 25 Cell 2 5 contains no treasure, move to Cell 31 Cell 3 1 contains no treasure, move to Cell 54 Cell 5 4 contains no treasure, move to Cell 13 Cell 1 3 contains no treasure, move to Cell 32 Cell 3 2 contains no treasure, move to Cell 45 Cell 4 5 contains no treasure, move to Cell 35 Cell 3 5 contains no treasure, move to Cell 23 Cell 2 3 contains no treasure, move to Cell 43 Cell 4 3 contains no treasure, move to Cell 51 Cell 5 1 contains no treasure, move to Cell 21 Cell 2 1 contains no treasure, move to Cell 14 Cell 1 4 contains no treasure, move to Cell 41 Cell 4 1 contains no treasure, move to Cell 33 Cell 3 3 contains no treasure, move to Cell 52 Cell 52 contains TREASURE Treasure found after 18 cells visited
Consider a new treasure map contained in file http://54.243.252.9/engr-1330-webroot/8-Labs/Lab07/treasure2.txt.
Develop a script to multiply a vector by a matrix.
Use the code blocks below to craft your answer.
#%reset -f # only if necessary
# create matrix A
amatrix = [[4.0,1.5,0.7,1.2,0.5],
[1.0,6.0,0.9,1.4,0.7],
[0.5,1.0,3.9,3.2,0.9],
[0.2,2.0,0.2,7.5,1.9],
[1.7,0.9,1.2,2.3,4.9]]
# create vector x
xvector = [0.595194878133,
0.507932173989,
0.831708392507,
0.630365599089,
1.03737526565 ]
# create null vector to store Ax
AXvector = [0 for i in range(0,len(xvector))] # populate with zeros
# print A
for i in range(0,len(amatrix),1):
print(amatrix[i][:])
# print x
for i in range(0,len(xvector),1):
print(xvector[i])
# perform the multiplication Ax put the result into Ax
for i in range(0,len(amatrix),1):
for j in range(0,len(xvector),1):
AXvector[i]= AXvector[i] + amatrix[i][j]*xvector[j]
# print Ax
for i in range(0,len(AXvector),1):
print(round(AXvector[i],3))
[4.0, 1.5, 0.7, 1.2, 0.5] [1.0, 6.0, 0.9, 1.4, 0.7] [0.5, 1.0, 3.9, 3.2, 0.9] [0.2, 2.0, 0.2, 7.5, 1.9] [1.7, 0.9, 1.2, 2.3, 4.9] 0.595194878133 0.507932173989 0.831708392507 0.630365599089 1.03737526565 5.0 6.0 7.0 8.0 9.0
Develop a script to multiply two matrices, just like in the Lesson. Apply the script to find $\mathbf{A}\mathbf{B}$ where.
\begin{gather} \mathbf{A} = \begin{pmatrix} 4.0 & 1.5 & 0.7 & 1.2 & 0.5 \\ 1.0 & 6.0 & 0.9 & 1.4 & 0.7 \\ 0.5 & 1.0 & 3.9 & 3.2 & 0.9 \\ 0.2 & 2.0 & 0.2 & 7.5 & 1.9 \\ 1.7 & 0.9 & 1.2 & 2.3 & 4.9 \\ \end{pmatrix} ~ \mathbf{B} = \begin{pmatrix} 0.27196 & -0.05581 & -0.03285 & -0.01687 & -0.007203 \\ -0.036787 & 0.186918 & -0.03206 & -0.011457 & -0.012618 \\ -0.02595 & -0.001333 & 0.268266 & -0.10875 & -0.004267 \\ 0.027048 & -0.050632 & 0.016499 & 0.14865 & -0.056198 \\ -0.093939 & 0.009124 & -0.056155 & -0.03519 & 0.236322 \\ \end{pmatrix} \end{gather}The two matrices are located in files:
http://54.243.252.9/engr-1330-webroot/8-Labs/Lab08/A-Matrix.txt
and:
http://54.243.252.9/engr-1330-webroot/8-Labs/Lab08/A-Inverse.txt
You should download these files before proceeding
# read file A-Matrix.txt
# read file A-Inverse.txt
# create a destination matrix C-matrix
# print A-Matrix
# print A-Inverse
# perform the multiplication put the result into C-matrix
# print C-matrix
List processing tips https://www.programiz.com/python-programming/del
Character replacement tips https://www.geeksforgeeks.org/python-string-replace/
Python file manipulations https://www.tutorialspoint.com/python/python_files_io.htm
A linear algebra primer https://numericalmethodssullivan.github.io/ch-linearalgebra.html
Python file manipulations https://www.tutorialspoint.com/python/python_files_io.htm
A Complete Beginners Guide to Matrix Multiplication for Data Science with Python Numpy https://towardsdatascience.com/a-complete-beginners-guide-to-matrix-multiplication-for-data-science-with-python-numpy-9274ecfc1dc6