Hello there. This post is a follow up post to my previous post on norms and dot products. Examples of computing norms and dot products in the programming languages R and Python will be shown.

** The Demand For Computing**

When you start learning mathematics, you learn the theory first for understanding and practice simple examples with pen and paper. With technology being more and more powerful and accessible, computational mathematics has become more popular. Why compute norms of vectors in the hundredth dimension by hand when it is easier and faster to do so with software.

**Norms and Dot Products in Python and R**

Even though I know R much more than Python, Python was the first programming language I learned when I took an Introduction to Programming course. MATLAB was another program I used from time to time but with Numpy and similar packages in Python, Python has more utility than MATLAB.

As someone who switched from mathematics to applied statistics, I use R very often.

In each example, the first set of code will be Python code and the second set of code will be code in R.

To import the Numpy package in Python to enable linear algebra functions we type in:

1 |
import numpy as np |

__Norms__

Given the vector , the norm of is . It can be computed as follows.

In Python:

1 2 3 4 5 6 7 |
## Norms: x = np.array([-3, 4]) print(np.linalg.norm(x)) 5.0 |

In R:

1 2 3 4 5 6 7 8 9 10 11 |
# a) a <- c(-3, 4) # Two ways of computing a norm: norm(a, type = "2") [1] 5 sqrt(sum(a^2)) [1] 5 |

If the vector in is , the norm would be .

In Python:

1 2 3 4 5 |
y = np.array([5, 1, 10, -2]) print(np.linalg.norm(y)) 11.401754251 |

In R:

1 2 3 4 5 6 7 |
b <- c(5, 1, 10, -2) norm(b, type = "2") 11.40175 sqrt(sum(b^2)) 11.40175 |

With the Python code, we express the vector as an array in the Numpy package and use the norm function to compute norms of vectors.

To create vectors in R, the c() function is used.

In R, there are two ways of computing norms (that I know of). The first one uses the norm function with type = “2”. (I don’t know why type 2, check the reference or help.) The second way is more direct and aligns with the mathematical definition of a norm. We squared each element in the vector, take the sum of squares and square root that sum.

__Distance Between Two Vectors__

Recall that in , the distance between vectors and is

To compute distance between vectors we do subtraction element-wise, square the results, sum the differences of squares and take the square root of the sum.

If we were to calculate the distance between the vectors and in , we would use the distance formula. The answer is around 12.884. The code is as follows.

In Python:

1 2 3 4 5 6 |
v = np.array([10, 5, -2, -1]) w = np.array([-1, 0, 2, 1]) print(np.linalg.norm(v - w)) 12.8840987267 |

In R:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
## Distance Between Two Vectors: v <- c(10, 5, -2, -1) w <- c(-1, 0, 2, 1) # Three ways of computing distance between vectors in R: sqrt(sum((v - w)^2)) [1] 12.8841 dist(rbind(v, w)) v w 12.8841 norm(v - w, type = "2") [1] 12.8841 |

The coding method is similar as before. The second way in R is strange but it works. (See one of the Reference links)

__Dot Products__

These examples for the dot products are the same examples in my previous post.

__Example One:__

The dot product of vectors and is -4.

In Python:

1 2 3 4 5 6 |
a = np.array([1, -2]) b = np.array([-10, -3]) print(np.dot(a, b)) -4 |

In R:

1 2 3 4 5 6 7 8 9 |
# Assuming equal lengths of vector the multiplication operator in R does # element wise multiplication (i.e. a1 * b1). # Example 1: a <- c(1, -2) b <- c(-10, -3) sum(a * b) [1] -4 |

__Example Two:__

Given vectors and , the dot product of these vectors in is -19.

In Python:

1 2 3 4 5 6 7 8 |
# Example Two: c = np.array([1, -2, 5, 3]) d = np.array([4, -3, -7, 2]) print(np.dot(c, d)) -19 |

In R:

1 2 3 4 5 6 7 |
# Example 2: c <- c(1, -2, 5, 3) d <- c(4, -3, -7, 2) sum(c * d) [1] -19 |

__Example Three:__

Suppose we are given the vectors , and . Evaluating gives us -18.

In Python:

1 2 3 4 5 6 7 8 9 |
# Example Three: (e + g) * f e = np.array([3, -5]) f = np.array([-1, 4]) g = np.array([3, 2]) print(np.dot(e + g, f)) -18 |

In R:

1 2 3 4 5 6 7 8 |
# Example 3: (e + g) * f e <- c(3, -5) f <- c(-1, 4) g <- c(3, 2) sum((e + g) * f) [1] -18 |

Computing the dot product in Python and Numpy is pretty straight forward.

Computing the dot product in R involves using element wise multiplication and then taking the sum.

__Notes__

We refer to the norm as the Euclidean norm. There are other norms and other distance measures available but those are not discussed here.

The decision between using R, Python or another language such as C++ is up to the user. I prefer R as I am used to it and I like the utility it provides. (Maybe eventually I will get more used to Python.)

__References__

These particular web links were useful.

http://stackoverflow.com/questions/5559384/euclidean-distance-of-two-vectors

http://stackoverflow.com/questions/10933945/how-to-calculate-the-euclidean-norm-of-a-vector-in-r

The featured image is from http://www.npl.co.uk/upload/img/maths-header.jpg.