# Gram-Schmidt procedure¶

Some helper methods and examples of how to find an orthonormal basis

In [1]:
import numpy as np

def gs(X):
Q, R = np.linalg.qr(X)
return Q


## Example from the lecture slides¶

This shows how an ON basis can be found. Columns of a are the ON-basis (so they are already normalized)

Note: Since we are engineers, 5.55111512e-17 = 0 in most of the cases.

In [2]:
m = np.array([[1,1,1],[1,1,0], [1,0,0]])
gs(m)

Out[2]:
array([[ -5.77350269e-01,  -4.08248290e-01,  -7.07106781e-01],
[ -5.77350269e-01,  -4.08248290e-01,   7.07106781e-01],
[ -5.77350269e-01,   8.16496581e-01,   5.55111512e-17]])

## Orthogonal but not normalized¶

This shows the orginal vectors that we would get if we did not normalize. We then normalize them to show that they are equivalent to those we got above.

In [3]:
v1 = np.array([1, 1,1])
v1/np.linalg.norm(v1)

Out[3]:
array([ 0.57735027,  0.57735027,  0.57735027])
In [4]:
v2 = np.array([-1/3, -1/3,  2/3])
v2/np.linalg.norm(v2)

Out[4]:
array([-0.40824829, -0.40824829,  0.81649658])
In [5]:
v3 = np.array([1/2, -1/2, 0])
v3/np.linalg.norm(v3)

Out[5]:
array([ 0.70710678, -0.70710678,  0.        ])