Section B.6 Gram-Schmidt orthogonalization
Warning B.6.1.
This page contains several redefinitions of a Python function inprod
. You will get incorrect results or errors if you Evaluate
a Sage cell defining that function in one subsection below, and then Evaluate
Sage cells that use a function by that same name in a different subsection below without evaluating the appropriate Sage cell near the beginning of that different subsection that redefines that inprod
function to the appropriate formula used in the example in that particular subsection.
The best way to avoid this problem is to Evaluate
every Sage cell in a particular subsection, from the beginning, in order.
Subsection B.6.1 An example in R4
Let's begin simply with the standard inner product on R4. Suppose we want to produce an orthogonal basis for the βhyperplaneβ in R4 defined by the equation
w+2x+3yβ4z=0.
First, we can obtain a basis for this space by treating this single equation as a homogeneous linear system. In other words, we want a basis for the null space of the matrix
[123β4].
Assigning parameters
x2=r,x3=s,x4=t
leads to basis
B={(β2,1,0,0),(β3,0,1,0),(4,0,0,1)}.
Let's load this basis into Sage.
xxxxxxxxxx
v1 = vector((-2,1,0,0))
v2 = vector((-3,0,1,0))
v3 = vector((4,0,0,1))
print(v1,',',v2,',',v3)
xxxxxxxxxx
v1 * v2
xxxxxxxxxx
e1 = v1
print('e1 =', e1)
xxxxxxxxxx
e2 = v2 - ( (v2*e1) / (e1*e1) ) * e1
print('e2 =',e2)
xxxxxxxxxx
e3 = v3 - ( (v3*e1) / (e1*e1) ) * e1 - ( (v3*e2) / (e2*e2) ) * e2
print('e3 =',e3)
xxxxxxxxxx
e2 = 5 * e2
e3 = 7 * e3
print(e1,',',e2,',',e3)
xxxxxxxxxx
print(e1 * e2)
print(e1 * e3)
print(e2 * e3)
xxxxxxxxxx
e1 = e1 / sqrt(e1*e1)
e2 = e2 / sqrt(e2*e2)
e3 = e3 / sqrt(e3*e3)
print(e1)
print(e2)
print(e3)
w+2x+3yβ4z=0.
xxxxxxxxxx
print(e1[0] + 2*e1[1] + 3*e1[2] - 4*e1[3])
print(e2[0] + 2*e2[1] + 3*e2[2] - 4*e2[3])
print(e3[0] + 2*e3[1] + 3*e3[2] - 4*e3[3])
e1[0]
means the first coordinate of vector e1
.)Subsection B.6.2 An example in C4
Here we will carry out the Gram-Schmidt portion of Example 37.4.7. It will get annoying typingconjugate
for every complex dot product calculation, so let's start by creating a Python function to return inner product values. Remember that the order of multiplication matters for the complex dot product.
xxxxxxxxxx
def inprod(u,v):
return v.conjugate() * u
print("inprod function loaded")
I
for the imaginary root of x2+1.
xxxxxxxxxx
v1 = vector((1,1,I,I))
v2 = vector((1,-1,I,I))
v3 = vector((0,0,1,0))
v4 = vector((0,0,0,1))
print(v1,',',v2,',',v3,',',v4)
xxxxxxxxxx
column_matrix([v1,v2,v3,v4]).rank()
\
to break it up over multiple input lines.
xxxxxxxxxx
e1 = v1
e2 = v2 - (inprod(v2,e1) / inprod(e1,e1)) * e1
e3 = v3 - (inprod(v3,e1) / inprod(e1,e1)) * e1 - (inprod(v3,e2) / inprod(e2,e2)) * e2
e4 = v4 \
- (inprod(v4,e1) / inprod(e1,e1)) * e1 \
- (inprod(v4,e2) / inprod(e2,e2)) * e2 \
- (inprod(v4,e3) / inprod(e3,e3)) * e3
print(e1,',',e2,',',e3,',',e4)
xxxxxxxxxx
e2 = 2 * e2
e3 = 3 * e3
e4 = 2 * e4
print(e1,',',e2,',',e3,',',e4)
xxxxxxxxxx
E = [e1,e2,e3,e4]
for e in E:
print("checking",e,"against all")
for f in E:
print(inprod(e,f))
Subsection B.6.3 A polynomial example with an integral inner product
Here we will carry out Example 37.4.4. First let's enter our initial (non-orthogonal) basis vectors into Sage.xxxxxxxxxx
v1 = 1
v2 = x
v3 = x^2
print(v1,',',v2,',',v3)
xxxxxxxxxx
def inprod(f,g):
return integrate(f*g,x,0,1)
inprod(1,x)
inprod(1,x)
should be computing the integral
β«101xdx,
which should evaluate to the area of a triangle of base and height both 1. Looks like the computation got it correct.
Now we carry out the Gram-Schmidt process.
xxxxxxxxxx
e1 = v1
e2 = v2 - (inprod(v2,e1) / inprod(e1,e1)) * e1
e3 = v3 - (inprod(v3,e1) / inprod(e1,e1)) * e1 - (inprod(v3,e2) / inprod(e2,e2)) * e2
print('e1 =',e1)
print('e2 =',e2)
print('e3 =',e3)
{1,xβ12,x2βx+16},
just as in Example 37.4.4. Let's double-check:
xxxxxxxxxx
print(inprod(e1,e2))
print(inprod(e1,e3))
print(inprod(e2,e3))
xxxxxxxxxx
f1 = e1 / sqrt(inprod(e1,e1))
f2 = e2 / sqrt(inprod(e2,e2))
f3 = e3 / sqrt(inprod(e3,e3))
print(f1,',',f2,',',f3)
Subsection B.6.4 A polynomial example with a βsamplingβ inner product
Now we'll repeat the Gram-Schmidt process on the standard basis of P2(R), but using the inner product
β¨p,qβ©=p(β1)q(β1)+p(0)q(0)+p(1)q(1).
Once again we create a Python function to return inner product values, but this time we'll use slightly more sophisticated Python/Sage programming techniques.
xxxxxxxxxx
def inprod(p,q):
inprod_value = 0
for x_value in [-1, 0, 1]:
inprod_value += p.subs(x = x_value) * q.subs(x = x_value)
return inprod_value
print("inprod function loaded")
xxxxxxxxxx
v1 = 1
v2 = x
v3 = x^2
print(v1,',',v2,',',v3)
xxxxxxxxxx
e1 = v1
e2 = v2 - (inprod(v2,e1) / inprod(e1,e1)) * e1
e3 = v3 - (inprod(v3,e1) / inprod(e1,e1)) * e1 - (inprod(v3,e2) / inprod(e2,e2)) * e2
print('e1 =',e1)
print('e2 =',e2)
print('e3 =',e3)
xxxxxxxxxx
print(inprod(e1,e2))
print(inprod(e1,e3))
print(inprod(e2,e3))
Subsection B.6.5 Bonus Fun
If you've taken a Python programming class, see if you can write a procedure (usingdef
) that takes a list B
and a function inprod
as parameters, and returns a list containing the results of performing the Gram-Schmidt process on the objects in B
using the input parameter function inprod
as the inner product function. Fun!