Euclidean space — theory with music taste: dot product, Gram matrix, angles, Gram-Schmidt and cross product (and the ± I did not need)
In quiz 3 on the Campus there was a question about how the norm of a vector is defined, and among the options were and . I ticked the one with ± and got it wrong. A length cannot be negative: the norm is the positive root, always. The ± is for when you look for the two unit vectors of a direction, which is something else, and in this article you will see both so you never mix them up again.
This topic adds to the vector spaces of topic 3 a tool for measuring: the dot product (or inner product). With it you compute lengths, angles and perpendiculars, and orthonormal bases, the orthogonal projection and the cross product appear. The example for the whole article is music taste: what you listen to each week of pop, rock and classical music is a vector, and the angle between two of those vectors says how alike two people are. It is the idea behind many recommender systems, and you will see it in Data Science more often than you think. After the theory you have the guided practice.
In a nutshell: a dot product is a rule that assigns a number to two vectors and satisfies four properties (symmetric, linear and positive definite). A vector space with a dot product is a Euclidean space. With it you define the norm , the angle and orthogonality (). In a basis, the product is written , with the Gram matrix.
Table of Contents
Before you start. For this topic all you need is what you saw in topic 3: what a vector of is, what a basis is and how to write the coordinates of a vector in it (and the change-of-basis matrix, for congruent matrices). As for matrices, multiplying a row by a matrix by a column. Nothing else. The topic has three big ideas, in this order: a rule to measure how much two vectors share (the dot product, and its Gram matrix); what comes out of it, lengths, angles and perpendiculars (norm, cosine, orthogonality); and how to make use of perpendiculars (orthonormal bases, Gram-Schmidt, projection), with the cross product and the scalar triple product at the end, which only exist in . If you get lost at any point, go back to music taste: the whole topic can be told with it.
Your taste is a vector
I write down how many hours a week each person in my group listens to pop, rock and classical:
| Person | Pop | Rock | Classical | Vector |
|---|---|---|---|---|
| Sergio | 2 | 1 | 2 | |
| Laura | 2 | 2 | 1 | |
| Pablo | 0 | 0 | 4 | |
| Ana | 3 | 4 | 0 |
They are vectors of , like the colours of topic 3, and they can be added and multiplied by numbers. But now the question is different: who am I most like? To answer it you need to measure, and a vector space on its own measures nothing: it does not know what a length or an angle is. That is what the dot product adds.
The dot product
Before the definition, let’s invent the rule in our heads. The question is: how would you measure, with a single number, how much Sergio and Laura share?
Genre by genre. If both of them listen to pop, that pop counts; if one of them listens to none of a genre, that genre should count for nothing. The operation that does exactly that is multiplying: if one of the two numbers of hours is 0, the product is 0. Then you add up the three genres:
| Genre | Sergio | Laura | Product | Why |
|---|---|---|---|---|
| Pop | 2 | 2 | 4 | Both listen to quite a lot: it counts a lot |
| Rock | 1 | 2 | 2 | Laura listens to more, but I only do 1: what is shared is small |
| Classical | 2 | 1 | 2 | Same as rock, the other way round |
| Total | 8 | You add what they share in each genre |
Why not simply add the hours? Because then it would count what only one person listens to: Pablo’s 4 hours of classical would make it look as if he shared something with Ana, who listens to no classical at all. With the product, Ana and Pablo give 0, which is fair.
Now notice four things this rule does without you asking for them. Sharing is mutual: Sergio with Laura gives the same as Laura with Sergio. If Laura listened to twice as much of everything, what is shared would double. If you put the hours of two people together in one list, what that list shares with Sergio is the sum of what each of them shares. And a person always shares something positive with themselves (me with me, ), unless they listen to nothing. Those four things, with their official names, are the definition:
A dot product on a real vector space is a rule that assigns to each pair of vectors a real number and satisfies, for all and :
- Symmetric: .
- Distributive: .
- Takes out scalars: .
- Positive definite: , and only if .
In , the standard dot product multiplies component by component and adds:
With the tastes: . The number is large when both listen to a lot of the same. And : Ana and Pablo do not share a single genre.
A real vector space with a dot product is called a Euclidean vector space. and with the standard product are the usual ones, but it is not the only possible product, and that is what makes the exam interesting.
The Gram matrix
If is a basis and you write and with their coordinates and in it, properties 2 and 3 turn the product into:
is the Gram matrix (or metric matrix) of the product in the basis : in position , the product of basis vector by basis vector . It is symmetric, because . With the standard product and the standard basis, is 1 if and 0 otherwise, so and you get back the sum of products.
Look carefully at the shapes: row times matrix times column. A row times an times an column gives a , a number, which is what a dot product has to give.
Writing , column times matrix times row. It cannot even be multiplied in that order ( by ), and if you swap the factors so that it fits, you get an matrix, not a number. It is the trap of a multiple-answer question in quiz 3: the two row column options are right (with first or with first, by symmetry) and none of the others.
Another dot product. Suppose pop and rock are alike and you want them to count “a bit together”. You define
It is symmetric, and it is positive definite (you will check it with Sylvester’s criterion in topic 6; for now, trust it). With this product, :
The calculation with G, term by term
Before doing the calculation, read what says. Each entry says how much it is worth to combine genre of one person with genre of the other. Here : pop with pop and rock with rock count double. : my pop with Laura’s rock (and my rock with her pop) also counts for something, even though it is not the same genre. : classical, as before. And the zeros: classical does not mix with anything.
means adding up over all the entries. The ones with a 0 contribute nothing, so five terms are left:
| Entry | Calculation | Contributes | Why |
|---|---|---|---|
| 8 | My pop (2) times Laura’s pop (2), with weight 2 | ||
| 4 | My pop (2) times Laura’s rock (2), with weight 1 | ||
| 2 | My rock (1) times Laura’s pop (2), with weight 1 | ||
| 4 | My rock (1) times Laura’s rock (2), with weight 2 | ||
| 2 | My classical (2) times Laura’s (1), with weight 1 | ||
| Total | 20 | The same as with |
With only the three diagonal entries would be left, with weight 1: , the standard product. Doing times the column first, as above, is faster in the exam; term by term you see where each number comes from.
Check you’ve got it: with the product G, what is the product of Sergio with Ana, and why does it go up so much compared with the standard one?
It is 31, against 10 with the standard product. Step by step: , and . It goes up so much because Ana only listens to pop and rock, which are exactly the genres reinforces: they weigh double and they also mix with each other. My classical, on the other hand, contributes nothing, because Ana has a 0 there.
The same pair of vectors, a different number. A dot product is not “the” product: it is a choice of how to measure.
Congruent matrices
If you change basis, the Gram matrix changes. With (the vectors of as columns, as in topic 3), the one in the new basis is
and and are called congruent matrices: they represent the same dot product in two different bases, and they have the same rank. For example, with the standard product () and the yellow, cyan and magenta basis from topic 3:
Each entry is a product of two basis vectors: , , and so on.
The norm and unit vectors
Before the formula, in your head: how long is Sergio’s arrow, ? If it were only pop and rock, , it would be the hypotenuse of a triangle with legs 2 and 1: . With classical, Pythagoras again: that hypotenuse and the height 2 are the legs of another triangle, and you get .
Now look at what is inside the root: , which is exactly , what Sergio shares with himself. So the length comes out of the dot product without inventing anything new. The root is needed because multiplies hours by hours; the root brings the number back to the scale of hours. And the root is the positive one because it is a length: also satisfies , but an arrow does not measure . That is the mistake from the quiz at the beginning.
The norm (or modulus, or length) of a vector is
with the positive root. It satisfies , only if , and . With the standard product of , it is Pythagoras: .
It measures how much you listen in total (in a “Euclidean” sense), not what you listen to. With the product above, the norm of is different: , so .
A unit vector is one with norm 1. Dividing a vector by its norm gives a unit vector with the same direction, and there are two in each direction, one for each sense:
Here there is a ±, and it is the one you must not carry over to the norm. Sergio’s unit vector is his profile: what he listens to, regardless of how much.
Writing . The norm is a length and it is unique and positive. The ± appears when you look for the unit vectors of a direction: .
The angle between two vectors
Before the definition, a question: does the dot product tell me who I am most like? Almost. With the friend who listens to twice as much of everything as me, , the product is , more than twice the one with Laura, even though he has exactly my taste. The dot product mixes two things: what you listen to and how much you listen. For likeness we only care about the first.
The way to remove the “how much” is the one from the previous section: divide each vector by its norm and keep its unit vector, its profile. My profile is , my friend’s is the same, and their product is 1. Laura’s is , and
It does not matter whether you divide before or after: since the product takes scalars out, it is the same as divided by , . And why is it called a cosine? In the plane, a unit vector at an angle from is , and its product with is . The product of two profiles is the cosine of the angle between them. That, with its name, is the definition:
The angle between two non-zero vectors is defined by
(The Cauchy-Schwarz inequality, , guarantees the quotient is between and .) In data science this cosine is called cosine similarity: it compares profiles, not amounts.
I am more like Laura than Pablo. And a friend who listened to twice as much of everything as me, , would have cosine 1 with me: the same profile, angle 0.
This is Sergio against the whole group, with the why of each row:
| Person | Product with | Norm | Cosine | Why |
|---|---|---|---|---|
| Laura | 8 | 3 | Listens to a bit of everything, like me, with more rock | |
| Pablo | 8 | 4 | Only shares my classical | |
| Ana | 10 | 5 | Only shares my pop and my rock | |
| Friend | 18 | 6 | 1 | Same profile, twice the hours |
Look at Pablo and Ana: both are at about from me, and they have nothing in common with each other. Each of them is like a different part of me.
Comparing dot products instead of cosines. With Ana the product is 10 and with Laura 8, and you would say I am more like Ana. But Ana listens to more hours in total (her norm is 5); once you divide, Laura clearly wins.
And if the dot product is 0, the cosine is 0 without looking at the norms, because the denominator is never 0: that is the next section.
Check you’ve got it: who is Laura more like, Pablo or Ana?
Ana. Step by step: and . The norms are 3, 4 and 5, so (about ) and (about ). Laura mostly listens to pop and rock, like Ana; with Pablo she only shares one hour of classical.
With the product the angle changes, because the way of measuring changes: , so
If pop and rock “count together”, Laura is even more like me. That is how it appears in the exam: you are given a dot product (in topic 6, from a bilinear form) and asked for the cosine of the angle between two vectors with that product.
Orthogonal vectors
Two non-zero vectors are orthogonal (perpendicular) if they make an angle of , that is, if
Ana and Pablo: . They have no genre in common; their profiles are perpendicular. In with the standard product, and are orthogonal: . And orthogonality depends on the product: and are orthogonal with the standard product, but with their product is .
Saying that two vectors are orthogonal if their dot product is 1. It is 0. A dot product of 1 between two unit vectors would mean they are the same vector.
Orthogonal and orthonormal bases
A basis is orthogonal if its vectors are pairwise orthogonal, and orthonormal if, in addition, all of them are unit vectors. In terms of the Gram matrix in that basis:
- is orthogonal is diagonal.
- is orthonormal .
The standard basis is orthonormal with the standard product. To go from an orthogonal basis to an orthonormal one, just divide each vector by its norm.
Why are they so popular? Because in an orthonormal basis the coordinates come out without solving any system: coordinate of is . And the dot product goes back to being the sum of products of coordinates.
The Gram-Schmidt process
Gram-Schmidt and the orthogonal projection are in the 2026/27 course guide, although they did not appear in the previous year’s notes. If your lecturer does not cover them, skip these two sections: the rest of the topic does not depend on them.
Before the formula, with two vectors and your head. You have and , and they are not perpendicular: their product is 1. You keep the first one as it is, . From the second you remove a piece of the first, , and you choose so that what is left is perpendicular to :
That is the fraction in the formula: . You do not need to memorise it: it comes from asking for the remainder to be perpendicular. What does not change is what they span: is a combination of and , and you get back by adding again.
With three vectors it is the same: from you remove a piece of and another of . And here is the clever part: since , when you multiply by the piece of disappears, and each coefficient comes out on its own, with the same fraction. With the original vectors that does not happen, because they are not perpendicular to each other.
The Gram-Schmidt process turns any basis into an orthogonal one that spans the same. The idea: from each vector you remove the part that “points” towards the ones you already have.
With , , (yellow, magenta and cyan, back to the colours) and the standard product:
- .
- , because and . To avoid dragging fractions, I multiply it by 2: (a multiple is still orthogonal).
- , because , and . Multiplied by : .
Check: , and . Dividing by the norms (, , ) gives an orthonormal basis.
The third vector, without skipping anything
The step for is the one that gets compressed the most, so here it is slowly:
| Step | Calculation | Why |
|---|---|---|
| 1 | How much points towards | |
| 2 | , coefficient | You divide so the remainder is perpendicular to |
| 3 | With the new , , not with | |
| 4 | , coefficient | The same fraction, now with |
| 5 | You subtract the two pieces, component by component | |
| 6 | You multiply by : it is still perpendicular |
Why can you use instead of ? Because if you multiply by a number , in the fraction the numerator gets multiplied by , the denominator by and the vector by : the cancels and the piece you subtract is the same.
Check you’ve got it: what do you get if, in the step for u3, you subtract the projection onto v2 instead of onto u2?
You get , which is not perpendicular to . Step by step: and , so you subtract : . And when you check, . It fails because is not perpendicular to : by removing the piece of you put some of back in.
At each step, subtract the projections onto the new vectors , not onto the original . If you subtract onto the originals, the result is not orthogonal and you only notice when you check.
The orthogonal projection
Before the definition, in your head and with Ana. What part of my taste is explained by Ana’s? What can be explained with Ana are her multiples, : listening like her, for more or fewer hours. You have to choose so that is as close to me as possible. The squared distance, expanding with the properties of the product, is
Trying a few values of :
| Squared distance | Why | ||
|---|---|---|---|
| 0 | 9 | You explain nothing: all my squared norm is left | |
| 5 | The smallest: it is the projection | ||
| You go a bit too far: it grows again | |||
| 1 | 14 | Copying Ana completely takes you further away than doing nothing |
The minimum of is at , and it is exactly the from Gram-Schmidt: . It is not a coincidence: the closest point is the one where what is left is perpendicular to Ana’s direction. And Pythagoras holds: the squared norm of the projection is , what is left has squared norm 5, and .
The orthogonal projection of onto a subspace is the vector of closest to . If is an orthogonal basis of :
and what is left, , is orthogonal to all of . Its norm is the distance from to . Gram-Schmidt is nothing but this repeated: from each vector you remove its projection onto what came before.
With tastes: what part of what I listen to is explained by Ana? , a single direction:
The “Ana-like” part of my taste is , and the rest, which includes all my classical, is perpendicular to it.
Just for the curious. Least squares is a projection. If a system has no solution (more equations than unknowns, as when fitting a line to many points), you look for the whose is closest to : the projection of onto the columns of . It comes from the normal equations . For the line through , , , you get , . It is the linear regression you will see in statistics, and in MATLAB, A \ b already computes it when the system has no exact solution.
The cross product
Only in . The cross product of and is the vector that satisfies:
- Norm: , the area of the parallelogram they form.
- Direction: perpendicular to and to .
- Sense: given by the right-hand rule (or corkscrew rule).
In an orthonormal basis it is computed with a “determinant” with the basis vectors in the first row:
It is perpendicular to both: and . Its norm, , is the area of the parallelogram of and ; and it agrees with the formula: , and .
Properties: (it is not commutative); it is perpendicular to both factors; if , if and only if they are parallel; and .
The cross product gives a vector perpendicular to a plane with a single calculation. It is what you will use in topic 8 for the equation of a plane, and here it gives you for free the implicit equation of a plane through the origin: the plane is .
The scalar triple product
The scalar triple product of three vectors of , in an orthonormal basis, is the determinant of their components:
In absolute value it is the volume of the parallelepiped they form. With Sergio, Laura and Pablo:
If it is 0, the three vectors lie in the same plane: they are linearly dependent, the same thing the determinant said in topic 3. The one of the standard basis, , is 1: the unit cube.
The same in MATLAB
dot, norm and cross do the standard dot product, the norm and the cross product; with another Gram matrix you write the formula as it is:
s = [2 1 2]; l = [2 2 1]; p = [0 0 4]; a = [3 4 0]; dot(s, l) % standard dot product norm(s) % norm cosa = dot(s, l) / (norm(s) * norm(l)) acos(cosa) * 180 / pi % the angle in degrees dot(a, p) % 0: orthogonal
Output:
ans =
8
ans =
3
cosa =
0.8889
ans =
27.2660
ans =
0
acos gives the angle in radians; that is why it is multiplied by . Now with the Gram matrix , with the vectors written as rows:
G = [2 1 0; 1 2 0; 0 0 1]; s * G * l' % row · G · column sqrt(s * G * s') % the norm with G c = (s * G * l') / (sqrt(s * G * s') * sqrt(l * G * l')) cross(s, l) det([s; l; p]) % scalar triple product
Output:
ans =
20
ans =
4.2426
c =
0.9428
ans =
-3 2 2
ans =
8
s * G * l' is exactly : s is a row and l' a column. If you write s' * G * l, MATLAB complains because the dimensions do not match. And Gram-Schmidt, with a loop that subtracts the projections onto the new vectors:
format rat
V = [1 1 0; 1 0 1; 0 1 1]'; % the original vectors, as columns
U = zeros(3);
for k = 1:3
w = V(:, k);
for j = 1:k-1 % subtract the projection onto each u_j already computed
w = w - (dot(V(:, k), U(:, j)) / dot(U(:, j), U(:, j))) * U(:, j);
end
U(:, k) = w;
end
U % orthogonal columns
abs([dot(U(:,1), U(:,2)), dot(U(:,1), U(:,3)), dot(U(:,2), U(:,3))]) < 1e-12 % are they 0?
Output:
U =
1 1/2 -2/3
1 -1/2 2/3
0 1 2/3
ans =
1×3 logical array
1 1 1
The columns of U are , and , the paper ones before removing fractions. And the three products between different columns are 0: the check gives 1 1 1 (true all three times). It is compared with a tolerance, < 1e-12, because the computer works with decimals and a 0 can come out as ; with format rat, those non-exact zeros show up as asterisks.
Try it in the MATLAB playground
In the website’s MATLAB playground you can build your own “recommender”:
% Who are you most like? Hours of pop, rock and classical
me = [2 1 2];
friends = [2 2 1; 0 0 4; 3 4 0]; % Laura, Pablo, Ana
for k = 1:size(friends, 1)
f = friends(k, :);
c = dot(me, f) / (norm(me) * norm(f));
fprintf('Friend %d: cosine %.4f\n', k, c)
end
Things to look at:
- Put your hours in
meand your friends’ infriends. The highest cosine is your musical twin. - Change
meto[4 2 4]: the cosines do not change. The cosine measures the profile, not the amount. - Add a friend
[0 0 1]and compare with Ana: cosine 0, orthogonal. - Change
dot(me, f)tome * G * f'(and thenormcalls to the root ofx * G * x') with theGabove, and watch the similarity with Laura go up.
Summary
| Concept | Formula | Watch out for |
|---|---|---|
| Standard dot product | Four properties: symmetric, linear, positive definite | |
| Matrix expression | , | Row · · column |
| Congruence | with the new basis as columns | |
| Norm | Positive, no ± | |
| Unit vector | Here there are two | |
| Angle | With the product you are given | |
| Orthogonal | 0, not 1 | |
| Orthogonal / orthonormal basis | diagonal / | Divide by the norms |
| Gram-Schmidt | Project onto the new ones | |
| Cross product | Determinant with | Only ; perpendicular to both |
| Scalar triple product | Volume; 0 if coplanar |
Three ideas to take away:
- The dot product is the measuring rule: lengths (), angles (the cosine) and perpendiculars (product 0) come from it. Changing the Gram matrix changes every measurement.
- In a basis, the product is row · · column, and holds in each entry the product of two basis vectors.
- An orthonormal basis makes everything easy; Gram-Schmidt builds one by subtracting projections, and the cross product gives the perpendicular to two vectors in .
Now it is your turn: in the Euclidean space guided practice we solve five problems step by step, checked in MATLAB: a Gram matrix in another basis, angles with two dot products (and why you should round at the end), a basis that is orthogonal with respect to a , Gram-Schmidt with a projection and the cosine part as it appears in the exam. And to check you have got it, the Euclidean space exercises go from easy to hard and come with commented solutions and a quiz like the Campus ones. If you come without the previous topic, first review the vector spaces theory. Next comes topic 5, linear transformations and diagonalisation.
Next step: practiceEuclidean space — guided practice with music taste: Gram matrix, angles, orthonormal bases, Gram-Schmidt and the exam’s cosine (and the decimals that got away from me)Did this article help you? Here is how you can support SergioLearns →



