Link to this document’s Jupyter Notebook

# Python Linear Algebra Packages¶

This tutorial 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.

## Assignment Overview¶

The jupytercheck Package is intended to provide students with immediate feedback to check answers inside of a Jupyter notebook. This was written with a Linear Algebra class in mind so it tries to do a robust comparison and take into consideration different object types as well as round off errors.

It works by providing a function called answercheck that takes in a variable to be checked and a “hash” which is a one-way function encoding the answer. The program generates a new hash based on the input variable and compares the two hash values. An output is provided that the answer appears correct or incorrect.

The program is also designed to run without installing anything in python. However, it does require the download of the correct file.

DO THIS: Two use answercheck we will need to download answercheck.py to your current working directory. You only really need to do this once. However, if you delete this file by mistake sometime during the semester, you can come back to this notebook and download it again by running the following cell:

from urllib.request import urlretrieve



Verify you have answercheck installed by running the following cell

from answercheck import checkanswer

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-2-4dd601693b2c> in <module>

1 import hashlib
2 import numpy as np
----> 3 import sympy as sym
4 import sys
5 import textwrap

ModuleNotFoundError: No module named 'sympy'


For more information about how answercheck works watch the following video:

from IPython.display import YouTubeVideo


NOTE make sure you do not change the checkanswer commands. The long string with numbers and letters is the secret code that encodes the true answer. This code is also called the HASH. Feel free to look at the answercheck.py code and see if you can figure out how it works?

## 2. Review of Python Math Package¶

DO THIS: Watch the following video about the python Math package.

from IPython.display import YouTubeVideo


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?

## 3. Review of Python Numpy Package¶

DO THIS: Watch the following video about the python Numpy package.

from IPython.display import YouTubeVideo


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))


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))


The numpy.linalg library is just a subset of the scipy.linalg library.

import scipy.linalg as la

x = la.solve(A, b)
print("X="+str(x))


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



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.

from IPython.display import YouTubeVideo

%matplotlib inline
import matplotlib.pylab as plt
import numpy as np
import imageio

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

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)

cropped = im[0:50,0:50,:]
plt.imshow(cropped)

cropped = im[50:,350:610,:]
plt.imshow(cropped)

red = im[:,:,0]
plt.imshow(red)
plt.colorbar()

#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)

green_only = np.zeros(im.shape).astype(int)
green_only[:,:,1] = im[:,:,1]

plt.imshow(green_only)

blue_only = np.zeros(im.shape).astype(int)
blue_only[:,:,2] = im[:,:,2]

plt.imshow(blue_only)


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()

plt.imshow(no_blue)


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

## 5. LaTeX Math¶

from IPython.display import YouTubeVideo


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:

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

Augmented matrix notation:

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

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?