Week 3: Inner-Product Spaces#
Key Terms#
Vector spaces with inner product and norm
\(\mathbb{R}^n\) and \(\mathbb{C}^n\)
Projections on the line
Orthonormal bases
The Gram-Schmidt procedure
Orthogonal and unitary matrices
Preparation and Syllabus#
Long Day: Sections 2.3, 2.4, and 2.5
Short Day: Section 2.6
Python demo for Week 3
Exercises – Long Days#
1: Orthonormal Basis (Coordinates). By Hand.#
Question a#
Do the vectors
constitute an orthonormal basis in \(\mathbb{R}^3\)?
Hint
The vectors must be pairwise orthogonal, meaning each of them must be perpendicular to each of the others. How is it that we check for that?
Hint
Two vectors are orthogonal precisely when their scalar product is 0, but more is needed to ensure orthonormality.
Hint
A basis is orthonormal if the vectors are mutually orthogonal and each of them has a length (norm) of 1.
Answer
\(\langle \pmb{u}_1, \pmb{u}_2 \rangle = \langle \pmb{u}_1, \pmb{u}_3 \rangle = \langle \pmb{u}_2, \pmb{u}_3 \rangle = 0\) and all three vectors have a norm of 1, so the three vectors constitute an orthonormal basis for \(\mathbb{R}^3\).
Question b#
Consider the vector \(\pmb{x} = [1,2,3]^T\). Compute the inner products \(\langle \pmb{x}, \pmb{u}_k \rangle\) for \(k=1,2,3\).
Hint
This is just the usual dot products \(\pmb{x} \cdot \pmb{u}_k\)
Question c#
Let us denote the basis by \(\beta = \pmb{u}_1, \pmb{u}_2, \pmb{u}_3\). State the coordinate vector \({}_{\beta} \pmb{x}\) for \(\pmb{x}\) with respect to \(\beta\). Compute the norm of both \(\pmb{x}\) and the coordinate vector \({}_{\beta} \pmb{x}\).
Question d#
Create the \(3\times 3\) matrix \(U = [\pmb{u}_1 \vert \pmb{u}_2 \vert \pmb{u}_3]\) with \(\pmb{u}_1, \pmb{u}_2, \pmb{u}_3\) as the three columns. Compute \(U^T \pmb{x}\) and compare the result with the previous exercise.
2: Orthonormal Basis (Construction). By Hand.#
Create an orthonormal basis in \(\mathbb{R}^3\), in which
is the first basis vector.
Hint
You can perhaps guess a unit vector that is perpendicular to the given vector.
Hint
What about \(\left(0,0,1\right)\)?
Hint
Can you guess another one? Try starting out by finding a vector that is perpendicular to both the given vector and to \(\left(0,0,1\right)\).
Hint
What about \(\left(1,-1,0 \right)\)? And now, what are missing?
Hint
\(\left(1,-1,0 \right)\) is to be normalized.
Answer
A suggestion of an orthonormal basis can be
but others are possible! Think about why that is the case.
3: Orthonormalizing. By Hand.#
Determine the solution set to the homogeneous equation
and justify for it being a subspace in \(\mathbb{R}^3\). Find an orthonormal basis for this solution space.
Hint
If we are to find an orthonormal basis for the solution space, we must first find a basis for the solution space, which means we must solve the equation.
Hint
Let \(x_2=t_1\) and \(x_3=t_2\).
Hint
With \(x_2=t_1\) and \(x_3=t_2\) we find that
but what is then the solution space?
Hint
The solution space is \(\mathrm{span}\lbrace(-1,1,0),(-1,0,1)\rbrace\), so a basis can be constituted by the vectors
Now they just need orthonormalization.
Hint
When two vectors are to be orthonormalized, we must turn them relative to one another within the space they span, such that they become orthogonal (are perpendicular to one another) while still spanning the same space, and we must also normalize them, meaning extend or shorten them so that they all have a length of 1.
Hint
Luckily we can do that by simply following the Gram-Schmidt method.
Answer
An orthonormal basis for the solution space to the equation can be constituted by the vectors
4: Orthogonal Projections#
Let \(\boldsymbol{y}=(2,1,2) \in \mathbb{R}^3\) be given. Then the projection of \(\boldsymbol{x} \in \mathbb{R}^3\) onto the line \(Y = \mathrm{span}\{\boldsymbol{y}\}\) is given by:
where \(\boldsymbol{u} = \frac{\boldsymbol{y}}{||\boldsymbol{y}||}\).
Question a#
Let \(\boldsymbol{x} = (1,2,3) \in \mathbb{R}^3\). Compute \(\operatorname{Proj}_Y(\boldsymbol{x})\), \(\operatorname{Proj}_Y(\boldsymbol{y})\) and \(\operatorname{Proj}_Y(\boldsymbol{u})\).
Question b#
We, as usual, consider all vectors as column vectors. Now set up the \(3 \times 3\) matrix \(P = \boldsymbol{u} \boldsymbol{u}^T\) and compute both \(P\boldsymbol{x}\) and \(P\boldsymbol{y}\).
5: An Orthonormal Basis for a Subspace of \(\mathbb{C}^4\)#
Question a#
Find an orthonormal basis \(\pmb{u}_1, \pmb{u}_2\) for the subspace \(Y = \mathrm{span}\{\pmb{v}_1, \pmb{v}_2\}\) spanned by the vectors:
v1 = Matrix([I, 1, 1, 0])
v2 = Matrix([0, I, I, sqrt(2)])
Hint
It can be found using the Gram-Schmidt algorithm. Check your calculation with the SymPy command GramSchmidt([v1,v2], orthonormal=True)
.
Question b#
Let
Compute \(\langle \pmb{x}, \pmb{u}_1 \rangle\), \(\langle \pmb{x}, \pmb{u}_2 \rangle\) as well as
What does this linear combination give? Does \(\pmb{x}\) belong to the subspace \(Y\)?
6: A Python Algorithm#
Question a#
Consider the following code and explain what it does. Before you run the code in a Jupyter Notebook you must explain what will be the output.
from sympy import *
from dtumathtools import *
init_printing()
x1, x2, x3 = symbols('x1:4', real=True)
eqns = [Eq(1*x1 + 2*x2 + 3*x3, 1), Eq(4*x1 + 5*x2 + 6*x3, 0), Eq(5*x1 + 7*x2 + 8*x3, -1)]
eqns
A, b = linear_eq_to_matrix(eqns,x1,x2,x3)
T = A.row_join(b) # augmented matrix
A, b, T
Question b#
We continue the Jupyter Notebook with the following code (do not run it yet). Go through the code by hand (calculate the for-loop iterations manually). Which \(T\) matrix will be the output? Copy/paste the code into a chatbot such as https://copilot.microsoft.com/ (log in with your DTU account) and ask the chatbot to explain the code line by line. Now check the result by running the code in a Python Notebook. Remember that T.shape[0]
gives the number of rows in matrix \(T\).
for col in range(T.shape[0]):
for row in range(col + 1, T.shape[0]):
T[row, :] = T[row, :] - T[row, col] / T[col, col] * T[col, :]
T[col, :] = T[col, :] / T[col, col]
T
Question c#
Write Python code that ensures zeroes above the diagonal in matrix \(T\) such that \(T\) ends out being in reduced-row echelon form.
Note
You should not take into account any potential divisions by zero (for general \(T\) matrices). We will assume that the calculations will work well.
Hint
You will need two for-loops, for instance:
for col in range(T.shape[0] - 1, -1, -1):
for row in range(col - 1, -1, -1):
Remember that index \(-1\) in Python refers to the last element.
Hint
Ask a chatbot such as https://copilot.microsoft.com/ (log in with your DTU account) for help if you get stuck. You should first work with the exercise on your own, though.
Answer
# Backward Elimination: Create zeros above the diagonal
for col in range(T.shape[0] - 1, -1, -1):
for row in range(col - 1, -1, -1):
T[row, :] = T[row, :] - T[row, col] * T[col, :]
T
Question d#
What algorithm is it that we have implemented? Test the same algorithm on:
x1, x2, x3, x4 = symbols('x1:5', real=True)
eqns = [Eq(1*x1 + 2*x2 + 3*x3, 1), Eq(4*x1 + 5*x2 + 6*x3, 0), Eq(4*x1 + 5*x2 + 6*x3, 0), Eq(5*x1 + 7*x2 + 8*x3, -1)]
A, b = linear_eq_to_matrix(eqns,x1,x2,x3,x4)
T = A.row_join(b) # augmented matrix
Answer
It is Gaussian elimination for solving linear equation systems, see also Mat1ha. The output is the reduced row-echelon form of the input matrix. We have not taken into account column switches, nor row switches, and division by zero is thus a risk in case the first \(n\) columns of an \(n \times m\) matrix \(T\) are not linearly independent.
7: Orthogonal Polynomials#
This is an exercise from the textbook. You can find help there.
Consider the list \(\alpha=1,x,x^{2},x^{3}\) of polynomials in \(P_{3}([-1,1])\) constructed with the \(L^{2}\)-inner product.
Question a#
Argument for why \(\alpha\) is a list of linearly independent vectors.
Hint
The assumption is that \(c_1 + c_2 x + c_3 x^2 + c_4 x^3 = 0\) for all \(x \in [-1,1]\). We want to show that \(c_1 = c_2 = c_3 = c_4 = 0\), since we then will have shown that the polynomials in \(\alpha\) are linearly independent.
Hint
Choose four different \(x\) values and evaluate the assumption for that choicethere. That will give four equations of the four unknowns \(c_1, c_2, c_3, c_4\). The \(x\) values must be chosen such that the equation system only has the zero solution. (Why?)
Hint
We choose for instance \(x = 0, 1, -1, 1/2\). This results in the following equations: \(c_{1} = 0, c_{1} + c_{2} + c_{3} + c_{4} = 0, c_{1} - c_{2} + c_{3} - c_{4} = 0, c_{1} + \frac{c_{2}}{2} + \frac{c_{3}}{4} + \frac{c_{4}}{8} = 0\)
Hint
Check that the four equations only have the zero solution. Conclude that the four polynomials are linearly independent.
Question b#
Utilize the Gram-Schmidt procedure on \(\alpha\) and show that the procedure gives a normalized version of the Legendre polynomials.
Hint
To compute the norms and the inner products you need to use integrate
in SymPy (or compute the integrals by hand). For example x = symbols('x', real=True)
and integrate(x**2, (x,-1,1))
.
Hint
The polynomial \(u_1\) can be computed by:
x = symbols('x', real=True)
v1 = 1
v2 = x
v3 = x**2
v4 = x**3
v1_norm = sqrt(integrate(v1**2, (x,-1,1)))
u1 = v1/v1_norm
u1
Hint
The polynomial \(u_2\) can be computed by:
w2 = v2 - integrate(v2*u1, (x,-1,1)) * u1
u2 = w2/sqrt(integrate(w2**2, (x,-1,1)))
u2
Hint
The polynomial \(u_3\) can be computed by:
w3 = v3 - integrate(v3*u1, (x,-1,1)) * u1 - integrate(v3*u2, (x,-1,1)) * u2
u3 = w3/sqrt(integrate(w3**2, (x,-1,1)))
Hint
The polynomial \(u_4\) can be computed by:
w4 = v4 - integrate(v4*u1, (x,-1,1)) * u1 - integrate(v4*u2, (x,-1,1)) * u2 - integrate(v4*u3, (x,-1,1)) * u3
u4 = w4/sqrt(integrate(w4**2, (x,-1,1)))
Answer
The answer is achieved by running the code given in the above hints. Compare your answer with legendre(0,x), legendre(1,x), legendre(2,x).factor(), legendre(3,x).factor()
. They should differ by a scaling factor.
Exercises – Short Day#
1: Matrix Multiplications. By Hand.#
Define
Let \(\pmb{a}_1, \pmb{a}_2,\pmb{a}_3,\pmb{a}_4\) denote the columns of \(A\). Let \(\pmb{b}_1, \pmb{b}_2,\pmb{b}_3\) denote the rows of \(A\). We will now calculate \(A\pmb{x}\) in two different ways.
Question a#
Method 1: As a linear combination of the columns. Compute the linear combination
Question b#
Method 2: As a “dot product” of the rows of \(A\) by \(\pmb{x}\). Compute
Note
Since \(\pmb{b}_k\) is a row vector, \((\pmb{b}_k)^T\) is a column vector. The product \(\pmb{b}_k \pmb{x}\) hence corresponds to the dot product of \(\pmb{x}\) and \((\pmb{b}_k)^T\).
Question c#
Compute \(A\pmb{x}\) using SymPy and compare with your calculations in the previous exercises.
2: A Subspace in \(\mathbb{C}^4\) and its Orthogonal Complement#
In \(\mathbb{C}^4\,\) the following vectors are given
A subspace \(Y\) in \(\mathbb{C}^4\) is determined by \(Y=\mathrm{span}\lbrace\pmb{v}_1,\pmb{v}_2,\pmb{v}_3,\pmb{v}_4\rbrace\).
Question a#
v1 = Matrix([1,1,1,1])
v2 = Matrix([3*I,I,I,3*I])
v3 = Matrix([2,0,-2,4])
v4 = Matrix([4-3*I,2-I,-I,6-3*I])
Run the command GramSchmidt([v1,v2,v3,v4], orthonormal=True)
in Python. What does Python tell you?
# GramSchmidt([v1, v2, v3, v4], orthonormal = True)
Question b#
Now show that \((\pmb{v}_1,\pmb{v}_2,\pmb{v}_3)\,\) is a basis for \(Y\), and determine the coordinate vector of \(\pmb{v}_4\,\) with respect to this basis.
Answer
The vectors \(\pmb{v}_1\), \(\pmb{v}_2\), and \(\pmb{v}_3\) are linearly independent, while \(\pmb{v}_4=2\pmb{v}_1-\pmb{v}_2+\pmb{v}_3\), so
constitutes the coordinate vector with respect to the \(v\) basis.
Question c#
State an orthonormal basis for \(Y\).
Hint
The vectors \(\pmb{v}_1\), \(\pmb{v}_2\), and \(\pmb{v}_3\) constitute a basis for \(Y\), so they must be orthonormalized using Gram-Schmidt.
Answer
constitutes an orthonormal basis for \(Y\).
Question d#
Find the coordinate vector of \(\pmb{v}_4 \in Y\) with respect to the orthonormal basis for \(Y\).
Hint
This can be computed using inner products (as usual) or via a fitting matrix-vector product.
Question e#
Determine the orthogonal complement \(Y^\perp\) in \(\mathbb{C}^4\) to \(Y\).
Hint
First, try guessing a vector and check whether it is located within \(Y\). That is, check whether it is linearly independent to the basis for \(Y\).
Hint
You could for example guess on \((1,0,0,0)\).
Hint
To be within the orthogonal compliment, we must find a vector that is orthogonal to the basis vectors in \(Y\).
Hint
Use Gram-Shmidt by continuing from Question b and just add the vector you have guessed above. You do not need to normalize the vector in the final step. (Why not?)
Answer
The orthogonal compliment is a 1-dimensional subspace of \(\mathbb{C}^4\) that is spanned by
Question f#
Choose a vector \(\pmb{y}\) in \(Y^\perp\) and choose a vector \(\pmb{x}\) in \(Y\). Compute \(\Vert \pmb{x} \Vert\), \(\Vert \pmb{y} \Vert\) and \(\Vert \pmb{x} + \pmb{y} \Vert\). Check that \(\Vert \pmb{x} \Vert^2 +\Vert \pmb{y} \Vert^2 = \Vert \pmb{x} + \pmb{y} \Vert^2\).
3: Orthogonal Projection on a Plane#
Let a matrix \(U = [\pmb{u}_1, \pmb{u}_2]\) be given by:
Question a#
Show that \(\pmb{u}_1, \pmb{u}_2\) is an orthonormal basis for \(Y = \mathrm{span}\{\pmb{u}_1, \pmb{u}_2\}\).
Answer
A calculation will show that \(\langle \pmb{u}_1, \pmb{u}_2 \rangle = 0\) and \(\Vert \pmb{u}_1 \Vert = \Vert \pmb{u}_2 \Vert = 1\). This shows that \(\pmb{u}_1, \pmb{u}_2\) is an orthonormal list of vectors. This list is thus linearly independent and hence a basis for \(Y\). We conclude that \(\pmb{u}_1, \pmb{u}_2\) is an orthonormal basis for \(Y\).
Question b#
Let \(P = U U^* \in \mathbb{R}^{3 \times 3}\). This will give us a projection matrix that describes the orthogonal projection \(\pmb{x} \mapsto P \pmb{x}\), \(\mathbb{R}^3 \to \mathbb{R}^3\) onto the plane \(Y = \mathrm{span}\{\pmb{u}_1, \pmb{u}_2\}\). Verify that \(P^2 = P\), \(P \pmb{u}_1 = \pmb{u}_1\), and \(P \pmb{u}_2 = \pmb{u}_2\).
Answer
From this it is easy to show that \(P \pmb{u}_1 = \pmb{u}_1\), and \(P \pmb{u}_2 = \pmb{u}_2\). In fact it applies that \(P (c_1 \pmb{u}_1 + c_2 \pmb{u}_2) = c_1 \pmb{u}_1 + c_2 \pmb{u}_2\) for all choices of \(c_1, c_2 \in \mathbb{R}\).
Question c#
Choose a vector \(\pmb{x} \in \mathbb{R}^3\) that does not belong to \(Y\), and find the projection \(\mathrm{proj}_Y(\pmb{x})\) of \(\pmb{x}\) down onto the plane \(Y\). Illustrate \(\pmb{x}\), \(Y\), and \(\mathrm{proj}_Y(\pmb{x})\) in a plot.
Answer
One can for instance choose \(\pmb{x} = [1,2,3]^T\). One must then compute \(\mathrm{proj}_Y(\pmb{x}) = P \pmb{x} = [2,0,2]^T\). Note that \([2,0,2]^T = 2\sqrt{2} \pmb{u}_2 \in Y\).
U = Matrix([[sqrt(3)/3, sqrt(2)/2], [sqrt(3)/3, 0], [-sqrt(3)/3, sqrt(2)/2]])
P = U * U.T
x = Matrix([1,2,3])
t1, t2 = symbols('t1 t2')
r = t1 * U[:,0] + t2 * U[:,1]
p = dtuplot.plot3d_parametric_surface(r[0], r[1], r[2], (t1, -5, 5), (t2, -5, 5), show = False, rendering_kw = {"alpha": 0.5})
p.extend(dtuplot.scatter((x, P*x), show = False))
p.xlim = [-5,5]
p.ylim = [-5,5]
p.zlim = [-5,5]
p.show()
Question d#
Show that \(\pmb{x} - \mathrm{proj}_Y(\pmb{x})\) belongs to \(Y^\perp\).
Hint
With the choice \(\pmb{x} = [1,2,3]^T\) we achieve \(\pmb{y} := \pmb{x} - \mathrm{proj}_Y(\pmb{x}) = [-1,2,1]^T\). You must show that the \(\pmb{y}\) vector is orthogonal to all vectors in \(Y\).
Hint
An arbitrary vector in \(Y\) can be written as \(c_1 \pmb{u}_1 + c_2 \pmb{u}_2\), where \(c_1, c_2 \in \mathbb{R}\).
Hint
Show that \(\langle \pmb{y}, \pmb{u}_1 \rangle = 0\) and \(\langle \pmb{y}, \pmb{u}_2 \rangle = 0\)
Answer
Since \(\langle c_1 \pmb{u}_1 + c_2 \pmb{u}_2, \pmb{y} \rangle = c_1 \langle \pmb{u}_1 , \pmb{y} \rangle + c_2 \langle \pmb{u}_2 , \pmb{y} \rangle = 0 + 0 = 0\) we see that \(\pmb{y} = [-1,2,1]^T \in Y^\perp\).
4: Unitary Matrices#
Let a matrix \(F\) be given by:
n = 4
F = 1/sqrt(n) * Matrix(n, n, lambda k,j: exp(2*pi*I*k*j/n))
F
Determine whether the following propositions are true or false:
\(F\) is unitary
\(F\) is invertible
\(F\) is orthogonal
\(F\) is symmetric
\(F\) is Hermitian
The columns of \(F\) constitute an orthonormal basis for \(\mathbb{C}^4\)
The columns of \(F\) constitute an orthonormal basis for \(\mathbb{R}^4\)
\(-F = F^{-1}\)
Answer
Yes
Yes
No
Yes
No
Yes
No
No
Remark: The matrix is called the Fourier matrix. It fulfills for all \(n\) that: \(\overline{F} = F^* = F^{-1}\) and \(F = F^T\). The map \(\pmb{x} \mapsto F^* \pmb{x}\) is called the descrete Fourier transform and is important in many parts of engineering science. See for instance https://bootcamp.uxdesign.cc/the-most-important-algorithm-of-all-time-9ff1659ff3ef - Fast Fourier transform is the same map as \(\pmb{x} \mapsto F^* \pmb{x}\), just implemented in a smart way.