Welcome to the World of Vectors!
In this chapter, we are going to explore Vectors. While you might have heard of vectors in Physics or Maths, in Computer Science, they are a powerful way to store and manipulate data. Whether it's representing a point in a 3D game, processing image pixels, or even how Netflix recommends movies to you, vectors are working behind the scenes.
Don't worry if you find the mathematical side a bit daunting at first—we will break it down step-by-step with simple analogies!
1. What is a Vector?
At its simplest, a vector is just a list of numbers. In Computer Science, we treat a vector as an Abstract Data Type (ADT). To be a proper vector, all the numbers inside it must come from the same "field," such as the set of Real Numbers (symbolized by \( \mathbb{R} \)).
Ways to Write a Vector
There are three main ways the AQA syllabus expects you to recognize a vector:
- As a List: A simple collection of numbers, e.g., \( [2.0, 3.14, -1.0, 2.71] \).
- Mathematical Notation: If a vector has 4 numbers and they are all Real numbers, we say it is a 4-vector over \( \mathbb{R} \), written as \( \mathbb{R}^4 \).
- Function Interpretation: We can think of a vector as a function that maps an index to a value.
Example of Function Interpretation:
If we have the vector \( [5.0, 12.0] \):
0 \( \mapsto \) 5.0 (The first position maps to 5.0)
1 \( \mapsto \) 12.0 (The second position maps to 12.0)
The symbol \( \mapsto \) simply means "maps to".
Quick Review: A vector is just a list of numbers from the same set (like Reals). If it has 3 numbers, it's a 3-vector.
2. Representing Vectors in Code
Depending on which programming language you use, you can represent a vector in different ways:
List or 1-D Array
This is the most common way. In Python, you'd use a list: [2.0, 3.0]. In VB.Net, you might declare a 1-D array: Dim example(3) As Single.
Dictionary
A dictionary is useful if you want to use the "function" approach.
Python Example: {0: 2.0, 1: 3.14, 2: -1.0}
Here, the key is the index (0, 1, 2) and the value is the actual number in the vector.
Did you know? Dictionaries are great for "sparse" vectors—vectors where most of the numbers are zero. You only store the positions that actually have a value!
3. Visualising Vectors
For A Level Computer Science, you need to be able to visualise a 2-vector (like [2.0, 3.0]) as an arrow on a graph.
- The tail of the arrow starts at the origin (0,0).
- The head of the arrow points to the coordinates (x, y).
Analogy: Think of a vector like a set of instructions for a pirate map. "[2, 3]" means "Walk 2 steps East and 3 steps North." The arrow shows the direct path from where you started to where you ended up.
4. Vector Operations
We can do "maths" with vectors to change what they represent. There are two main operations you must know:
Vector Addition (Translation)
To add two vectors, you just add the numbers in the same positions together.
If \( u = [1, 5] \) and \( v = [2, 1] \), then \( u + v = [1+2, 5+1] = [3, 6] \).
Key Concept: In computer graphics, vector addition is used for Translation. This is just a fancy word for moving an object from one place to another without turning it.
Scalar-Vector Multiplication (Scaling)
A scalar is just a single number (not a list). To multiply a vector by a scalar, you multiply every number in the list by that scalar.
If \( k = 2 \) and \( v = [3, 4] \), then \( 2 \times v = [6, 8] \).
Key Concept: This is used for Scaling. It makes the "arrow" longer or shorter. If you multiply by 2, you are "zooming in" or doubling the size of the movement.
Takeaway: Addition = Moving. Scalar Multiplication = Resizing.
5. Convex Combination of Vectors
This sounds complicated, but it's basically a way of finding a point that lies somewhere between two other points.
A convex combination of two vectors \( u \) and \( v \) is written as:
\( \alpha u + \beta v \)
But there are two strict rules:
1. \( \alpha \) and \( \beta \) must be 0 or higher.
2. \( \alpha + \beta \) must equal exactly 1.
Analogy: Imagine mixing paint. If \( u \) is Red paint and \( v \) is Blue paint, a convex combination is any color you can make by mixing them (like Purple). You can't use "negative" paint, and your total mixture must add up to 100% (which is 1).
6. The Dot Product (Scalar Product)
The Dot Product is a way of multiplying two vectors to get a single number as the result.
How to calculate it:
Multiply the corresponding items and then add them all up.
If \( u = [u_1, u_2] \) and \( v = [v_1, v_2] \), then:
\( u \cdot v = (u_1 \times v_1) + (u_2 \times v_2) \)
Step-by-Step Example:
Find the dot product of \( [2, 3] \) and \( [4, 5] \):
1. Multiply the first pair: \( 2 \times 4 = 8 \)
2. Multiply the second pair: \( 3 \times 5 = 15 \)
3. Add them together: \( 8 + 15 = 23 \)
The Dot Product is 23.
Why do we use it?
The most important application for your exam is finding the angle between two vectors.
- If the dot product is 0, the vectors are at a 90-degree angle (perpendicular).
- It helps computers understand how "aligned" two pieces of data are.
Common Mistake to Avoid: Students often try to give a vector as the answer to a dot product. Remember: The result of a dot product is always a single number (a scalar), not a list!
Quick Review Table
Vector Addition: Result is a Vector. Used for moving (translation).
Scalar Multiplication: Result is a Vector. Used for resizing (scaling).
Dot Product: Result is a Number. Used for finding angles.
Convex Combination: Result is a Vector. Used for finding points in between.
Key Takeaway
Vectors are more than just lists; they are the language of space and relationship in Computer Science. If you can add them, multiply them by a single number, and calculate their dot product, you have mastered the core of this chapter!