# 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


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


```python
vector = np.array([1,6])
plt.arrow(0, 0, *vector, head_width=.1, head_length=0.1)
plt.xlim(0,max(vector)+1)       
plt.ylim(0,max(vector)+1);
```

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


```python
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

vector = np.array([3, 2, 3])

ax.quiver(*(0,0,0), *vector)
ax.set_xlim([0, max(vector) + 1])
ax.set_ylim([0, max(vector) + 1])
ax.set_zlim([0, max(vector) + 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? 


```python
vector = np.array([1,6])
quiver = np.array([vector, vector*5, vector*np.pi, vector*-np.exp(1)])

for v in quiver:
    plt.arrow(0, 0, *v, head_width=1, head_length=1)
    
plt.xlim(quiver.min()-1,quiver.max()+1)       
plt.ylim(quiver.min()-1,quiver.max()+1);
```

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


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

for v in quiver:
    plt.arrow(0, 0, *v, head_width=0.5, head_length=0.5)
    
plt.xlim(0,quiver.max()+1)       
plt.ylim(0,quiver.max()+1);
```

## 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}$


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

for v in quiver:
    plt.arrow(0, 0, *v, head_width=0.5, head_length=0.5)
    
plt.xlim(0,quiver.max()+1)       
plt.ylim(0,quiver.max()+1);
```

$\vec{a-b}$ is the length and direction connecting the tips of $\vec{a}$ and $\vec{b}$

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



```python
np.dot(np.array([ 7, 22,  4, 16]),
       np.array([12,  6,  2,  9]))
```




    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}


```python
np.cross(np.array([5, 7, 2]),
         np.array([3, 4, 6]))
```




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



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

\begin{align}
\vec{e} = \begin{bmatrix} 1 \\ 1 \\ 1 \\ 8 \end{bmatrix}
\qquad
\vec{f} = \begin{bmatrix} 3 \\3 \\ 3 \\ 3 \end{bmatrix}
\end{align}


```python
(np.linalg.norm(np.array([1, 1, 1, 8])), 
 np.linalg.norm(np.array([3, 3, 3, 3])))
```




    (8.18535277187245, 6.0)



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

\begin{align}
\vec{g} = \begin{bmatrix} 1 \\ 0 \\ -1  \end{bmatrix}
\qquad
\vec{h} = \begin{bmatrix} 1 \\ \sqrt{2} \\ 1 \end{bmatrix}
\end{align}


```python
np.dot(np.array([1,        0, -1]),
       np.array([1, 2**(1/2),  1]))
```




    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}


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

quiver = [A, B, C, D, E]

# You can multipy any two matrices where the number of columns of the first matrix 
# is equal to the number of rows of the second matrix.

for M1 in quiver:
    for M2 in quiver:
        if M1 is M2:
            continue
        if M1.shape[1] == M2.shape[0]:
            print('Can multiply:', M1, M2, sep='\n')
            print('\n')
        if M1.shape[0] == M2.shape[1]:
            print('Can multiply:', M2, M1, sep='\n')
            print('\n')
```

    Can multiply:
    [[2 4 6]]
    [[1 2]
     [3 4]
     [5 6]]
    
    
    Can multiply:
    [[1 2]
     [3 4]
     [5 6]]
    [[ 9  6  3]
     [ 4  7 11]]
    
    
    Can multiply:
    [[ 9  6  3]
     [ 4  7 11]]
    [[1 2]
     [3 4]
     [5 6]]
    
    
    Can multiply:
    [[1 0 0]
     [0 1 0]
     [0 0 1]]
    [[1 2]
     [3 4]
     [5 6]]
    
    
    Can multiply:
    [[1 2]
     [3 4]
     [5 6]]
    [[1 3]
     [5 7]]
    
    
    Can multiply:
    [[2 4 6]]
    [[1 2]
     [3 4]
     [5 6]]
    
    
    Can multiply:
    [[2 4 6]]
    [[1 0 0]
     [0 1 0]
     [0 0 1]]
    
    
    Can multiply:
    [[ 9  6  3]
     [ 4  7 11]]
    [[1 2]
     [3 4]
     [5 6]]
    
    
    Can multiply:
    [[1 2]
     [3 4]
     [5 6]]
    [[ 9  6  3]
     [ 4  7 11]]
    
    
    Can multiply:
    [[ 9  6  3]
     [ 4  7 11]]
    [[1 0 0]
     [0 1 0]
     [0 0 1]]
    
    
    Can multiply:
    [[1 3]
     [5 7]]
    [[ 9  6  3]
     [ 4  7 11]]
    
    
    Can multiply:
    [[1 0 0]
     [0 1 0]
     [0 0 1]]
    [[1 2]
     [3 4]
     [5 6]]
    
    
    Can multiply:
    [[2 4 6]]
    [[1 0 0]
     [0 1 0]
     [0 0 1]]
    
    
    Can multiply:
    [[ 9  6  3]
     [ 4  7 11]]
    [[1 0 0]
     [0 1 0]
     [0 0 1]]
    
    
    Can multiply:
    [[1 2]
     [3 4]
     [5 6]]
    [[1 3]
     [5 7]]
    
    
    Can multiply:
    [[1 3]
     [5 7]]
    [[ 9  6  3]
     [ 4  7 11]]
    
    


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


```python
# the result of multiplying two matrices will have the shape of 
# the first matrix's rows and the second matrix's columns

CD = np.matmul(C, D)
print(CD)

AE = np.matmul(A, E)
print(AE)

BA = np.matmul(B, A)
print(BA)
```

    [[ 9  6  3]
     [ 4  7 11]]
    [[11 17]
     [23 37]
     [35 57]]
    [[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}


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

print(np.transpose(F))
```

    [[20 19 18 17]
     [16 15 14 13]
     [12 11 10  9]
     [ 8  7  6  5]
     [ 4  3  2  1]]
    [[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}

The product of any square matrix with its corresponding identity matrix is the same martrix. 

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



```python
H = np.array([[12, 11],
              [ 7, 10]])
np.linalg.det(H)
```




    43.0




```python
J = np.array([[ 0, 1, 2],
              [ 7,10, 4],
              [ 3, 2, 0]])
np.linalg.det(J)
```




    -19.999999999999996



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


```python
np.linalg.inv(H)
```




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




```python
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 $J^{-1}J$. Is $HH^{-1} == J^{-1}J$? Why or Why not?


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




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




```python
np.matmul(np.linalg.inv(J), J)
```




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



No, they are not equal. They don't have the same dimensions.

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



0bc7053001268ae1b464a090b3a191edda12bde6
ipynb
Jupyter Notebook
05-Linear-Algebra/01_Linear_Algebra_Assignment.ipynb
shalevy1/data-science-journal
2a6beaf5bf328e257b638a695983457a9f3cd7ce
MIT
2019-03-05T04:44:48.000Z
2022-03-24T09:47:48.000Z
05-Linear-Algebra/01_Linear_Algebra_Assignment.ipynb
pesobreiro/data-science-journal
82a72b4ed5ce380988fac17b0acd97254c2b5c86
MIT
05-Linear-Algebra/01_Linear_Algebra_Assignment.ipynb
pesobreiro/data-science-journal
82a72b4ed5ce380988fac17b0acd97254c2b5c86
MIT
2019-03-07T05:08:03.000Z
2022-01-05T11:32:51.000Z
Qwen/Qwen-72B
1. YES
2. YES