-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvectorProjection.py
44 lines (32 loc) · 1.53 KB
/
vectorProjection.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
import sympy as sp
print("We will calculate the projection of vector a onto vector b.")
def negative_checked_matrix(vector):
vector_features = []
for feature in vector:
if feature.startswith('-'):
# If the user specifies a negative component
neg_component = feature[1:] # Remove the '-' sign
vector_features.append(-sp.symbols(neg_component))
else:
vector_features.append(sp.symbols(feature))
return sp.Matrix(vector_features)
# Define the symbols for the vectors
a_vector = input("Enter symbols for vector a (comma-separated like a1,a2,a3): ").split(",")
b_vector = input("Enter symbols for vector b (comma-separated like b1,b2,b3): ").split(",")
a = negative_checked_matrix(a_vector) if len(a_vector) == 3 else sp.Matrix(sp.symbols('a1 a2 a3'))
b = negative_checked_matrix(b_vector) if len(b_vector) == 3 else sp.Matrix(sp.symbols('b1 b2 b3'))
print("Calculating...")
# Calculate the dot product
dot_product = a.dot(b)
# Calculate the magnitude squared of vector b
magnitude_b_squared = b.dot(b)
# Calculate the projection of a onto b
projection = (dot_product / magnitude_b_squared) * b
# Print the signed magnitude of the projection
print(f"The projection of vector a onto vector b is: {projection}")
# Calculate the magnitude of vector b
magnitude_b = sp.sqrt(magnitude_b_squared)
# Calculate c as the signed magnitude of the projection
c = dot_product / magnitude_b
# Print the value of c
print(f"The signed magnitude of the projection of vector a onto vector b (c) is: {c}")