Linear Algebra with JavaScript: Vectors

This is part of the course “Linear Algebra with JavaScript”.

Image for post
Image for post
GitHub Repository with Source Code

Vectors are the precise way to describe directions in space. They are built from numbers, which form the components of the vector. In the picture below, you can see the vector in two-dimensional space that consists of two components. In the case of a three-dimensional space vector will consists of three components.

Image for post
Image for post
the vector in 2D space

We can write class for vector in 2D and call it Vector2D and then write one for 3D space and call it Vector3D, but what if we face a problem where vectors represent not a direction in the physical space. For example, we may need to represent color(RGBA) as a vector, that will have four components — red, green, blue and alpha channel. Or, let’s say we have a vector that gives fractions of proportions out of n choices, for example, the vector that describes the probability of each out five horses winning the race. So we make a class that not bound to dimensions and use it like this:

Vector Operations

Consider two vectors, and assume that α ∈ R is an arbitrary constant. The following operations are defined for these vectors:

Image for post
Image for post
basic vector operations

Visualizations of all of these operations, except of cross-product you can find here. In GitHub repository alongside with library lives React project, where library we are building used to create visualizations. If you are interested in learning how these two-dimensional visualizations made with React and SVG, check out this story.

Addition and Subtraction

Just like numbers, you can add vectors and subtract them. Performing arithmetic calculations on vectors simply require carrying out arithmetic operations on their components.

Image for post
Image for post
vectors addition
Image for post
Image for post
vectors subtraction

Methods for addition receives other vector and return new vectors build from sums of corresponding components. In subtraction, we are doing the same but replace plus on minus.

Scaling

We can also scale a vector by any number α ∈ R, where each component is multiplied by the scaling factor α. If α > 1 the vector will get longer, and if 0 ≤ α < 1 then the vector will become shorter. If α is a negative number, the scaled vector will point in the opposite direction.

Image for post
Image for post
scaling vector

In scaleBy method, we return a new vector with all components multiplied by a number passed as a parameter.

Length

A vector length is obtained from Pythagoras’ theorem.

Image for post
Image for post
vectors length

The length method is very simple since Math already has a function we need.

Dot Product

The dot product tells us how similar two vectors are to each other. It takes two vectors as input and produces a single number as an output. The dot product between two vectors is the sum of the products of corresponding components.

Image for post
Image for post
dot product

In the dotProduct method, we receive another vector as a parameter and via reduce method obtain the sum of the products of corresponding components.

Before we look at how vectors directions relate to each other, we implement the method that will return a vector with length equal to one. Such vectors are useful in many contexts. When we want to specify a direction in space, we use a normalized vector in that direction.

Image for post
Image for post
using dot product

If we take the dot product of two normalized vectors and the result is equal to one it means that they have the same direction. To compare two float numbers, we will use the areEqual function.

If we take the dot product of two normalized vectors and the result is equal to minus one it means that they have the exact opposite direction.

If we take the dot product of two normalized vectors and the result is zero, it means that they are perpendicular.

Cross Product

The cross product is only defined for three-dimensional vectors and produces a vector that is perpendicular to both input vectors.

Image for post
Image for post

In our implementation of the cross product, we assume that method used only for vectors in three-dimensional space.

Other useful methods

In real-life applications those methods will not be enough, for example, we may want to find an angle between two vectors, negate vector, or project one to another.

Before we proceed with those methods, we need to write two functions to convert an angle from radians to degrees and back.

Angle Between

Negate

To make a vector directing to the negative direction we need to scale it by minus one.

Project On

Image for post
Image for post
project v on d

With Length

Often we may need to make our vector a specific length.

Equal To

To check if two vectors are equal we will use areEqual function for all components.

Unit Vector and Basis

We can think of a vector as a command to “go a distance vx in the x-direction, a distance vy in the y-direction and vz in the z-direction.” To write this set of commands more explicitly, we can use multiples of the vectors ̂i, ̂j, and ̂k. These are the unit vectors pointing in the x, y, and z directions, respectively:

Image for post
Image for post
standard basis {i, ̂j, ̂k}

Any number multiplied by ̂i corresponds to a vector with that number in the first coordinate. For example:

Image for post
Image for post

One of the most important concepts in the study of vectors is the concept of a basis. Consider the space of three-dimensional vectors ℝ³. A basis for ℝ³ is a set of vectors {ê₁, ê₂ , ê₃} which can be used as a coordinate system for ℝ³. If the set of vectors {ê₁, ê₂, ê₃} is a basis then we can represent any vector v⃗∈ℝ³ as coefficients (v₁, v₂, v₃) with respect to that basis:

Image for post
Image for post
vector with respect to basis {ê₁, ê₂, ê₃}

The vector v⃗ is obtained by measuring out a distance v₁ in the ê₁
direction, a distance v₂ in the ê₂ direction, and a distance v₃ in the
ê₃ direction.

A triplet of coefficients by itself does not mean anything unless we know the basis being used. A basis is required to convert mathematical objects like the triplet (a, b, c) into real-world ideas like colors, probabilities or locations.

Next part ->

Check out my app at increaser.org to get more done by working smarter.

Reach the next level of focus and productivity with increaser.org.

Image for post
Image for post
Increaser

Written by

Software engineer, creator of increaser.org. More at geekrodion.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store