fromvector1importAC,r,reduce'''revised 190227''''''vector operations''''''zero vector'''O=[0,0,0]'''basis vectors'''I=[1,0,0]J=[0,1,0]K=[0,0,1]defva(a,b):'''vector addition'''return[a_i+b_ifor (a_i,b_i)inzip(a,b)]defvs(aList):'''vector sum: add together a list of vectors'''returnreduce(va,aList)defvm(a,n):'''multiplication of vector a by scalar n'''return[n*iforiina]defvdist(a,b):'''distance from a to b, i.e. b - a'''returnva(b,vm(a,-1))defu(a):'''unit vector'''returnvm(a,1.0/r(a))defmd2v(m,d):'''magnitude (m) and direction (d) to vector'''returnvm(u(d),m)defcp(a,b):'''cross product'''iflen(a)==2:returna[0]*b[1]-a[1]*b[0]iflen(a)==3:return[a[1]*b[2]-a[2]*b[1],a[2]*b[0]-a[0]*b[2],a[0]*b[1]-a[1]*b[0]]defdp(a,b):'''dot product'''returnsum([i*jfor (i,j)inzip(a,b)])defstp(a,b,c):'''scalar triple product'''returndp(a,cp(b,c))defang(a,b):'''angle between a and b'''returnAC(dp(a,b)/(r(a)*r(b)))defaxangs(a):'''angles between vector a and each axis'''return[ang(a,i)foriin(I,J,K)]defcomp(a,b):'''scalar projection of a onto b'''returndp(a,u(b))defproj(a,b):'''vector projection of a onto b'''returnvm(u(b),comp(a,b))deforth(a,b):'''proj(a, b) + orth(a, b) == a'''returnvdist(proj(a,b),a)
During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:
Ensure the proper functioning of the site (essential cookies); and
Track your browsing to send you personalized communications if you have created a professional account on the site and can be contacted (audience measurement cookies).
With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.