import scipy
from numpy import *
#Variable Declaration
A=array([10,-4,6])
B=array([2,1,0])
ax=array([1,0,0]) #Unit vector along x direction
ay=array([0,1,0]) #Unit vector along y direction
az=array([0,0,1]) #Unit vector along z direction
#Calculations
Ay=dot(A,ay) #Component of A along y direction
l=scipy.sqrt(dot(3*A-B,3*A-B)) #Magnitude of the vector 3A-B
#Defining the x,y and z components of the unit vector along A+2B
ux=round(dot(A+2*B,ax)/scipy.sqrt(dot(A+2*B,A+2*B)),4)
uy=round(dot(A+2*B,ay)/scipy.sqrt(dot(A+2*B,A+2*B)),4)
uz=round(dot(A+2*B,az)/scipy.sqrt(dot(A+2*B,A+2*B)),4)
u=array([ux,uy,uz])
#Results
print 'The component of A along y direction is',Ay
print 'Magnitude of 3A-B =',round(l,2)
print 'Unit vector along A+2B is',u
输出:
The component of A along y direction is -4
Magnitude of 3A-B = 35.74
Unit vector along A+2B is [ 0.9113 -0.1302 0.3906]