<a href="https://colab.research.google.com/github/johanaluna/AB-Demo/blob/master/module1-linear-algebra-review/Johana_DS7_Linear_Algebra_Assignment.ipynb" target="_parent"></a>

# Part 1 - Scalars and Vectors

For the questions below it is not sufficient to simply provide answer to the questions, but you must solve the problems and show your work using python (the NumPy library will help a lot!) Translate the vectors and matrices into their appropriate python  representations and use numpy or functions that you write yourself to demonstrate the result or property. 

## 1.1 Create a two-dimensional vector and plot it on a graph


```
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from numpy import random
a= np.array([np.random.randint(0,8,2), np.random.randint(0,8,2),np.random.randint(0,8,2)])
a
```




    array([[3, 7],
           [6, 2],
           [3, 6]])




```
plt.arrow(0,0, a[0,0], a[0,1],head_width=.05, head_length=0.25, color ='red' )
plt.arrow(0,0, a[1,0], a[1,1],head_width=.05, head_length=0.25, color ='green')
plt.arrow(0,0, a[2,0], a[2,1],head_width=.05, head_length=0.25, color ='blue')
plt.xlim(0,8)          
plt.ylim(0,8)
plt.title("Scaled Vectors")
plt.show()
```

## 1.2 Create a three-dimensional vecor and plot it on a graph


```
from mpl_toolkits.mplot3d import Axes3D

```


```
b= np.array([np.random.randint(7,size=3),np.random.randint(7,size=3),np.random.randint(7,size=3)])
b
```




    array([[4, 4, 4],
           [0, 2, 6],
           [5, 6, 3]])




```
 X, Y, Z = zip(*b)
 ini=np.array([0,0,0])
 Z
```




    (4, 6, 3)




```
fig = plt.figure()
colors = ['b','r','k']
ax = fig.add_subplot(111, projection='3d')

ax.quiver(*ini,X,Y,Z, length=1, color='g')
ax.set_xlim([0, 7])
ax.set_ylim([0, 7])
ax.set_zlim([0, 7])
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show();
```

## 1.3 Scale the vectors you created in 1.1 by $5$, $\pi$, and $-e$ and plot all four vectors (original + 3 scaled vectors) on a graph. What do you notice about these vectors? 


```
from math import e, pi
print(e)
print(pi)
```

    2.718281828459045
    3.141592653589793



```
fig = plt.figure(figsize=(13,13))

plt.arrow(0,0, 5*a[0,0], 5*a[0,1],head_width=.5, head_length=0.25, color ='#a5fafa' )
plt.arrow(0,0, 5*a[1,0], 5*a[1,1],head_width=.5, head_length=0.25, color ='#a5fafa')
plt.arrow(0,0, 5*a[2,0], 5*a[2,1],head_width=.5, head_length=0.25, color ='#a5fafa')

plt.arrow(0,0, pi*a[0,0], pi*a[0,1],head_width=.5, head_length=0.25, color ='orange' )
plt.arrow(0,0, pi*a[1,0], pi*a[1,1],head_width=.5, head_length=0.25, color ='orange')
plt.arrow(0,0, pi*a[2,0], pi*a[2,1],head_width=.5, head_length=0.25, color ='orange')

plt.arrow(0,0, e*a[0,0], e*a[0,1],head_width=.5, head_length=0.25, color ='red')
plt.arrow(0,0, e*a[1,0], e*a[1,1],head_width=.5, head_length=0.25, color ='red')
plt.arrow(0,0, e*a[2,0], e*a[2,1],head_width=.5, head_length=0.25, color ='red')

plt.arrow(0,0, a[0,0], a[0,1],head_width=.5, head_length=0.25, color ='blue' )
plt.arrow(0,0, a[1,0], a[1,1],head_width=.5, head_length=0.25, color ='blue')
plt.arrow(0,0, a[2,0], a[2,1],head_width=.5, head_length=0.25, color ='blue')

plt.xlim(0,40)          
plt.ylim(0,40)
plt.title("Scaled Vectors")
plt.show()
```

## 1.4 Graph vectors $\vec{a}$ and $\vec{b}$ and plot them on a graph

\begin{align}
\vec{a} = \begin{bmatrix} 5 \\ 7 \end{bmatrix}
\qquad
\vec{b} = \begin{bmatrix} 3 \\4 \end{bmatrix}
\end{align}


```
a=[5,7]
b=[3,4]
np_a=np.array(a)
np_b=np.array(b)
np_a
```




    array([5, 7])




```
fig = plt.figure(figsize=(6,6))
plt.arrow(0,0, np_a[0], np_a[1],head_width=.5, head_length=0.25, color ='orange')
plt.arrow(0,0, np_b[0], np_b[1],head_width=.5, head_length=0.25, color ='red')
plt.xlim(0,10)          
plt.ylim(0,10)
plt.title("Scaled Vectors")
plt.show()
```

## 1.5 find $\vec{a} - \vec{b}$ and plot the result on the same graph as $\vec{a}$ and $\vec{b}$. Is there a relationship between vectors $\vec{a} \thinspace, \vec{b} \thinspace \text{and} \thinspace \vec{a-b}$


```
dif_ab=np_a-np_b
fig = plt.figure(figsize=(6,6))
plt.arrow(0,0, np_a[0], np_a[1],head_width=.5, head_length=0.25, color ='orange')
plt.arrow(0,0, np_b[0], np_b[1],head_width=.5, head_length=0.25, color ='red')
plt.arrow(0,0, dif_ab[0], dif_ab[1],head_width=.5, head_length=0.25, color ='green')
plt.xlim(0,10)          
plt.ylim(0,10)
plt.title("Scaled Vectors")
plt.show()

```

## 1.6 Find $c \cdot d$

\begin{align}
\vec{c} = \begin{bmatrix}7 & 22 & 4 & 16\end{bmatrix}
\qquad
\vec{d} = \begin{bmatrix}12 & 6 & 2 & 9\end{bmatrix}
\end{align}



```
c=[7,22,4,16]
d=[12,6,2,9]
np_c=np.array(c)
np_d=np.array(d)

dot_cd=np.dot(np_c,np_d)
dot_cd
```




    368



##  1.7 Find $e \times f$

\begin{align}
\vec{e} = \begin{bmatrix} 5 \\ 7 \\ 2 \end{bmatrix}
\qquad
\vec{f} = \begin{bmatrix} 3 \\4 \\ 6 \end{bmatrix}
\end{align}


```
e=[5,7,2]
f=[3,4,6]
np_e=np.array(e)
np_f=np.array(f)
cross_ef=np.cross(np_e,np_f)
cross_ef
```




    array([ 34, -24,  -1])



## 1.8 Find $||g||$ and then find $||h||$. Which is longer?

\begin{align}
\vec{g} = \begin{bmatrix} 1 \\ 1 \\ 1 \\ 8 \end{bmatrix}
\qquad
\vec{h} = \begin{bmatrix} 3 \\3 \\ 3 \\ 3 \end{bmatrix}
\end{align}


```
g=[1,1,1,8]
h=[3,3,3,3]
np_g=np.array(g)
np_h=np.array(h)
norm_g=np.linalg.norm(np_g)
norm_h=np.linalg.norm(np_h)
print('G:',norm_g)
print('H:',norm_h)
```

    G: 8.18535277187245
    H: 6.0


# Part 2 - Matrices

## 2.1 What are the dimensions of the following matrices? Which of the following can be multiplied together? See if you can find all of the different legal combinations.
\begin{align}
A = \begin{bmatrix}
1 & 2 \\
3 & 4 \\
5 & 6
\end{bmatrix}
\qquad
B = \begin{bmatrix}
2 & 4 & 6 \\
\end{bmatrix}
\qquad
C = \begin{bmatrix}
9 & 6 & 3 \\
4 & 7 & 11
\end{bmatrix}
\qquad
D = \begin{bmatrix}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1
\end{bmatrix}
\qquad
E = \begin{bmatrix}
1 & 3 \\
5 & 7
\end{bmatrix}
\end{align}


```
A=np.array([[1,2],[3,4],[5,6]])
B=np.array([[2,4,6]])
C=np.array([[9,6,3],[4,7,11]])
D=np.array([[1,0,0],[0,1,0],[0,0,1]])
E=np.array([[1,3],[5,7]])
E.shape
```




    (2, 2)




```
A_size=A.shape
B_size=B.shape
C_size=C.shape
D_size=D.shape
E_size=E.shape
```

## 2.2 Find the following products: CD, AE, and BA. What are the dimensions of the resulting matrices? How does that relate to the dimensions of their factor matrices?


```
CD=np.matmul(C,D)
print('size C ' , C_size)
print('size D ',D_size)
print('size CD',CD.shape)

AE=np.matmul(A,E)
print('\nsize A ' , A_size)
print('size E ',E_size)
print('size AE',AE.shape)

BA=np.matmul(B,A)
print('\nsize B ' , B_size)
print('size A ',A_size)
print('size BA',BA.shape)
```

    size C  (2, 3)
    size D  (3, 3)
    size CD (2, 3)
    
    size A  (3, 2)
    size E  (2, 2)
    size AE (3, 2)
    
    size B  (1, 3)
    size A  (3, 2)
    size BA (1, 2)


## 2.3  Find $F^{T}$. How are the numbers along the main diagonal (top left to bottom right) of the original matrix and its transpose related? What are the dimensions of $F$? What are the dimensions of $F^{T}$?

\begin{align}
F = 
\begin{bmatrix}
20 & 19 & 18 & 17 \\
16 & 15 & 14 & 13 \\
12 & 11 & 10 & 9 \\
8 & 7 & 6 & 5 \\
4 & 3 & 2 & 1
\end{bmatrix}
\end{align}


```
F=np.array([[20,19,18,17],[16,15,14,13],[12,11,10,9],[8,7,6,5],[4,3,2,1]])
print(F.shape)
F
```

    (5, 4)





    array([[20, 19, 18, 17],
           [16, 15, 14, 13],
           [12, 11, 10,  9],
           [ 8,  7,  6,  5],
           [ 4,  3,  2,  1]])




```
F_tranpose=F.T
print(F_tranpose.shape)
F_tranpose
```

    (4, 5)





    array([[20, 16, 12,  8,  4],
           [19, 15, 11,  7,  3],
           [18, 14, 10,  6,  2],
           [17, 13,  9,  5,  1]])



# Part 3 - Square Matrices

## 3.1 Find $IG$ (be sure to show your work) 😃

\begin{align}
G= 
\begin{bmatrix}
12 & 11 \\
7 & 10 
\end{bmatrix}
\end{align}


```
G=np.array([[12,11],[7,10]])
G
```




    array([[12, 11],
           [ 7, 10]])




```
def getMatrixDeternminant(m):
    #base case for 2x2 matrix
    if len(m) == 2:
        return m[0][0]*m[1][1]-m[0][1]*m[1][0]

def getMatrixInverse(m):
    determinant = getMatrixDeternminant(m)
    #special case for 2x2 matrix:
    if len(m) == 2:
        return [[m[1][1]/determinant, -1*m[0][1]/determinant],
                [-1*m[1][0]/determinant, m[0][0]/determinant]]

getMatrixInverse(G)
```




    [[0.23255813953488372, -0.2558139534883721],
     [-0.16279069767441862, 0.27906976744186046]]




```
print(np.linalg.inv(G))
```

    [[ 0.23255814 -0.25581395]
     [-0.1627907   0.27906977]]


## 3.2 Find $|H|$ and then find $|J|$.

\begin{align}
H= 
\begin{bmatrix}
12 & 11 \\
7 & 10 
\end{bmatrix}
\qquad
J= 
\begin{bmatrix}
0 & 1 & 2 \\
7 & 10 & 4 \\
3 & 2 & 0
\end{bmatrix}
\end{align}



```
H= np.array([[12,11],[7,10]])
J=np.array([[0,1,2],[7,10,4],[3,2,0]])
det_H=np.linalg.det(H)
det_J=np.linalg.det(J)
print('DET H:',det_H)
print('DET J:',det_J)
```

    DET H: 43.000000000000014
    DET J: -19.999999999999996


## 3.3 Find $H^{-1}$ and then find $J^{-1}$


```
inv_H=np.linalg.inv(H)
inv_J=np.linalg.inv(J)
print('Inv H:\n',inv_H)
print('Inv J:\n',inv_J)
```

    Inv H:
     [[ 0.23255814 -0.25581395]
     [-0.1627907   0.27906977]]
    Inv J:
     [[ 0.4  -0.2   0.8 ]
     [-0.6   0.3  -0.7 ]
     [ 0.8  -0.15  0.35]]


## 3.4 Find $HH^{-1}$ and then find $J^{-1}J$. Is $HH^{-1} == J^{-1}J$? Why or Why not?


```
H_invH=H*inv_H
print('H_invH:\n',H_invH)
J_invJ=J*inv_J
print('\nJ_invJ\n:',J_invJ)

H_invH==J_invJ
```

    H_invH:
     [[ 2.79069767 -2.81395349]
     [-1.13953488  2.79069767]]
    
    J_invJ
    : [[ 0.  -0.2  1.6]
     [-4.2  3.  -2.8]
     [ 2.4 -0.3  0. ]]


    /usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:6: DeprecationWarning: elementwise comparison failed; this will raise an error in the future.
      





    False



# Stretch Goals: 

A reminder that these challenges are optional. If you finish your work quickly we welcome you to work on them. If there are other activities that you feel like will help your understanding of the above topics more, feel free to work on that. Topics from the Stretch Goals sections will never end up on Sprint Challenges. You don't have to do these in order, you don't have to do all of them. 

- Write a function that can calculate the dot product of any two vectors of equal length that are passed to it.
- Write a function that can calculate the norm of any vector
- Prove to yourself again that the vectors in 1.9 are orthogonal by graphing them. 
- Research how to plot a 3d graph with animations so that you can make the graph rotate (this will be easier in a local notebook than in google colab)
- Create and plot a matrix on a 2d graph.
- Create and plot a matrix on a 3d graph.
- Plot two vectors that are not collinear on a 2d graph. Calculate the determinant of the 2x2 matrix that these vectors form. How does this determinant relate to the graphical interpretation of the vectors?



419ea42600e334b2f203bf713f89ac229dd6e498
ipynb
Jupyter Notebook
module1-linear-algebra-review/Johana_DS7_Linear_Algebra_Assignment.ipynb
johanaluna/AB-Demo
0db3cb58567b1b433d428e30852c2de63c742cea
MIT
module1-linear-algebra-review/Johana_DS7_Linear_Algebra_Assignment.ipynb
johanaluna/AB-Demo
0db3cb58567b1b433d428e30852c2de63c742cea
MIT
module1-linear-algebra-review/Johana_DS7_Linear_Algebra_Assignment.ipynb
johanaluna/AB-Demo
0db3cb58567b1b433d428e30852c2de63c742cea
MIT
Qwen/Qwen-72B
1. YES
2. YES