4.2 The Gram-Schmidt Process
How to "orthgonalize" a basis
1Reading¶
Material related to this page, as well as additional exercises, can be found in ALA 4.2.
2Learning Objectives¶
By the end of this page, you should know:
- the Gram-Schmidt process for finding an orthogonal basis of a vector space
3The Gram-Schmidt Process¶
Hopefully we’ve convinced you that orthogonal bases are useful, so now the natural question becomes: how do I compute one? That’s where the famed Gram-Schmidt Process (GSP) comes into play.
The idea behind GSP is farily straightforward: given an initial basis for a vector space, iteratively modify until it is orthogonal. Let’s start with a simple concrete example, and then introduce the general algorithm:
The Gram-Schmidt process simply repeats this process over and over if there are more than two vectors, but the idea remains the same: at each step you subtract off the directions of the current vector that are parallel with previous ones.
3.1Python break!¶
In the following code, we demonstrate the Gram-Schmidt Process to obtain an orthogonal (and orthonormal) basis from a basis that is not orthogonal using a for loop in Python.
# The Gram Schmidt process
import numpy as np
x1 = np.array([-2, 1, 0, 0])
x2 = np.array([1, 0, 1, 0])
x3 = np.array([3, 0, 0, 1])
x = [x1, x2, x3] # basis that is not orthogonal
v = [x1] # initialize the basis that is orthogonal
u = [x1/np.linalg.norm(x1)] # initialize the basis that is orthogonal
for i in range(1, len(x)):
v_i = x[i]
for j in range(i):
v_i = v_i - (np.dot(x[i], v[j])/np.dot(v[j], v[j]))*v[j]
v.append(v_i)
u.append(v_i/np.linalg.norm(v_i))
print("Orthogonal basis (not orthonormal): \n", v)
print("Orthonormal basis: \n", u)
Orthogonal basis (not orthonormal):
[array([-2, 1, 0, 0]), array([0.2, 0.4, 1. , 0. ]), array([ 0.5, 1. , -0.5, 1. ])]
Orthonormal basis:
[array([-0.89442719, 0.4472136 , 0. , 0. ]), array([0.18257419, 0.36514837, 0.91287093, 0. ]), array([ 0.31622777, 0.63245553, -0.31622777, 0.63245553])]