Last active
August 19, 2020 06:46
-
-
Save Pascal-0x90/01e0a2271156010b9ca6e0821d9806e9 to your computer and use it in GitHub Desktop.
Python3 Implementation of Gram Schmidt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
''' | |
This is using the lesson from the following PDF: | |
https://www.math.tamu.edu/~yvorobet/MATH304-503/Lect3-07web.pdf | |
''' | |
def sub_vect(v1,v2): | |
sets = [] | |
if len(v1) != len(v2): | |
return -1 | |
for i in range(len(v1)): | |
sets.append(v1[i] - v2[i]) | |
return sets | |
def mult_scalar(v,scalar): | |
sets = [] | |
for i in range(len(v)): | |
sets.append(v[i] * scalar) | |
return sets | |
def dot_prod(v1,v2): | |
sets = [] | |
if len(v1) != len(v2): | |
return -1 | |
for i in range(len(v1)): | |
sets.append(v1[i] * v2[i]) | |
return sum(sets) | |
def gram_schmidt(vectors): | |
vector_list = [] | |
counter = 0 | |
for vector in vectors: | |
for v in vector_list: | |
vector = sub_vect(vector,mult_scalar(v,dot_prod(vector,v)/dot_prod(v,v))) | |
vector_list.append(vector) | |
return vector_list | |
v1 = [4,1,3,-1] | |
v2 = [2,1,-3,4] | |
v3 = [1,0,-2,7] | |
v4 = [6,2,9,-5] | |
vectors = [v1, v2, v3, v4] | |
ortho_basis = gram_schmidt(vectors) | |
print(f"FLAG: {ortho_basis[3][1]:.5f}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment