Euclidean 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)

In MATLAB assignment 3 I got a cosine question wrong while having the method right: I wrote 0.3741 where it was 0.3746, and in the second part, −0.5899 instead of −0.5898. The mistake was in the fourth decimal place, and it came from rounding the roots before dividing. The box compares numbers, so it did not matter that the idea was right. In this practice you will see why it happens and how to avoid it: round only at the end.

In this practice we do by hand what you saw in the Euclidean space theory, with the usual music tastes: the Gram matrix of the standard product in a non-standard basis, angles with two different dot products (and the decimals), a basis that is orthogonal with respect to a Gram matrix that is not the identity, Gram-Schmidt with a projection and, finally, the cosine part as it appears in the exam, with its marks. Each problem is checked in MATLAB, as in Assignment 3.

Before you start

Make sure you are clear on:

  • How to multiply a row by a matrix by a column (topic 1).
  • What a basis is and how to write the coordinates of a vector in it (topic 3).
  • The formulas of the topic: XtGYX^t G Y, ∥u⃗∥=+u⃗⋅u⃗\lVert \vec u \rVert = +\sqrt{\vec u \cdot \vec u} and cos⁡α=u⃗⋅v⃗∥u⃗∥∥v⃗∥\cos \alpha = \frac{\vec u \cdot \vec v}{\lVert \vec u \rVert \lVert \vec v \rVert}.
  • That orthogonal means product 0, and orthonormal, in addition, norm 1.

If any of them sounds shaky, review the theory before going on.

The usual tastes, in hours a week of pop, rock and classical: Sergio (2,1,2)(2, 1, 2), Laura (2,2,1)(2, 2, 1), Pablo (0,0,4)(0, 0, 4) and Ana (3,4,0)(3, 4, 0).

How to solve a dot product problem on paper

  1. Data: identify the dot product. If it is the standard one, G=IG = I; if you are given an expression, write its matrix GG (in position (i,j)(i, j), the coefficient of xiyjx_i y_j).
  2. Shape: write each product as row ⋅\cdot GG ⋅\cdot column. First compute GG times the column and then the row times the result.
  3. Measure: norms with the positive root, cosines with the quotient. Keep roots as roots until the end.
  4. Check: a cosine is always between −1-1 and 11; an orthogonal basis has a diagonal GG; a projected vector leaves a perpendicular remainder.

Problem 1: the Gram matrix in another basis

Statement. In R3\mathbb{R}^3 with the standard dot product consider the basis B′={(1,2,0),(0,1,1),(1,0,−1)}B' = \{(1, 2, 0), (0, 1, 1), (1, 0, -1)\}.

a) Compute the Gram matrix G′G' of the dot product with respect to B′B'.

b) Two vectors have coordinates X′=(1,1,0)X' = (1, 1, 0) and Y′=(0,1,1)Y' = (0, 1, 1) in B′B'. Compute their dot product with G′G' and check it by moving them to the standard basis.

The idea

Each entry of G′G' is the product of two basis vectors: gij′=e⃗i′⋅e⃗j′g'_{ij} = \vec e'_i \cdot \vec e'_j. Or, with matrices, G′=CtIC=CtCG' = C^t I C = C^t C, with CC the vectors of B′B' as columns. And in b) the product is done with the coordinates in B′B' and the G′G' of that basis, never mixing bases.

G′G' is symmetric: you only need the diagonal and what is above it, six products instead of nine.

In words: the basis B′ B' is not the usual one. Its vectors neither have norm 1 nor are perpendicular to each other. The vector with coordinates X′=(1,0,0) X' = (1, 0, 0) in B′ B' is actually (1,2,0) (1, 2, 0) , whose norm is 5 \sqrt5 , not 1. If you used the usual product with coordinates in B′ B' , you would be treating the vectors of B′ B' as if they were the standard ones, and they are not.

G′ G' stores exactly what you are missing about the basis. On the diagonal, the squared norm of each of its vectors. Off the diagonal, how much they “overlap” two by two: a 0 would mean perpendicular; a positive number, that they form an acute angle; a negative one, obtuse. That is why, before doing any computation, you can already expect a 5 in the first entry (the 5 \sqrt5 from before, squared). And in b) you get a free check: the product has to come out the same both ways, with G′ G' and in the standard basis. If it does not, one of the two computations is wrong.

Step by step

a) The six products:

EntryProductValue
g11′g'_{11}(1,2,0)⋅(1,2,0)(1, 2, 0) \cdot (1, 2, 0)1+4+0=51 + 4 + 0 = 5
g12′g'_{12}(1,2,0)⋅(0,1,1)(1, 2, 0) \cdot (0, 1, 1)0+2+0=20 + 2 + 0 = 2
g13′g'_{13}(1,2,0)⋅(1,0,−1)(1, 2, 0) \cdot (1, 0, -1)1+0+0=11 + 0 + 0 = 1
g22′g'_{22}(0,1,1)⋅(0,1,1)(0, 1, 1) \cdot (0, 1, 1)0+1+1=20 + 1 + 1 = 2
g23′g'_{23}(0,1,1)⋅(1,0,−1)(0, 1, 1) \cdot (1, 0, -1)0+0−1=−10 + 0 – 1 = -1
g33′g'_{33}(1,0,−1)⋅(1,0,−1)(1, 0, -1) \cdot (1, 0, -1)1+0+1=21 + 0 + 1 = 2
G′=(52122−11−12) G' = \begin{pmatrix} 5 & 2 & 1 \\ 2 & 2 & -1 \\ 1 & -1 & 2 \end{pmatrix}

b) First G′G' times the column Y′Y', then the row X′tX'^t times the result:

G′Y′=(5⋅0+2⋅1+1⋅12⋅0+2⋅1−1⋅11⋅0−1⋅1+2⋅1)=(311),X′t(G′Y′)=(1,1,0)⋅(3,1,1)=3+1+0=4 G' Y' = \begin{pmatrix} 5 \cdot 0 + 2 \cdot 1 + 1 \cdot 1 \\ 2 \cdot 0 + 2 \cdot 1 – 1 \cdot 1 \\ 1 \cdot 0 – 1 \cdot 1 + 2 \cdot 1 \end{pmatrix} = \begin{pmatrix} 3 \\ 1 \\ 1 \end{pmatrix}, \qquad X'^t (G' Y') = (1, 1, 0) \cdot (3, 1, 1) = 3 + 1 + 0 = 4
The Gram matrix of a basisThe three vectors of B' on the left and their Gram matrix on the right: diagonal 5, 2, 2 and off-diagonal 2, 1 and −1. g'12 = e1 · e2 = 2 is highlighted. Below, the product of (1,1,0) and (0,1,1) with G' gives 4.vectors of B'e₁ = (1, 2, 0)e₂ = (0, 1, 1)e₃ = (1, 0, −1)52122-11-12G'eᵢ · eⱼg'₁₂ = e₁ · e₂= 0 + 2 + 0 = 2(1, 1, 0) · G' · (0, 1, 1)ᵗ = (1, 1, 0) · (3, 1, 1) = 4
Each entry is the product of two basis vectors.

Check

In the standard basis, u⃗=1⋅(1,2,0)+1⋅(0,1,1)=(1,3,1)\vec u = 1 \cdot (1, 2, 0) + 1 \cdot (0, 1, 1) = (1, 3, 1) and v⃗=(0,1,1)+(1,0,−1)=(1,1,0)\vec v = (0, 1, 1) + (1, 0, -1) = (1, 1, 0). With the standard product, u⃗⋅v⃗=1+3+0=4\vec u \cdot \vec v = 1 + 3 + 0 = 4. The same number, as it must be: the dot product does not depend on the basis, only its matrix does.

Check it in MATLAB

format rat
C = [1 0 1; 2 1 0; 0 1 -1];    % the vectors of B' as columns
Gp = C' * C                    % G' = C^t I C
X = [1; 1; 0]; Y = [0; 1; 1];  % coordinates in B'
X' * Gp * Y                    % product with G'
dot(C * X, C * Y)              % the same, moving to the standard basis

Output:

Gp =

       5              2              1       
       2              2             -1       
       1             -1              2       

ans =

       4       

ans =

       4       

C * X moves the coordinates from B′B' to the standard basis (the change-of-basis matrix from topic 3), and there dot works.

Questions you might ask

Why G′=CtCG' = C^t C and not CCtC C^t? Because entry (i,j)(i, j) of CtCC^t C is row ii of CtC^t (basis vector ii) times column jj of CC (vector jj): exactly e⃗i′⋅e⃗j′\vec e'_i \cdot \vec e'_j. CCtC C^t multiplies components, not vectors.

Does G′G' have the same rank as II? Yes, 3: they are congruent, and congruence preserves rank.

Doing the product in b) with the identity matrix, X′tY′=0+1+0=1X'^t Y' = 0 + 1 + 0 = 1. The coordinates are in B′B', so the matrix is the one of B′B'. Using II only works with coordinates in an orthonormal basis.

Try it yourself

  • Compute the Gram matrix of the standard product in the basis {(1,1,0),(1,−1,0),(0,0,2)}\{(1, 1, 0), (1, -1, 0), (0, 0, 2)\}. (It comes out diagonal, diag(2,2,4)\text{diag}(2, 2, 4): the basis is orthogonal.)
  • With that basis, the product of (1,2,1)(1, 2, 1) and (0,1,1)(0, 1, 1) (coordinates in it) is 8. Check it in the standard basis.

Problem 2: one angle with two dot products

Statement. a) In R2\mathbb{R}^2 define the dot product (x1,x2)⋅(y1,y2)=2x1y1+x1y2+x2y1+3x2y2(x_1, x_2) \cdot (y_1, y_2) = 2x_1 y_1 + x_1 y_2 + x_2 y_1 + 3x_2 y_2. Compute the cosine of the angle between u⃗=(1,0)\vec u = (1, 0) and v⃗=(0,1)\vec v = (0, 1) with this product and with the standard one. b) With the standard product of R3\mathbb{R}^3, compute the cosine of the angle between (1,2,−1)(1, 2, -1) and (3,0,2)(3, 0, 2) to four decimal places.

The idea

a) First the Gram matrix: on the diagonal, the coefficients of x1y1x_1 y_1 and x2y2x_2 y_2; off it, that of x1y2x_1 y_2 (which is the same as that of x2y1x_2 y_1). With GG, everything comes from XtGYX^t G Y. b) is the assignment calculation: the key thing is not to round the roots before dividing.

With u⃗=(1,0)\vec u = (1, 0) and v⃗=(0,1)\vec v = (0, 1), XtGYX^t G Y is directly an entry of the matrix: g12g_{12}. And ∥u⃗∥2=g11\lVert \vec u \rVert^2 = g_{11}, ∥v⃗∥2=g22\lVert \vec v \rVert^2 = g_{22}.

Step by step

a) The Gram matrix is

G=(2113) G = \begin{pmatrix} 2 & 1 \\ 1 & 3 \end{pmatrix}

and with the standard vectors: u⃗⋅v⃗=g12=1\vec u \cdot \vec v = g_{12} = 1, ∥u⃗∥=g11=2\lVert \vec u \rVert = \sqrt{g_{11}} = \sqrt2, ∥v⃗∥=g22=3\lVert \vec v \rVert = \sqrt{g_{22}} = \sqrt3:

cos⁡α=12⋅3=16≈0.4082⟹α≈65.91∘ \cos \alpha = \frac{1}{\sqrt2 \cdot \sqrt3} = \frac{1}{\sqrt6} \approx 0.4082 \quad \Longrightarrow \quad \alpha \approx 65.91^\circ

With the standard product, (1,0)⋅(0,1)=0(1, 0) \cdot (0, 1) = 0: they are orthogonal, 90∘90^\circ. The same two vectors are perpendicular with one product and not with the other.

b) (1,2,−1)⋅(3,0,2)=3+0−2=1(1, 2, -1) \cdot (3, 0, 2) = 3 + 0 – 2 = 1, ∥(1,2,−1)∥=1+4+1=6\lVert (1, 2, -1) \rVert = \sqrt{1 + 4 + 1} = \sqrt6 and ∥(3,0,2)∥=9+0+4=13\lVert (3, 0, 2) \rVert = \sqrt{9 + 0 + 4} = \sqrt{13}. Combining the roots before computing anything:

cos⁡α=16⋅13=178=0.11323…≈0.1132 \cos \alpha = \frac{1}{\sqrt6 \cdot \sqrt{13}} = \frac{1}{\sqrt{78}} = 0.11323\ldots \approx 0.1132

Look at what happens if you round first: 6≈2.45\sqrt6 \approx 2.45 and 13≈3.61\sqrt{13} \approx 3.61 give 1/(2.45⋅3.61)=1/8.8445=0.11307…1 / (2.45 \cdot 3.61) = 1 / 8.8445 = 0.11307\ldots, which rounds to 0.11310.1131. One wrong digit, the box in red.

Way of computingResultTo 4 decimals
1/781 / \sqrt{78}, rounding at the end0.113228…0.113228\ldots0.11320.1132
Roots rounded to two decimals0.113064…0.113064\ldots0.11310.1131
Same vectors, two products, and roundingOn the left, (1,0) and (0,1) make 90 degrees with the standard product and about 66 degrees with the matrix G. On the right, 1 over root 78 gives 0.11323, but rounding the roots first gives 0.11306.standardu · v = 0 → 90°with Gcos = 1/√6 → 65.91°1/√78= 0.11323…1/(2.45 · 3.61)= 0.11306…
The angle depends on the product; the fourth digit, on when you round.

Why does such a small difference in the roots move the fourth digit? Step by step, both computations side by side:

StepWithout roundingRounding firstWhy
6 \sqrt6 2.449490… 2.449490\ldots 2.45 2.45 You round up: about 5 ten-thousandths too many
13 \sqrt{13} 3.605551… 3.605551\ldots 3.61 3.61 Up again: about 44 ten-thousandths too many
Denominator78=8.831761… \sqrt{78} = 8.831761\ldots 8.8445 8.8445 Both errors go the same way and add up: the denominator grows by 0.14 %
Cosine0.113228… 0.113228\ldots 0.113064… 0.113064\ldots If the denominator grows by 0.14 %, the quotient drops by 0.14 %: about 16 hundred-thousandths
To 4 decimals0.1132 0.1132 0.1131 0.1131 0.113228 0.113228 was only 8 hundred-thousandths above 0.11315 0.11315 , the rounding boundary, and the error is twice that

The lesson is not that you have to round “better”, but that the error from rounding along the way is carried to the end, and it only has to land near a boundary to change the digit you are asked for. With 1/78 1 / \sqrt{78} there is nothing to carry: a single rounding, in the last step.

Check

Both cosines are between −1-1 and 11. And in a), cos⁡α>0\cos \alpha > 0: the product GG “brings the two axes closer”, because g12=1>0g_{12} = 1 > 0.

Check you’ve got it: what would happen if in a) both cross terms had a minus sign?

That is, with 2x1y1−x1y2−x2y1+3x2y2 2x_1 y_1 – x_1 y_2 – x_2 y_1 + 3x_2 y_2 . The cosine would change sign: cos⁡α=−16≈−0.4082 \cos \alpha = -\frac{1}{\sqrt6} \approx -0.4082 and α≈114.09∘ \alpha \approx 114.09^\circ . Step by step: the Gram matrix is now G=(2−1−13) G = \begin{pmatrix} 2 & -1 \\ -1 & 3 \end{pmatrix} . The diagonal does not change, so the norms are still g11=2 \sqrt{g_{11}} = \sqrt2 and g22=3 \sqrt{g_{22}} = \sqrt3 . The only thing that changes is the product, u⃗⋅v⃗=g12=−1 \vec u \cdot \vec v = g_{12} = -1 , and with it the sign of the cosine. It is still a dot product: it is symmetric and its minors are 2>0 2 > 0 and 2⋅3−1=5>0 2 \cdot 3 – 1 = 5 > 0 . If before G G “brought the two axes closer”, now it “pushes them apart”: 65.91∘+114.09∘=180∘ 65.91^\circ + 114.09^\circ = 180^\circ , the same angle seen from the other side.

Check it in MATLAB

format short
G = [2 1; 1 3]; u = [1; 0]; v = [0; 1];
c = (u' * G * v) / (sqrt(u' * G * u) * sqrt(v' * G * v))
acos(c) * 180 / pi
c2 = dot([1 2 -1], [3 0 2]) / (norm([1 2 -1]) * norm([3 0 2]))

Output:

c =

    0.4082

ans =

   65.9052

c2 =

    0.1132

MATLAB never rounds along the way: it keeps about 16 digits and only trims when displaying the result. That is why, in the assignment, you copy what comes out, not what you would compute with the calculator rounding each step.

Questions you might ask

How do I know that expression is a dot product? It is symmetric (the coefficients of x1y2x_1 y_2 and x2y1x_2 y_1 are equal) and positive definite: u⃗⋅u⃗=2x12+2x1x2+3x22>0\vec u \cdot \vec u = 2x_1^2 + 2x_1 x_2 + 3x_2^2 > 0 if u⃗≠0⃗\vec u \neq \vec 0. In topic 6 you will check it in one go with the minors of GG: 2>02 > 0 and 2⋅3−1=5>02 \cdot 3 – 1 = 5 > 0.

And if I am asked for the angle and not the cosine? α=arccos⁡(cos⁡α)\alpha = \arccos(\cos \alpha), between 0∘0^\circ and 180∘180^\circ. With the calculator in degrees, or multiplying by 180/π180 / \pi if it comes out in radians.

Putting the coefficient of x1y2x_1 y_2 divided by 2 in GG. That is done with the quadratic forms of topic 6 (where x1x2x_1 x_2 merges the two cross terms); with the expression of a dot product, x1y2x_1 y_2 and x2y1x_2 y_1 already come separately and each goes to its position as it is.

Try it yourself

  • With the same GG, compute ∥(1,1)∥\lVert (1, 1) \rVert. (2+2+3=7\sqrt{2 + 2 + 3} = \sqrt7.)
  • Cosine, with the standard product, between (1,1,1)(1, 1, 1) and (1,0,0)(1, 0, 0). (1/3≈0.57741 / \sqrt3 \approx 0.5774.)

Problem 3: is this basis orthogonal?

Statement. In R3\mathbb{R}^3 consider the dot product with Gram matrix (in the standard basis) G=(210120001)G = \begin{pmatrix} 2 & 1 & 0 \\ 1 & 2 & 0 \\ 0 & 0 & 1 \end{pmatrix}, the one from the theory where pop and rock “count together”. a) Is the standard basis orthogonal? b) Check that S={(1,−1,0),(1,1,0),(0,0,1)}S = \{(1, -1, 0), (1, 1, 0), (0, 0, 1)\} is an orthogonal basis and, starting from it, compute an orthonormal basis.

The idea

A basis is orthogonal if the Gram matrix in it is diagonal. For the standard basis, it is GG itself. For SS, you have to compute the three cross products with GG (or the matrix S G StS \, G \, S^t, with the vectors of SS as rows). Then each vector is divided by its norm, also with GG.

G(1,1,0)t=(3,3,0)G (1, 1, 0)^t = (3, 3, 0) and G(1,−1,0)t=(1,−1,0)G (1, -1, 0)^t = (1, -1, 0). With those two columns, every product can be done in your head.

Step by step

a) In the standard basis the Gram matrix is GG, which is not diagonal: e⃗1⋅e⃗2=g12=1≠0\vec e_1 \cdot \vec e_2 = g_{12} = 1 \neq 0. It is not orthogonal with this product (although it is with the standard one).

b) Cross products:

  • (1,−1,0)⋅G(1,1,0)=(1,−1,0)⋅(3,3,0)=3−3+0=0(1, -1, 0) \cdot_G (1, 1, 0) = (1, -1, 0) \cdot (3, 3, 0) = 3 – 3 + 0 = 0.
  • (1,−1,0)⋅G(0,0,1)=(1,−1,0)⋅G(0,0,1)t=(1,−1,0)⋅(0,0,1)=0(1, -1, 0) \cdot_G (0, 0, 1) = (1, -1, 0) \cdot G(0, 0, 1)^t = (1, -1, 0) \cdot (0, 0, 1) = 0.
  • (1,1,0)⋅G(0,0,1)=(1,1,0)⋅(0,0,1)=0(1, 1, 0) \cdot_G (0, 0, 1) = (1, 1, 0) \cdot (0, 0, 1) = 0.

Pairwise orthogonal. They are three independent vectors (non-zero orthogonal vectors always are), so SS is an orthogonal basis. The norms:

∥(1,−1,0)∥G=(1,−1,0)⋅(1,−1,0)=2,∥(1,1,0)∥G=(1,1,0)⋅(3,3,0)=6,∥(0,0,1)∥G=1 \lVert (1, -1, 0) \rVert_G = \sqrt{(1, -1, 0) \cdot (1, -1, 0)} = \sqrt2, \quad \lVert (1, 1, 0) \rVert_G = \sqrt{(1, 1, 0) \cdot (3, 3, 0)} = \sqrt6, \quad \lVert (0, 0, 1) \rVert_G = 1

Dividing:

Bon={12(1,−1,0),  16(1,1,0),  (0,0,1)} B_{\text{on}} = \left\{ \tfrac{1}{\sqrt2} (1, -1, 0), \; \tfrac{1}{\sqrt6} (1, 1, 0), \; (0, 0, 1) \right\}
Orthogonal with respect to GThe matrix S G Sᵗ is diagonal 2, 6, 1, with zeros off the diagonal. Dividing each vector by its norm with G, root 2, root 6 and 1, gives the orthonormal basis.S · G · Sᵗ200060001zeros off: orthogonal÷ ‖·‖(1, −1, 0) / √2(1, 1, 0) / √6(0, 0, 1)orthonormal basis with respect to G
On the diagonal, the squared norms; off it, zeros.

Check

The Gram matrix of SS with GG is diag(2,6,1)\text{diag}(2, 6, 1): zeros off the diagonal and, on the diagonal, the squares of the norms. Dividing by the norms gives the identity.

Check it in MATLAB

G = [2 1 0; 1 2 0; 0 0 1];
S = [1 -1 0; 1 1 0; 0 0 1];    % the vectors of S as rows
S * G * S'                     % their Gram matrix: diagonal

Output:

ans =

     2     0     0
     0     6     0
     0     0     1

With the vectors as rows, the Gram matrix is S * G * S' (row ii times GG times column jj). With the vectors as columns it would be C' * G * C, the congruence formula: it is the same with C=StC = S^t.

Questions you might ask

Why is the standard basis not orthogonal here? Because orthogonality depends on the product: with GG, “pop” and “rock” have a product of 1, not 0. In the drawing, the axes “come closer”.

Do I have to prove separately that SS is a basis? No: a set of non-zero pairwise orthogonal vectors is always independent, and three independent vectors in R3\mathbb{R}^3 are a basis.

Computing the norms in b) with the standard product: ∥(1,1,0)∥=2\lVert (1, 1, 0) \rVert = \sqrt2. With GG it is 6\sqrt6. If the statement gives a product, every measurement (products, norms, angles) is done with it.

Try it yourself

  • Is the basis {(1,0,0),(1,−2,0),(0,0,1)}\{(1, 0, 0), (1, -2, 0), (0, 0, 1)\} orthogonal with GG? (Yes: (1,0,0)⋅G(1,−2,0)=2−2=0(1, 0, 0) \cdot_G (1, -2, 0) = 2 – 2 = 0.)
  • Compute its orthonormal basis. (Norms 2\sqrt2, 6\sqrt6 and 1.)

Problem 4: Gram-Schmidt and a projection

Statement. A playlist is described by two taste “directions”: w⃗1=(1,1,1)\vec w_1 = (1, 1, 1) (a bit of everything) and w⃗2=(1,2,0)\vec w_2 = (1, 2, 0) (more rock than pop, no classical). Let W=L{w⃗1,w⃗2}W = L\{\vec w_1, \vec w_2\}, with the standard product.

a) Compute an orthogonal basis of WW by Gram-Schmidt.

b) Compute the orthogonal projection of Sergio’s taste, s⃗=(2,1,2)\vec s = (2, 1, 2), onto WW, and the distance from s⃗\vec s to WW.

The idea

Gram-Schmidt: u⃗1=w⃗1\vec u_1 = \vec w_1, and from w⃗2\vec w_2 you remove its projection onto u⃗1\vec u_1. With the orthogonal basis, the projection onto WW is the sum of the projections onto each u⃗j\vec u_j, and the distance is the norm of what is left.

w⃗2⋅u⃗1=3\vec w_2 \cdot \vec u_1 = 3 and u⃗1⋅u⃗1=3\vec u_1 \cdot \vec u_1 = 3: the coefficient of the first subtraction is 1.

In words, with the playlist. w⃗1=(1,1,1) \vec w_1 = (1, 1, 1) is “a bit of everything”. Projecting any taste onto (1,1,1) (1, 1, 1) gives its average in each genre: w⃗2=(1,2,0) \vec w_2 = (1, 2, 0) adds up to 3 hours over three genres, one hour on average, and that is why its projection is exactly 1⋅(1,1,1) 1 \cdot (1, 1, 1) . What Gram-Schmidt removes from w⃗2 \vec w_2 is that “a bit of everything” part, which was already in w⃗1 \vec w_1 . What is left, (0,1,−1) (0, 1, -1) , is what makes w⃗2 \vec w_2 different: one hour more rock and one hour less classical than the average. That difference no longer shares anything with w⃗1 \vec w_1 , and that is why it is perpendicular.

The same happens with the projection of s⃗ \vec s : its coefficient on u⃗1 \vec u_1 is 53 \frac53 , my average hours ((2+1+2)/3 (2 + 1 + 2) / 3 ), and its coefficient on u⃗2 \vec u_2 says how much I look like “more rock than classical”.

Step by step

a)

u⃗1=(1,1,1),u⃗2=w⃗2−w⃗2⋅u⃗1u⃗1⋅u⃗1 u⃗1=(1,2,0)−33(1,1,1)=(0,1,−1) \vec u_1 = (1, 1, 1), \qquad \vec u_2 = \vec w_2 – \frac{\vec w_2 \cdot \vec u_1}{\vec u_1 \cdot \vec u_1} \, \vec u_1 = (1, 2, 0) – \frac33 (1, 1, 1) = (0, 1, -1)

Check: (1,1,1)⋅(0,1,−1)=0+1−1=0(1, 1, 1) \cdot (0, 1, -1) = 0 + 1 – 1 = 0.

b) With s⃗⋅u⃗1=2+1+2=5\vec s \cdot \vec u_1 = 2 + 1 + 2 = 5, s⃗⋅u⃗2=0+1−2=−1\vec s \cdot \vec u_2 = 0 + 1 – 2 = -1, u⃗1⋅u⃗1=3\vec u_1 \cdot \vec u_1 = 3 and u⃗2⋅u⃗2=2\vec u_2 \cdot \vec u_2 = 2:

projWs⃗=53(1,1,1)−12(0,1,−1)=(53,  53−12,  53+12)=(53,76,136) \text{proj}_W \vec s = \frac53 (1, 1, 1) – \frac12 (0, 1, -1) = \left( \tfrac53, \; \tfrac53 – \tfrac12, \; \tfrac53 + \tfrac12 \right) = \left( \tfrac53, \tfrac76, \tfrac{13}{6} \right)

What is left:

s⃗−projWs⃗=(2−53,  1−76,  2−136)=(13,−16,−16) \vec s – \text{proj}_W \vec s = \left( 2 – \tfrac53, \; 1 – \tfrac76, \; 2 – \tfrac{13}{6} \right) = \left( \tfrac13, -\tfrac16, -\tfrac16 \right)

and the distance is its norm:

d(s⃗,W)=19+136+136=636=16≈0.4082 d(\vec s, W) = \sqrt{\tfrac19 + \tfrac{1}{36} + \tfrac{1}{36}} = \sqrt{\tfrac{6}{36}} = \frac{1}{\sqrt6} \approx 0.4082

My taste is very close to that playlist: almost everything I listen to is explained by its two directions.

Projection onto a planeSketch of the plane W spanned by u1 = (1,1,1) and u2 = (0,1,−1), Sergio’s vector (2,1,2) and its projection (5/3, 7/6, 13/6) on the plane. The dashed green segment joining them is the rest, perpendicular to the plane, of length 1 over root 6.u₁u₂projs = (2, 1, 2)restWproj = 5/3·u₁ − 1/2·u₂= (5/3, 7/6, 13/6)rest = (1/3, −1/6, −1/6)d(s, W) = 1/√6 ≈ 0.4082sketch, not to scale
The distance to a subspace is the length of the perpendicular rest.

The same projection, without skipping anything:

StepComputationResultWhy
1s⃗⋅u⃗1=2+1+2 \vec s \cdot \vec u_1 = 2 + 1 + 2 5 5 What my taste shares with “a bit of everything”
2u⃗1⋅u⃗1=1+1+1 \vec u_1 \cdot \vec u_1 = 1 + 1 + 1 3 3 You divide by it so the size of u⃗1 \vec u_1 does not count
353 \frac{5}{3} 53(1,1,1) \frac53 (1, 1, 1) My average hours, in all three genres
4s⃗⋅u⃗2=0+1−2 \vec s \cdot \vec u_2 = 0 + 1 – 2 −1 -1 I listen to less rock than classical: I go against u⃗2 \vec u_2
5−1u⃗2⋅u⃗2=−12 \frac{-1}{\vec u_2 \cdot \vec u_2} = \frac{-1}{2} −12(0,1,−1) -\frac12 (0, 1, -1) The minus sign removes rock and adds classical
6Add 3 and 5(53,76,136) \left( \tfrac53, \tfrac76, \tfrac{13}{6} \right) Adding works because u⃗1 \vec u_1 and u⃗2 \vec u_2 are perpendicular
7s⃗ \vec s minus step 6(13,−16,−16) \left( \tfrac13, -\tfrac16, -\tfrac16 \right) What the playlist does not explain about my taste
819+136+136=436+236 \tfrac19 + \tfrac{1}{36} + \tfrac{1}{36} = \tfrac{4}{36} + \tfrac{2}{36} 16 \frac{1}{\sqrt6} The norm of the rest: 6/36=6/6=1/6 \sqrt{6/36} = \sqrt6 / 6 = 1 / \sqrt6

Look at step 6: if u⃗1 \vec u_1 and u⃗2 \vec u_2 were not perpendicular, adding them would count twice what they share. That is what Gram-Schmidt was for.

Check

The remainder has to be perpendicular to all of WW: (13,−16,−16)⋅(1,1,1)=13−13=0(\tfrac13, -\tfrac16, -\tfrac16) \cdot (1, 1, 1) = \tfrac13 – \tfrac13 = 0 and (13,−16,−16)⋅(0,1,−1)=−16+16=0(\tfrac13, -\tfrac16, -\tfrac16) \cdot (0, 1, -1) = -\tfrac16 + \tfrac16 = 0.

Check it in MATLAB

format rat
W = [1 1 1; 1 2 0]';                                 % w1 and w2 as columns
u1 = W(:,1); u2 = W(:,2) - dot(W(:,2), u1) / dot(u1, u1) * u1
s = [2; 1; 2];
p = dot(s, u1) / dot(u1, u1) * u1 + dot(s, u2) / dot(u2, u2) * u2
r = s - p
format short
norm(r)

Output:

u2 =

       0       
       1       
      -1       

p =

       5/3     
       7/6     
      13/6     

r =

       1/3     
      -1/6     
      -1/6     

ans =

    0.4082

The fractions are the paper ones, and the distance, 1/6≈0.40821 / \sqrt6 \approx 0.4082.

Questions you might ask

Can I project without orthogonalising first? With the sum formula, no: it only works with an orthogonal basis. With any basis you have to solve a system (the normal equations from the theory).

Why is the distance the norm of the remainder? Because the remainder is perpendicular to WW: it is the “shortest jump” from s⃗\vec s to WW, like the height of a point above the floor.

Projecting with the original basis, s⃗⋅w⃗1w⃗1⋅w⃗1w⃗1+s⃗⋅w⃗2w⃗2⋅w⃗2w⃗2\frac{\vec s \cdot \vec w_1}{\vec w_1 \cdot \vec w_1} \vec w_1 + \frac{\vec s \cdot \vec w_2}{\vec w_2 \cdot \vec w_2} \vec w_2. As w⃗1\vec w_1 and w⃗2\vec w_2 are not orthogonal, you count the shared part twice and the remainder is no longer perpendicular.

Try it yourself

  • Project (1,0,0)(1, 0, 0) onto WW. ((13,13,13)(\tfrac13, \tfrac13, \tfrac13), because its product with u⃗2\vec u_2 is 0.)
  • Compute a vector perpendicular to WW with the cross product: (1,1,1)×(1,2,0)(1, 1, 1) \times (1, 2, 0). Is it parallel to the remainder in b)? (Yes: (−2,1,1)=−6⋅(13,−16,−16)(-2, 1, 1) = -6 \cdot (\tfrac13, -\tfrac16, -\tfrac16).)

Problem 5: how the exam asks it

Exam-style part. [5 points] In R3\mathbb{R}^3, with respect to the standard basis, consider the dot product f(u⃗,v⃗)=2x1x2+x1y2+y1x2+y1y2+3z1z2f(\vec u, \vec v) = 2x_1 x_2 + x_1 y_2 + y_1 x_2 + y_1 y_2 + 3 z_1 z_2, with u⃗=(x1,y1,z1)\vec u = (x_1, y_1, z_1) and v⃗=(x2,y2,z2)\vec v = (x_2, y_2, z_2). a) (1 point) Write its Gram matrix GG and the matrix expression of ff. b) (1.5 points) Compute the norms of u⃗=(1,0,1)\vec u = (1, 0, 1) and v⃗=(0,1,−1)\vec v = (0, 1, -1). c) (1.5 points) Compute the cosine of the angle between u⃗\vec u and v⃗\vec v with respect to ff. d) (1 point) Give a non-zero vector orthogonal to u⃗\vec u with respect to ff.

The idea

It is part c) of the bilinear forms exercise in the exams, on its own: there you are given a bilinear form, you check in topic 6 that it is a dot product, and then you compute the cosine with it. Careful with the notation: here x1,y1,z1x_1, y_1, z_1 are the components of the first vector and x2,y2,z2x_2, y_2, z_2 those of the second.

Row 1 of GG holds the coefficients of x1x2x_1 x_2, x1y2x_1 y_2 and x1z2x_1 z_2; row 2, those of y1x2y_1 x_2, y1y2y_1 y_2, y1z2y_1 z_2; row 3, those of z1z_1.

Step by step

a) Reading coefficients: x1x2→2x_1 x_2 \to 2, x1y2→1x_1 y_2 \to 1, y1x2→1y_1 x_2 \to 1, y1y2→1y_1 y_2 \to 1, z1z2→3z_1 z_2 \to 3, and 0 elsewhere:

G=(210110003),f(u⃗,v⃗)=(x1y1z1)G(x2y2z2) G = \begin{pmatrix} 2 & 1 & 0 \\ 1 & 1 & 0 \\ 0 & 0 & 3 \end{pmatrix}, \qquad f(\vec u, \vec v) = \begin{pmatrix} x_1 & y_1 & z_1 \end{pmatrix} G \begin{pmatrix} x_2 \\ y_2 \\ z_2 \end{pmatrix}

It is symmetric, as a dot product must be.

b) Gu⃗ t=(2+0+0,  1+0+0,  0+0+3)=(2,1,3)G \vec u^{\,t} = (2 + 0 + 0, \; 1 + 0 + 0, \; 0 + 0 + 3) = (2, 1, 3), so f(u⃗,u⃗)=(1,0,1)⋅(2,1,3)=2+0+3=5f(\vec u, \vec u) = (1, 0, 1) \cdot (2, 1, 3) = 2 + 0 + 3 = 5 and ∥u⃗∥=5\lVert \vec u \rVert = \sqrt5.

Gv⃗ t=(0+1+0,  0+1+0,  0+0−3)=(1,1,−3)G \vec v^{\,t} = (0 + 1 + 0, \; 0 + 1 + 0, \; 0 + 0 – 3) = (1, 1, -3), so f(v⃗,v⃗)=(0,1,−1)⋅(1,1,−3)=0+1+3=4f(\vec v, \vec v) = (0, 1, -1) \cdot (1, 1, -3) = 0 + 1 + 3 = 4 and ∥v⃗∥=2\lVert \vec v \rVert = 2.

c) f(u⃗,v⃗)=(1,0,1)⋅Gv⃗ t=(1,0,1)⋅(1,1,−3)=1+0−3=−2f(\vec u, \vec v) = (1, 0, 1) \cdot G \vec v^{\,t} = (1, 0, 1) \cdot (1, 1, -3) = 1 + 0 – 3 = -2:

cos⁡α=f(u⃗,v⃗)∥u⃗∥ ∥v⃗∥=−25⋅2=−15≈−0.4472⟹α≈116.57∘ \cos \alpha = \frac{f(\vec u, \vec v)}{\lVert \vec u \rVert \, \lVert \vec v \rVert} = \frac{-2}{\sqrt5 \cdot 2} = -\frac{1}{\sqrt5} \approx -0.4472 \quad \Longrightarrow \quad \alpha \approx 116.57^\circ

d) w⃗=(x,y,z)\vec w = (x, y, z) is orthogonal to u⃗\vec u if f(u⃗,w⃗)=(2,1,3)⋅(x,y,z)=2x+y+3z=0f(\vec u, \vec w) = (2, 1, 3) \cdot (x, y, z) = 2x + y + 3z = 0. For example, w⃗=(−1,2,0)\vec w = (-1, 2, 0): −2+2+0=0-2 + 2 + 0 = 0. (Any vector of that plane, except 0⃗\vec 0, will do.)

The cosine part, step by stepFive rows: the matrix G, the norms root 5 and 2, the product f(u, v) = −2, the cosine −1 over root 5 and the condition 2x + y + 3z = 0 with the vector (−1, 2, 0).a) G[[2,1,0],[1,1,0],[0,0,3]]b) ‖u‖, ‖v‖√5, 2c) f(u, v)−2cos α−1/√5 ≈ −0.4472d) ⟂ u2x + y + 3z = 0 → (−1, 2, 0)
Everything comes from G: products, norms, cosine and perpendiculars.

Check it in MATLAB

G = [2 1 0; 1 1 0; 0 0 3]; u = [1; 0; 1]; v = [0; 1; -1];
nu = sqrt(u' * G * u), nv = sqrt(v' * G * v)
c = (u' * G * v) / (nu * nv)
acos(c) * 180 / pi
format rat
null((G * u)', 'r')     % the vectors orthogonal to u with f, as columns

Output:

nu =

    2.2361

nv =

     2

c =

   -0.4472

ans =

  116.5651

ans =

      -1/2           -3/2     
       1              0       
       0              1       

null((G * u)', 'r') solves 2x+y+3z=02x + y + 3z = 0 and gives a basis of the plane of vectors orthogonal to u⃗\vec u: (−12,1,0)(-\tfrac12, 1, 0) and (−32,0,1)(-\tfrac32, 0, 1). The first, times 2, is our (−1,2,0)(-1, 2, 0).

Check and how it is marked

The cosine is between −1-1 and 11, and it is negative: with ff, u⃗\vec u and v⃗\vec v make an obtuse angle, although with the standard product their product, 0+0−1=−10 + 0 – 1 = -1, is also negative and the angle is a different one (cos⁡=−1/2\cos = -1/2, 120∘120^\circ).

Indicative split of the 5 points (mine, not the official one):

PartWhat is assessedPoints
a)GG correctly placed and symmetric0.5
a)Expression row · GG · column0.5
b)Each norm with GG and the positive root0.75 + 0.75
c)f(u⃗,v⃗)f(\vec u, \vec v) with GG0.75
c)The quotient and the cosine0.75
d)The condition 2x+y+3z=02x + y + 3z = 0 and a non-zero vector1

Questions you might ask

And if ff were not a dot product? Then you could not talk about norm or angle, and the exam says “if possible”: that is why the form is classified first (topic 6). Here GG has minors 22, 2−1=12 – 1 = 1 and 1⋅3=31 \cdot 3 = 3, all positive: it is positive definite.

Can I give w⃗=(0,0,0)\vec w = (0, 0, 0) in d)? No: the statement asks for a non-zero vector, and 0⃗\vec 0 is orthogonal to everything without contributing anything.

Computing the cosine with the standard product because “the vectors are in the standard basis”. The standard basis tells you in which coordinates the vectors come; the product is given by ff. With the standard one you would get −12-\tfrac12, and the whole part, zero marks.

Try it yourself

  • With the same ff, cosine between (1,0,0)(1, 0, 0) and (0,1,0)(0, 1, 0). (1/2≈0.70711 / \sqrt2 \approx 0.7071.)
  • Is (1,−2,1)(1, -2, 1) orthogonal to u⃗\vec u with ff? (2−2+3=32 – 2 + 3 = 3: no.)

Common mistakes in this practice

  • Doing the products with the identity when the statement gives another Gram matrix (or coordinates in another basis).
  • Writing column ⋅\cdot GG ⋅\cdot row.
  • Putting ±\pm in the norm.
  • Rounding roots or quotients before the last step.
  • Halving the cross terms of the expression of a dot product (that belongs to quadratic forms).
  • Projecting with a basis that is not orthogonal.

Try it in the MATLAB playground

In the website’s MATLAB playground you have a small dot product “calculator” with any matrix you like:

G = [2 1 0; 1 1 0; 0 0 3];          % change the dot product here
u = [1; 0; 1]; v = [0; 1; -1];      % and the vectors here (as columns)
pe = u' * G * v;
nu = sqrt(u' * G * u); nv = sqrt(v' * G * v);
fprintf('u·v = %g, |u| = %.4f, |v| = %.4f, cos = %.4f\n', pe, nu, nv, pe / (nu * nv))

Things to look at:

  • Set G = eye(3) and compare with the results of problem 5: the standard product is the case G=IG = I.
  • Try a GG that is not symmetric, [2 1 0; 0 1 0; 0 0 3], and swap u and v: you get a different number. That is why a dot product requires a symmetric GG.
  • With G = [1 2; 2 1] and u = [1; -1], u' * G * u is negative: it is not a dot product and the root does not exist.

Summary

  • The Gram matrix in a basis holds in (i,j)(i, j) the product of vector ii by vector jj; with the standard product, G′=CtCG' = C^t C.
  • Every product is computed as row ⋅\cdot GG ⋅\cdot column, first GG times the column.
  • Angles and orthogonality depend on the product: two vectors can be perpendicular with one and not with another.
  • Round only at the end; MATLAB never rounds along the way.
  • Gram-Schmidt subtracts projections onto the new vectors, and the projection onto a subspace is done with an orthogonal basis.

Checklist: have you practised it?

  • Can you build the Gram matrix of a basis and use it with coordinates in that basis?
  • Do you write the matrix of a dot product given by an expression without halving the cross terms?
  • Have you checked that the same pair of vectors changes angle when the product changes?
  • Do you keep roots as roots until the last step?
  • Can you check whether a basis is orthogonal with GG and orthonormalise it?
  • Have you done Gram-Schmidt and a projection, and checked that the remainder is perpendicular?
  • Have you run the MATLAB blocks and checked the outputs?

If you answered yes to almost everything, you are ready for the Euclidean space exercises, where I no longer give you the step by step and there is a 20-question quiz like the Campus ones.

Next step: exercisesEuclidean space — exam-style exercises with music taste: norms, cosines, Gram matrix, Gram-Schmidt and a Campus-style quiz (and the form I ticked too many times)

Did this article help you? Here is how you can support SergioLearns →