Vectors¶
In the first chapter, the idea of a vector was introduced as a physical quantity that has both a magnitude and a direction. A vector in three-dimensional space can be represented as:
Its magnitude (or length) is given by:
In Python, we can represent vectors using NumPy arrays and calculate their magnitude as follows:
import numpy as np
x, y, z = 3, 4, 5
r = np.array([x, y, z])
# r = np.array([3, 4, 5]) equivalent to the to lines above
magnitude = np.linalg.norm(r)
print(magnitude)Inner product¶
Suppose we have two vectors and , visualized below.
Source
import numpy as np
import matplotlib.pyplot as plt
F = np.array([0, 1, 0])
r = np.array([2, 0, 0])
plt.figure()
plt.quiver(0, 0, F[0], F[1], angles='xy', scale_units='xy', scale=1, color='r', label='F')
plt.quiver(0, 0, r[0], r[1], angles='xy', scale_units='xy', scale=1, color='b', label='r')
plt.xlim(-1, 3)
plt.ylim(-1, 3)
plt.legend()
plt.grid()
plt.show()What should be noted is that these two vectors are orthogonal: they don’t share any direction. We know that the work done by a force in the direction of the displacement is zero if the force and displacement are orthogonal and can show this by taking the inner product. The inner product is also called the dot product and is denoted by a dot between the two vectors: . Mathematically, the inner product of these two vectors is defined as:
Its outcome is a scalar quantity. For our specific vectors, we have:
More formally, for any inner product in , the inner product of two vectors and is defined as
Using Python, we can compute the inner product as follows:
import numpy as np
F1 = np.array([0, 1, 0])
r1 = np.array([2, 0, 0])
inner_product = np.dot(F1, r1)
print(inner_product) # Output: 0Cross product¶
The cross product was introduced in this book first in the context of torque. We had an arm and a force and defined the torque as the cross product of these two vectors:
we can calculate the cross product as:
The outcome of a cross product is a vector quantity, thus with both a magnitude and direction. Moreover, the direction of the resulting vector is orthogonal to both and , following the right-hand rule. Note: , meaning the direction is reversed.
import numpy as np
r = np.array([2, 0, 0])
F = np.array([0, 1, 0])
torque = np.cross(r, F)
print(torque) # Output: [0 0 2]Vector fields¶
A vector field, for instance a force field or an electric field, assigns a vector to every point in space. When visualized, each point in space has an arrow indicating both the direction and the magnitude of the vector. By changing the function F below, you can create your own vector fields.
Source
import numpy as np
import matplotlib.pyplot as plt
def plot_vector_field(F, xlim=(-2, 2), ylim=(-2, 2), N=20, scale=None):
x = np.linspace(xlim[0], xlim[1], N)
y = np.linspace(ylim[0], ylim[1], N)
X, Y = np.meshgrid(x, y)
Fx, Fy = F(X, Y)
plt.figure(figsize=(5, 5))
plt.quiver(X, Y, Fx, Fy, scale=scale)
plt.xlabel("x")
plt.ylabel("y")
plt.xlim(xlim)
plt.ylim(ylim)
plt.show()
def F(x, y):
return -y, x
plot_vector_field(F)Matrices¶
Matrix multiplication¶
A matrix can be used to transform a vector into another vector, do an operation on a vector. (Note! It has much more functionalities such as representing systems of equations, but we will not go into that here). For example, consider a matrix and a vector :
We now can multiply the matrix with the vector to obtain a new vector :
Rotation matrices¶
Two dimensional¶
unit matrix
rotation matrix
We can introduce a rotation matrix that rotates a vector in two-dimensional space. Below this show for a matrix swaps the x and y coordinates:
90 degrees rotation
Given this concept we can introduce a matrix that rotates a vector by 90 degrees counterclockwise:
import numpy as np
import matplotlib.pyplot as plt
F = np.array([1, 0])
A = np.array([[0, -1], [1, 0]]) # 90 degrees rotation matrix
R = A @ F # Matrix multiplication to rotate vector F
plt.figure(figsize=(6,6))
plt.quiver(0, 0, F[0], F[1], angles='xy', scale_units='xy', scale=1, color='r', label='original vector')
plt.quiver(0, 0, R[0], R[1], angles='xy', scale_units='xy', scale=1, color='b', label='rotated vector')
plt.xlim(-1, 2)
plt.ylim(-1, 2)
plt.legend()
plt.show()45 degrees rotation
In a similar way, we can define a rotation matrix for a 45-degree clockwise rotation:
In Python, we can implement this as follows:
import numpy as np
import matplotlib.pyplot as plt
F = np.array([1, 0])
angle = 45 # degrees
theta = np.radians(-angle) # Convert angle to radians for clockwise rotation
A = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]])
R = A @ F # Matrix multiplication to rotate vector F
plt.figure(figsize=(6,6))
plt.quiver(0, 0, F[0], F[1], angles='xy', scale_units='xy', scale=1, color='r', label='original vector')
plt.quiver(0, 0, R[0], R[1], angles='xy', scale_units='xy', scale=1, color='b', label='rotated vector')
plt.xlim(-1, 2)
plt.ylim(-1, 2)
plt.legend()
plt.show()