DEV Community

Jidneya
Jidneya

Posted on • Edited on

Math in computer science: Vectors

Time for some more math. This week I would like to share something new i have learned: vectors.

This is a very interesting topic because most people think that it is use din physics to represent forces or velocity, but this topic is also used a lot in computer science.

What are vectors?

Now let me first define a vector. Most of you will already know that a vector can be used to define a point in a 3d space. This is true, however in the world of computer science a vector is also represented by a list of numbers or a function. By a function i just mean a mapping a values and not a mathematical function that will perform operation on an input.

Vectors are quite useful because of their ability to represent both magnitude and direction. This is why they are used for many things like video games, new models of AI, and even plane navigation systems.

Operations on vectors

Now let us see what we can do with vectors:

  • adding and subtracting vectors: to do this you just need to add or subtract the respective x and y values of each vector

  • Scaling vectors: Simply multiply all the coordinates with the scaling factor.

  • Convex combination: This is where you have two vectors and you find a vector that lies in the region enclose by the two. To do this you make a new vector of the form:
    au + bv
    where 'u' and 'v' are the two vectors we have and 'a' and 'b' are just scaling factors, however there are some restrictions:
    a + b = 1
    'a' and 'b' are greater than zero

  • Dot product: i know it sounds scary but trust me it is not. To find the dot product of two vectors you simply multiple the corresponding coordinates for each ,e.g. multiply the x coordinates and y coordinates. Next you just add the products an that's that.

  • Finding the angle between vectors: This is a very useful one. For this one there is a formula that is below (credit to mathsathome.com):

Image description

Let me explain a few points. 'a.b' means that dot product of 'a' and 'b'. Finally |a| simply means that absolute value, this means that no matter if 'a' is positive or negative |a| will always give a positive value. You can consider it to return the magnitude of the vector as the sign just represents the direction.

Conclusion

Vectors are very useful tools and now you now the basic functions and can start implementing them in your own projects to explore their many capabilities that make them such versatile tools in many fields such as physics. Thank you for reading.

Top comments (0)