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.

Vector

A vector in is a column of real numbers. These real numbers are called the components or entries of the vector.

Ex: is a vector in

**Fig 1: Vector **

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

A vector in , say , is drawn as an arrow pointing from the first component in to the second component in .

Norm of a vector: The norm of a vector ∈ 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 ’s components. The norm of a vector is represented as . 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)
print(norm)

Vector Operations

Let us get familiar with the fundamental vector operations:

- Vector addition (addition of two vectors), and
- 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**

` ````
```%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)
println(vec_sum)

**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, with a real number, is written as:

**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)
println(B)

**Note:**

- Multiplying a vector by a positive real number k preserves its direction and multiplies its norm by k.
- Multiplying a vector by a negative real reverses its direction and multiplies its norm by k.
- 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

` ````
```%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)
//Subtracting
val sol = B.minus(n)
println(sol)

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 . The linear combination of these vectors is expressed as:

where are real numbers.

These ‘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))
println(sol)

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.

Span

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: is said to be in the span of the vector-list if:

where ∈ .

Let us solve a problem to improve our understanding.

**Q: Determine whether the vector lies in the span of set of vectors: .**

**Approach:**

Let us first assume the equation: , where are real numbers and try to find the solution 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]