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


```
# importing the libraries used in class

import matplotlib.pyplot as plt
import math
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
```

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


```
# Done in class
# plt.arrow(x,y,dx,dy) draws an arrow from (x,y) to (dx,dy) 
# xlim and ylim is range of the borders of the graph, title sets the title

twod_vector = [0.5,0.5]
plt.arrow(0,0,twod_vector[0],twod_vector[1],head_width=0.02,head_length=0.02,color="green")
plt.xlim(0,1)
plt.ylim(0,1)
plt.title("Two Dimensional Vector")
plt.show()
```

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


```
# Done in class

vector_3d = [0.6, 0.8, 0.3]
vector_3d_v = np.array([[0,0,0,0.6,0.8,0.3]])

# From what I understand, quiver creates a vector from point (X,Y,Z) to (U,V,W) so in this case the shown vector is from (0,0,0) to (0.6,0.8,0.3)
# The line below makes X, Y, Z, U, V, W into 1 element tuples with the corresponding value in the array, don't know why "X, Y, Z, U, V, W = 0,0,0,0.6,0.8,0.3" wasn't used instead
X, Y, Z, U, V, W = zip((*vector_3d_v))
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.quiver(X,Y,Z,U,V,W,length=1)
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()
```


```
# First 3 lines of the above code block is replaced by the 1st line in this code block, same output
X, Y, Z, U, V, W = 0,0,0,0.6,0.8,0.3
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.quiver(X, Y, Z, U, V, W,length=1)
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? 


```
# I use the math library to get e and pi.
# I need to plot them from biggest to smallest so the smallest ones don't get covered.
# These vectors are all in the same direction. Each of these vectors can be multiplied by a factor to be equal to any of these vectors.

plt.arrow(0,0,5*twod_vector[0],5*twod_vector[1],head_width=0.02,head_length=0.02,color="green")
plt.arrow(0,0,math.pi*twod_vector[0],math.pi*twod_vector[1],head_width=0.02,head_length=0.02,color="red")
plt.arrow(0,0,twod_vector[0],twod_vector[1],head_width=0.02,head_length=0.02,color="blue")
plt.arrow(0,0,-math.e*twod_vector[0],-math.e*twod_vector[1],head_width=0.02,head_length=0.02,color="yellow")
plt.xlim(-3,5)
plt.ylim(-3,5)
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}


```
# Similar to 1.1 except the vectors are given

a = [5,7]
b = [3,4]
plt.arrow(0,0,a[0],a[1],head_width=0.02,head_length=0.1,color="red",width=0.05)
plt.arrow(0,0,b[0],b[1],head_width=0.02,head_length=0.1,color="blue",width=0.05)
plt.xlim(0,8)
plt.ylim(0,8)
```

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


```
# The relationship between a,b, and a-b is that a-b+b = a. a-b = [2,3], if I add b to it, I'll get: a-b+b = [2+3,3+4] = [5,7] which is a
# Also, (a-b)+b = b+(a-b), they all end up at the same place [5,7]
plt.arrow(0,0,a[0],a[1],head_width=0.02,head_length=0.1,color="red",width=0.05)
plt.arrow(0,0,b[0],b[1],head_width=0.02,head_length=0.1,color="blue",width=0.05)
plt.arrow(0,0,a[0]-b[0],a[1]-b[1],head_width=0.02,head_length=0.1,color="purple",width=0.05)
plt.xlim(0,8)
plt.ylim(0,8)
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}



```
# dot product is the sum of the products of the corresponding entries 

def dot(x,y): # x and y are lists with the same length, representing vectors
  Dot = 0
  for n in range(len(x)):
    Dot+=x[n]*y[n]
  return Dot
```


```
dot([7,22,4,16],[12,6,2,9])
```




    368




```
#np.dot returns the dot product of 2 arrays

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

np.dot(c,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}


```
# Cross Product
# if p = [a,b,c] and q = [x,y,z] p cross q would be [bz-cy,-(az-cx),ay-bx]
# these components are the determinants of the 2x2 matrix that excludes a column, for example: bz - cy is the determinant of the matrix below, it excludes the 1st column (a z)
# b c
# y z
# so if e = [5,7,2] and f = [3,4,6], e cross f would be [(7)(6)-(2)(4),-((5)(6)-(3)(2)), (5)(4)-(3)(7)] = [42 - 8, -(30 - 6), 20-21] = [34,-24,1]
```


```
def cross(x,y): # x and y are vectors with 3 dimensions (lists with 3 numbers) in this instance
  a = x[1]*y[2] - x[2]*y[1]
  b = -(x[0]*y[2] - y[0]*x[2])
  c = x[0]*y[1] - x[1]*y[0]
  return [a,b,c]
```


```
cross([5,7,2],[3,4,6])
```




    [34, -24, -1]




```
# np.cross returns the cross product of 2 vectors

e = np.array([5,7,2])
f = np.array([3,4,6])

np.cross(e,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}


```
# ||x|| is the sqrt of the sum of squares of the values
# ||g|| = sqrt(1^2 + 1^2 + 1^2 + 8^2) = sqrt(1+1+1+64) = sqrt(67)
```


```
def mag(vec): #vec is vector which is a list
  leng = 0
  for n in vec:
    leng+=n**2
  return leng**0.5
```


```
print("||g|| is", mag([1,1,1,8])) #sqrt(67)
print("||h|| is", mag([3,3,3,3])) #sqrt(36) 

```

    ||g|| is 8.18535277187245
    ||h|| is 6.0



```
# np.linalg.norm can also be used to figure out ||x||

g = np.array([1,1,1,8]) 
h = np.array([3,3,3,3]) 

print("||g|| is", np.linalg.norm(g))
print("||h|| is", np.linalg.norm(h))
```

    ||g|| is 8.18535277187245
    ||h|| is 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}


```
# to multiply matrices, with dimensions rows by columns, the columns of 1st matrix = rows of 2nd matrix
# and the product has rows of 1st matrix by columns of 2nd matrix
# m*n matrix times n*p matrix equals m*p matrix

# Purpose of the code below is to answer the question "Which of the following [matrices] can be multiplied together?"
# Value is set to dimensions of corresponding Key matrix
# Using dictionary so I can print out the letters.

Dimdict = {"A":[3,2],
           "B":[1,3],
           "C":[2,3],
           "D":[3,3],
           "E":[2,2]}


# testings all 25 combinations, x[0] is rows, x[1] is columns, 

for m in Dimdict:
  for n in Dimdict:
    if Dimdict[m][1]==Dimdict[n][0]:
      print(f'{m}{n} is a legal combination. The dimensions are {Dimdict[m]} * {Dimdict[n]}.')


```

    AC is a legal combination. The dimensions are [3, 2] * [2, 3].
    AE is a legal combination. The dimensions are [3, 2] * [2, 2].
    BA is a legal combination. The dimensions are [1, 3] * [3, 2].
    BD is a legal combination. The dimensions are [1, 3] * [3, 3].
    CA is a legal combination. The dimensions are [2, 3] * [3, 2].
    CD is a legal combination. The dimensions are [2, 3] * [3, 3].
    DA is a legal combination. The dimensions are [3, 3] * [3, 2].
    DD is a legal combination. The dimensions are [3, 3] * [3, 3].
    EC is a legal combination. The dimensions are [2, 2] * [2, 3].
    EE is a legal combination. The dimensions are [2, 2] * [2, 2].


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


```

```

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

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

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


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

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

# Go Beyond: 

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?



65845051b4d175a32e81287f7be242c673ef1acd
ipynb
Jupyter Notebook
Copy_of_Vectors_and_Matricest.ipynb
dealom/Vectors-and-Matrices
6edeb8b9d3e5852d2b63f08d17ac98ad1d731f33
MIT
Copy_of_Vectors_and_Matricest.ipynb
dealom/Vectors-and-Matrices
6edeb8b9d3e5852d2b63f08d17ac98ad1d731f33
MIT
Copy_of_Vectors_and_Matricest.ipynb
dealom/Vectors-and-Matrices
6edeb8b9d3e5852d2b63f08d17ac98ad1d731f33
MIT
Qwen/Qwen-72B
1. YES
2. YES