We can use NumPy to perform multiplication and division in vectors and matrices using a single line of code.

Import the library:

import numpy as np

We can multiply the vectors and matrices in many ways, using np.dot(), np.multiply() and np.outer().

Dot Multiplication of Vectors

E.g., Vector 1 = [1, 2, 3, 4, 5] and Vector 2 = [2, 6, 7, 8, 9]

→ (1 * 2) + (2 * 6) + (3 * 7) + (4 * 8) + (5 * 9) = 112

Code:

array1 = np.array([1, 2, 3, 4, 5])
array2 = np.array([2, 6, 7, 8, 9])
print("Dot product of vectors:", np.dot(array1, array2))

Output:

Dot product of vectors: 112

The program raises an error if the vectors are not at the same size:

array1 = np.array([1, 2, 3, 4, 5, 6])
array2 = np.array([2, 6, 7, 8, 9])
print("Dot product of vectors:", np.dot(array1, array2))