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

  1. AnswerCheck tool
  2. Review of Python Math Package
  3. Review of Python Numpy Package
  4. Advanced Python Indexing
  5. LaTeX Math
  6. Assignment wrap-up

1. AnswerCheck tool

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:

Verify you have answercheck installed by running the following cell

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

Direct Link to the Youtube video.

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.

Direct Link to the Youtube video.

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

QUESTION: What does the hypot function do?

Put your answer to the above question here.


3. Review of Python Numpy Package

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

Direct Link to the Youtube video.

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

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

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

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


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.

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.

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.

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.


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