CS47 : Which way are you pointing?


Vectors - it's a bit maths (and only for AQA).

We are learning ...
  • About vector representation
  • About vector operations and applications
So that we can ...
  • Be familiar with the concept of a vector and how to specify them
    - Dictionary representation
    - List representation
    - 1-D array representation
    - Visualising a vector as an arrow
  • Perform vector addition and scalar-vector multiplication
  • Perform convex combination of two vectors, u and v
  • Perform dot or scalar product of two vectors
    - Application of dot product



Activity 1 What is a vector and what is not?

Data structures are fickle. They are what they are, or rather what they need to be. If a particular data structure represents or models a system which behaves like one thing, then the data structure represents that thing. It's your choice as a programmer which 'container' you wrap around your data depending on what you are required to do with it. The operations which are available to the data structure make it behave in different ways.

Vectors find application in all sorts of fields. You might have met them in maths or physics where they represent a point in space, a force or a direction of travel. They also find application in navigation, weather prediction, engineering, computer science, game design and economics.

The strange thing is that they look like regular arrays / lists but they don't behave like them.

https://en.wikipedia.org/wiki/Vector
Click me for more!

When we write vectors, they are always represented by a letter with an arrow over the top. For instance ...
A vector is a one-dimensional dynamic array (the computer scientists view)

Here is a list ...
  • As far as we (the programmer) are concerned the term vector and array are basically the same thing (unless you program in a language with native vector support) and can be used interchangeably when programming.
  • Classically, 'arrays' are defined as fixed size (i.e. static) areas of memory whereas vectors are 'containers' which can change in magnitude, growing and shrinking as required.
  • Both arrays and vectors are homogeneous - they can contain only one data type.
  • Vector data structures are always one dimensional. A zero-dimensional arrays is called a scalar and a multi-dimensional array is often referred to as a matrix.



Vector data structures also have different built in methods to a standard array so we can perform special vector operations on them. Before we learn about them, let's have a go at making some vectors.

Task 1.1
Representing vectors



Remember that a vector looks like a one-dimensional array, but it behaves differently due either to built in methods of a vector object or due to bespoke methods which we can write to handle the vector object natively. 

Get ready to code

Open up the Python programming environment, IDLE.

At the prompt

We are going to use a third party library called
Numpy to create our Python vectors. To check if you have Numpy installed, type ...

>>> import numpy as np

... and if you don't get an error, you are good to go! If you do get an error, ask your teacher to install Numpy for you - better not try it yourself because it's a bit tricky (and depends on the version of Python you use). Let's start by creating a normal Python list ...

>>> u = [6,12,8]

Technically, the Python list, u, is neither an array nor a vector since is shares some of the properties of both and some of the properties of neither(!) Python lists are dynamic - can grow and shrink in size (vectors can but arrays cannot) and can store mixed data types (neither arrays nor vectors can).

>>> v = np.array([6,12,8])

The Numpy array, v, behaves like a vector (but it's called an array - go figure!) It is actually a container which allows the enclosed list to be manipulated according to vector arithmetic. However, it is unlike a vector in that it can hold mixed data types.


This particular Numpy array can be described as a 3-Vector over ℤ (or ℤ3) because it consists of three elements of type ℤ (the set of integers). We refer to this as the vector's dimension. In this case, a three-dimensional vector is represented by a one-dimensional list (notice the conflict in the use of the term 'dimension'!)

>>> w = np.array([4.3,5.5,6.0,7.2])

This Numpy vector can be described as a 4-Vector over ℝ (or ℝ4) because, you've guessed it, it consists of four elements of type ℝ (the set of reals). This is a four-dimensional vector (difficult for humans to conceptualise, but a vector nonetheless!)

Just like lists, we can access elements from our vectors using definite iteration.

>>> for coord in u : print(coord)
>>> for coord in v : print(coord)
>>> for coord in w : print(coord)

The elements in a vector are ranked with an integer which represents the number of elements before the current element. In a standard vector, there is an implied rank (the element's index in the list) which allows us to access any element from the vector using standard list indexing.

>>> u[0]
>>> v[0]
>>> w[0]

If we were using a language which did not have a native vector type, we could use a dictionary to enforce the ranking using explicit key:value pairs.

>>> x = {0:3.4, 1:2.2, 2:6.1, 3:9.9}

Then we could access the elements in the same way.

>>> x[0]

However, we would not benefit from the specialised vector operations which come with a native vector object and would have to code bespoke methods for our 'dictionary vector'.

Representing the vector like a mathematician

Finally, we could also represent a vector as a function, f : S → ℝ which maps (↦) a set of indices, S = {0,1,2,3} called the domain to a set of, say, real numbers (ℝ) in the co-domain, {3.4, 2.2, 6.1, 9.9} ...

0 ↦ 3.4
1 ↦ 2.2
2 ↦ 6.1
3 ↦ 9.9

... but now we are getting a bit too mathematical!

In your notebooks

Write about what happened. Pay particular attention to the three different ways of creating vectors (using a native vector data type, a dictionary and a function) and make some notes on each.

OUTCOME : Notes on different ways of representing / creating vectors


A vector is an object with both direction and magnitude (the physicists view)

In our 3 dimensional, Euclidean world, we can represent vectors in a vector space which have both a magnitude and a direction (a Euclidean vector). Representing vectors as arrows in this way is familiar to us in our studies in physics and coordinate geometry.

Task 1.2
 Khan Academy

Create a Khan Academy account

If you haven't yet created an account on Khan Academy, you should do it, right now! The course on linear algebra and matrices contains a fantastic series of videos which explains what a vector is and how they represent a physical vector space and Sal Khan explains things much better than I ever could! Notice that in the video, Sal draws the vectors using a symbol with an arrow over the top ...
In your notebooks

Take some notes whilst you watch the videos.

Vector introduction for linear algebra (5:48)

Real coordinate spaces (6:20)

OUTCOME : Notes about vectors, from the videos.


Length of a vector

The length of a vector (the distance between the vector space origin (0,0) and the vector) is simply calculated using good ole' Pythagorean theorem. Nice! The length of the vector is also known as it's magnitude or norm and is denoted by the double double vertical bar symbol (see below, in the task).


Good Old Pythagoras! He's a lad.

Task 1.3
 Vector length

On the worksheet

Download and print the worksheet 'Vector length' and use the following vector format to complete the sheet. Print out the sheet and hand it in for assessment.


OUTCOME : A bunch of correct answers to a bunch of easy questions!


Vector as member of a vector space (the mathematicians view)

Everything that physics calls a vector is also a vector in mathematics. But there are things that mathematicians call vectors which physicists wouldn't. Nevertheless, all vectors occupy a vector space of one or more dimensions. In the mathematical sense, the elements of the vector do not necessarily correspond to positions in our 3D Euclidean vector space (i.e. do not represent magnitude and direction) but they still behave mathematically in the same way.

Vectors in the mathematical sense are more abstract.

Task 1.4
 And the difference is ...

In your notebooks

Explain the difference between a physicists view of a vector and a mathematicians view. You may need to go back and re-read some of your notes to help you.

OUTCOME : Understanding of different views of vectors.


Activity 2 Vector operations 

The whole point of vectors is that they have particular mathematical behaviour ...
  • Vector addition achieves translation (moving the vector in it's vector space)
  • Scalar multiplication achieves scaling (increasing the 'size' of the vector in it's vector space)
  • Dot / scalar product (used to determine the angle between two vectors)
  • Convex combination (used to draw the minimum bounding polyhedron between vectors viewed as points).

Vector addition

OK - here is another video ('cause Sal Khan explains it so much better than me!) from Khan Academy. Whilst you are watching the video, take some notes in your notebooks.

Adding vectors (7:11)



Python doesn't come with any vector operations built in. If you create two lists with Python and add them together, even though they look like vectors, they do not behave like vectors. Consider the following task ...

Task 2.1
Vector addition in Python

Evidence what you do in this first part of the task through a combination of screenshots and written explanation in a word processed document.



Get ready to code

Open up the Python programming environment, IDLE.

Investigating the behaviour of normal Python lists

Create the following two lists (which look like vectors,
but aren't) by typing the following commands and pressing the  ENTER  key each time.

>>> a = [1,2,3]
>>> b = [4,5,6]

Now add them together ...

>>> a + b

You should see that Python has concatenated the lists. It has not treated them like vectors.

Investigating the operation of Numpy vectors

You can
only carry out the following tasks if you have installed Numpy so do that first. Type the following commands at the prompt, pressing the  ENTER  key after each one.

>>> import numpy as np
>>> a = np.array([1,2,3])
>>> b = np.array([4,5,6])

Now add them together ...

>>> a + b

Python (with a little help from Numpy) has performed vector addition!

In your notebooks

Write about what you have found out through this activity.


On the worksheet

Download and complete the worksheet 'Vector addition'.

OUTCOME : Evidence of the difference between list addition and vector addition.


Scalar multiplication

One more video by Sal! You have to watch the video carefully, although I truly believe that this man is legend and you really won't even realise you are listening, or learning. He repeats himself a lot, but that's good!

Multiplying a vector by a scalar (5:43)

Better had then!



Task 2.2
 Scalar multiplication

Once again, we can use Python to demonstrate the difference between standard list multiplication and vector multiplication. Evidence the following work using a combination of screenshots and written explanations.


Get ready to code

Open up the Python programming environment, IDLE. Make sure you have Numpy installed.

At the prompt

Type the following commands at the prompt, pressing the  ENTER  key after each one.

>>> import numpy as np
>>> a = [1,2,3]
>>> b = np.array([1,2,3])
>>> a * 3
>>> b * 3

In your notebooks

Write about what happened, the difference between list multiplication and vector multiplication and the uses for scalar multiplication. What does it actually do?

On the worksheet

Download and complete the worksheet 'Scalar vector multiplication'.

OUTCOME : Explanation of scalar multiplication


Dot / scalar product (algebraic and geometric)

The dot / scalar product of two vectors can, amongst other things, be used to ...
  • Determine an scalar product of two vectors;
  • Determine whether two vectors are parallel;
  • Find the angle between two vectors.
The algebraic dot / scalar product is calculated by finding the sum of all the products of the like terms in the vectors concerned, shown algebraically like this ...
 

The algebraic dot / scalar product always results in a scalar. The value of the scalar is more often used to find the angle between the two vectors but can have uses of it's own. The dot product is a measure of 'how much' of two vectors point in the same direction - how well they are aligned or the component of one vector that acts in the same direction as another. The dot product could be used in the following situations ...
  • Mechanical work is the dot product of force and displacement vectors (physics);
  • Fluid flow through a metal ring / magnetic flux (physics);
  • Calculating the resultant cost of my shopping (not physics).

Task 2.3
 Algebraic dot product

STAGE ONE

Download and complete the worksheet Algebraic dot product.docx.

STAGE TWO

We could really do with having Python do all this work for us because it's tedious! You will need to Numpy library installed in Python to carry out these tasks so make sure you have that before you carry on. If you haven't got Numpy installed, ask your teacher to help you.


  • Open up the Python programming environment, IDLE.

  • Again, as we have done before, we need to make a couple of vectors ...

    >>> import numpy as np
    >>> a = np.array([4,2,4])
    >>> b = np.array([2,3,4])

  • ... and then use Numpy to calculate the algebraic dot product.

    >>> np.dot(a,b)

  • Now self assess your answers to the worksheet Algebraic dot product.docx using Numpy!

STAGE THREE (Optional, except if you are studying physics or shopping)

Let's move away from the abstract and look at the concrete.


  • Open up the Python programming environment, IDLE.

  • Work done

    Using Python, let's calculate the work done when an object subject to a force moves through a two dimensional vector space. Consider the following situation where a force, F, moves an object from position A to position B in a two-dimensional vector space.


    At the prompt : First, let's define our two position vectors, A and B and use Numpy to calculate the displacement vector, D ...

    >>> A = np.array([7,5])
    >>> B = np.array([3,11])
    >>> D = B-A

    The force vector, F is simply defined as ...

    >>> F = np.array([-1,5])

    ... and the work done (in Joules) as the dot product, F.D

    >>> np.dot(F,D)

    In your notebooks : Now calculate the work done in the following situation. Use Python and Numpy to help you and show your working, including sketches of the vector space. You can use Graph paper.pdf to help.

    1) A body moves from point (1,4,3) to point (2,2,2) and is subject to a force vector (4,4,4).
    2) A body moves from point (5,12,4) to point (0,0,0) and is subject to a force vector (1,1,1).
    3) A body moves from point (7,7,7) to point (7,7,7) and is subject to a force vector (12,8,4).

  • Fluid flow

    This is an application to fluid dynamics. Consider a wire ring suspended in a flow of fluid. The surface area of the ring can be represented by a surface vector (SV) whose direction represents the angle that the ring is tilted and whose magnitude is proportional to the surface area. If this surface vector is subject to a flow vector (FV), we can use the dot product to calculate the flow rate of fluid through the ring.


    At the prompt : Again, we can create two vectors using Numpy to help us ...

    >>> SV = np.array([-3,5])
    >>> FV = np.array([0,12])

    ... and use Numpy to calculate the fluid flow rate

    >>> np.dot(SV,FV)

    Notice that if I rotate the ring towards the direction of the fluid flow, as long as I maintain the same magnitude of SV (because I do not change the size of the ring), the value of the dot product (flow rate) will increase.



    At the prompt : Try recalculating the flow rate in this situation now (using the new values of the vectors SV and FV) and confirm that you are happy that the value of the dot product (and hence the flow rate) has increased.

    In your notebooks : Calculate the flow rate in the following situations. Use Python, Numpy and Pythagoras to help you and show all your working, including sketches of the vector space. You might want to use Graph paper.pdf to help you to do this.

    1) Consider rotating the metal ring anticlockwise so that the x component of SV is -5. Use Pythagoras theorem and the original SV (-3,5) to calculate the magnitude of the SV and hence the y component of this new position. Then use the dot product to calculate the new fluid flow rate.
    2) Consider rotating the ring so that it is edge on to the fluid flow. What would be the value of SV now? Use this value to calculate the fluid flow rate - does your answer seem sensible?
    3) Generate a different problem where the SV has a smaller magnitude (and hence the ring has a smaller surface area) and calculate the fluid flow now. Again, does your answer seem sensible?

  • Shopping list

    Lastly, let's do some shopping. For this, we need to use a dictionary representation of a vector (we saw this in an earlier section) and hence, we can't really use Numpy to help us. I'm going to show you how to set up your vector but I'm not going to show you how to manually perform the dot product - I'm going to let you do that yourself!

    In a script : Set up a vector stock list like this in your script ...

    stock = {
             "bread"   : 1.32,

             "cheese"  : 2.55,
             "milk"    : 0.85,
             "jam"     : 3.48,
             "sausage" : 2.77
            }


    (This may seem a little strange when you consider that dictionary vectors have a 'rank', rather than a key.)

    Now, here is my shopping list ...

    shopping = {
                "jam"     : 1,
                "milk"    : 3,
                "sausage" : 1
               }

    I bet you can see how to calculate the total cost of my shopping can't you! Write yourself a script which calculates the total cost of the shopping. Can you see anything slightly 'non-vector' like about these lists? Tell your teacher if you can!

OUTCOME : Practical applications of the dot product.


Did you see anything interesting in the results of the exercise? You should have found that some of the dot products you calculated were positive, some were negative and some were zero and the value changes depending on the angle between the vectors. The magnitude of the dot product clearly depends on the angle to which the vectors are aligned.


Geometrically, therefore, we can say that ...
  • Dot product > 0 indicates an acute angle between the vectors;
  • Dot product = 0 indicates the vectors are orthogonal (at right angles).
  • Dot product < 0 indicates an obtuse angle between the vectors;


We can also work out the actual angle between the vectors as well. If you are interested, it can be shown that the dot product of two vectors is related to the angle between the vectors by the following expression ...


But what does this mean and how do I use it? Well ...

Task 2.4
 Geometric dot product

Guess what? Download and complete the worksheet Geometric dot product.docx. Boom!

OUTCOME : Completed worksheet



Convex combinations

Finding the points on, inside or outside a line which lies between two vectors. This finds applications in the generation of a 'convex hull' - the smallest polyhedra that contains all the points in a set of points. 


This really is getting complicated, I wonder whether one final worksheet will help ...

Task 2.5
 Combinations of the convex kind.

Really, another one? Download and complete the worksheet Convex combinations.docx.

OUTCOME : Completed worksheet.


Convex combinations (or convex hulls as they generate) can be used in games programming and route finding.

http://theory.stanford.edu/~amitp/GameProgramming/AStarComparison.html
Check for a concave object before you enter it!
Click the image for more details ...

Extension Activities 

How about these?
  • Using vector maths in game design from Unity3D. This includes details on a different type of vector product (the cross product) and it's application to game design.

  • I would really suggest that if you are interested, you watch the Vectors and Spaces course on Khan Academy. It will explain lots of the derivations of different vector algebra rules like Dot Product.

  • There is one other type of vector operation that we haven't covered which is called the cross-product. Do some research and find out how it is calculated and what it can be used for (the Unity3D video is helpful here).

What's next?

Before you hand your book in for checking, make sure you have completed all the work required and that your book is tidy and organised. Your book will be checked to make sure it is complete and you will be given a spicy grade for effort.

END OF TOPIC ASSESSMENT