# 05 Pre-Class Assignment: Gauss-Jordan Elimination¶

## Goals for today’s pre-class assignment¶

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

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-11545911a367> in <module>
1 #  Load Useful Python Libraries
----> 2 get_ipython().run_line_magic('matplotlib', 'inline')
3 import matplotlib.pylab as plt
4 import numpy as np
5 import sympy as sym

~/REPOS/MTH314_Textbook/MakeTextbook/envs/lib/python3.9/site-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line, _stack_depth)
2342                 kwargs['local_ns'] = self.get_local_scope(stack_depth)
2343             with self.builtin_trap:
-> 2344                 result = fn(*args, **kwargs)
2345             return result
2346

~/REPOS/MTH314_Textbook/MakeTextbook/envs/lib/python3.9/site-packages/decorator.py in fun(*args, **kw)
230             if not kwsyntax:
231                 args, kw = fix(args, kw, sig)
--> 232             return caller(func, *(extras + args), **kw)
233     fun.__name__ = func.__name__
234     fun.__doc__ = func.__doc__

~/REPOS/MTH314_Textbook/MakeTextbook/envs/lib/python3.9/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
185     # but it's overkill for just that one bit of state.
186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
188
189         if callable(arg):

~/REPOS/MTH314_Textbook/MakeTextbook/envs/lib/python3.9/site-packages/IPython/core/magics/pylab.py in matplotlib(self, line)
97             print("Available matplotlib backends: %s" % backends_list)
98         else:
---> 99             gui, backend = self.shell.enable_matplotlib(args.gui.lower() if isinstance(args.gui, str) else args.gui)
100             self._show_matplotlib_backend(args.gui, backend)
101

~/REPOS/MTH314_Textbook/MakeTextbook/envs/lib/python3.9/site-packages/IPython/core/interactiveshell.py in enable_matplotlib(self, gui)
3511         """
3512         from IPython.core import pylabtools as pt
-> 3513         gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
3514
3515         if gui != 'inline':

~/REPOS/MTH314_Textbook/MakeTextbook/envs/lib/python3.9/site-packages/IPython/core/pylabtools.py in find_gui_and_backend(gui, gui_select)
278     """
279
--> 280     import matplotlib
281
282     if gui and gui != 'auto':

ModuleNotFoundError: No module named 'matplotlib'


## 1. Sympy RREF function¶

In class we talked about the Python sympy library which has a “reduced row echelon form” (rref) function that runs a much more efficient version of the Gauss-Jordan function. To use the rref function you must first convert your matrix into a sympy.Matrix and then run the function. For example, lets do this for the following matrix $$B$$:

B = np.matrix([[ 50, 13, 30 ], [100, 26, 60 ],  [20.5, 25, 650]])
sym.Matrix(B).rref()


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

sym.Matrix(B).rref()[0]


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) mean? hint: read the documentation for rref.

How lets consider the multi-week example from a previous 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


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



## 2. Calculating Vector Length, Normalization, Distance and Dot¶

In this section we will cover some of the basic vector math we will use this semester.

DO THIS: Watch the following summary video about calculation of vector length, Normalizing vectors and the distance between points then answer the questions.

from IPython.display import YouTubeVideo


### Vector:¶

$(a_1, a_2, \dots a_n)$
$(b_1, b_2, \dots b_n)$

### Length:¶

$length = \sqrt{a_1^2 + a_2^2 + \dots + a_n^2}$

### Normalization:¶

$\frac{1}{length}(a_1, a_2, \dots a_n)$

### Distance:¶

$distance = \sqrt{(a_1 - b_1)^2 + (a_2 - b_2)^2 + \dots + (a_n - b_n)^2}$

QUESTION: Calculate length of vector (4.5, 2.6, 3.3, 4.1)?

#Put your answer to the above question here

from answercheck import checkanswer



QUESTION: What is a normalized form of the vector (4.5, 2.6, 3.3, 4.1)?

#Put your answer to the above question here

from answercheck import checkanswer



QUESTION: What is the distance between (4.5, 2.6, 3.3, 4.1) and (4, 3, 2, 1)?

#Put your answer to the above question here

from answercheck import checkanswer



### Dot Product:¶

$dot(a,b) = a_1b_1 + a_2b_2 +\dots + a_nb_n$

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



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



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



## 3. Vector spaces in $$R^n$$¶

There are two properties that define a vector space these are:

• Closed under scalar multiplication

For now, we will consider vector spaces in $$R^n$$ which are just vectors of real numbers (ex: [10,20,3.2], [5,8,32], [8,-0.7], etc) where $$n$$ is just the length of the vector (ex: 3, 3, and 2 in the earlier example). In the general case a vector does not have to be composed of real numbers but can be almost any type of object as long as it maintains the two above properties, we will get into this concept later in the semester. In the case of real number the above concepts can be described as follows:

• Closed under addition means that if we add any two real vectors vectors (i.e. $$u,v \in R^n$$) then the result is also in $$R^n$$). This is easy to understand if you think about adding any two real vectors there is no way to get a result that is not also a real vector. A way to say this mathematically is as follows:

$\text{if } u,v \in R^n$
$\text{then } u+v \in R^n$
• Closed under scalar multiplication means that if we have any scalar number ($$s \in R$$) and we multiply it by a real vector ($$v \in R^n$$) then the result is also a vector in $$R^n$$. Since multiplying a real number by a real number results in a real number this one is also true. Or we can say it as follows:

$\text{if } s \in R \text{ and } v \in R^n$
$\text{then } sv \in R^n$

The following are some properties of vector addition and multiplication for vectors $$u$$ and $$v$$:

1. $$u + v = v + u$$ Commutative property

2. $$u + (v + w) = (u + v) + w$$ Associative property

3. $$u+0 = 0 + u = u$$ Property of zero vector

4. $$u + (-u) = 0$$ Property of the negative vector

5. $$c(u+v) = cu + cv$$ Distributive properties

6. $$(c+d)u = cu+du$$ Distributive Properties

7. $$c(du) = (cd)u$$ Distributed Properties

8. $$1u = u$$ Scalar multiplication by 1

QUESTION: Compute the following linear combinations for $$u = (1,2), v = (4,-1)$$, and $$w = (-3,5)$$.

(a) $$a = u+w$$

from answercheck import checkanswer



(b) $$a = 2u+v$$

from answercheck import checkanswer



(c) $$a = u+3w$$

from answercheck import checkanswer



## 4. Assignment wrap up¶

Assignment-Specific QUESTION: What is the distance between (4.5, 2.6, 3.3, 4.1) and (4, 3, 2, 1)?

QUESTION: Summarize what you did in this assignment.

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

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

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

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

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?

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