Operations with Vectors#
Scalar Multiplication#
Scalar multiplicaiton is when we multiply a vector by a scalar. We do this by multiplying each component of the vector by the scalar. This can be expressed as:
where, \(\lambda\) is a real number.
Example
Simplify \(7(-12, 4)\).
Solution: We multiply each component of the vector by 7 giving:
This can be performed with a NumPy array.
import numpy as np
7 * np.array([-12, 4])
array([-84, 28])
Vector Addition and Subtraction#
When we add or subtract two vectors, we add or subtract each of the individual components of the vectors. This can be expressed as:
where \((a, b, c)\) and \((x, y, z)\) share the same base vectors.
Important
If the vectors have a different number of base components, for example \((3, 6) + (1, 0, 7)\) we cannot complete the addition. For all vector on vector operations, both vectors must have the same number of base components.
Example
\((2, 2, 0) + (5, -1, 3)\)
\(3(a, 4a, 0) - a(7, 0, -1)\)
\((1, 3, 8) + (2, 1, -5) + (-1, 2, -1)\)
Solution:
\((2, 2, 0) + (5, -1, 3) = (3+5, 2-1, 0+3) = (7, 1, 3)\)
\(3(a, 4a, 0) - a(7, 0, -1) = (3a, 12a, 0)-(7a, 0, -a) = (3a-7a, 12a-0, 0+a)\) \( = (-4a, 12a, a)\)
\((1, 3, 8)+(2, 1, -5) + (-1, 2, -1) = (1+2-1, 3+1+2, 8-5-1) = (2, 6, 2)\)
Again, NumPy can implement this for numerical examples.
np.array([2, 2, 0]) + np.array([5, -1, 3])
array([7, 1, 3])
np.array([1, 3, 8]) + np.array([2, 1, -5]) + np.array([-1, 2, -1])
array([2, 6, 2])
However, the middle example includes the algebraic \(a\), which is possible to compute with pure NumPy.
Instead sympy should be used.
from sympy import symbols
a = symbols('a')
3 * np.array([a, 4 * a, 0]) - a * np.array([7, 0, -1])
array([-4*a, 12*a, a], dtype=object)
Vector Mulitplication: Dot Product#
There are two ways of multiplying two vectors together. The first is the dot (or scalar) product, which is defined to be:
where \(\theta\) is the angle between \(\vec{A}\) and \(\vec{B}\).
Important
Taking the dot product of two vectors produces a scalar quantity.
Given two vectors, the dot product can also be calculated in the following way:
The dot product can be thought of as how much one vector is pointing in the direction of the other. This is indicated with the plot below.
The part of \(\vec{A}\) that goes in the same direction as \(\vec{B}\) has a length of \(|\vec{A}|\cos{(\theta)}\), making the dot product the length of \(\vec{B}\) times the length of \(\vec{A}\) that is in the same direction as \(\vec{B}\).
Example
Calculate the following:
\(\begin{pmatrix} 3 \\ 9 \\ 1 \end{pmatrix} \cdot \begin{pmatrix} 1 \\ -2 \\ 10 \end{pmatrix}\)
\(\begin{pmatrix} 1 \\ 0 \end{pmatrix} \cdot \begin{pmatrix} \sqrt{2} \\ \sqrt{2} \end{pmatrix}\)
Solution:
\(\begin{pmatrix} 3 \\ 9 \\ 1 \end{pmatrix} \cdot \begin{pmatrix} 1 \\ -2 \\ 10 \end{pmatrix} = 3 \times 1 + 9 \times (-2) + 1 \times 10 = 3 - 18 + 10 = -5\)
\(\begin{pmatrix} 1 \\ 0 \end{pmatrix} \cdot \begin{pmatrix} \sqrt{2} \\ \sqrt{2} \end{pmatrix} = 1\times \sqrt{2} + 0 \times\sqrt{2} = \sqrt{2}\)
The NumPy library includes a np.dot function that can be used to find the dot product of two vectors.
np.dot(np.array([3, 9, 1]), np.array([1, -2, 10]))
-5
np.dot(np.array([1, 0]), np.array([np.sqrt(2), np.sqrt(2)]))
1.4142135623730951
Angle Between Force Vectors
An ion moving through solution has 2 forces acting upon it, a resistive force from the medium with a vector of \((3, \sqrt{7}, 3)\), and an electromagnetic foce from an electric field with a vector of \((-5, 3, 4)\). What is the angle between these two vectors?
Solution: To calculate this, we find the dot product of the two vectors and then employ the definition of the dot product to find the angle. The dot product of the two vectors is:
Using the definition \(\vec{A}\cdot\vec{B} = |\vec{A}||\vec{B}| \cos{(\theta)}\), we can not find out what \(\theta\) is. First, we must find the magnitudes of the two vectors:
and
Putting all this information into the equation \(\vec{A}\cdot\vec{B} = |\vec{A}||\vec{B}| \cos{(\theta)}\), gives us:
As one would expect, we can bring aspects of NumPy together to perform this with code.
A = np.array([3, np.sqrt(7), 3])
B = np.array([-5, 3, 4])
angle = np.arccos(np.dot(A, B) / (np.linalg.norm(A) * np.linalg.norm(B)))
np.rad2deg(angle)
81.97260125982753
Work/Force
What is the work done \(w\) be the vector force \(\vec{F} = (3t\vec{i} + 3\vec{j})\;\textrm{N}\) on a particle of velocity \(\vec{V} = (5\vec{i} - t\vec{j})\;\textrm{ms}^{-1}\) in the time interval \(0 < t < 3\;\textrm{s}\), given that:
Solution: First, we must find the dot product \(\vec{F}\cdot\vec{v}\).
Now we have found the dot product, we can substitute it in the integral and solve.
Let’s look at how we can get a numerical estimate of this result with the rectangle rule approach to estimate the integral.
We start with defining a range of t, from 0 to 3.
width = 0.0001
t = np.arange(0, 3, width)
t += width / 2
t
array([5.00000e-05, 1.50000e-04, 2.50000e-04, ..., 2.99975e+00,
2.99985e+00, 2.99995e+00])
Next, we compute the vector \(\vec{F}\) for all the values of \(t\).
F = np.array([3 * t, 3 * np.ones_like(t)])
F
array([[1.50000e-04, 4.50000e-04, 7.50000e-04, ..., 8.99925e+00,
8.99955e+00, 8.99985e+00],
[3.00000e+00, 3.00000e+00, 3.00000e+00, ..., 3.00000e+00,
3.00000e+00, 3.00000e+00]])
The same is done for \(\vec{v}\).
v = np.array([5 * np.ones_like(t), -t])
And we can perform the dot product.
dot_product = np.sum(F * v, axis=0)
Finally, we estimate the integral using the rectangle rule.
np.sum(dot_product * width)
54.00000000000001
This is a good estimate of the value found algebraically above.
Vector Multiplication: Cross Product#
The cross product (or vector product) is defined to be:
where, \(\theta\) is the angle between the two vectors and \(\vec{\hat{n}}\) is the unit vector in the direction of the new vector.
Important
The cross product takes two vectors and produces a new vector. This new vector is in the direction **perpendicular** to the plane that \(\vec{A}\) and \(\vec{B}\) are in. It is only possible to take the cross product of two 3-D vectors (and technically 7-D vectors as well).
The cross product is also dependent on the order that the vectors appear in the product. If we change the order of the vectors in a cross product, then our answer becomes negative. The vector points in the opposite direction.
The right hand rule determines the way which the vector produced will point. We use the rule as follows:
Curl your fingers from the first vector in the cross product to the second vector and the direction your thumb points is the direction of the vector produced by the cross product.
In this case, \(\vec{A} \times \vec{B}\) will point out of the page, and \(\vec{B}\times\vec{A}\) will point into the page.
Calculating the Cross Product#
When using the standard base vector of \(\vec{\hat{i}}\), \(\vec{\hat{j}}\), and \(\vec{\hat{k}}\), we can calculate the cross product by considering the cross product of the base vectors with each other.
Taking the cross products of two vectors is just like expanding brackets. The cross product of two general vectors \((a\vec{i} + b\vec{j} + c\vec{k}) \times (x\vec{i} + y\vec{j} + z\vec{k})\), would give \(ax(\vec{\vec{i}\times\vec{i}}) + ay(\vec{i}\times\vec{j}) + az(\vec{i}\times\vec{k})+\ldots\). Fully expanded and simplified gives:
where, \(a\), \(b\), \(c\), \(x\), \(y\), and \(z\) are real numbers.
Example
Calculate the following cross product:
*Solution: So to find this, we use the formula above, where \(a=3\), \(b=1\), \(c=-2\), \(x=2\), \(y=-1\), \(z=1\), and therefore:
This could also be written:
The NumPy library can also be used to compute the cross product between two arrays.
np.cross(np.array([3, 1, -2]), np.array([2, -1, 1]))
array([-1, -7, -5])