mathlinear algebra
vectors
arrows you add and stretch, and the dot product that gives length, angle and projection.
a vector is an arrow, a length and a direction, and it's also two numbers, how far across and how far up. add arrows head to tail, stretch them with a number, and the dot product turns two arrows into one number that says how much they point the same way.
- add
- (x, y) + (u, v) = (x + u, y + v) head to tail on the grid, part by part on paper
- scale
- k·(x, y) = (kx, ky) k < 0 flips the arrow
- length
- |a| = √(x² + y²) pythagoras on the two parts
- dot product
- xu + yv = |a| |b| cos θ 0 means perpendicular. negative means past 90°
- projection
- (a · b / |b|²) b a's shadow on b, a · b / |b| long
- signed area
- xv − yu the parallelogram a and b span. the determinant on the matrices page
- |a + b|
- ≤ |a| + |b| lengths don't add unless the arrows point the same way
- the zero vector
- (0, 0) length 0 and no direction. normalizing it divides by 0
- a position, a velocity, a force: anything with a size and a direction
- how much two things point the same way: lighting, similarity, work
- the columns of a matrix, on the next page
the idea
a vector is an arrow. it has a length and a direction, and that’s all: slide it anywhere on the page and it’s the same vector. a velocity is one, a force is one, the step from one point to another is one. to work with it on paper, write down how far it goes across and how far up: goes 3 to the right and 1 up. the two numbers are its components, and the arrow and the pair of numbers are the same thing seen two ways.
everything on this page is one of a few moves on arrows: add two, stretch one by a number, measure one, and multiply two into a single number, the dot product. the first three are in the movie.
an animation on a grid from x = −3.5 to 6.5 and y = −3.5 to 4.5. first a = (3, 1) in cyan grows from the origin, with its two parts drawn under it: 3 across, 1 up, and the readout |a| = √10 ≈ 3.16. then b = (1, 3) in amber appears at the origin and slides to the tip of a, and a + b = (4, 4) is drawn in white from the origin, 4√2 ≈ 5.66 long. third, b from the origin and a copy of a from its tip close a parallelogram with area 3·3 − 1·1 = 8. fourth, a alone is stretched to 2a = (6, 2) and then shrunk through 0 to −a = (−3, −1), along a dotted line through the origin. fifth, −b hangs off the tip of a and lands on a − b = (2, −2), the arrow from the tip of b to the tip of a. last, 2a − b: a stretched to (6, 2), −b from its tip, and the white result (5, −1).
explore
drag the tips. the white arrow is , and the dashed copy of hanging off the tip of is the head-to-tail route to it. the gray bar on the line of is the shadow of on : the part of that points along . watch the dot product as the angle passes 90°.
drag a tip, or tab to it and use the arrow keys: 0.5 a step, 5 with shift
- a
- (3, 1), |a| = √10 ≈ 3.16
- b
- (1, 3), |b| = √10 ≈ 3.16
- a + b
- (4, 4), |a + b| = √32 ≈ 5.66
- a · b, part by part
- 3·1 + 1·3 = 6
- |a| |b| cos θ
- 3.16 · 3.16 · 0.60 = 6.00
- θ, the angle between
- 53.1°
- shadow of a on b
- (0.60, 1.80), 1.90 long
- signed area
- 3·3 − 1·1 = 8
a · b is positive: the angle is under 90°, and the shadow of a falls along b, 1.90 long.
a = (3, 1) in cyan and b = (1, 3) in amber from the origin, on a grid from −6 to 6 each way, with a + b = (4, 4) in white, a dashed copy of b from the tip of a, the angle between them marked with an arc, and the shadow of a on b as a gray bar along b's line. drag either tip on a half-unit grid, or tab to it and use the arrow keys, and the readout follows: |a| = |b| = √10 ≈ 3.16 to start, |a + b| = √32 ≈ 5.66, a · b = 3·1 + 1·3 = 6, the same as |a| |b| cos θ = 3.16 · 3.16 · 0.60, θ ≈ 53.1°, the shadow (0.6, 1.8), 1.90 long, and the signed area 3·3 − 1·1 = 8. when the tips are at right angles a · b reads 0 and the shadow is a point; past 90° it goes negative and the shadow falls behind b's tail. a tip on the origin is the zero vector: no direction, no angle.
try it
a = (2, −1) and b = (1, 3). what is a + b?
pick one.
a practice exercise with a small grid under each question: the sum of two vectors, a combination like 2a − b, the length of a vector with a whole length like (3, 4), whether a dot product is positive, zero or negative, and which of four vectors is perpendicular to a given one. after an answer the grid draws it: the result in white, the two parts of a length, or the angle between the arrows.
the rules
an arrow, or two numbers
a vector is written as its components: how far it goes along and along . and are the arrows of length 1 along the two axes, so the components are also the amounts of and in the vector: . a 3d vector has a third component, , and everything below works the same with one more term; the cg fundamentals page does the 3d versions, with code. the arrow has no fixed place: drawn from the origin and drawn from are the same vector. a point is also written as two numbers, but it’s a place, not a step; the pitfalls below come back to that.
adding and subtracting
head to tail: put the tail of on the tip of , and runs from the tail of to the tip of . part by part: the steps add and the steps add, because walking across and walking up are separate walks. the order doesn’t matter, and the two orders close a parallelogram whose diagonal is the sum. is the arrow from the tip of to the tip of : the step that takes you from to , so . it’s the same as : add the flipped arrow.
scaling
a plain number stretches an arrow, which is why numbers are called scalars next to vectors. is twice as long, half as long, the same length the other way, and is the zero vector. the direction never changes except to flip. two vectors are parallel when one is a multiple of the other, and all the multiples of one arrow lie on one line through the origin.
length
the components are the two legs of a right triangle and the arrow is its hypotenuse, so pythagoras gives the length: and . scaling scales it, . the zero vector has length 0 and no direction: it’s the one arrow that doesn’t point. in code the length is Math.hypot(x, y), which gives exactly 5 for Math.hypot(3, 4).
unit vectors
divide an arrow by its own length and it’s 1 long, the same way: a direction with the size taken out. , and . a unit vector times a length is an arrow of that length in that direction, which is how a speed and a heading become a velocity. the zero vector can’t be normalized: .
linear combinations and span
stretch each arrow by its own number and add: a linear combination. everything you can reach that way is the span. one arrow spans a line. two arrows that aren’t parallel span the whole plane: for any target there is exactly one pair that lands on it, and finding the pair is solving two equations in two unknowns, the linear systems page. two parallel arrows span only a line: the second adds no new direction. and are the simplest pair, and with them the coefficients are the coordinates, . any other pair that isn’t parallel works as a basis too, with different numbers for the same point.
u and v aren't parallel, so some s and t reach every point of the plane. move the sliders until the white tip lands on the target (1, 2).
two arrows from the origin on a grid from −6.5 to 6.5 across and −4.5 to 4.5 up, u = (2, 1) in cyan and v = (−1, 1) in amber to start, two sliders s and t from −2 to 2 in half steps, s·u dashed in cyan, t·v dashed in amber from its tip, their sum s·u + t·v in white, and a hollow target at (1, 2). it opens at s = 1, t = 0, on u itself; s = 1 and t = 1 land on the target: 1·(2, 1) + 1·(−1, 1) = (1, 2). a row of buttons swaps the pair: i = (1, 0) and j = (0, 1), where s = 1 and t = 2 reach it and the two numbers are the coordinates, or u with w = (−1, −0.5), parallel to it, where every combination stays on a dotted line through the origin and the target is out of reach.
the dot product
two vectors in, one number out. on paper: multiply matching parts and add. in the picture: the two lengths times the cosine of the angle between them. the two forms agree, which the key idea below shows, and each has its job: the first is what you compute, the second is what it means. is 1 when the arrows point the same way, 0 at a right angle and when they’re opposite, so the sign of says which side of 90° the angle is on, with no trig at all. , so the dot product holds the length too. it’s symmetric, , and it spreads over sums, , both of which you can read off the component form.
perpendicular
a dot product of 0 means , a right angle, unless one of the arrows is the zero vector. in 2d a perpendicular is one move away: swap the components and flip one sign. is turned a quarter turn counter-clockwise, and . for that’s .
projection
drop a perpendicular from the tip of onto the line of . where it lands is the shadow of on : the part of that points along . its length is , the side next to in a right triangle whose hypotenuse is , and by the second form of the dot product that’s . multiply the unit vector of by that length and you have the shadow as a vector, which is the first line. the sign comes along: a negative length means the shadow falls behind the tail of . what’s left over, , is perpendicular to , so every vector splits into a part along and a part across it. for the page’s pair, the shadow of on is , long, and the rest is , which dots with to 0.
the angle
solve the second form for the cosine, then take . for and : , so . two unit vectors skip the division: their dot product is the cosine itself. on a computer, rounding can push the ratio a hair past 1, where has no answer; the cg fundamentals page shows the case and the fix, an form, which is what the explore figure uses. for the direction of one vector as an angle from the axis, it’s , on the trig page.
signed area
the parallelogram with sides and has base and height . the cross-multiplied components give the same area with a sign: positive when is counter-clockwise from , negative when it’s clockwise, 0 when they’re parallel. for the page’s pair, . the sign is how a game tells whether a point is left or right of a line. in 3d the same arithmetic, done three times, is the cross product, a vector perpendicular to both; the cg fundamentals page covers it. on the matrices page this number is the determinant of the matrix with and as its columns.
worked examples
a step between points. and . the step from to is , and its length is the distance between them: . is a legal sum of numbers but not a meaningful place.
a unit vector. . , so . check: . a speed of 10 in that direction is the velocity .
the angle between two arrows. and . . and , and their product is . so , and exactly.
a projection. a force pulls on a block on a ramp that runs along . how much of the force pulls along the ramp? . as a vector: . the rest, , presses into the ramp: it’s perpendicular to , since .
which side. and . : negative, so the angle is past 90° (it’s about ). : positive, so is counter-clockwise from , and the parallelogram they span has area 14.
reaching a point. write as . part by part: and . add the two equations: , so and . check: . two equations, two unknowns: the linear systems page does this in general.
the key idea up close
why the two dot products agree
the two forms of the dot product look unrelated: one is arithmetic on components, the other is two lengths and an angle. the law of cosines ties them together.
a triangle: a = (3, 1) in cyan and b = (1, 3) in amber from the origin, the angle θ between them marked at the origin, and a − b = (2, −2) in white from the tip of b to the tip of a as the third side. the sides are labeled |a| = √10, |b| = √10 and |a − b| = √8: about 3.16, 3.16 and 2.83.
put and tail to tail. the third side of the triangle is , and the angle between and is . the law of cosines, pythagoras with a correction for the angle, says
now take the left side apart with components. write for , the component form, and use two facts about it that are plain arithmetic: , and it spreads over sums and differences the way multiplication does. then
both say what the squared third side is. set them equal, cancel from both sides and divide by :
with the page’s pair: , and . the same argument runs in 3d with one more component, which is why a shader can take the cosine of the angle between a normal and a light without ever finding the angle.
pitfalls
- |a + b| isn’t |a| + |b|. the vectors add part by part, but their lengths don’t, unless the arrows point the same way. for the page’s pair, while . the triangle inequality says which way it always goes: , with equality only for the same direction.
- normalizing the zero vector. has length 0, so dividing by it is : NaN in javascript, and NaN spreads to everything it touches. check the length first, as the comment in the cg page’s
normalizesays. - acos of a rounded ratio. can come out a hair past 1, and of that is NaN. the cg fundamentals page shows the case and the fix.
- a point isn’t a vector. both are written . a point is a place, a vector is a step. point minus point is a vector, point plus vector is a point, and point plus point means nothing. the cg fundamentals page keeps them apart with a fourth coordinate.
- a · b is only the cosine for unit vectors. . but and : scale either arrow and the dot product scales with it. divide by the lengths first, or normalize both.
- the dot product is a number, not a vector. has no direction, so is a stretched and is a stretched : two different vectors. doesn’t mean anything.
practice
-
and . find , , and .
answer
part by part: , , . .
-
find the unit vector in the direction of .
answer
, so the unit vector is . check: .
-
. show that is perpendicular to it, and find the angle between and .
answer
, so they’re perpendicular: is with the parts swapped and one sign flipped. , and , so and .
-
find the shadow of on , and the part of that’s perpendicular to .
answer
and , so the shadow is , which is long. the rest is , and , so it’s perpendicular. the two parts add back to .
-
find and with . what’s the signed area of the parallelogram and span, and what does it have to do with the answer?
answer
part by part: and . the second gives ; in the first, , so and . check: . the signed area is : negative, so is clockwise from . it’s not 0, so the two aren’t parallel, and that’s why exactly one pair reaches , or any other point.
-
when does equal ? try with , then with .
answer
only when the arrows point the same way, so the head-to-tail route doesn’t bend. does: , , and has length , the sum. doesn’t: too, so , but is only long. in fact : these two are perpendicular, and the sum is the hypotenuse.