<a href="https://colab.research.google.com/github/machinelearning147/100DaysOfMLCode/blob/master/Day1/Day1.ipynb" target="_parent"></a>

**Basics**



```
# import required packages

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
```

\begin{equation}
u = 
\begin{pmatrix} x \\y\end{pmatrix}
\end{equation}


```
# numpy array [a vector]
u = np.array([1,2])
```


```
# visualizing the vector on 2-dimentional co-ordinate system
x_coords, y_coords = u
plt.scatter(x_coords, y_coords, color=["r"])
plt.axis([0, 9, 0, 6])
plt.grid('on')
plt.show()
```


```
# function to plot vector
def plot_vector2d(vector2d, origin=[0, 0], **options):
    """
    plot a vector as arrow from origin to the given vector2d
    Args: vector2d
    returns: arrow plot
    """
    return plt.arrow(origin[0], origin[1], vector2d[0], vector2d[1],
              head_width=0.2, head_length=0.3, length_includes_head=True,
              **options)
```


```
plot_vector2d(u, color="b")
plt.scatter(x_coords, y_coords, color=["r"])

plt.axis([0, 9, 0, 6])
plt.grid('on')
plt.show()
```


```
u = np.array([0,2])
v = np.array([5,0])

plot_vector2d(u, origin=[0,0], color="b")
plot_vector2d(v, origin=[0,0], color="r")

x1,x2,y1,y2 = -5, 10, -5, 5
plt.axis([x1,x2,y1,y2])
plt.grid('on')
plt.show()
```

**Rotation of 2-D vectors**

\begin{equation}Rx = x{}^{'}\end{equation}
\begin{equation}
\begin{pmatrix} cos\theta & -sin\theta \\ sin\theta & cos\theta\end{pmatrix} 
\begin{pmatrix} x\\ y \end{pmatrix}
=
\begin{pmatrix} x{}^{'}\\ y{}^{'} \end{pmatrix} 
\end{equation}



```
def rotate(th):
    "2-D rotational matrix"
    R = np.array([[np.cos(th), -1*np.sin(th)], [np.sin(th), np.cos(th)]])
    return R
```

**Applying roatation on the vectors**


```
th = np.pi/4
print(f'Ratation angle: \n {np.degrees(th)}')
R = rotate(th)
print(f'Rotational matrix:\n {np.round(R)}')
uR = (np.dot(u,R))
vR = (np.dot(v,R))
print(f'Rotated u: \n {uR}')
print(f'Rotated u: \n {vR}')
```

    Ratation angle: 
     45.0
    Rotational matrix:
     [[ 1. -1.]
     [ 1.  1.]]
    Rotated u: 
     [1.41421356 1.41421356]
    Rotated u: 
     [ 3.53553391 -3.53553391]


**Plotting the actual and roatated vectors**


```

plot_vector2d(u, origin=[1,1], color="b", alpha=0.3)
plot_vector2d(v, origin=[0,0], color="r", alpha=0.3)

plot_vector2d(uR, origin=[1,1], color="b", alpha=1)
plot_vector2d(vR, origin=[0,0], color="r", alpha=1)

x1,x2,y1,y2 = -5, 10, -5, 5
plt.axis([x1,x2,y1,y2])
plt.grid('on')
plt.show()
```

**Vector Addition**




```
plot_vector2d(u, color="r")
plot_vector2d(v, color="b")
plot_vector2d(v, origin=u, color="b", linestyle="dotted")
plot_vector2d(u, origin=v, color="r", linestyle="dotted")
plot_vector2d(u+v, color="g")
plt.axis([-5, 10, -5, 5])

plt.grid('on')
plt.show()
```


```

```

6ac3b26c344029a8b55036979834fb1557fc1d0f
ipynb
Jupyter Notebook
Day1/Day1.ipynb
machinelearning147/100DaysOfMLCode
016746b7dd534c0ab87d1218ca8b5fb109a243f6
MIT
2021-07-14T15:05:45.000Z
2021-07-14T15:05:45.000Z
Day1/Day1.ipynb
machinelearning147/100DaysOfMLCode
016746b7dd534c0ab87d1218ca8b5fb109a243f6
MIT
Day1/Day1.ipynb
machinelearning147/100DaysOfMLCode
016746b7dd534c0ab87d1218ca8b5fb109a243f6
MIT
Qwen/Qwen-72B
1. YES
2. YES