You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson covers vectors as a data structure and mathematical concept, as specified in the OCR A-Level Computer Science (H446) specification, section 1.4. Vectors are used in graphics, physics simulations, machine learning, and many other areas of computing.
A vector is a mathematical object that has both magnitude (size) and direction. In computing, vectors are represented as ordered lists of numbers.
| Concept | Description | Example |
|---|---|---|
| Scalar | A single number (magnitude only) | Temperature: 20, Speed: 5 |
| Vector | An ordered list of numbers (magnitude and direction) | Position: (3, 4), Velocity: (2, -1, 5) |
Vectors can be represented as lists or arrays of numbers. A vector with n components is called an n-dimensional vector.
2D vector: v = (3, 4) -- 2 components
3D vector: w = (1, -2, 5) -- 3 components
4D vector: u = (1, 0, 3, 7) -- 4 components
A vector can also be thought of as a function that maps indices to values:
# Representing vectors as lists
v = [3, 4]
w = [1, -2, 5]
# Accessing components
print(v[0]) # Output: 3
print(w[2]) # Output: 5
Two vectors of the same dimension can be added by adding their corresponding components.
Formula: If u = (u1, u2, ..., un) and v = (v1, v2, ..., vn), then:
u + v = (u1 + v1, u2 + v2, ..., un + vn)
u = (3, 4, 1) and v = (1, -2, 5)
u + v = (3+1, 4+(-2), 1+5) = (4, 2, 6)
def vector_add(u, v):
return [u[i] + v[i] for i in range(len(u))]
u = [3, 4, 1]
v = [1, -2, 5]
print(vector_add(u, v)) # Output: [4, 2, 6]
Adding two 2D vectors is like following one displacement then the other. If you walk 3 steps east and 4 steps north (3, 4), then 1 step east and 2 steps south (1, -2), your total displacement is (4, 2).
Multiplying a vector by a scalar (a single number) scales each component.
Formula: If k is a scalar and v = (v1, v2, ..., vn), then:
kv = (k * v1, k * v2, ..., k * vn)
k = 3, v = (2, -1, 4)
3v = (32, 3(-1), 3*4) = (6, -3, 12)
If k > 1, the vector gets longer. If 0 < k < 1, it gets shorter. If k < 0, the direction reverses.
def scalar_multiply(k, v):
return [k * v[i] for i in range(len(v))]
v = [2, -1, 4]
print(scalar_multiply(3, v)) # Output: [6, -3, 12]
print(scalar_multiply(0.5, v)) # Output: [1.0, -0.5, 2.0]
The dot product of two vectors produces a scalar (a single number). It measures how aligned two vectors are.
Formula: If u = (u1, u2, ..., un) and v = (v1, v2, ..., vn), then:
u . v = u1v1 + u2v2 + ... + un*vn
u = (3, 4) and v = (2, -1)
u . v = (32) + (4(-1)) = 6 + (-4) = 2
| Dot Product Value | Meaning |
|---|---|
| Positive | Vectors point in roughly the same direction |
| Zero | Vectors are perpendicular (at right angles) |
| Negative | Vectors point in roughly opposite directions |
def dot_product(u, v):
return sum(u[i] * v[i] for i in range(len(u)))
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.