# Computing Norms & Dot Products In R & Python

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:

Norms

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

In Python:

In R:

If the vector in is , the norm would be .

In Python:

In R:

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:

In R:

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:

In R:

Example Two:

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

In Python:

In R:

Example Three:

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

In Python:

In R:

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