<a href="https://colab.research.google.com/github/tesseract314/100-pandas-puzzles/blob/master/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. 


```
import math
import matplotlib.pyplot as plt
import numpy as np
```

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


```
red = [1, 1]

plt.arrow(0,0, red[0], red[1],head_width=.05, head_length=0.05, color ='red')

plt.xlim(0,2)          
plt.ylim(0,2)
plt.title("One Two-Dimentional Vector")

plt.show()
```

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


```
from mpl_toolkits.mplot3d import Axes3D

red_3d = [1, 1, 1]

vectors = np.array([[0, 0, 0, 1, 1, 1]])

X, Y, Z, U, V, W = zip(*vectors)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.quiver(X, Y, Z, U, V, W, length=1, color='red')
ax.set_xlim([0, 1])
ax.set_ylim([0, 1])
ax.set_zlim([0, 1])
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? 


```
red = [1, 1]

blue = np.multiply(5, red)
green = np.multiply(np.pi, red)
yellow = np.multiply(-np.exp(1), red)

plt.arrow(0,0, red[0], red[1],head_width=.05, head_length=0.05, color ='red', width = 0.05)
plt.arrow(0,0, blue[0], blue[1],head_width=.05, head_length=0.05, color ='blue')
plt.arrow(0,0, green[0], green[1],head_width=.05, head_length=0.05, color ='green')
plt.arrow(0,0, yellow[0], yellow[1],head_width=.05, head_length=0.05, color ='yellow')

plt.xlim(-5,6)          
plt.ylim(-5,6)
plt.title("Scaled Vectors")
plt.show()

# The vectors are all in one line, just different lengths.
# The -e is in the same line but pointed in opposite direction
```

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

plt.arrow(0,0, a[0], a[1],head_width=.05, head_length=0.05, color ='red')
plt.arrow(0,0, b[0], b[1],head_width=.05, head_length=0.05, color ='blue')

plt.xlim(0,6)          
plt.ylim(0,8)
plt.title("Plot of a and b 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}$


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

a_minus_b = np.subtract(a, b)

plt.arrow(0,0, a[0], a[1],head_width=.05, head_length=0.05, color ='red')
plt.arrow(0,0, b[0], b[1],head_width=.05, head_length=0.05, color ='blue')
plt.arrow(0,0, a_minus_b[0], a_minus_b[1],head_width=.05, head_length=0.05, color ='green')

plt.xlim(0,6)          
plt.ylim(0,8)
plt.title("a Minus b in Green")
plt.show()

# The difference between a and b is intuitively represented by the green vector
# To get from the tip of blue (b) to the tip of red (a), you go over two and up 3
# This is exactly the green (a minus b) vector
```

## 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 = np.array([7, 22, 4, 16])
d = np.array([12, 6, 2, 9])

print('The dot product of c and d:')
np.dot(c, d)
```

    The dot product of c and d:





    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 = np.array([5, 7, 2])
f = np.array([3, 4, 6])

print('The cross product of e and f:')
np.cross(e, f)
```

    The cross product of e and f:





    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 = np.array([1, 1, 1, 8])
h = np.array([3, 3, 3, 3])

print('Norm of g:')
print(np.linalg.norm(g))
print('\n')
print('Norm of h:')
np.linalg.norm(h)

#The Norm of g is longer
```

    Norm of g:
    8.18535277187245
    
    
    Norm of h:





    6.0



## 1.9 Show that the following vectors are orthogonal (perpendicular to each other):

\begin{align}
\vec{i} = \begin{bmatrix} 1 \\ 0 \\ -1  \end{bmatrix}
\qquad
\vec{j} = \begin{bmatrix} 1 \\ \sqrt{2} \\ 1 \end{bmatrix}
\end{align}


```
i = np.array([1, 0, -1])
j = np.array([1, np.sqrt(2), 1])

np.dot(i, j)

# These two vectors are orthogonal because their dot product equals zero.
# This means that these two vectors are linearly independent
# No way to scale one to get the other -- on different planes
```




    0.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}


```
# Matrices Dimensions
# A = 3x2
# B = 1x3
# C = 2x3
# D = 3x3
# E = 2x2

# A*C, A*E
# B*D
# C*A, C*D
# D*A
# E*C

# There are 7 possible combinations of multiplication

```

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


```
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]])
```


```
# CD
np.matmul(C, D)
```




    array([[ 9,  6,  3],
           [ 4,  7, 11]])




```
#AE
np.matmul(A, E)
```




    array([[11, 17],
           [23, 37],
           [35, 57]])




```
#BA
np.matmul(B, A)
```




    array([[44, 56]])



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


```
F.T
```




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




```
# The main diagonal of F and F^t are the same
# The the other numbers in F^t are flipped around the main diagonal
# The dimension of F is 5x4, while the dimension of F is 4x5
```

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


```
# Identity matrix
I = np.array([[1, 0],
              [0, 1]])
```


```
# The product of any matrix with its Identity Matrix is just the original matrix
np.matmul(G, I)
```




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



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


```
# Determinant of H

np.linalg.det(H)
```




    43.000000000000014




```
# Determinant of J

np.linalg.det(J)
```




    -19.999999999999996



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


```
# Inverse of H

np.linalg.inv(H)
```




    array([[ 0.23255814, -0.25581395],
           [-0.1627907 ,  0.27906977]])




```
# Inverse of J

np.linalg.inv(J)
```




    array([[ 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 $G^{-1}G$. Is $HH^{-1} == G^{-1}G$? Why or Why not?


```
#HH^-1

np.matmul(H, np.linalg.inv(H))
```




    array([[1.00000000e+00, 5.55111512e-16],
           [2.22044605e-16, 1.00000000e+00]])




```
#G^-1G

np.matmul(np.linalg.inv(G), G)
```




    array([[1.00000000e+00, 6.66133815e-16],
           [1.11022302e-16, 1.00000000e+00]])




```
# The two above results are the same because multiplying any matrix by its
# inverse will give you an indentiy matrix (i.e. 1's on main diagonal and 0's everywhere else)
```

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




```
# Dot Product function

def dot_product(v1, v2):
  dotproduct = 0
  for i,j in zip(v1, v2):
    dotproduct += i * j
    
  return dotproduct
```


```
# Using same vectors above (c and d) to calculate dot product new function

dot_product(c, d)

# Same result as above with numpy
```




    368




```

```

eaaee515303c15d83877cd889dd9c74877adaed4
ipynb
Jupyter Notebook
Linear_Algebra_Assignment.ipynb
tesseract314/100-pandas-puzzles
7a4e7c64ddfc354d2703c1c41f423c604a271857
MIT
Linear_Algebra_Assignment.ipynb
tesseract314/100-pandas-puzzles
7a4e7c64ddfc354d2703c1c41f423c604a271857
MIT
Linear_Algebra_Assignment.ipynb
tesseract314/100-pandas-puzzles
7a4e7c64ddfc354d2703c1c41f423c604a271857
MIT
Qwen/Qwen-72B
1. YES
2. YES