Download (right-click, save target as ...) this page as a jupyterlab notebook from: Lab8-TH
LAST NAME, FIRST NAME
R00000000
ENGR 1330 Laboratory 8 - Homework
# 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)
Define the matrix A and the vector u in Python. Then perform all of the tasks below.
\begin{gather} \mathbf{A} = \begin{pmatrix} 1 & 3 & 5 & 7 \\ 2 & 4 & 6 & 8 \\ -3 & -2 & -1 & 0 \\ \end{pmatrix} ~ \mathbf{u} = \begin{pmatrix} 10 \\ 20 \\ 30 \\ \end{pmatrix} \end{gather}Use the code blocks below to craft your answer.
#%reset -f # only if necessary
# read/create matrix A
A = [[1,3,5,7],
[2,4,6,8],
[-3,-2,-1,0]]
# read/create vector u
u = [10,20,30]
# print A
print("A - matrix")
for i in range(len(A)):
print(A[i])
# print u
print("u - vector")
print(u)
# determine and Print the shape of A
rows = len(A)
cols = len(A[0])
print('A rows = ',rows,'A cols = ',cols)
print('determine and Print the shape of u')
print('u rows = ',len(u))
print('Print the first column of A')
for i in range(len(A)):
print(A[i][0])
print('Print the first two rows of A')
print(A[0])
print(A[1])
print('Print the first two entries of u')
print(u[0])
print(u[1])
print('Print the last two entries of u')
print(u[-2:])
print('Print the bottom left 2×2 partition (submatrix) of A')
for i in range(1,len(A)):
print(A[i][:2])
print('Print the middle two elements of the middle row of A')
print(A[1][1:3])
A - matrix [1, 3, 5, 7] [2, 4, 6, 8] [-3, -2, -1, 0] u - vector [10, 20, 30] A rows = 3 A cols = 4 determine and Print the shape of u u rows = 3 Print the first column of A 1 2 -3 Print the first two rows of A [1, 3, 5, 7] [2, 4, 6, 8] Print the first two entries of u 10 20 Print the last two entries of u [20, 30] Print the bottom left 2×2 partition (submatrix) of A [2, 4] [-3, -2] Print the middle two elements of the middle row of A [4, 6]
Use your script to multiply two matrices, just like in the Lab (in-Lab portion). Apply the script to find $\mathbf{A}\mathbf{B}$ where.
\begin{gather} \mathbf{A} = \begin{pmatrix} 1 & 2 \\ 3 & 4 \\ 5 & 6 \\ \end{pmatrix} ~~~~ \mathbf{B} = \begin{pmatrix} 7 & 8 & 9 \\ 10 & 11 & 12 \\ \end{pmatrix} \end{gather}The two matrices are located in files:
http://54.243.252.9/engr-1330-webroot/8-Labs/Lab08/Amat.txt
and:
http://54.243.252.9/engr-1330-webroot/8-Labs/Lab08/Bmat.txt
You should download these files before proceeding
# read file Amat.txt
A = [] # empty list to the map information
infile = open("Amat.txt","r") # open a read connection
for line in infile:
A.append([float(n) for n in line.strip().split(",")])
infile.close()
# read file Bmat.txt
B = [] # empty list to the map information
infile = open("Bmat.txt","r") # open a read connection
for line in infile:
B.append([float(n) for n in line.strip().split(",")])
infile.close()
# create a destination matrix ABmatrix
AB = [[0 for i in range(len(B[0]))] for i in range(len(A))]
# print Amat
print('A matrix')
for i in range(len(A)):
print(A[i])
# print Bmat
print('B matrix')
for i in range(len(B)):
print(B[i])
# perform the multiplication put the result into ABmatrix
for i in range(0,len(A)):
for j in range(0,len(B[0])):
for k in range(0,len(A[0])):
AB[i][j]=AB[i][j]+A[i][k]*B[k][j]
# print ABmatrix
print('AB matrix')
for i in range(len(AB)):
print(AB[i])
A matrix [1.0, 2.0] [3.0, 4.0] [5.0, 6.0] B matrix [7.0, 8.0, 9.0] [10.0, 11.0, 12.0] AB matrix [27.0, 30.0, 33.0] [61.0, 68.0, 75.0] [95.0, 106.0, 117.0]
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