Exercises in NumPy Vector Algebra

A Tour of Must-Know Vector Operations with NumPy

George Marklow
6 min readDec 29, 2024
By User:Acdx, CC BY-SA 4.0, https://commons.wikimedia.org/w/index.php?curid=5609640

Introduction

I put together this article to summarise important vector operations using NumPy.

Installing Python and NumPy

You can install Python and NumPy on your machine or use one of the many convenient online compiler sites.

To install it on your local machine, visit the Downloads section of the Python website https://www.python.org/downloads/. Once installed, you can follow the instructions to install NumPy https://numpy.org/install/

Once both are installed, bring up a command console and type each line listed below followed by ENTER each time to prove both Python and NumPy are successfully installed:

python
import numpy as np
print(np.array([1]))

You should see the output, which proves both installations were successful:

[1]

Basics

Vector Equality

In this trivial example, we can check if vectors and their corresponding entries are equal

Using NumpPy’s array_equal function, we can see that the second and fourth vectors are equal:

u1 = np.array([1,2,3])
u2 = np.array([2,3,1])
u3 = np.array([1,3,2])
u4 = np.array([2,3,1])

np.array_equal(u2,u4) # True
np.array_equal(u1,u2) # False

Addition and Multiplication

Given vectors

the sum of these two vectors is

and multiplying the first vector by a scalar quantity gives you

So, for example, given the following vectors

we can calculate

straightforwardly in NumPy by combining scalar multiplication of vectors with addition/subtraction:

u = np.array([2,-7,1])
v = np.array([-3,0,4])
w = np.array([0,5,-8])

print(2 * u + 3 * v - 5 * w) # [ -5 -39 54]

Linear Combination

We can express the following vector:

as a linear combination of these three vectors

Using NumPy, we first use the column_stack function to create a 2-D array. Then, we use linalg.solve to solve the linear matrix equation, finding coefficients for the vector representing the linear combination:

v = np.array([1, -2, 5])
u1 = np.array([1, 1, 1])
u2 = np.array([1, 2, 3])
u3 = np.array([2, -1, 1])
a = np.column_stack((u1,u2,u3))

print(np.linalg.solve(a,v)) # [-6. 3. 2.]

Using these coefficients, we can express v using a linear combination of the three u vectors:

Dot Product and Orthogonality

Given vectors

we can find the dot product by summing the products of the corresponding entries of the two vectors

These vectors are orthogonal, meaning perpendicular to each other if the dot product is zero

We can find the dot product of any pair of the following vectors

by using the dot function in NumPy and find that

  • u is orthogonal to v, and
  • u is orthogonal to w
u = np.array([5, 4, 1])
v = np.array([3, -4, 1])
w = np.array([1, -2, 3])

print(np.dot(v,w)) # 14
print(np.dot(u,v)) # 0
print(np.dot(u,w)) # 0

Norm

Given a vector

the norm is the non-negative square root of the dot product of the vector with itself, i.e.

For example, given the following vector

we can calculate the norm using the NumPy linalg.norm function

u = np.array([3, -12, -4])
print(np.linalg.norm(u)) # 13.0

Note that a unit vector has a norm of 1, i.e.

For example, we can show that the following is a unit vector

u = np.array([1/2, -1/6, 5/6, 1/6])
print(np.linalg.norm(u)) # 1.0

Normalising a Vector

The vector

is the unit vector in the direction of v

For example, take the following vector

We can find the unique unit vector coefficients in the direction of u by dividing the vector by the norm of the vector

u = np.array([3, -4])
norm = np.linalg.norm(u)
print(u / norm) # [ 0.6 -0.8]

Angle, Projection and Distance

Angle

We can find the cosine of the angle between vectors by dividing the dot product of vectors by the product of their norms

For example, let

Below is how to calculate the angle, in radians and degrees, between these vectors using NumPy (see notes on NumPy’s clip function).

u = np.array([1,-2,3])
v = np.array([2,4,5])

uv = np.dot(u,v)
u_norm = np.linalg.norm(u)
v_norm = np.linalg.norm(v)
cos_theta = uv / (u_norm * v_norm)
cos_theta = np.clip(cos_theta, -1.0, 1.0)
angle_radians = np.arccos(cos_theta)
angle_degrees = np.degrees(angle_radians)

print("Angle in radians:", angle_radians) # Angle in radians: 1.204062267702623
print("Angle in degrees:", angle_degrees) # Angle in degrees: 68.98768621031138

Projection

Furthermore, we can calculate the projection between vectors u and v, denoted proj(u,v), of one vector onto another:

Using the same vectors as above, we can find the projection using NumPy as follows:

u = np.array([1,-2,3])
v = np.array([2,4,5])
uv = np.dot(u,v)
v_norm_squared = np.linalg.norm(v) ** 2
proj = (uv / v_norm_squared) * v
print(proj) # [0.4, 0.8, 1. ]

Distance

Finally, we can calculate the distance between vectors u and v using the formula

or, more succinctly

Reusing vectors u and v again, we can define the distance between two vectors as follows:

distance = np.linalg.norm(u - v)
print(distance) # 6.4031242374328485

Cross Product

The cross product of two 3-D vectors is

Think of it as a third vector that is perpendicular to both u and v and whose magnitude represents the area of the parallelogram formed by u and v.

For example, for vectors u and v

we can calculate the cross product with NumPy using the cross function:

cross_product = np.cross(u,v)
print(cross_product) # [-22 1 8]

Thanks for reading! Let me know what you think in the comments section below, and don’t forget to subscribe 👍

References

--

--

George Marklow
George Marklow

Written by George Marklow

George is a software engineer, author, blogger, and abstract artist who believes in helping others to make us happier and healthier.

No responses yet