

# Vector algebra basics: definitions and notation

A vector is represented by an ordered pair of numbers, usually denoted as $\mathbf{a} = (a_x, a_y)$. The magnitude of a vector, denoted as $||\mathbf{a}||$, is the length of the vector. The direction of a vector is determined by its angle with respect to the positive $x$-axis.

To perform vector addition, we can simply add the corresponding components of the vectors. For example, if $\mathbf{a} = (a_x, a_y)$ and $\mathbf{b} = (b_x, b_y)$, then $\mathbf{a} + \mathbf{b} = (a_x + b_x, a_y + b_y)$.

To perform vector subtraction, we subtract the corresponding components of the vectors. For example, if $\mathbf{a} = (a_x, a_y)$ and $\mathbf{b} = (b_x, b_y)$, then $\mathbf{a} - \mathbf{b} = (a_x - b_x, a_y - b_y)$.

## Exercise
Solve the following vector addition and subtraction problems:

1. $\mathbf{a} = (3, 4)$ and $\mathbf{b} = (1, 2)$. Calculate $\mathbf{a} + \mathbf{b}$ and $\mathbf{a} - \mathbf{b}$.
2. $\mathbf{c} = (5, -3)$ and $\mathbf{d} = (-2, 1)$. Calculate $\mathbf{c} + \mathbf{d}$ and $\mathbf{c} - \mathbf{d}$.

# Vector addition and subtraction

Scalar multiplication is performed by multiplying each component of the vector by the scalar. For example, if $\mathbf{a} = (a_x, a_y)$ and $k$ is a scalar, then $k\mathbf{a} = (ka_x, ka_y)$.

To find the scalar projection of a vector $\mathbf{a}$ onto another vector $\mathbf{b}$, we first find the unit vector in the direction of $\mathbf{b}$ and then multiply the magnitude of $\mathbf{a}$ by the dot product of $\mathbf{a}$ and the unit vector.

## Exercise
Solve the following scalar multiplication and projection problems:

1. Calculate $2\mathbf{a}$ and $-3\mathbf{b}$, where $\mathbf{a} = (2, 3)$ and $\mathbf{b} = (1, -2)$.
2. Find the scalar projection of $\mathbf{a} = (3, 4)$ onto $\mathbf{b} = (2, 1)$.

# Scalar projection of vectors

The cross product of two vectors $\mathbf{a}$ and $\mathbf{b}$ is a vector that is perpendicular to both $\mathbf{a}$ and $\mathbf{b}$. It is denoted as $\mathbf{a} \times \mathbf{b}$.

To find the cross product of two vectors, we can use the formula:

$$\mathbf{a} \times \mathbf{b} = (a_yb_z - a_zb_y, a_zb_x - a_xb_z, a_xb_y - a_yb_x)$$

## Exercise
Solve the following cross product problems:

1. Find the cross product of $\mathbf{a} = (2, 3, 4)$ and $\mathbf{b} = (1, -2, 3)$.
2. Find the cross product of $\mathbf{c} = (3, 4, 5)$ and $\mathbf{d} = (-2, 1, 4)$.

# Vector multiplication and cross product

The dot product of two vectors $\mathbf{a}$ and $\mathbf{b}$ is a scalar that is equal to the product of the magnitudes of the vectors and the cosine of the angle between them. It is denoted as $\mathbf{a} \cdot \mathbf{b}$.

To find the dot product of two vectors, we can use the formula:

$$\mathbf{a} \cdot \mathbf{b} = a_xb_x + a_yb_y + a_zb_z$$

## Exercise
Solve the following dot product problems:

1. Find the dot product of $\mathbf{a} = (2, 3, 4)$ and $\mathbf{b} = (1, -2, 3)$.
2. Find the dot product of $\mathbf{c} = (3, 4, 5)$ and $\mathbf{d} = (-2, 1, 4)$.

# Introduction to NumPy and its use in vector calculations

To use NumPy, you need to install it using pip:

```
pip install numpy
```

Then, you can import it into your Python script:

```python
import numpy as np
```

NumPy arrays are used to store and manipulate vectors. For example, you can create a NumPy array with the components of a vector:

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

## Exercise
Create NumPy arrays for the vectors $\mathbf{a} = (2, 3, 4)$ and $\mathbf{b} = (1, -2, 3)$. Perform the following operations using NumPy functions:

1. Calculate the magnitude of $\mathbf{a}$.
2. Calculate the dot product of $\mathbf{a}$ and $\mathbf{b}$.
3. Calculate the cross product of $\mathbf{a}$ and $\mathbf{b}$.

# NumPy arrays: creation and manipulation

To reshape a NumPy array, you can use the `reshape()` function. For example, to reshape a 1D array into a 2D array, you can use:

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

Element-wise operations are performed on corresponding elements of two arrays. For example, you can add two arrays element-wise using the `+` operator:

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

## Exercise
Create NumPy arrays for the vectors $\mathbf{a} = (2, 3, 4)$ and $\mathbf{b} = (1, -2, 3)$. Perform the following operations using NumPy functions:

1. Reshape $\mathbf{a}$ into a 2D array with shape (2, 2).
2. Perform element-wise addition of $\mathbf{a}$ and $\mathbf{b}$.
3. Perform element-wise subtraction of $\mathbf{a}$ and $\mathbf{b}$.

# Common NumPy functions for vector operations

Broadcasting is a mechanism that allows NumPy to perform operations on arrays of different shapes. It involves "stretching" the smaller array to match the larger array's shape. For example, if we have two arrays `a` and `b`, and `a` has shape (2, 1) and `b` has shape (2, 2), then NumPy can perform element-wise addition using broadcasting:

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

To perform broadcasting, you can use the `numpy.broadcast_to()` function. For example, to broadcast the shape of `a` to match the shape of `b`, you can use:

```python
a_broadcasted = np.broadcast_to(a, b.shape)
```

## Exercise
Create NumPy arrays for the vectors $\mathbf{a} = (1, 2, 3, 4)$ and $\mathbf{b} = (5, 6, 7, 8)$. Perform the following operations using NumPy functions:

1. Reshape $\mathbf{a}$ into a 2D array with shape (2, 2).
2. Perform element-wise addition of $\mathbf{a}$ and $\mathbf{b}$ using broadcasting.
3. Perform element-wise subtraction of $\mathbf{a}$ and $\mathbf{b}$ using broadcasting.

# Applications of vector algebra in physics: kinematics, momentum, and angular momentum

Kinematics is the study of motion. It involves the study of position, velocity, acceleration, and displacement. In this context, vectors are used to represent the position, velocity, and acceleration of objects.

Momentum is a vector quantity that is defined as the product of the mass and velocity of an object. It is denoted as $\mathbf{p}$. In physics, momentum is used to describe the transfer of energy and momentum between objects.

Angular momentum is a vector quantity that is defined as the cross product of the position vector and the linear momentum of an object. It is denoted as $\mathbf{L}$. In physics, angular momentum is used to describe the rotation of objects around a fixed point.

## Exercise
Calculate the following quantities using vector algebra:

1. The position vector of an object at time $t = 1$ and $t = 2$.
2. The velocity vector of an object at time $t = 1$.
3. The acceleration vector of an object at time $t = 1$.
4. The momentum of an object with mass $m = 2$ and velocity $\mathbf{v} = (3, 4)$.
5. The angular momentum of an object with mass $m = 2$ and velocity $\mathbf{v} = (3, 4)$ and position $\mathbf{r} = (1, 2)$.

# Applications of NumPy in physics simulations

For example, you can use NumPy to simulate the motion of an object under the influence of gravity. You can create arrays for the initial position, velocity, and acceleration of the object, and then use NumPy functions to update the position and velocity at each time step.

## Exercise
Simulate the motion of an object under the influence of gravity. The object starts at position $\mathbf{r} = (0, 0)$, with an initial velocity of $\mathbf{v} = (0, 10)$ and an acceleration due to gravity of $\mathbf{g} = (0, -9.8)$. Calculate the position of the object at time $t = 10$ and $t = 20$.

# Example problems and solutions

Problem 1:
A car is moving with a constant velocity of $\mathbf{v} = (5, 3)$. Calculate the position of the car at time $t = 10$.

Solution:
The position of the car at time $t = 10$ can be calculated using the formula:

$$\mathbf{r} = \mathbf{v}t$$

So, the position of the car at time $t = 10$ is $\mathbf{r} = (50, 30)$.

Problem 2:
A projectile is launched with an initial velocity of $\mathbf{v} = (10, 20)$ and an angle of $30$ degrees with respect to the horizontal. Calculate the maximum height of the projectile.

Solution:
To find the maximum height, we can use the formula:

$$h = \frac{v^2\sin^2(\theta)}{2g}$$

Where $v$ is the magnitude of the initial velocity, $\theta$ is the angle with respect to the horizontal, and $g$ is the acceleration due to gravity.

So, the maximum height of the projectile is:

$$h = \frac{(10^2 + 20^2)\sin^2(30)}{2(9.8)} \approx 11.8$$

## Exercise
Solve the following problems using vector algebra and NumPy:

1. A car is moving with a constant velocity of $\mathbf{v} = (5, 3)$. Calculate the position of the car at time $t = 10$.
2. A projectile is launched with an initial velocity of $\mathbf{v} = (10, 20)$ and an angle of $30$ degrees with respect to the horizontal. Calculate the maximum height of the projectile.