In order to successfully complete this assignment you need to participate both individually and in groups during class. If you attend class in-person then have one of the instructors check your notebook and sign you out before leaving class. If you are attending asyncronously, turn in your assignment using D2L no later than 11:59pm on the day of class. See links at the end of this document for access to the class timeline for your section.

In-Class Assignment: Gauss-Jordan

Image showing an instructor writing on the board instruction for Gauss-Jordan.  These include 1) 1 Row 1 column 1 2) Kill the rest of the column 3) 1 Row 2 Column 2 4) Kill the rest of the column 4) Answer as a point (x,y).  Although not really an algorithm these instructions give a general idea of how Gauss-Jordan works

Image from: YouTube

#Load Useful Python Libraries 
%matplotlib inline
import matplotlib.pylab as plt
import numpy as np
import sympy as sym
sym.init_printing(use_unicode=True)

1. Pre-class assignment review


2. Generalize the procedure

We are going to think about Gauss-Jordan as an algorithm. First I want you to think about how you would Gneralize the procedure to work on any matrix. Do the following beofre moving on to the next section.

**DO THIS:** Use the following matrix to think about how you would solve any system of equations using the Gauss-Jordan elimination algorithm. Focus on the steps.

$$ \left[ \begin{matrix} a & b & c \\ e & f & g \\ i & j & k \end{matrix} \, \middle\vert \, \begin{matrix} d \\ h \\ l \end{matrix} \right] $$

**QUESTION:** Describe the first three mathematical steps you would do to put the above equation into a reduced row echelon form using Gauss-Jordan method?

Psudocode

**DO THIS:** Write down the steps you would complete to implement the Gauss-Jordan elimination algorithm as a computer programer. Some questions to answer:

  1. What are the inputs?
  2. What are the outputs?
  3. How many and what types of loops would you have to guarantee success of your program?

Once you have thought this though the instructor will work with you to build the algorithm.


3. Basic Gauss Jordan

The following is implementation of the Basic Gauss-Jordan Elimination Algorithm for Matrix $A^{m\times n}$ (Pseudocode):

for i from 1 to m:
    for j from 1 to n   
        if i ≠ j:
            Ratio = A[j,i]/A[i,i]
            #Elementary Row Operation 3
            for k from 1 to m:
                A[j,k] = A[j,k] - Ratio * A[i,k]
            next k
        endif
    next j

    #Elementary Row Operation 2
    Const = A[i,i]
    for k from 1 to m:
        A[i,k] = A[i,k]/Const
next i

**DO THIS**: using the Pseudocode provided above, write a basic_gauss_jordan function which takes a list of lists $A$ as input and returns the modified list of lists:

Put your answer here.

Lets check your function by applying the basic_gauss_jordan function and check to see if it matches the answer from matrix $A$ in the pre-class video:

A = [[1, 1, 1, 2], [2, 3, 1, 3], [0, -2, -3, -8]]
answer = basic_gauss_jordan(A)
sym.Matrix(answer)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-1a510226e6f1> in <module>
      1 A = [[1, 1, 1, 2], [2, 3, 1, 3], [0, -2, -3, -8]]
----> 2 answer = basic_gauss_jordan(A)
      3 sym.Matrix(answer)

NameError: name 'basic_gauss_jordan' is not defined
answer_from_video = [[1, 0, 0, -1], [0, 1, 0, 1], [0, 0, 1, 2]]
np.allclose(answer, answer_from_video)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-81798363f967> in <module>
      1 answer_from_video = [[1, 0, 0, -1], [0, 1, 0, 1], [0, 0, 1, 2]]
----> 2 np.allclose(answer, answer_from_video)

NameError: name 'answer' is not defined

The above psuedocode does not quite work properly for all matrices. For example, consider the following augmented matrix:

$$ B = \left[ \begin{matrix} 0 & 1 & 33\\ 5 & 3 & 7 \\ 6 & 69 & 4 \end{matrix} \, \middle\vert \, \begin{matrix} 30 \\ 90 \\ 420 \end{matrix} \right] $$

**QUESTION** Explain why doesn't the provided basic_gauss_jordan function work on the matrix $B$?

Put your answer to the above question here.

**QUESTION** Describe how you could modify matrix $B$ so that it would work with basic_gauss_jordan AND still give the correct solution?

Put your answer to the above question here.

# Put your code here

4. Sympy RREF function

The Python sympy library has a "reduced row echelon form" (rref) function which runs a much more efficent version of the Gauss-Jordan function. To use the rref funciton you must first convert your matrix into a sympy.Matrix and then run the function. For example, lets do this for the above matrix $B$ which I will show again here:

sym.Matrix(B).rref()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-b960e968afb8> in <module>
----> 1 sym.Matrix(B).rref()

NameError: name 'B' is not defined

This function outputs two values (a matrix an a tuple). For the purposes of this class we only the matrix. I generally use the following syntac when using rref()

sym.Matrix(B).rref()[0]
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-6-373e2e7cca9a> in <module>
----> 1 sym.Matrix(B).rref()[0]

NameError: name 'B' is not defined

**QUESTION**: Although we do not use it often in this course, what does the second output of the rref mean (i.e. what does (0,1,2) mean? hint: read the documentation for rref.

Put your answer to the above question here

How lets consider the multi-week example from the pre class assignment, where:

Week 1: $$ c + b = 30 $$ $$ 20c + 25b = 690 $$

Week 2: $$ c + b = 35 $$ $$ 20c + 25b = 750 $$

Week 3: $$ c + b = 30 $$ $$ 20c + 25b = 650 $$

**DO THIS**: Write a $2 \times 5$ augmented matrix representing the 6 equations above. (you can just copy and paste this from the pre-class if you got it right there), Name your Matrix $G$ to verify your answer using the checkanswer function below.

#Put your answer to the above question here. 

The following function will apply the rref function to the matrix $G$ and store it in a variable called, wait for it, rref:

rref,_ = sym.Matrix(G).rref()
rref
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-8-9fcd3b8096ea> in <module>
----> 1 rref,_ = sym.Matrix(G).rref()
      2 rref

NameError: name 'G' is not defined

**QUESTION**: Given the above, How many hours did Giselle work as a capenter for the three weeks and how many hours did she work as a blacksmith. Fill in your answers below to check if you are correct:

#Replace the zeros with your answers
carpenter_week1 = 0
carpenter_week2 = 0
carpenter_week3 = 0
blacksmith_week1 = 0
blacksmith_week2 = 0
blacksmith_week3 = 0
from answercheck import checkanswer

hours = [[carpenter_week1, carpenter_week2, carpenter_week3],
         [blacksmith_week1, blacksmith_week2, blacksmith_week3]]
hours = np.matrix(hours).astype('float')

checkanswer.matrix(hours,'b2d4a73cac3c95204f5ed743b507093a');
CheckWarning: Matrix contains negative values for zero...
    Converting to positive values of zero using  ```A[A==-0] = 0```.

Testing [[0. 0. 0.]
 [0. 0. 0.]]
Answer seems to be incorrect

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-10-dfedafeefd8c> in <module>
      5 hours = np.matrix(hours).astype('float')
      6 
----> 7 checkanswer.matrix(hours,'b2d4a73cac3c95204f5ed743b507093a');

~/_CMSE314_F20/CMSE314/answercheck.py in matrix(A, hashtag, decimal_accuracy)
    150         """Function to check matrix type before hashing."""
    151         A = checkanswer.make_matrix(A, decimal_accuracy)
--> 152         return checkanswer.basic(A, hashtag)
    153 
    154     # TODO: Not complete or tested.

~/_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 df24671116f09dd27a782ce09c010e05

Congratulations, we're done!

If you attend class in-person then have one of the instructors check your notebook and sign you out before leaving class. If you are attending remote, turn in your assignment using D2L.

Course Resources:

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.