Theorem : Gram–Schmidt Let be a -dimensional subspace of , and let be any basis for . Then the set of vectors is an orthogonal basis for , where
and where in general
By rewriting the above equations for ,
In matrix form
Given below is an implementation of Gram-Schmidt orthogonalization in Julia
using LinearAlgebra
N = 10;
A = rand(N,N); #row vectors to be orthogonalized
U = zeros(N,N);
U[1,:] = A[1,:];
V = zeros(N,N); # for the for loop
V[1,:] = A[1,:];
U[2,:] = A[2,:] - dot(U[1,:],A[2,:])*U[1,:]/dot(U[1,:],U[1,:])
U[3,:] = A[3,:] - dot(U[1,:],A[3,:])*U[1,:]/dot(U[1,:],U[1,:]) - dot(U[2,:],A[3,:])*U[2,:]/dot(U[2,:],U[2,:])
println(dot(U[1,:],U[2,:]))
println(dot(U[1,:],U[3,:]))
println(dot(U[2,:],U[3,:]))
#for loop for a matrix of size N
for i in 2:N
V[i,:] = A[i,:]
for j in 1:(i-1)
V[i,:] -= dot(V[j,:],A[i,:])*V[j,:]/dot(V[j,:],V[j,:])
end
end
println(dot(V[1,:],V[2,:]))
println(dot(V[1,:],V[3,:]))
println(dot(V[2,:],V[3,:]))
println(dot(V[N,:],V[N-1,:]))
Using row operations
using LinearAlgebra
function test()
N = 4;A = rand(N,N);B = Matrix(1.0I,N,N);U = zeros(N,N);U[1,:] = A[1,:]; V = zeros(N,N);V[1,:] = A[1,:];
row_operations = []
B[2,1] = -dot(U[1,:],A[2,:])/dot(U[1,:],U[1,:])
push!(row_operations,B)
X = B*A
B = Matrix(1.0I,N,N)
B[3,1] = -dot(X[1,:],A[3,:])/dot(X[1,:],X[1,:])
B[3,2] = -dot(X[2,:],A[3,:])/dot(X[2,:],X[2,:])
push!(row_operations,B)
X = B*X
println(dot(X[1,:],X[2,:]))
println(dot(X[1,:],X[3,:]))
println(dot(X[2,:],X[3,:]))
X = zeros(N,N);X = copy(A);#X[1,:] = A[1,:];
row_operations = []
for i in 2:N
C = Matrix(1.0I,N,N)
for j in 1:(i-1)
C[i,j] = -dot(X[j,:],A[i,:])/dot(X[j,:],X[j,:])
end
push!(row_operations,C)
X = C*X
end
println(dot(X[1,:],X[2,:]))
println(dot(X[1,:],X[3,:]))
println(dot(X[2,:],X[3,:]))
println(dot(X[N,:],X[N-1,:]))
end
test()
Top comments (0)