Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Vectors in Python without numpy
- # juhani Kaukoranta
- # 11.2.2019
- from math import sqrt, acos, degrees
- from operator import mul
- def dotproduct(a,b):
- return sum(map(mul,a,b))
- def norm(a):
- # norm, length of vector a
- return sqrt(dotproduct(a,a))
- def anglerad(a,b):
- # angle between vectors a and b in radians
- return acos(dotproduct(a,b)/norm(a)/norm(b))
- def angledeg(a,b):
- # angle between vectors a and b in radians
- return degrees(anglerad(a,b))
- def crossproduct(a,b):
- assert len(a) == len(b) == 3, 'Only 3D vectors, set missing component to 0'
- a1,a2,a3 = a
- b1,b2,b3 = b
- return [a2*b3 - a3*b2, a3*b1 - a1*b3, a1*b2 - a2*b1]
- print("for example input vector [-3,5,4] are inputed : -3 5 4")
- # vector1
- input_vector = input("Enter first vector's components separated by space ")
- # muutetaan listan merkkijonokomponentit liukuluvuiksi
- vector1 = [float(i) for i in input_vector.split()]
- print("vector1 = ",vector1)
- # vector2
- input_vector = input("Enter second vector's components separated by space ")
- vector2 = [float(i) for i in input_vector.split()]
- print("vector2 = ",vector2)
- # dot product, norm, angle, cross product
- print("dotproduct of vectors = ",dotproduct(vector1,vector2))
- print("norm, length of vector1 = ",norm(vector1))
- print("norm, length of vector2 = ",norm(vector2))
- print("angle between vectors in radians = ",anglerad(vector1,vector2))
- print("angle between vectors in degrees = ",angledeg(vector1,vector2))
- print("cross product of vectors = ",crossproduct(vector1,vector2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement