Skip to main content

Linear Algebra with SageMath Learn math with open-source software

Section 10.5 Gram-Schmidt Process

The Gram-Schmidt process is an algorithm used to create an orthonormal set from a linearly independent set of vectors. An orthonormal set of vectors has two key properties:
Suppose \(v_1, v_2, \ldots, v_n\text{,}\) is a linearly independent set of vectors, the algorithm can be described as follows. We begin by finding the orthogonal set:
\begin{equation*} u_k = v_k - \sum_{j=1}^{k-1} \frac{v_k \cdot u_j}{||u_j||^2} u_j \end{equation*}
We then normalize these vectors to obtain the orthonormal set:
\begin{equation*} e_k = \frac{u_k}{||u_k||} \end{equation*}

Subsection 10.5.1 Finding an Orthogonal Basis

We will first implement Gram-Schmidt manually. In the following example, we use the basis \(\{v_1, v_2\}\) of a subspace of \(\R^3\text{.}\)
If \(p_2 = \frac{v_2 \cdot u_1}{||u_1||^2} u_1 \text{.}\) Then we can compute the orthogonal set by:
\(u_1 = v_1,\quad u_2 = v_2 - p_2\)
The Gram-Schmidt algorithm is implemented in Sage as a method for matrices. Therefore, we need to write a matrix where the rows are the vectors of the original set. Sage returns two matrices, the vectors in the orthogonal basis are the rows of the first matrix. The second matrix contains the coefficients in the linear combinations at each step of the process. Let’s start by constructing the matrix from the set of linearly independent vectors \(\{v_1, v_2\}\text{.}\)
Now we apply gram_schmidt() method and consider the first matrix returned.
The matrix \(B\) contains the orthogonal basis as rows. Let’s check that the basis \(\{u_1, u_2\}\) obtained manually coincides with this one.

Subsection 10.5.2 Finding the Orthonormal Basis

For the last step of the algorithm, we can manually normalize the orthogonal basis obtained.
Alternatively, we can use the option orthonormal=True in the gram_schmidt() method.
Let’s write the vectors \(e_1,e_2\) obtained manually as rows of a matrix M and compare it with the matrix B of orthonormal vectors obtained directly.