# 12 In-Class Assignment: Matrix Representation¶

Image from: wikipedia

## Agenda for today’s class (80 minutes)¶

%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-e3122a161773> in <module>
----> 1 get_ipython().run_line_magic('matplotlib', 'inline')
2 import matplotlib.pylab as plt
3 import numpy as np
4 import sympy as sym
5 sym.init_printing(use_unicode=True)

~/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'


## 2. Matrix Representation of Vector Spaces¶

Consider the following matrix $$A$$.

$\begin{split} \left[ \begin{matrix} 1 & 0 & 3 \\ 0 & 1 & 5 \\ 1 & 1 & 8 \end{matrix} \right] \end{split}$

QUESTION: What is the reduced row echelon form of $$A$$?

# Put your answer to the above question here.

from answercheck import checkanswer



ROW SPACE The first and second (non zero) rows of the above matrix “spans” the same space as the orignal three row vectors in $$A$$. We often call this the “row space” and it can be written as a linear combination of the non-zero rows of the reduced row echelon form:

$row(A) = r(1,0,3)^\top+s(0,1,5)^\top$

DO THIS: Calculate the solutions to the system of homogeneous equations $$Ax=0$$. This is often called the NULL SPACE or sometimes KERNEL of $$A$$.

#Put your asnwer here


DO THIS: We introduced two subspaces. Pick one vector from the row space of $$A$$ and another vector from the null space of $$A$$. Find the dot product of these two vector.

#Put your answer here


Question: Did you get the same value for the dot product? Explain your answer.

DO THIS: What is the reduced row echelon form of $$A^T$$?

#Put your answer here


COLUMN SPACE: The first and second (non zero) rows of the above matrix “spans” the same space as the original three column vectors in $$A$$. We often call this the “column space” (or “image space”) of $$A$$ and it can be written as a linear combination of the non-zero rows of the reduced row echelon form of $$A^T$$:

$col(A) = a(1,0,1)^\top+b(0,1,1)^\top$

DO THIS: Calculate the solutions to the system of homogeneous equations $$A^Tx=0$$. This is often called the NULL SPACE of $$A^T$$.

Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)

### Example #1¶

Consider the following system of linear equations.

$x_1 - x_2 + x_3 = 3$
$-2x_1 + 2x_2 - 2x_3 = -6$

DO THIS: What are the solutions to the above system of equations?

# Put your code here


DO THIS: Come up with a specific arbitrary solution (any solution will do) to the above set of equations.

DO THIS: Now consider only the left hand side of the above matrix and solve for the kernel (null Space) of A:

$\begin{split} A = \left[ \begin{matrix} 1 & -1 & 1 \\ -2 & 2 & -2 \end{matrix} \right] \end{split}$
#Put your answer here


DO THIS: Express an arbitrary solution as the sum of an element of the kernel of the transformation defined by the matrix of coefficients and a particular solution.

DO THIS: Discuss in your group and the class your solution from above. How does the solution to $$Ax=b$$ relate to the solution to $$Ax=0$$. If you were to plot all solutions, what shape does it take? How does this shape relate to the kernel?

## 3. Practice Nutrition¶

Big Annie’s Mac and Cheese fans want to improve the levels of protein and fiber for lunch by adding broccoli and canned chicken. The nutrition information for the foods in this problem are

Nutrient

Mac and Cheese

Broccoli

Chicken

Shells and White Cheddar

Calories

270

51

70

260

Protein (g)

10

5.4

15

9

Fiber (g)

2

5.2

0

5

She wants to achieve the goals with exactly 400 calories, 30 g of protein, and 10 g of fiber by choosing the combination of these three or four serving. (Assume that we can have non-integer proportions for each serving.)

Question a: We consider all four choices of food together. Formulate the problem into a system of equations $$$Ax = b.$$$$Create your matrix$$A$$and the column vector$$b$ in np.matrix.

import numpy as np
A = np.matrix()
b = np.matrix()


Question b: In this and next question, we only consider three out of the four choices. What proportions of these servings of the three food (Mac and Cheese, Broccoli, and Chicken) should be used to meet the goal? (Hint: formulate it as a system of equations and solve it).

#Put your answer here


Question c: She found that there was too much broccoli in the proportions from part (b), so she decided to switch from classical Mac and Cheese to Annie’s Whole Wheat Shells and White Cheddar. What proportions of servings of the new three food should she use to meet the goals?

#Put your answer here


Question d: Based on the solutions to parts (b) and (c), what are the possible proportions of serving for the four food that meet the goal.

#Put your answer here