-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
37 lines (29 loc) · 1.16 KB
/
calculator.py
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
30
31
32
33
34
35
36
37
from vector import Vector
def calculate(vectors: list):
"""
Calculate the othogonal basis.
:param vectors: a list of vector objects
"""
# Setup the variables.
newValue = []
subtract = Vector([0] * len(vectors[0]))
# Make Vector objects from the vectors given.
for vector in range(len(vectors)):
vectors[vector] = Vector(vectors[vector])
# Check to see if they are already orthogonal.
count = 0
for i in range(len(vectors)):
if Vector.isOrthogonal(vectors[i], vectors[i-1]):
count += 1
# If they are already orthogonal, return false.
if count == len(vectors):
return [x.value for x in vectors]
# Perform the algorithm.
for i in range(len(vectors)):
newValue.append(vectors[i] + subtract)
try: # Subtract until you cannot any more. Then, we are done.
subtract -= (vectors[i+1].dotProduct(newValue[i]) / newValue[i].dotProduct(newValue[i])) * newValue[i]
except(IndexError):
break
# Return the result.
return [[round(z, 3) for z in x.value] for x in newValue]