-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathccb_assignment_2_3.py
More file actions
91 lines (60 loc) · 1.78 KB
/
Copy pathccb_assignment_2_3.py
File metadata and controls
91 lines (60 loc) · 1.78 KB
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
# Task 3: Define a function for computing the cross products of two arrays of vector
__author__ = "Cristián Calvo Barentin"
__email__ = "calvo@arch.ethz.ch"
__date__ = "30.10.2019"
def cross_vectors(u,v):
"""
The cross product between two given vectors
Paremeters
u (list) - first vector
v (list) - second vector
Returns
3-tuple - The cross product
"""
c = (u[1] * v[2] - u[2] * v[1],
u[2] * v[0] - u[0] * v[2],
u[0] * v[1] - u[1] * v[0])
return c
def cross_two_arrays(U, V):
"""
the cross products of two arrays of vector if they have the same length
Paremeters
U (list) - first array
V (list) - second array
Returns
list - The cross products
"""
if len(U) != len(V):
raise Exception("Arrays don't have the same length")
return [cross_vectors(U[i],V[i]) for i in range(len(U))]
def cross_two_arrays_np(U, V):
"""
the cross products of two arrays of vector if they have the same length
Paremeters
U (list) - first array
V (list) - second array
Returns
numpy array - The cross products
"""
#transform lists into numpy arrays
U_np = np.array(U)
V_np = np.array(V)
if len(U_np) != len(V_np):
raise Exception("Arrays don't have the same length")
return np.cross(U_np, V_np)
if __name__ == '__main__':
import numpy as np
a = [
[0.1, 0.0, 0.1],
[0.0, 0.0, 0.1],
[1.0, 0.0, 0.0],
[1.0, 1.0, 0.0]
]
b =[
[2.0, 0.3, 0.0],
[1.0, 0.36, -0.3],
[1.25, 15.0, 3.0],
[0.0, -4.0, 7.41]
]
print(cross_two_arrays(a, b))
print(cross_two_arrays_np(a,b))