In order to successfully complete this assignment, you must do the required reading, watch the provided videos, and complete all instructions. The embedded survey form must be entirely filled out and submitted on or before 11:59pm on the day before class. Students must come to class the next day prepared to discuss the material covered in this assignment.

Pre-Class Assignment: Python Linear Algebra Packages

This pre-class assignment is designed to provide a review of the Python packages we will be using in this course.

I think even experienced Python programmers may learn something from the videos. However, feel free to run them at a faster speed and/or skip ahead and review the google doc.

1. Inner Product and Complexity

**DO THIS:** Review _Sections 1.4 and 1.5 of the Boyd and Vandenberghe text and answer the questions below.

**QUESTION:** What is the dot product between $u = [ 1, 7, 9, 11]$ and $v = [ 7, 1, 2, 2]$ (Store the information in a variable called uv)?

Put your answer to the above question here.

from answercheck import checkanswer

checkanswer(uv,'a684eceee76fc522773286a895bc8436');
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-039e770f67ab> in <module>
      1 from answercheck import checkanswer
      2 
----> 3 checkanswer(uv,'a684eceee76fc522773286a895bc8436');

NameError: name 'uv' is not defined

**QUESTION:** What is the norm of vector $u$ defined above (store this value in a variabled called n)?

Put your answer to the above question here.

from answercheck import checkanswer

checkanswer(n,'b5b9dc568a9d7d71a6b4c5c95327c6a2');
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-d932271522df> in <module>
      1 from answercheck import checkanswer
      2 
----> 3 checkanswer(n,'b5b9dc568a9d7d71a6b4c5c95327c6a2');

NameError: name 'n' is not defined

**QUESTION:** What is the distance between points $u$ and $v$ defined above. (put your answer in a variable named d)

Put your answer to the above question here.

from answercheck import checkanswer

checkanswer(d,'f09106bd6e8d2573f8bcf0a2073a8a24');
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-7fd68173bd89> in <module>
      1 from answercheck import checkanswer
      2 
----> 3 checkanswer(d,'f09106bd6e8d2573f8bcf0a2073a8a24');

NameError: name 'd' is not defined

2. Review of Python Math Package

Direct Link to the Youtube video.

from IPython.display import YouTubeVideo
YouTubeVideo("PBlKeuzUf5g",width=640,height=320, cc_load_policy=True)

**DO THIS:** In the following cell, load the math package and run the hypot function with inputs (3,4).

#Put your answer here

**QUESTION:** What does the hypot function do?

Put your answer to the above question here.


3. Review of Python Numpy Package

Direct Link to the Youtube video.

from IPython.display import YouTubeVideo
YouTubeVideo("_hbWtNgstlI",width=640,height=320, cc_load_policy=True)

The Python Numpy library has a matrix object which can be initialized as follows:

import numpy as np
A = np.matrix([[1,1], [20,25]])
b = np.matrix([[30],[690]])
print("A="+str(A))
print("b="+str(b))
A=[[ 1  1]
 [20 25]]
b=[[ 30]
 [690]]

Python can solve equations in the $Ax=b$ format with the numpy.linalg library. For example:

import numpy as np

x = np.linalg.solve(A, b)
print("x="+str(x))
x=[[12.]
 [18.]]

The numpy.linalg library is just a subset of the scipy.linalg library. Oddly you can't load the SciPy library the same way. Instead you can call it as follows:

import scipy.linalg as la

x = la.solve(A, b)
print("X="+str(x))
X=[[12.]
 [18.]]

**DO THIS:** Convert the following system of linear equations to numpy matrices and solve it sing a Python linear algebra solver (Store the solutions in a vector named x). $$ 18x+21y = 226$$ $$ 72x-3y = 644$$

##Put your answer to the above question here.
from answercheck import checkanswer

checkanswer.vector(x,'756ca9fa3951fad0e623b2a8315d5fd7');
CheckWarning: passed variable is <class 'numpy.ndarray'> and not a numpy.matrix.
    Trying to convert to a array matrix using ```A = np.matrix(A)```.


CheckWarning: numpy.matrix is row vector...
    Trying to convert to a column vector using ```A = A.T```.

Testing [[12. 18.]]
Answer seems to be incorrect

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-11-9698db3c0fd0> in <module>
      1 from answercheck import checkanswer
      2 
----> 3 checkanswer.vector(x,'756ca9fa3951fad0e623b2a8315d5fd7');

~/_CMSE314_F20/CMSE314/answercheck.py in vector(A, hashtag, decimal_accuracy)
    104     def vector(A, hashtag=None, decimal_accuracy = 5):
    105         A = checkanswer.make_vector(A, decimal_accuracy)
--> 106         return checkanswer.basic(A, hashtag)
    107 
    108     def eq_vector(A, hashtag=None, decimal_accuracy = 5):

~/_CMSE314_F20/CMSE314/answercheck.py in basic(var, hashtag)
     48             else:
     49                 print("Answer seems to be incorrect\n")
---> 50                 assert checktag == hashtag, f"Answer is incorrect {checktag}"
     51         else:
     52             raise TypeError(f"No answer hastag provided: {checktag}")

AssertionError: Answer is incorrect db03b41005ab488f64e4028c15f96340

4. Advanced Python Indexing

This one is a little long and reviews some of the information from the last video. However, I really like using images as a way to talk about array and matrix indexing in Numpy.

Direct Link to the Youtube video.

from IPython.display import YouTubeVideo
YouTubeVideo("XSyiafkKerQ",width=640,height=360, cc_load_policy=True)
%matplotlib inline
import matplotlib.pylab as plt
import numpy as np
import imageio

#from urllib.request import urlopen, urlretrieve
#from scipy.misc import imsave

url = 'https://res.cloudinary.com/miles-extranet-dev/image/upload/ar_16:9,c_fill,w_1000,g_face,q_50/Michigan/migration_photos/G21696/G21696-msubeaumonttower01.jpg'
im = imageio.imread(url)

im[10,10,0] = 255
im[10,10,1] = 255
im[10,10,2] = 255

#Show the image
plt.imshow(im);
im[20,20,:] = 255
plt.imshow(im)
<matplotlib.image.AxesImage at 0x7f38ac388a58>
cropped = im[0:50,0:50,:]
plt.imshow(cropped)
<matplotlib.image.AxesImage at 0x7f38ac36a908>
cropped = im[50:,350:610,:]
plt.imshow(cropped)
<matplotlib.image.AxesImage at 0x7f38ac33dbe0>
red = im[:,:,0]
plt.imshow(red)
plt.colorbar()
<matplotlib.colorbar.Colorbar at 0x7f38ac246358>
#Note python changed slightly since the making of the video.  
# We added the astype funciton to ensure that values are between 0-255
red_only = np.zeros(im.shape).astype(int)
red_only[:,:,0] = red

plt.imshow(red_only)
<matplotlib.image.AxesImage at 0x7f38a407a7b8>
green_only = np.zeros(im.shape).astype(int)
green_only[:,:,1] = im[:,:,1]

plt.imshow(green_only)
<matplotlib.image.AxesImage at 0x7f38a4058198>
blue_only = np.zeros(im.shape).astype(int)
blue_only[:,:,2] = im[:,:,2]

plt.imshow(blue_only)
<matplotlib.image.AxesImage at 0x7f38ad473a58>

**DO THIS:** Modify the following code to set all of the values in the blue channel to zero using only one simple line of indexing code.

no_blue = im.copy()
#####Start your code here #####

#####End of your code here#####  
plt.imshow(no_blue)
<matplotlib.image.AxesImage at 0x7f38ad471550>

**QUESTION:** What was the command you use to set all of the values of blue inside no_blue to zero?

Put your answer to the above question here.


5. LaTeX Math

Direct Link to the Youtube video.

from IPython.display import YouTubeVideo
YouTubeVideo("qgSa7n_zQ3A",width=640,height=320, cc_load_policy=True)

Since this is a "Matrix Algebra" course, we need to learn how to do 'matrices' in LaTeX. Double click on the following cell to see the LaTeX code to build a matrix:

Basic matrix notation:

$$ \left[ \begin{matrix} 1 & 0 & 4 \\ 0 & 2 & -2 \\ 0 & 1 & 2 \end{matrix} \right] $$

Augmented matrix notation:

$$ \left[ \begin{matrix} 1 & 0 & 4 \\ 0 & 2 & -2 \\ 0 & 1 & 2 \end{matrix} ~ \middle\vert ~ \begin{matrix} -10 \\ 3 \\ 1 \end{matrix} \right] $$

**DO THIS:** Using LaTeX, create an augmented matrix for the following system of equations:

$$~4x + 2y -7z = 3~$$$$12x ~~~~~~~~+ ~z = 10$$$$-3x -~y + 2z = 30$$

Put your LaTeX code here. (Hint: copy and paste from above)

**QUESTION:** In LaTeX, what special characters are used to separate elements inside a row?

Put your answer to the above question here.


6. Assignment wrap-up

Please fill out the form that appears when you run the code below. You must completely fill this out in order to receive credit for the assignment!

Direct Link to Google Form

If you have trouble with the embedded form, please make sure you log on with your MSU google account at googleapps.msu.edu and then click on the direct link above.

**Assignment-Specific QUESTION:** What is the answers you calculated using Python for $x$ and $y$ to above system of linear equations: $ 18x+21y = 226$ and $ 72x-3y = 644$?

Put your answer to the above question here

**QUESTION:** Summarize what you did in this assignment.

Put your answer to the above question here

**QUESTION:** What questions do you have, if any, about any of the topics discussed in this assignment after working through the jupyter notebook?

Put your answer to the above question here

**QUESTION:** How well do you feel this assignment helped you to achieve a better understanding of the above mentioned topic(s)?

Put your answer to the above question here

**QUESTION:** What was the most challenging part of this assignment for you?

Put your answer to the above question here

**QUESTION:** What was the least challenging part of this assignment for you?

Put your answer to the above question here

**QUESTION:** What kind of additional questions or support, if any, do you feel you need to have a better understanding of the content in this assignment?

Put your answer to the above question here

**QUESTION:** Do you have any further questions or comments about this material, or anything else that's going on in class?

Put your answer to the above question here

**QUESTION:** Approximately how long did this pre-class assignment take?

Put your answer to the above question here

from IPython.display import HTML
HTML(
"""
<iframe 
	src="https://cmse.msu.edu/cmse314-pc-survey" 
	width="100%" 
	height="1000px" 
	frameborder="0" 
	marginheight="0" 
	marginwidth="0">
	Loading...
</iframe>
"""
)

Congratulations, we're done!

To get credits for this assignment, you must fill out and submit the above survey form on or before the assignment due date.

Course Resources:

Written by Dr. and Dirk Colbry, Michigan State University Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.