Before getting started, suppose you are blindfolded and asked to pick up a pen from a table. The only clue you are given is that the pen is at a distance of one arm from you. Would you be able to pick it up in one go?

Absolutely not!

Of course, you “may” pick it up but it’s uncertain. You may end up swinging your arm in one arm’s radius of yours.

So now, would you be able to pick up the pen having said that it’s at one arm’s distance in front of you? 

Seems possible right?

This is exactly where the application of vectors in our real-life comes into the picture unknowingly. Wondering how? Let me explain.

In the first case when you were told that the pen is at one arm’s distance from you, you only know the magnitude of the distance which is why you end up puzzled in locating the pen.

But in the second case, along with the magnitude of the distance, you know the direction(in front of you) as well which made your task a cakewalk.

Let’s kick-start our learning and make more such problems easier to solve.


A vector in \mathbb{R}^n  is a column of n real numbers. These real numbers are called the components or entries of the vector.

Ex: \begin{bmatrix}5\\4\end{bmatrix} is a vector in \mathbb{R}^2 

Fig 1: Vector \begin{bmatrix}5\\4\end{bmatrix}

					%use s2 
// define a vector 
var v = DenseVector(arrayOf(5.0, 4.0)) 

Output: [5.000000, 4.000000]

A vector in \mathbb{R}^n , say v, is drawn as an arrow pointing from the first component in v to the second component in v.

Norm of a vector:  The norm of a vector v\mathbb{R}^n  is the length of the associated arrow when drawn as stated above, which may be calculated as the square root of the sum of the squares of v’s components. The norm of a vector v is represented as \lvert v\rvert. A vector whose norm is 1 is called a unit vector. 

Norm of a vector can be calculated as follows:

					%use s2 
//define a vector to find its norm 
var v = DenseVector(arrayOf(3.0, 4.0)) 
//get the components of the vector 
val x = v.get(1) 
val y = v.get(2) 
//calculate the square root of the sum of the squares of vector's components 
val norm = (x.pow(2.0) + y.pow(2.0)).pow(0.5) 
Output: 5.0

Vector Operations

Let us get familiar with the fundamental vector operations:

  1. Vector addition (addition of two vectors), and
  2. Scalar multiplication (multiplication of a real number and a vector).

Vector Addition

This is the simple addition of two or more vectors whose summing concatenates them tail-to-head. A vector addition is denoted with “+” between two vectors.

Fig 2: Vector Addition

\begin{bmatrix}1\\2\end{bmatrix}+\begin{bmatrix}4\\2\end{bmatrix} = \begin{bmatrix}5\\4\end{bmatrix}

					%use s2 
// define 2 vectors
var v1 = DenseVector(arrayOf(1.0, 2.0)) 
var v2 = DenseVector(arrayOf(4.0, 2.0)) 
//adding two vectors 
val vec_sum=v1.add(v2) 
Output: [5.000000, 4.000000]

Scalar multiplication

Multiplying a vector by a real number is called scalar multiplication. Scalar multiplication is denoted by placing the scalar adjacent to the vector. 

Ex: Multiplying a vector, v = \begin{bmatrix}2\\1\end{bmatrix} with a real number, s = 2 is written as: 2\begin{bmatrix}2\\1\end{bmatrix} = \begin{bmatrix}4\\2\end{bmatrix}

Fig 3: Scalar Multiplication

					%use s2 
// define a vector 
var v = DenseVector(arrayOf(2.0, 1.0)) 
//define a scalar 
val s = 2.0 
// B = v*s 
val B = v.scaled(s) 

Output: [4.000000, 2.000000]


  1. Multiplying a vector by a positive real number k preserves its direction and multiplies its norm by k.
  2. Multiplying a vector by a negative real reverses its direction and multiplies its norm by k.
  3. Multiplying a vector by -1 reverses its direction and preserves its norm.

Let us illustrate an example implementing the operations involved in the concepts explained above.

Q: Simplify 4\begin{bmatrix}1.1\\2.2\end{bmatrix}-\begin{bmatrix}1.0\\2.4\end{bmatrix} 

					%use s2 
// define 1st vector 
var m = DenseVector(arrayOf(1.1, 2.2)) 
//define scalar to multiply 
val scalar = 4.0 
//define 2nd vector 
var n = DenseVector(arrayOf(1.0, 2.4)) 
// Scalar Multiplication 
val B = m.scaled(scalar)
val sol = B.minus(n) 

Output: [3.400000, 6.400000]

Ever played games like Catapult Quest or Angry Birds? If you keenly observe, in games, vectors are used to store positions, directions and velocities just like the one below.

  • The position vector indicates how far the object is.
  • The velocity vector indicates the time taken or the amount of force that must be given.
  • The direction vector indicates the direction in which the force should be applied.

Linear Combination

Let us define a list of vectors v_1, v_2,..., v_k. The linear combination of these vectors is expressed as:


 where c_1, c_2,.., c_k are real numbers.

These c‘s are called the weights of the linear combination.

					%use s2 
// define vector v1 
var v1 = DenseVector(arrayOf(1.0, 2.0)) 
//define c1 
val c1 = 3.0 
//Repeat the same for required number of terms 
var v2 = DenseVector(arrayOf(3.0, 3.0)) 
val c2 = 1.0 
var v3 = DenseVector(arrayOf(5.0, 3.0)) 
val c3 = 2.0 
// Scalar Multiplication 
val A = v1.scaled(c1) 
val B = v2.scaled(c2) 
val C = v3.scaled(c3) 
//Vector Addition 
var sol = A.add(B.add(C)) 
Output: [16.000000, 15.00000]

Let us illustrate the same using graph for a better understanding.

Fig 4: Linear Combination of vectors

Note: An integer linear combination is a linear combination in which all the weights (c’s) are integers.


The span of a given list of vectors is defined as the set of all vectors which can be written as the linear combination of the given list of vectors.

Ex: \begin{bmatrix}x\\y\end{bmatrix} is said to be in the span of the vector-list \left\{\begin{bmatrix}x_1\\y_1\end{bmatrix},\begin{bmatrix}x_2\\y_2\end{bmatrix}, \begin{bmatrix}x_3\\y_3\end{bmatrix}\right\} if:

\begin{bmatrix}x\\y\end{bmatrix} = \alpha \begin{bmatrix}x_1\\y_1\end{bmatrix}+\beta \begin{bmatrix}x_2\\y_2\end{bmatrix}+\gamma \begin{bmatrix}x_3\\y_3\end{bmatrix}

where \alpha, \beta, \gamma\mathbb{R}.

Let us solve a problem to improve our understanding.

Q: Determine whether the vector \begin{bmatrix}19\\10\\-1\end{bmatrix} lies in the span of set of vectors: S = \left\{\begin{bmatrix}3\\-1\\2\end{bmatrix}, \begin{bmatrix}-5\\0\\1\end{bmatrix}, \begin{bmatrix}1\\7\\-4\end{bmatrix}\right\}.


Let us first assume the equation: c_1v_1+c_2v_2+c_3v_3 = v, where c_1, c_2, c_3 are real numbers and try to find the solution [c_1, c_2, c_3] if exists.

					%use s2 
// define vectors v1,v2,v3,v 
var v1 = DenseVector(arrayOf(3.0, -1.0, 2.0)) 
var v2 = DenseVector(arrayOf(-5.0, 0.0, 1.0)) 
var v3 = DenseVector(arrayOf(1.0, 7.0, -4.0)) 
var v = DenseVector(arrayOf(19.0, 10.0, -1.0)) 
//we need to determine whether there exists a solution for the equation: c1v1+c2v2+c3v3 = v 
//for real numbers c1,c2,c3. 
// Create a 3x3 matrix for v1,v2,v3 
val A = DenseMatrix(arrayOf
       (doubleArrayOf(3.0, -5.0, 1.0),
        doubleArrayOf(-1.0, 0.0, 7.0), 
        doubleArrayOf(2.0, 1.0, -4.0))) 
//now we need to find whether there exists a solution for Ac=v where c = {c1, c2, c3} 
//and if yes, print [c1, c2, c3] 
// Create a solver for linear system 
val precision = 1e-15 
val solver = LinearSystemSolver(precision) 
// Solve for Ac 
val soln = solver.solve(A) 
// solution for Ac = v 
val c = soln.getParticularSolution(v) 
println("soln is: $c") 
// verification 
val Ac = A.multiply(c) 
// Ac = v 
println("Ac = $Ac, same as $v")

soln is: [4.000000, -1.000000, 2.000000]

Ac = [19.000000, 10.000000, -1.000000] , same as [19.000000, 10.000000, -1.000000]