Week 2:

Litus RamosLitus Ramos
6 min read

Unit vectors, scalar multiplication and dot product

In this weekly post, I will explain you how to work with unit vectors, how to normalize vectors, how to do scalar multiplication and the dot product, I hope you find it useful!

Introduction:

Before we start, I would like to think about the importance of vectors. At the moment they don’t seem important, but, if you want to reach the objective that I do (Quantum Computing) you will need to be “best friend” with vectors! You will use it everywear!!! For example, for describing any quantum system, you will need vectors, not just qbits, any quantum system: an electron, an atom, or one million qbits!

Unit vectors:

Definition: A unit vector, is a vector that has magnitude = 1, you could think that is a little stupid, but, trust me, it isn´t! Unit vectors are used for describing directons without looking to the magnitude, they are really important. An example of a unit vector would be:

$$v=(1,0)$$

Why? Because his magnitude is 1! We can prove it:

$$\|\mathbf{v}\|=\sqrt{1²+0²}=1$$

You could think: “There are lots of vectors that haven’t magnitude=1” And it is true, but there is a way to make a normal vector to become a unit vector, this is called normalazing, its formula is:

$$\hat{\mathbf{v}} = \frac{\mathbf{v}}{\|\mathbf{v}\|}$$

This v with a hat, means “unit vector”, so, as you can see, for normalizing a vector, you need to do a division between the vector and its magnitude, here is an example:

$$\mathbf{v}=(3,4)$$

The normalized vector is:

$$\hat{\mathbf{v}} = \frac{(3, 4)}{5} = \left( \frac{3}{5}, \frac{4}{5} \right) =(0.6,0.8)$$

So herewe have applied the formula, and as you can see, now we have a unit vector, in the same direction as (3,4) but now, smaller, with less magnitude! Here we divided (3,4) with its magnitude, that is 5. You could do this with every vector that exists! I propose an excercise for anybody who wants to do it. Normalize the vector (7,8), and write it down in the coments!

Here you have learnt about the impotance of unit vectors, because you can pick a vector with any direction, normalize it, and you will have a vector, in the same direction, but with magnitude 1! I encourage you to draw the vector (3,4) and the vector (0.6,0.8) in a cartesian plane, and check that they point in the same direction!

Scalar multipilication:

Multipying a scalar number with a vector is just multiplying each component of the vector with the scalar, like this:

$$3(1,2)=(3·1,3·2)=(3,6)$$

You could check again that the magnitude of the vector has changed, but not the direction!!!! The only way to change the direction with a scalar is multiplying by a negative number, this just gives you a vector with the exactly opposite direction. This is really useful, for example, if you have a vector (1,1) and you want to make it really big, you could multiply by a giant nuber, and you will have a giant vector! And in the same direction! I could understand that you don’t realize about the importance of this, but don’t worry, you will!

Dot product:

The dot product is an operation between vectors, and gives you a number, you can calculate it so:

$$\vec{u} \cdot \vec{v} = u_1 v_1 + u_2 v_2$$

For example, If we have these two vectors:

$$\vec{u}=(3,4) \vec{v} = (1,2)$$

Its dot product would be:

$$\vec{u} \cdot \vec{v}=(3*1) +(4*2)=3+8=11$$

The simbol * means multiplication, it is like when we multiplicate in Python.

When the dot product is 0, the two vectors ar perpendicular between them!

You could think that this isn’t useful, but in fact, you can use the dot product for calculating the angle between two vectors! (You know that at the moment just with 2D). You can do it like this:

$$\theta = \cos^{-1} \left( \frac{\mathbf{v_1} \cdot \mathbf{v_2}}{\|\mathbf{v_1}\| \|\mathbf{v_2}\|} \right)$$

So here, you don’t need to know how to calculate de arccos ( or inverse cosine), you can do it with a calculator, or with Python (later I will explain you how to do it!). For example, the vectors:

$$\vec{v}=(0,5) \vec{w}=(5,0)$$

Wich angle is between this two? Lets calculate it!

$$\theta = \cos^{-1} \left( \frac{\mathbf{v_1} \cdot \mathbf{v_2}}{\|\mathbf{v_1}\| \|\mathbf{v_2}\|} \right) \mathbf{v_1} \cdot \mathbf{v_2} = (0 \cdot 5) + (5 \cdot 0) = 0 \|\mathbf{v_1}\| = \sqrt{0^2 + 5^2} = \sqrt{25} = 5 \|\mathbf{v_2}\| = \sqrt{5^2 + 0^2} = \sqrt{25} = 5| \theta = \cos^{-1} \left( \frac{0}{5 \times 5} \right) \theta = \cos^{-1} \left( \frac{0}{25} \right) \theta = \cos^{-1} (0) \theta = 90^\circ \quad \text{(o } \frac{\pi}{2} \text{ rad)}$$

I know that it doesn’t look good, but with Latex here in Hashnode is the maximum I can do. But as you can see, the result is 90º. We can prove it!

As you can see, between this two vectors, there is an angle of 90º!!!

Application to Python.

As you should know, now we will apply all the concepts that we learnt in Python!

  1. Unit vectors and normalizing vectors:

    Here is an example of code for checking if a vector is normalized or no:

    ```python import numpy as np v=np.array([5,7]) def checking_unit_vectors (): magnitude=np.sqrt((v[0]2)+(v[1]2)) if magnitude ==1: print("It is a unit vector") else: print("It isn't a unit vector, you nee to normalize it!")

checking_unit_vectors()


    You can change the variable for the vector that you want!!!( Remember that if you don’t understand something of the code, please write it in comments, so I can help you!)

2. **Scalar multiplication:**

    Thisis so simple, but it will be so useful when we start with linear combinations! In both Python and real life! There is a simple example:

    ```python
    import numpy as np
    v=np.array([4,8])
    print(3*v)

I know this is so simple, but at the moment we start with this!

  1. Dot product:

    For calculating the dot prouct in Python, you need to use this function:

     import numpy as np
     v1=np.array([1,2])
     v2=np.array([3,5])
     d_p=np.dot(v1,v2)
     print(d_p)
    

    And for calculating the angle between them:

     import numpy as np
    
     v1=np.array([5,4])
     v2=np.array([5,0])
     p_p=np.dot(v1,v2)
     magnitude1=np.linalg.norm(v1)
     magnitude2=np.linalg.norm(v2)
     pxp=magnitude1*magnitude2
     angle_rad=np.arccos(p_p/pxp)
     angle_grad=np.degrees(angle_rad)
    
     print(angle_grad)
    

    Here you use np.arccos for calculating the angle, but in radians, and with another function np.degrees, you turn it into degrees!

Excercises (optional):

Obviously, this is completely optional, if you want to practice this, I prupose you a few excercises, if you want to do it, send the answers in thecoments please!:

  1. Create a function in Python that normalizes a vector

  2. Do 3 scalar multiplication

  3. Calculate the angle between [3,5] and [1,2] but with a notebook and a calculator, not Python! When you finsih, draw the vectors and check if you are right!

  4. If you want do some extra excercises

Conclusion:

All the things that you just read are fundamental in quantu computing, the state of a qbit is represented with unit vectors, scalar multiplication is the begining of linear combinations, that are important in Machine Learning, for the forward propagation, and the dot product it is also really useful!

What’s next?

The next week we will skip to week 4, because we covered the contents of the 3rd in these two posts! We wil cover the cross product (or vectorial product), how to represent vector in 3D with a paper and with python and much more. Have a nice day!

1
Subscribe to my newsletter

Read articles from Litus Ramos directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Litus Ramos
Litus Ramos