# Vectors and Vector Operations

### <mark style="color:blue;">1. What Is a Vector?</mark>

A <mark style="color:red;">**vector**</mark> is a mathematical quantity that has:

* **Magnitude** (length)
* **Direction**

#### Example:

Displacement of 5 km North is a vector, but just 5 km is a scalar.

### <mark style="color:blue;">2. Vector Representation</mark>

A vector in 2D space is written as:

$$
\overrightarrow{V\_{1}}=\begin{bmatrix}
v\_{1} \ v\_{2}

\end{bmatrix}
$$

```python
import numpy as np

v = np.array([3, 4])
print(v)
```

#### Output:

```python
[3 4]
```

### <mark style="color:blue;">3. Types of Vectors</mark>

* **Zero vector**: \[0,0]
* **Unit vector**: Magnitude is 1
* **Equal vectors**: Same magnitude and direction
* **Opposite vectors**: Same magnitude, opposite direction

### <mark style="color:blue;">4. Vector Operations</mark>

#### <mark style="color:orange;">4.1 Addition & Subtraction</mark>

#### **Mathematics**:

$$
\overrightarrow{A}+\overrightarrow{B}=\begin{bmatrix}
a\_{1}+b\_{1} \ a\_{2}+b\_{2}

\end{bmatrix}
$$

**Python**:

```python
A = np.array([2, 3])
B = np.array([4, 1])

add = A + B
sub = A - B

print("Addition:", add)
print("Subtraction:", sub)
```

#### Output:

```python
Addition: [6 4]
Subtraction: [-2  2]
```

#### <mark style="color:orange;">4.2 Scalar Multiplication</mark>

#### **Mathematics**:

$$
k.\overrightarrow{A}=\begin{bmatrix}
k.a\_{1} \ k.a\_{2}

\end{bmatrix}
$$

**Python**:

```python
v = np.array([2, -1])
k = 3
scaled = k * v

print("Scaled vector:", scaled)
```

#### Output:

```python
Scaled vector: [ 6 -3]
```

#### <mark style="color:orange;">4.3 Dot Product</mark>

#### **Mathematics**:

$$
\overrightarrow{A}.\overrightarrow{B}=a\_{1}.b\_{1}+a\_{2}.b\_{2}
$$

Result is a scalar.

**Python**:

```python
a = np.array([1, 2])
b = np.array([3, 4])

dot = np.dot(a, b)
print("Dot product:", dot)
```

#### Output:

```python
Dot product: 11
```

Use to find <mark style="color:red;">**angle**</mark> or test <mark style="color:red;">**orthogonality**</mark>.

#### <mark style="color:orange;">4.4 Cross Product (Only in 3D)</mark>

#### **Mathematics**:

$$
\overrightarrow{A}\times \overrightarrow{B}=\begin{bmatrix}
a\_{2}b\_{3}-a\_{3}b\_{2} \ a\_{3}b\_{1}-a\_{1}b\_{3}
\ a\_{1}b\_{2}-a\_{2}b\_{1}

\end{bmatrix}
$$

**Python**:

```python
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

cross = np.cross(a, b)
print("Cross product:", cross)
```

#### Output:

```python
Cross product: [-3  6 -3]
```

#### <mark style="color:orange;">4.5 Magnitude or Norm</mark>

#### **Mathematics**:

$$
\left| \overrightarrow{A} \right|=\sqrt{{a\_{1}^{2}}+{a\_{2}^{2}}}
$$

**Python**:

```python
v = np.array([3, 4])
magnitude = np.linalg.norm(v)

print("Magnitude:", magnitude)
```

#### Output:

```python
Magnitude: 5.0
```

#### <mark style="color:orange;">4.6 Unit Vector</mark>

#### **Mathematics**:

$$
\hat{A}=\frac{\overrightarrow{A}}{\left| \overrightarrow{A} \right|}
$$

**Python**:

```python
v = np.array([3, 4])
unit_v = v / np.linalg.norm(v)

print("Unit vector:", unit_v)
```

#### Output:

```python
Unit vector: [0.6 0.8]
```

#### <mark style="color:orange;">4.7 Angle Between Vectors</mark>

#### **Mathematics**:

$$
\theta=cos^{-1}(\frac{\overrightarrow{A}.\overrightarrow{B}}{|\overrightarrow{A}||\overrightarrow{B}|})
$$

**Python**:

```python
from numpy import dot, arccos
from numpy.linalg import norm
from numpy import rad2deg

a = np.array([1, 2])
b = np.array([2, 3])

cos_theta = dot(a, b) / (norm(a) * norm(b))
angle_rad = arccos(cos_theta)
angle_deg = rad2deg(angle_rad)

print("Angle (degrees):", angle_deg)
```

#### Output:

```python
Angle (degrees): 7.125016348901757
```

### <mark style="color:blue;">5. Vector Visualization in 2D & 3D</mark>

#### <mark style="color:orange;">5.1 Plotting a Vector in 2D</mark>

Let’s plot a vector from the origin to point (x, y).

#### **Python Code**:

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

# Define vector
v = np.array([4, 3])
origin = np.array([0, 0])  # Starting point (0,0)

# Plot
plt.figure(figsize=(6, 6))
plt.quiver(*origin, v[0], v[1], angles='xy', scale_units='xy', scale=1, color='blue')
plt.xlim(-1, 6)
plt.ylim(-1, 6)
plt.grid()
plt.gca().set_aspect('equal')
plt.title("2D Vector Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
```

`plt.quiver()` draws an arrow from the origin.

#### Output:

<figure><img src="https://3561236956-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5YU9oQgnizEAfDKK4w2y%2Fuploads%2F7eqhpMMB580JDm8HbY3B%2F00014.png?alt=media&#x26;token=0459cf46-ab8a-4b8f-8744-bfcc56b6990e" alt=""><figcaption></figcaption></figure>

<mark style="color:orange;">**5.2 Plotting Multiple Vectors in 2D**</mark>

```python
# Two vectors
a = np.array([2, 3])
b = np.array([4, 1])

# Plot
plt.figure(figsize=(6, 6))
plt.quiver(0, 0, a[0], a[1], color='r', angles='xy', scale_units='xy', scale=1, label='Vector a')
plt.quiver(0, 0, b[0], b[1], color='g', angles='xy', scale_units='xy', scale=1, label='Vector b')
plt.xlim(-1, 6)
plt.ylim(-1, 6)
plt.grid()
plt.gca().set_aspect('equal')
plt.title("Multiple Vectors in 2D")
plt.legend()
plt.show()
```

#### Output:

<figure><img src="https://3561236956-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5YU9oQgnizEAfDKK4w2y%2Fuploads%2FvdvZ0Dp862pDWepVteMC%2F00015.png?alt=media&#x26;token=3ee44804-99ed-4e60-9cad-5fa689023e6a" alt=""><figcaption></figcaption></figure>

#### <mark style="color:orange;">5.3 Plotting Vectors in 3D</mark>

```python
from mpl_toolkits.mplot3d import Axes3D

# 3D Vector
v = np.array([2, 3, 4])

# Plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.quiver(0, 0, 0, v[0], v[1], v[2], color='purple')

# Axes limits
ax.set_xlim([0, 5])
ax.set_ylim([0, 5])
ax.set_zlim([0, 5])
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')
ax.set_title("3D Vector Plot")
plt.show()
```

### <mark style="color:blue;">6. Plot Vector Addition Graphically</mark>

```python
a = np.array([2, 1])
b = np.array([1, 3])
c = a + b  # Resultant

plt.figure(figsize=(6, 6))
plt.quiver(0, 0, a[0], a[1], color='r', scale=1, angles='xy', scale_units='xy', label='a')
plt.quiver(a[0], a[1], b[0], b[1], color='g', scale=1, angles='xy', scale_units='xy', label='b')
plt.quiver(0, 0, c[0], c[1], color='b', scale=1, angles='xy', scale_units='xy', label='a + b')

plt.xlim(-1, 5)
plt.ylim(-1, 5)
plt.grid()
plt.gca().set_aspect('equal')
plt.legend()
plt.title("Vector Addition (Head-to-Tail Method)")
plt.show()
```

#### Output:

<figure><img src="https://3561236956-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5YU9oQgnizEAfDKK4w2y%2Fuploads%2FNVzYblB3MVQCvqJtA94p%2F00016.png?alt=media&#x26;token=83d03881-30a1-4e87-9254-ee67398b5e04" alt=""><figcaption></figcaption></figure>
