Vector Analysis program. Input vectors U and V. Calculate dot product, cross product, scalar projections, vector projections, the angle between vectors, norm of vectors, and normalize vectors, along with switching vectors U and V.
import numpy as np from math import * def rndV(vec): for i in range(vec.size): vec[i] = round(vec[i],4) return vec def projuv(u,v): puv = v*((np.dot(u,v)/np.dot(v,v))) puv = rndV(puv) print("\nProj of U onto V:") print("=",puv.tolist(),"\n") input("press [enter] to continue") vecan() def compuv(u,v): cuv = ((np.dot(u,v)/normv(v))) print("\nComp of U onto V:") print("=",round(cuv,5),"\n") input("press [enter] to continue") vecan() def normv(v): return sqrt(sum((v**2))) def angleUV(u,v): print("\nThe angle between U and V:") print("=",round(acos(np.dot(u,v)/(normv(u)*normv(v)))*180/pi,5),"deg\n") input("press [enter] to continue") vecan() def inputUV(): global a, b a = np.array(eval(input("Vector U: "))) b = np.array(eval(input("Vector V: "))) vecan() def switch(u, v): global a, b a = v b = u print("U=",a.tolist()) print("V=",b.tolist()) input("\npress [enter] to continue") vecan() def normalize(u, v): global a,b a = rndV(u/normv(u)) b = rndV(v/normv(v)) print("unit_U=\n"," ",a.tolist()) print("unit_V=\n"," ",b.tolist(),"\n") input("press [enter] to continue") vecan() def vecan(): k = 0 print("\n========Vector Analysis=======") print("1: Input Vectors") print("2: U dot V") print("3: U cross V") print("4: Normalize U and V") print("5: Proj U on V") print("6: Comp U on V") print("7: Angle between U and V") print("8: Switch U and V") print("9: Quit") k = int(input("[1-9]?")) print() if k == 1: inputUV() elif k == 2: print("\nU dot V:") print("=",np.dot(a, b)) input("\npress [enter] to continue") vecan() elif k == 3: print("\nU cross V:") ucv = np.cross(a, b) ucv = rndV(ucv) print("=",ucv.tolist()) input("\npress [enter] to continue") vecan() elif k == 4: normalize(a, b) elif k == 5: projuv(a, b) elif k == 6: compuv(a, b) elif k == 7: angleUV(a, b) elif k == 8: switch(a, b) else: print("The End")