NumPy | Vector Multiplication
NumPy is a Python library used for performing numerical computations. It provides an efficient way to work with vectors and matrices especially when performing vector multiplication operations. It is used in various applications such as data science, machine learning, physics simulations and many more. In this article, we’ll focus on three major types of vector multiplication
1. Scalar Product
Scalar multiplication refers to the operation where a scalar (a single number) is multiplied by each element of a vector. This operation is common in many applications like scaling vectors or adjusting data points in machine learning. In NumPy scalar multiplication is straightforward, you can simply multiply a scalar value with a vector and each element of the vector gets multiplied by the scalar.
- Given vector v is given as
[4,1] where it has two components:4 and1 . - When we multiply a vector by a scalar i.e a constant number lets say 5, we multiply each component of the vector by that scalar. In this case scalar is
5 . - So we multiply the vector v by 5:
Thus, the resulting vector after scalar multiplication is
Scalar Multiplication in Python
We will be using NumPy and math. Here w becomes
import numpy as np
import math
v = np.array([4, 1])
w = 5 * v
print("w = ", w)
Output:
w = [20 5]
2. Dot Product
Dot product is also known as the inner product of two vectors is a scalar value obtained by multiplying corresponding elements of the vectors and summing them up. It is a key operation in machine learning, particularly in measuring similarity between vectors or computing projections. To compute the dot product in NumPy you can use np.dot().
1. Given Vectors:
-
\mathbf{a} = \left[ a_x, a_y, a_z \right] \mathbf{b} = \left[ b_x, b_y, b_z \right]
These are three-dimensional vectors, where each component represents the vector's magnitude along the x, y and z axes, respectively.
2. Dot Product Formula: Dot product of two vectors a and b is calculated as:
Where:
\left| \mathbf{a} \right| and\left| \mathbf{b} \right| are the magnitudes of vectors a and b, respectively.\theta is the angle between the two vectors.
Formula is equivalent to the following component-wise expression:
This means that dot product is the sum of the products of corresponding components of the two vectors and result of the dot product is a scalar quantity.
Dot Product Multiplication in Python
v=
import numpy as np
import math
v = np.array([2, 1])
s = np.array([3, -2])
d = np.dot(v, s)
print(d)
d_alt = v @ s
print(d_alt)
Output:
4 4
3. Cross Product
Cross product is a vector operation that returns a new vector perpendicular to both of the input vectors and is only applicable in 3D space. It is essential in fields such as physics, computer graphics and robotics especially when working with 3D rotations or torque calculations. Cross product is computed using np.cross() in NumPy and result is a vector that is orthogonal to both input vectors.
1. Given Vectors:
\mathbf{a} = \left[ a_x, a_y, a_z \right] \mathbf{b} = \left[ b_x, b_y, b_z \right]
These are three-dimensional vectors where a and b represent the components along the x, y and z axes.
2. Cross Product Formula:
Where:
|\mathbf{a}| and|\mathbf{b}| are the magnitudes (lengths) of the vectors.\theta is the angle between the two vectors.
Result of the cross product is a vector that is perpendicular to both a and b and the magnitude of this vector is proportional to the area of the parallelogram formed by a and b.
3. Determinant Approach for Cross Product: Cross product can also be computed using the determinant of a matrix.
Where:
\mathbf{i}, \mathbf{j}, \mathbf{k} are the unit vectors in the x, y and z directions, respectively.- Matrix involves the components of a and b.
Cross Product in Python
Cross product of v =
Result is a vector r, which is perpendicular to both v and s.
import numpy as np
import math
v = np.array([4, 9, 12])
s = np.array([21, 32, 44])
r = np.cross(v, s)
print(r)
Output:
[ 12 76 -61]
By mastering vector multiplication techniques like scalar multiplication, dot product and cross product with NumPy you can solve complex problems in data science, machine learning, physics and engineering.