-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvector.py
154 lines (118 loc) · 4.48 KB
/
vector.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
140
141
142
143
144
145
146
147
148
149
150
151
152
import numpy as np
from numpy.core.numeric import cross
import os
import sys
os.system('cls')
red = "\033[0;31m"
white = "\033[0;37m"
#functions:
#+-*
# moudlus,dot, cross
# check parallel, find theta, find orthogonal
#parallelgram area
# proj, perp
#volume
#special input process
def vector_create():
n = int(input("Please give the number of rows: \n"))
print("Now please input your vector, separated by space\n")
array = np.array([input().strip().split() for _ in range(n)], int)
return array
#vector addition/subtraction from numpy
def add_vector(vector1,vector2):
return vector1 + vector2
def subt_vector(vector1,vector2):
return (vector1-vector2)
#vector multiolication/moudlus from numpy
def mult_vector(n,vector2):
return (n*vector2)
def magni_vector(vector1):
return (np.linalg.norm(vector1))
# dot, cross
def dot_vector(vector1,vector2):
arr2 = np.squeeze(np.asarray(vector1))
arr1 = np.squeeze(np.asarray(vector2))
return np.dot(arr1,arr2)
def cross_vector(vector1,vector2):
res = np.cross(vector1.T,vector2.T)
return res.T
#proj perp
def proj_vector(vector,d):
numrator = dot_vector(vector,d)
denominator = magni_vector(d)**2
proj = ((numrator/denominator)*d)
return proj
def perp_vector(vector,d):
proj = proj_vector(vector,d)
perp = vector-proj
return perp
def print_vector(vector):
for j in range(len(vector)):
print(vector[j][0], end = ' ')
print()
def calculate():
try:
print()
print("Here are some operations you can choose. Please select the correct NUMBER\n")
print("Here are some vector operations you can choose\n")
choice = input("1) Addition 2) Multiplication 3) Subtraction 4) Norm 5) Dot Product 6) Cross Product 7) Projection 8) Perpendicular 0)Quit to Menu\n")
if choice=='0':
return
elif choice == '1' or choice == '3' or choice == '6' or choice =='7' or choice =='8':
arr1 = vector_create()
arr2 = vector_create()
print("----Your result is----")
if choice == '1':
res = add_vector(arr1, arr2)
elif choice == '3':
res = subt_vector(arr1, arr2)
elif choice == '6':
res = cross_vector(arr1,arr2)
elif choice == '7':
res = proj_vector(arr1,arr2)
elif choice =='8':
res = perp_vector(arr1,arr2)
print_vector(res)
ask = input("Do you want to calculate it again? Press 1 to play, Press 0 to quit.\n")
if ask == '1':
calculate()
else:
return
elif choice =='2':
n = int(input("give the coefficient\n"))
arr1 = vector_create()
res = mult_vector(n,arr1)
print("----Your result is----")
print_vector(res)
ask = input("Do you want to calculate it again? Press 1 to play, Press 0 to quit.\n")
if ask == '1':
calculate()
else:
return
elif choice == '4':
arr2 = vector_create()
res = magni_vector(arr2)
print("----Your result is----")
print (res)
ask = input("Do you want to calculate it again? Press 1 to play, Press 0 to quit.\n")
if ask == '1':
calculate()
else:
return
elif choice =='5':
Arr1 = vector_create()
Arr2 = vector_create()
print("----Your result is----")
res = dot_vector(Arr1,Arr2)
print(res)
ask = input("Do you want to calculate it again? Press 1 to play, Press 0 to quit.\n")
if ask == '1':
calculate()
else:
return
else:
print(red, "Please input correctly!", white)
calculate()
except:
print(red, "An error occured, please check your input format",white)
calculate()