-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector_operations.py
139 lines (106 loc) · 3.29 KB
/
vector_operations.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# This wasn't part of any problem, I just wanted to write these functions on my own
import math
def vector_sum(vetor1, vetor2):
resp = [0 for i in range(len(max(vetor1, vetor2)))]
for i in range(len(vetor1)):
resp[i] = vetor1[i] + vetor2[i]
return resp
def scalar_product(vetor1, vetor2):
resp = 0
for i in range(len(vetor1)):
resp += vetor1[i] * vetor2[i]
return resp
def vectorial_product(vetor1, vetor2):
return [(vetor1[1] * vetor2[2] - vetor1[2] * vetor2[1]), \
(vetor1[2] * vetor2[0] - vetor1[0] * vetor2[2]), \
(vetor1[0] * vetor2[1] - vetor1[1] * vetor2[0])]
def vector_module(vetor):
resp = 0
for i in range(len(vetor)):
resp = math.sqrt(resp ** 2 + vetor[i] **2)
return resp
def angle_btwn_vectors(vetor1, vetor2): # arccos(A.B/|A||B|)
return math.degrees(math.acos(scalar_product(vetor1, vetor2)
/ (vector_module(vetor1) * vector_module(vetor2))))
print('Vector sum')
print()
print('Testing: [1, 1, 1], [2, 2, 2]')
print('Expected: [3, 3, 3]')
print('Answer:', vector_sum([1, 1, 1], [2, 2, 2]))
print()
print('Testing: [1, 1], [2, 2]')
print('Expected: [3, 3]')
print('Answer:', vector_sum([1, 1], [2, 2]))
print()
print('Testing: [1, 10, -1], [2, 20, -2]')
print('Expected: [3, 30, -3]')
print('Answer:', vector_sum([1, 10, -1], [2, 20, -2]))
print()
print('Scalar product')
print()
print('Testing: [1, 1, 1], [2, 2, 2]')
print('Expected: 6')
print('Answer:', scalar_product([1, 1, 1], [2, 2, 2]))
print()
print('Testing: [1, 1], [2, 2]')
print('Expected: 4')
print('Answer:', scalar_product([1, 1], [2, 2]))
print()
print('Testing: [1, 10, -1], [2, 20, -2]')
print('Expected: 204')
print('Answer:', scalar_product([1, 10, -1], [2, 20, -2]))
print()
print('Vector module')
print()
print('Testing: [1, 1, 1]')
print('Expected: 1.7320508075688772')
print('Answer:', vector_module([1, 1, 1]))
print()
print('Testing: [1, 1]')
print('Expected: 1.4142135623730951')
print('Answer:', vector_module([1, 1]))
print()
print('Testing: [1, 10, -1]')
print('Expected: 10.099504938362077')
print('Answer:', vector_module([1, 10, -1]))
print()
print('Vectorial product')
print()
print('Testing: [1, 1, 1], [2, 2, 2]')
print('Expected: ')
print('Answer:', vectorial_product([1, 1, 1], [2, 2, 2]))
print()
print('Testing: [1, 1, 0], [2, 2, 0]')
print('Expected: ')
print('Answer:', vectorial_product([1, 1, 0], [2, 2, 0]))
print()
print('Testing: [1, 10, -1], [2, 20, -2]')
print('Expected: ')
print('Answer:', vectorial_product([1, 10, -1], [2, 20, -2]))
print()
print('Angle between vectors')
print()
print('Testing: [2, 0], [0, 2]')
print('Expected: 90')
print('Answer:', angle_btwn_vectors([2, 0], [0, 2]))
print()
print('Testing: [1, 0], [2, 2]')
print('Expected: 45')
print('Answer:', angle_btwn_vectors([1, 0], [2, 2]))
print()
print('Testing: [1, 0], [-1, 1]')
print('Expected: 135')
print('Answer:', angle_btwn_vectors([1, 0], [-1, 1]))
print()
print('Testing: [1, 0], [-1, -1]')
print('Expected: 135')
print('Answer:', angle_btwn_vectors([1, 0], [-1, -1]))
print()
print('Testing: [1, 1], [2, 2]')
print('Expected: 0')
print('Answer:', angle_btwn_vectors([1, 1], [2, 2]))
print()
print('Testing: [1, 1], [-1, -1]')
print('Expected: 180')
print('Answer:', angle_btwn_vectors([1, 1], [-1, -1]))
print()