What a vector is
A vector is an ordered list of numbers. Each number in the list is a component. The order matters: each position has its own meaning.
Walk 3 km east, then 1 km north. The vector records the walk: its first component is the distance east and its second the distance north, both in km. The list is a different walk, 1 km east and 3 km north.
Vectors are named with bold lower-case letters and written as columns:
In running text, means this column. The components of are and : the small number says which position.
An ordinary number, such as 3 or , is called a scalar, to tell it apart from a vector. The name comes from scaling, later in this lesson.
Two components, or n
stands for the real numbers. is the set of all vectors with two real components, and is the set of all vectors with components.
A shop that sells four products can record its stock as a vector in . The vector says: 12 of the first product, none of the second, 7 of the third and 30 of the fourth. Each position always stands for the same product.
Two vectors are equal when they have the same number of components and match position by position. So .
Rule: A vector
An ordered list of numbers, written as a column: a vector in . The component is the number in position , and each position has its own meaning and units.
A vector as an arrow
In the plane, draw as an arrow from the origin to the point . Its components are its run and rise, as in lesson 1.2.1: 3 across, then 1 up. A negative component steps left or down, so goes 2 left and 3 up.
An arrow records a move, not a place, so it can start anywhere. Drawn from , the vector ends at ; it still moves 3 across and 1 up. Every copy with the same steps is the same vector.
A vector's components are its steps across and up; a negative component steps left or down. The same vector can start anywhere: each copy of moves 3 across and 1 up.
Drawn from the origin, the tip of is the point with the same coordinates, . That is why a vector and a point are often written the same way. In the arrows live in space; beyond three components there is no picture, but the arithmetic below works the same.
Adding vectors
To add two vectors, add the components in matching positions. For and :
As walks: 3 east and 1 north, then 1 east and 2 north, ends 4 east and 3 north of the start. Only vectors with the same number of components can be added; has no meaning.
In the picture, start where ends: tip to tail. The sum is the arrow from the start of to the end of . Walking first and then reaches the same corner, so the two routes form a parallelogram, and .
Rule: Adding vectors
Add matching components. In the plane, the sum is the far corner of the parallelogram on and . Lesson 1.7.2 added complex numbers the same way, part by part.
Drag the tips of and , and check that each component of is a sum. Then turn to point almost opposite to : the parallelogram flattens, and the sum can come out shorter than itself. The lengths in the readout come in lesson 3.1.2.
Explore
Drag the tips of u and v. Their sum u + v is the far corner of the parallelogram they make: walk along u, then along v.
- u
- (3, 1), length 3.1623
- v
- (1, 2), length 2.2361
- u + v
- (3 + 1, 1 + 2) = (4, 3)
In there is no picture, but the rule is the same. The shop above receives a delivery , and adds it to its stock one product at a time:
| product | 1 | 2 | 3 | 4 |
|---|---|---|---|---|
| stock | 12 | 0 | 7 | 30 |
| delivery | 6 | 10 | 0 | 12 |
| 18 | 10 | 7 | 42 |
Try it
np.array turns a list into a NumPy array, which adds component by component. The first line prints [4 3] twice: the order of a sum does not matter.
Python lists are different: + joins two lists end to end. The last lines show that, and the error NumPy gives for vectors of different sizes.
Scaling a vector
To multiply a vector by a scalar , multiply every component by . This is called scaling. For , and .
Every multiple of lies on the line through the origin and . What does depends on its size and its sign:
- stretches. points the same way and is twice as long.
- shrinks. is half as long.
- turns the arrow round. points the opposite way. is turned round and twice as long.
- gives .
Scaling by 2, , and 0. Every multiple stays on the dotted line through the origin and .
The vector whose components are all 0 is the zero vector, written . It is an arrow of length 0 with no direction, just the origin. Adding it changes nothing: .
Rule: Scaling
Multiply every component by the scalar . The arrow keeps its line; a negative turns it round. Lesson 3.1.2 shows that its length is multiplied by , the size of (lesson 0.3.3).
For the shop, three weeks of the same delivery is .
Try it
Multiplying a NumPy array by a number scales every component. 0.5 * u prints [1.5 0.5]: the components have become decimals.
A list multiplied by 2 is repeated instead: 2 * [3, 1] is [3, 1, 3, 1].
Subtracting vectors
Subtract component by component too: . It is the same as adding times : .
In the picture, is the arrow from the tip of to the tip of . Walk , then , and you arrive at : .
The order matters. is the same arrow turned round, from the tip of to the tip of . And .
Rule: Subtracting vectors
Subtract matching components, . In the plane, is the arrow from the tip of to the tip of .
runs from the tip of to the tip of ; dashed, the same arrow drawn from the origin. The combination walks twice, then , and ends at .
A week of sales leaves the shop with . The 0 says the third product has sold out.
Try it
The first line prints , and . The second checks that lands on .
Linear combinations
Scaling and adding together build the most important object in linear algebra. With scalars and , the vector is a linear combination of and .
With and , . Tip to tail, that is two steps of and one of , as in the second picture above. With and , .
Rule: Linear combination
Scale each vector by its own scalar, then add. Any number of vectors combine the same way: , where each bold is a whole vector.
A fruit shop packs two kinds of box. Box A holds 2 apples, 1 pear and no lemons; box B holds 1 apple, 3 pears and 2 lemons. As vectors in , and .
An order of 4 box A and 2 box B holds :
| apples | pears | lemons | |
|---|---|---|---|
| 8 | 4 | 0 | |
| 2 | 6 | 4 | |
| 10 | 10 | 4 |
In symbols, one component at a time: the apples are , the pears and the lemons .
Which points can combinations reach?
The point is . In fact every point of the plane is some combination , because and point in different directions. If were a multiple of , every combination would stay on the line through .
Module 3.3 finds and for any target, and lesson 3.5.1 names the set of all combinations. Lesson 3.2.1 shows that a matrix times a vector is a linear combination of the matrix's columns.
Try it
The first line prints the three combinations of and above: , and .
The second line is the fruit order, .
The rules of arithmetic carry over
Each component is an ordinary number, so the usual rules of arithmetic hold for vectors too. For vectors , , and scalars , :
- , the parallelogram
- and
- and
So vector expressions expand and collect like ordinary algebra: .
Try it
Each line computes both sides of one rule, with . The last line checks the expanded expression: both sides are .
Vectors in NumPy
A NumPy array is the standard way to hold a vector in Python. Three details matter from the start.
- Positions count from 0. The first component is
u[0], and isu[1]. len(u)is the number of components . NumPy also reports the shape,(2,)for a vector in .- A number plus an array adds to every component.
u + 1gives[4 2]. It is a NumPy shortcut; in maths you would write .
Try it
The first line prints , , the number of components and the shape. The last line reads the third component of the shop's stock, s[2], which is 7.
Common mistakes
- The order of components matters. and are different vectors: 3 east and 1 north is not 1 east and 3 north.
- Only vectors of the same size add. has no meaning, and NumPy raises
ValueError. - Python lists are not vectors.
[3, 1] + [1, 2]joins them into[3, 1, 1, 2], and2 * [3, 1]repeats them. Usenp.array. - A number plus a vector is not defined in maths. NumPy's
u + 1adds 1 to every component; in maths, write . - Subtract in the right order. runs from the tip of to the tip of ; points the other way.
- A negative scalar turns the arrow round. is twice as long as , not shorter; it points the opposite way.
- NumPy counts from 0. The first component is
u[0].
Vectors and their arithmetic
0 of 12 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Summary card
- A vector is an ordered list of numbers, its components, written as a column: in . An ordinary number is a scalar.
- In the plane, a vector is an arrow, across and up. The same arrow can start anywhere; drawn from the origin, its tip is the point .
- Add and subtract component by component. goes tip to tail, to the far corner of the parallelogram; runs from the tip of to the tip of .
- scales every component: stretches, shrinks, turns round, and gives the zero vector .
- A linear combination scales each vector, then adds.
- In NumPy,
+,-andc * uwork component by component on arrays; Python lists join and repeat instead.