Download (right-click, save target as ...) this page as a jupyterlab notebook from: Lab8


Laboratory 8: Matrices a Red Pill Approach

LAST NAME, FIRST NAME

R00000000

ENGR 1330 Laboratory 8 - In-Lab


Reading Files, Working with 2D Lists

Is the file A-Inverse.txt indeed the inverse of A-Matrix.txt ?


Example - Using the Treasure Map

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.

Now we have the map, we can use list delete and slicing to remove un-necessary data

Now we can use our treasure map search to complete the example


Exercise 0

Consider a new treasure map contained in file http://54.243.252.9/engr-1330-webroot/8-Labs/Lab07/treasure2.txt.


Example

Develop a script to multiply a vector by a matrix.

\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{x} = \begin{pmatrix} 0.595194878133 \\ 0.507932173989 \\ 0.831708392507 \\ 0.630365599089 \\ 1.03737526565 \\ \end{pmatrix} \end{gather}

Use the code blocks below to craft your answer.

Exercise 1

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


References

  1. List processing tips https://www.programiz.com/python-programming/del

  2. Character replacement tips https://www.geeksforgeeks.org/python-string-replace/

  3. Python file manipulations https://www.tutorialspoint.com/python/python_files_io.htm

  4. A linear algebra primer https://numericalmethodssullivan.github.io/ch-linearalgebra.html

  5. Python file manipulations https://www.tutorialspoint.com/python/python_files_io.htm

  6. 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