Gram-Schdmit method allows us to constructively transform any basis (b1,b2,....,bn) of an n-dimensional vector space V into an orthogonal/orthonormal basis (u1,u2,....un) of V. The method interactively constructs an orthogonal basis as follows
u1=b1
u2=b2−u1.uT1b2uT1.u1
..
uk=bk− projection of uk onto the span of (u1,u2,...uk−1)
The k th basis vector bk is projected onto the subspace spanned by the first k−1 constructed orthogonal vectors u1,u2,......uk−1. This projection is then subtracted from bk and yields a vector uk that is orthogonal to the (k−1) dimensional subspace spanned by u1,u2,......uk−1.Repeating this procedure for all n basis vectors (b1,b2,....,bn) yields an orthogonal basis (u1,u2,....un) of V. If we normalize uk, we obtain a ortho normal basis where ||uk||=1 for k=1…n
Consider a simple example
import numpy as np
b1=np.array([2,0])
b2=np.array([1,1])
u1=b1
u2=b2-((np.outer(u1,u1)/np.dot(u1,u1)).dot(b2))
print("b1")
print(b1)
print("b2")
print(b2)
print("orthogonal vectors")
print("u1")
print(u1)
print("u2")
print(u2)
print("dot product of u1 and u2")
print(u1.dot(u2))
o/p
b1
[2 0]
b2
[1 1]
orthogonal vectors
u1
[2 0]
u2
[0. 1.]
dot product of u1 and u2
0.0
Orthogonalization in three dimension
import numpy as np
np.set_printoptions(precision=4)
b1=np.array([1,2,2])
b2=np.array([-1,0,2])
b3=np.array([0,0,1])
u1=b1
u2=b2-((np.outer(u1,u1)/np.dot(u1,u1)).dot(b2))
u3=b3-((np.outer(u1,u1)/np.dot(u1,u1)).dot(b3))-((np.outer(u2,u2)/np.dot(u2,u2)).dot(b3))
print("b1")
print(b1)
print("b2")
print(b2)
print("b3")
print(b3)
print("orthogonal vectors")
print("u1")
print(u1)
print("u2")
print(u2)
print("u3")
print(u3)
print("length of u1 u2 u3")
lu1=np.sqrt(u1.dot(u1))
lu2=np.sqrt(u2.dot(u2))
lu3=np.sqrt(u3.dot(u3))
print(lu1,lu2,lu3)
print("normalized vectors u1 u2 u3")
u1=u1/lu1
u2=u2/lu2
u3=u3/lu3
print(u1)
print(u2)
print(u3)
np.set_printoptions(suppress=True)
print("dot product of u1 and u2")
display(round(u1.dot(u2)))
print("dot product of u1 and u3")
print(round(u1.dot(u3)))
print("dot product of u2 and u3")
display(round(u2.dot(u3)))
o/p
b1
[1 2 2]
b2
[-1 0 2]
b3
[0 0 1]
orthogonal vectors
u1
[1 2 2]
u2
[-1.3333 -0.6667 1.3333]
u3
[ 0.2222 -0.2222 0.1111]
length of u1 u2 u3
3.0 2.0 0.3333333333333333
normalized vectors u1 u2 u3
[0.3333 0.6667 0.6667]
[-0.6667 -0.3333 0.6667]
[ 0.6667 -0.6667 0.3333]
dot product of u1 and u2
0
dot product of u1 and u3
0
dot product of u2 and u3
0
Compact code
import numpy as np
def gram_schmidt(A):
"""Orthogonalize a set of vectors stored as the columns of matrix A."""
# Get the number of vectors.
n = A.shape[1]
for j in range(n):
# To orthogonalize the vector in column j with respect to the
# previous vectors, subtract from it its projection onto
# each of the previous vectors.
for k in range(j):
A[:, j] -= np.dot(A[:, k], A[:, j]) * A[:, k]
A[:, j] = A[:, j] / np.linalg.norm(A[:, j])
return A
if __name__ == '__main__':
A = np.array([[1.0, 0.0, 1.0], [1.0, 0.0, -1.0], [0.0, 3.0, 4.0]])
print(gram_schmidt(A))
Example ( university question)
Use the Gram-Schmidt process to find an orthogonal basis for the column space of the following matrix.
[2101−11031111]
here
u1=b1=[2101]
u2=b2−(u1uT1)||u1||2.b2
u2=[1−131]−16[4202210100002101][1−131]u2=[0.3333−1.33333.0.6667]
u3=b3−(u1uT1)||u1||2.b3−(u2uT2)||u2||2.b3
u3=[−0.73530.94120.38240.5294]
Example ( University Question)
Use Gram-Schmidt process to find an orthonormal basis from the ordered basis B={b1,b2}, where b1=[111] and b2=[−120]
u1=b1=[111]
u2=b2−u1.uT1||u21||.b2
=[−120]−13[111111111][−120]
=[−120]−13[111]
=[−4/35/3−1/3]
length of u1=√3
length of u2=√423
So the normalized vectors are
u1=1√3.[111]
u2=1√42.[−45−1]
Note that u1.u2=0
Example ( University Question)
Obtain an orthonormal basis with respect to the standard inner space for the subspace of R3 generated by (1,0,1),(1,0,−1) and (0,3,4)
u1=b1=[101]u2=b2−u1.uT1||u21||.b2=[10−1]−12[101000101][10−1]=[10−1]−12[000]=[10−1]u3=b3−(u1uT1)||u1||2.b3−(u2uT2)||u2||2.b3
u3=[030]length of
u1=√2length of
u2=√2length of u3=3
So the normalized vectors are
u1=1√2.[101]
u2=1√2.[10−1]
u3=13.[030]
Example ( University Question)
Using the Gram-Schmidt method, construct the orthonormal basis (u1,u2,u3) to the following sequence of vectors w1=[1111],w2=[11−1−1],w3=[0−121] of R4
u1=w1=[1111]u2=w2−u1.uT1||u21||.w2
u2=[11−1−1]−14[111111111111][−1−111]
u2=[11−1−1]
u3=w3−(u1uT1)||u1||2.w3−(u2uT2)||u2||2.w3
u3=[0−121]−14[111111111111][0−121]−14[11−1−111−1−1−1−111−1−111][0−121]u3=[0−121]−[1/21/21/21/2]−[−1−111]u3=[1/2−1/21/2−1/2]
The length of vectors
||u1||=2,||u2||=2,||u3||=1
So the normalized orthogonal vectors ( orthonormal vectors) are
u1=[1/21/21/21/2],u2=[1/21/2−1/2−1/2],u3=[1/2−1/21/2−1/2]
Comments
Post a Comment