###################### VECTORS ######################## print("VECTORS") #return DOT PRODUCT of two vectors def dot_product(u, v): if len(u) != len(v): print("Error. vectors not same length") else: return sum([u[i]*v[i] for i in range(len(u))]) ''' sum = 0 for i in range(len(u)): sum += u[i] * v[i] return sum ''' u1=[1,2,3,4,5] u2=[2,3,4,5,6] print(dot_product(u1,u2)) #70 u3=[1,0] u4=[0,1] u5=[-1,0] u33=[3,0] u43=[0,3] u53=[-3,0] u6=[.7071,.7071] print(dot_product(u3,u4)) #0 print(dot_product(u3,u5)) #-1 print(dot_product(u3,u6)) #.7071 print(dot_product(u33,u43)) #0 print(dot_product(u33,u53)) #-9 #return vector multiplied by a scalar def scalar_multiply(u,a): return [a*c for c in u] print(scalar_multiply(u1,-3)) #return MAGNITUDE (length, norm) of a vector def magnitude(u): return math.sqrt(sum([c**2 for c in u])) ''' sum_sqrs = 0 for c in u: sum_sqrs += c**2 return math.sqrt(sum_sqrs) ''' print(magnitude(u3)) #1 print(magnitude(u4)) #1 print(magnitude(u5)) #1 print(magnitude(u6)) #1 print(magnitude(u1)) #7.4161 print(math.sqrt(dot_product(u1,u1))) # root(dot_product of a vector with itself) = norm #return the ANGLE between two vectors def angle(u,v): return math.acos(dot_product(u,v) / (magnitude(u)*magnitude(v))) print(angle(u4,u3)) #90 pi/2 print(angle(u5,u3)) #180 pi print(angle(u53,u33)) #180 pi print(angle(u43,u33)) #90 pi #return CROSS PRODUCT of two 3-vectors def cross_product(u,v): if len(u)!=3 or len(v)!=3: print("Cross product only of vectors of length 3") else: uv_cross = [] uv_cross.append(u[1]*v[2] - u[2]*v[1]) uv_cross.append(u[2]*v[0] - u[0]*v[2]) uv_cross.append(u[0]*v[1] - u[1]*v[0]) return uv_cross a=[3,-3,1] b=[4,9,2] print(cross_product(a,b)) #āˆ’15iāˆ’2j+39k #area of parallelogram spanned by two 3-vectors print(magnitude(cross_product(a,b))) #41.8330 # = mag(a)*mag(b)*sin alpha # alpha = arcsin(mag(aXb)/(mag(a)mag(b)) print(math.asin(magnitude(cross_product(a,b))/(magnitude(a)*magnitude(b)))) #1.2694 c=[-12,12,-4] print(cross_product(a,c)) #0+0+0