# 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 math and graphing libraries
import matplotlib.pyplot as plt
import numpy as np
```


```
# Create a 2-d vector
two_d = [.1,.2]

# Plot 2-d vector:
fig,ax=plt.subplots()
ax.grid()
plt.xlim(-1,1)
plt.ylim(-1,1)
ax.vlines((0,1),1,-1)
ax.hlines((0,1),1,-1)
plt.arrow(0,0,
          two_d[0],
          two_d[1],
          head_width = .02,
          head_length = .02,
          linewidth = 3,
          color = 'blue',
          length_includes_head=True)

plt.show()
```

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


```
# import 3d plotting tool:
from mpl_toolkits.mplot3d import Axes3D

# Create 3-d vector
three_d = [.2,.3,.8]


vectors = np.array([[0,0,0,three_d[0],three_d[1],three_d[2]]])
zip(*vectors)
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='green',linewidth=4)
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? 


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

    2.718281828459045
    3.141592653589793



```
A = np.array(two_d)
B = np.array(two_d)*5
C = np.array(two_d)*e
D = np.array(two_d)*pi

fig,ax = plt.subplots()
ax.grid()
plt.xlim(-1,1)
plt.ylim(-1,1)

plt.arrow(-1, -1, A[0], A[1], head_width=.1, head_length=0.05, color='blue')
plt.arrow(-1, -1, B[0], B[1], head_width=.1, head_length=0.05, color='green')
plt.arrow(-1, -1, C[0], C[1], head_width=.1, head_length=0.05, color = 'red')
plt.arrow(-1, -1, D[0], D[1], head_width=.1, head_length=0.05, color = 'purple')
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}


```
# create vectors using numpy arrays:
vector_a, vector_b = np.array([5,7]),np.array([3,4])

# plot vectors a and b:
fig, ax = plt.subplots(figsize=(7,7))
ax.grid(color='green')
ax.set_axisbelow(True)
ax.minorticks_on()
ax.grid(which='minor',
        linestyle=':',
        linewidth='0.5',
        color='k')
ax.vlines(0,-5,10)
ax.hlines(0,-5,10)
plt.ylim(-5,10)
plt.xlim(-5,10,)
plt.arrow(0,0,
          vector_a[0],vector_a[1],
          length_includes_head=True,
          head_width=.3,head_length=.5, width=.1,
          color = 'blue')
plt.arrow(0,0,
          vector_b[0],vector_b[1],
          length_includes_head=True,
          head_width=.3,head_length=.5,width=.1,
          color='red')
vector_subtraction = vector_a - vector_b
vector_subtraction
plt.arrow(0,0,
          vector_subtraction[0],vector_subtraction[1],
          length_includes_head=True, head_width=.3,head_length=.5,
          width=.1,color='purple')
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}$


```
vector_subtraction = vector_a - vector_b
vector_subtraction
```




    array([2, 3])



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



```
vector_c = np.array([7,22,4,16])
vector_d = np.array([12,6,2,9])
vector_dot_product = np.dot(vector_c,vector_d)
vector_dot_product
```




    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}


```
vector_e = np.array([[5],[7],[2]])
vector_f = np.array([[3],[4],[6]])
vector_multiply = vector_e*vector_f
vector_multiply
```




    array([[15],
           [28],
           [12]])



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

norm_g = np.linalg.norm(g)
print(g,'\nNorm of g: ', norm_g)
norm_h = np.linalg.norm(h)
print(h,'\nNorm of h: ', norm_h)
print('\nNorm of g is longer.')
```

    [[1]
     [1]
     [1]
     [8]] 
    Norm of g:  8.18535277187245
    [[3]
     [3]
     [3]
     [3]] 
    Norm of h:  6.0
    
    Norm of g is longer.


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

**Dimensions of:**
- A = 3X2
- B = 1X3
- C = 2X3
- D = 3X3
- E = 2X2


```
def dimensions():
    print('Dimension of A: ',A.shape)
    print('Dimension of B: ',B.shape)
    print('Dimension of C: ',C.shape)
    print('Dimension of D: ',D.shape)
    print('Dimension of E: ',E.shape)     
dimensions()                 
```

    Dimension of A:  (3, 2)
    Dimension of B:  (1, 3)
    Dimension of C:  (2, 3)
    Dimension of D:  (3, 3)
    Dimension of E:  (2, 2)



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


```
def multiDim(matrix_A,matrix_B):
    combo_list = []
    for i in matrix_A:
        for b in matrix_B:
            if i.shape[1] == b.shape[0]:
                combo_list.append(np.matmul(i,b))
    return combo_list

matrix_A = [A,B,C,D,E]         
matrix_B = [A,B,C,D,E] 
combs = ['AxC','AxE','BxA','BxD','CxA','CxD','DxA','DxD','ExC','ExE'] 
for i in range(0,10):
    print(combs[i],' = \n',multiDim(matrix_A,matrix_B)[i],'\n')
```

    AxC  = 
     [[17 20 25]
     [43 46 53]
     [69 72 81]] 
    
    AxE  = 
     [[11 17]
     [23 37]
     [35 57]] 
    
    BxA  = 
     [[44 56]] 
    
    BxD  = 
     [[2 4 6]] 
    
    CxA  = 
     [[ 42  60]
     [ 80 102]] 
    
    CxD  = 
     [[ 9  6  3]
     [ 4  7 11]] 
    
    DxA  = 
     [[1 2]
     [3 4]
     [5 6]] 
    
    DxD  = 
     [[1 0 0]
     [0 1 0]
     [0 0 1]] 
    
    ExC  = 
     [[21 27 36]
     [73 79 92]] 
    
    ExE  = 
     [[16 24]
     [40 64]] 
    


## 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.dot(C,D)
AE = np.dot(A,E)
BA = np.dot(B,A)

print('Original Dimensions:')
dimensions()

print('CD dims: \n',CD.shape)
print('AE dims: \n',AE.shape)
print('BA dims: \n',BA.shape)
```

    Original Dimensions:
    Dimension of A:  (3, 2)
    Dimension of B:  (1, 3)
    Dimension of C:  (2, 3)
    Dimension of D:  (3, 3)
    Dimension of E:  (2, 2)
    CD dims: 
     (2, 3)
    AE dims: 
     (3, 2)
    BA dims: 
     (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('Dimensions of F: ',F.shape)
print('Dimensions of F-transpose: ',F.T.shape)
```

    Dimensions of F:  (5, 4)
    Dimensions of F-transpose:  (4, 5)


# Part 3 - Square Matrices

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

You don't have to do anything crazy complicated here to show your work, just create the G matrix as specified below, and a corresponding 2x2 Identity matrix and then multiply them together to show the result. You don't need to write LaTeX or anything like that (unless you want to).

\begin{align}
G= 
\begin{bmatrix}
13 & 14 \\
21 & 12 
\end{bmatrix}
\end{align}


```
G = np.array([[13,14],
              [21,12]])
I = np.array([[1,0],
              [0,1]])
np.matmul(I,G)
```




    array([[13, 14],
           [21, 12]])



## 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]])
print('Determinant of H: \n',np.linalg.det(H),'\n')
print('Determinant of J: \n',np.linalg.det(J),'\n')
```

    Determinant of H: 
     43.000000000000014 
    
    Determinant of J: 
     -19.999999999999996 
    


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


```
print('Inverse of H: \n',np.linalg.inv(H),'\n')
print('Inverse of J: \n',np.linalg.inv(J))
```

    Inverse of H: 
     [[ 0.23255814 -0.25581395]
     [-0.1627907   0.27906977]] 
    
    Inverse of 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? 

Please ignore Python rounding errors. If necessary, format your output so that it rounds to 5 significant digits (the fifth decimal place).

**Answer**
> They are not equal, because they are different dimensions.


```
print('H multiplied by its Inverse:\n',np.matmul(H,np.linalg.inv(H)),'\n')
print("J's Inverse multiplied by J:\n",np.matmul(np.linalg.inv(J),J))
```

    H multiplied by its Inverse:
     [[1.00000000e+00 5.55111512e-16]
     [2.22044605e-16 1.00000000e+00]] 
    
    J's Inverse multiplied by J:
     [[ 1.00000000e+00  2.22044605e-16  0.00000000e+00]
     [-1.11022302e-16  1.00000000e+00  0.00000000e+00]
     [-1.66533454e-16 -1.11022302e-16  1.00000000e+00]]


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


```
def calcDot(vec_a,vec_b):
    return np.dot(vec_a,vec_b)

r = np.random.randint(-20,20)
va = np.array([r,r])
vb = np.array([r,r])


calcDot(va,vb)    
```




    392




- Write a function that can calculate the norm of any vector


```
def calcNorm(vec):
    return np.linalg.norm(vec)

calcNorm(va)    
```




    19.79898987322333




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



bb7b90ce4198eb672043dacd1b4797a0c8e16c8e
ipynb
Jupyter Notebook
DataScience/DS-Unit-1-Sprint-3-Linear-Algebra/module1-vectors-and-matrices/LS_DS_131_Vectors_and_Matrices_Assignment.ipynb
dustin-py/Education
96ad77b9859278fdd9d3fc6a37f1568696af4dfa
MIT
2020-12-04T22:01:58.000Z
2021-05-28T18:13:44.000Z
DataScience/DS-Unit-1-Sprint-3-Linear-Algebra/module1-vectors-and-matrices/LS_DS_131_Vectors_and_Matrices_Assignment.ipynb
dustin-py/Education
96ad77b9859278fdd9d3fc6a37f1568696af4dfa
MIT
DataScience/DS-Unit-1-Sprint-3-Linear-Algebra/module1-vectors-and-matrices/LS_DS_131_Vectors_and_Matrices_Assignment.ipynb
dustin-py/Education
96ad77b9859278fdd9d3fc6a37f1568696af4dfa
MIT
Qwen/Qwen-72B
1. YES
2. YES