-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath_library.py
26 lines (21 loc) · 1.01 KB
/
math_library.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
def scalar_product(vector1, vector2):
if len(vector1) != len(vector2):
raise ValueError("Vectors must be of the same length")
return sum(x * y for x, y in zip(vector1, vector2))
def vector_product(vector1, vector2):
if len(vector1) != 3 or len(vector2) != 3:
raise ValueError("Both vectors must be of length 3")
return [
vector1[1] * vector2[2] - vector1[2] * vector2[1],
vector1[2] * vector2[0] - vector1[0] * vector2[2],
vector1[0] * vector2[1] - vector1[1] * vector2[0]
]
def matrix_product(matrix1, matrix2):
if len(matrix1[0]) != len(matrix2):
raise ValueError("Number of columns in the first matrix must be equal to the number of rows in the second matrix")
result = [[0 for _ in range(len(matrix2[0]))] for _ in range(len(matrix1))]
for i in range(len(matrix1)):
for j in range(len(matrix2[0])):
for k in range(len(matrix2)):
result[i][j] += matrix1[i][k] * matrix2[k][j]
return result