home *** CD-ROM | disk | FTP | other *** search
/ El Mac 9 / El Mac 9.iso / Shareware / Applications / MathPad 2.4 / Examples / incl / vector ops < prev   
Encoding:
Text File  |  1995-04-30  |  695 b   |  20 lines  |  [TEXT/MPad]

  1. -- Arrays can be used to represent vectors. Addition, subtraction and multiplication by a scalar can be done directly. The following functions implement other basic vector operations.
  2.  
  3. dot(A,B) = sum(A[i]*B[i],i,1,count(A))
  4.  
  5. magnitude(A) = sqrt(dot(A,A))
  6.  
  7. cross(A,B) = {A[2]*B[3]-A[3]*B[2],  -- 3D only
  8.               A[3]*B[1]-A[1]*B[3],
  9.               A[1]*B[2]-A[2]*B[1]}
  10.  
  11. -- cartesian to spherical coordinates
  12. spherical(A) = {magnitude(A),
  13.                 acos(A[3]/magnitude(A)),
  14.                 atan2(A[2],A[1])}
  15.  
  16. -- spherical to cartesian coordinates
  17. cartesian(r,theta,phi) = {r*sin(theta)*cos(phi),
  18.                           r*sin(theta)*sin(phi),
  19.                           r*cos(theta) }
  20.