In: Computer Science
1. Writing new functions for the following:
2. Use Python lists to representing vectors and positions. Note that the vectors and positions can be either 2D or 3D, so your code should use the length of the input lists (using the len() function) to determine whether 2D or 3D is used.
3. Plan carefully by understanding the inputs and outputs needed for each function.
4. Test each function by calling it at least once.
5. Include documentation for each function with descriptive comments.
import math
#to find magnitude of a given 2-D or 3-D vector
def magnitude(vector):
#check for 2-D vector
if len(vector)==2:
return math.sqrt(vector[0]**2+vector[1]**2)
#3-D vector
else:
return math.sqrt(vector[0] ** 2 + vector[1] ** 2+vector[2]**2)
#to normalize a given 2-D or 3-D vector
def normalize(vector):
m=magnitude(vector)
#magnitude of a given vector should be > 0
if m>0:
#2-D
if len(vector) == 2:
vector[0] = vector[0] / m
vector[1] = vector[1] / m
#3-D
else:
vector[0] = vector[0] / m
vector[1] = vector[1] / m
vector[2] = vector[2] / m
return vector
#to find the Dot-Product of two given vector (2-D or 3-D)
def dotProduct(vector1,vector2):
dP=0
if len(vector1) == len(vector2):
for i in range(0,len(vector1)):
dP=dP+(vector1[i]*vector2[i])
return dP
#to find the Cross-Product of two given vector (2-D or 3-D)
def crossProduct(vector1,vector2):
#for 3-D
if len(vector1) == 3 and len(vector2) == 3:
cP =(vector1[1] * vector2[2]) - (vector1[2] * vector2[1])+(vector1[2] * vector2[0]) - (vector1[0] * vector2[2])+(vector1[0] * vector2[1]) - (vector1[1] * vector2[0])
#for 2-D
else:
cP = (vector1[0] * vector2[1]) - (vector1[1] * vector2[0])
return cP
#to find the distance of two given position(2-D or 3-D)
def distance(pos1,pos2):
# for 2-D
if len(pos1) and len(pos2) == 2:
return math.sqrt((pos1[0]-pos2[0])**2+(pos1[1]-pos2[1])**2)
# for 3-D
else:
return math.sqrt((pos1[0]-pos2[0])**2+(pos1[1]-pos2[1])**2+(pos1[2]-pos2[2])**2)
# to find the angle between two given vectors
def findAngle(vector1,vector2):
angle=math.degrees(math.acos(dotProduct(vector1,vector2)/(magnitude(vector1)*magnitude(vector2))))
return angle
#main() to test the functionality of each functions
def main():
#create two 3-D vectors
vector1 = [3, 2, 1]
vector2 = [4, 7, 5]
#create two positions in 3-D
pos1 = [7, 9, 5]
pos2 = [3, 4, 2]
print("Given vector1 is :")
print(str(vector1[0])+"i+ "+str(vector1[1])+"j+ "+str(vector1[2])+"k ")
print("Given vector2 is :")
print(str(vector2[0]) + "i+ " + str(vector2[1]) + "j+ " + str(vector2[2]) + "k ")
print("Magnitude of vector1 is ",magnitude(vector1))
print("Magnitude of vector2 is ", magnitude(vector2))
print("Normalize form of vector1 is ")
vector3 = normalize(vector1)
print(str(vector3[0]) + "i+ " + str(vector3[1]) + "j+ " + str(vector3[2]) + "k ")
print("Normalize form of vector2 is ")
vector3 = normalize(vector2)
print(str(vector3[0]) + "i+ " + str(vector3[1]) + "j+ " + str(vector3[2]) + "k ")
print("Dot product of vector1 and vector2 is ", dotProduct(vector1,vector2))
print("Cross product of vector1 and vector2 is ", crossProduct(vector1, vector2))
print("Distance between two positions is ",distance(pos1,pos2))
print("Angle between two vectors is ",findAngle(vector1,vector2))
#calling main()
if __name__ == '__main__':
main();
Sample Input and Output of above code:
Given vector1 is :
3i+ 2j+ 1k
Given vector2 is :
4i+ 7j+ 5k
Magnitude of vector1 is 3.7416573867739413
Magnitude of vector2 is 9.486832980505138
Normalize form of vector1 is
0.8017837257372732i+ 0.5345224838248488j+ 0.2672612419124244k
Normalize form of vector2 is
0.4216370213557839i+ 0.7378647873726218j+ 0.5270462766947299k
Dot product of vector1 and vector2 is 0.8733260632194672
Cross product of vector1 and vector2 is 0.1408590424547527
Distance between two positions is 7.0710678118654755
Angle between two vectors is 29.15251940703007
Code screenshots: