-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgramschmidt.py
More file actions
29 lines (25 loc) · 798 Bytes
/
gramschmidt.py
File metadata and controls
29 lines (25 loc) · 798 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import math
#vectors are implemented as lists here
def default_scalar_prod(vector1, vector2):
"""Calculates the default scalar product of two vectors."""
res = 0
v = zip(vector1, vector2)
for i in v:
res += i[0]*i[1]
return res
def norm(vector):
"""Calculates the Euclidean norm of a vector."""
return math.sqrt(default_scalar_prod(vector, vector))
def gramschmidt(vectors):
"""Orthonormalizes a set of vectors using the Gram-Schmidt-process."""
res = [map(lambda x: x*(1/norm(vectors[0])), vectors[0])]
del vectors[0]
zws = []
for v in vectors:
for w in res:
zws.append(map(lambda x: x*default_scalar_prod(v, w), w))
for z in zws:
v = map(lambda x: x[0]-x[1], zip(v, z))
v = map(lambda x: x*(1/norm(v)), v)
res.append(v)
return res