home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / SCRIPTS.ZIP / scripts / linear-algebra / cross.m < prev    next >
Text File  |  1999-10-26  |  2KB  |  57 lines

  1. ## Copyright (C) 1995, 1996  Kurt Hornik
  2. ## 
  3. ## This program is free software; you can redistribute it and/or modify
  4. ## it under the terms of the GNU General Public License as published by
  5. ## the Free Software Foundation; either version 2, or (at your option)
  6. ## any later version.
  7. ## 
  8. ## This program is distributed in the hope that it will be useful, but
  9. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11. ## General Public License for more details. 
  12. ## 
  13. ## You should have received a copy of the GNU General Public License
  14. ## along with this file.  If not, write to the Free Software Foundation,
  15. ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16.  
  17. ## -*- texinfo -*-
  18. ## @deftypefn {Function File} {} cross (@var{x}, @var{y})
  19. ## Computes the vector cross product of the two 3-dimensional vectors
  20. ## @var{x} and @var{y}.  For example,
  21. ## 
  22. ## @example
  23. ## @group
  24. ## cross ([1,1,0], [0,1,1])
  25. ##      @result{} [ 1; -1; 1 ]
  26. ## @end group
  27. ## @end example
  28. ## @end deftypefn
  29.  
  30. ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at>
  31. ## Created: 15 October 1994
  32. ## Adapted-By: jwe
  33.  
  34. function z = cross (x, y)
  35.   
  36.   if (nargin != 2)
  37.     usage ("cross (x, y)");
  38.   endif
  39.  
  40.   if (length (x) == 3 && length (y) == 3)
  41.  
  42.     z = [x(2)*y(3) - x(3)*y(2); x(3)*y(1) - x(1)*y(3); x(1)*y(2) - x(2)*y(1)];
  43.  
  44.     x_nr = rows (x);
  45.     y_nr = rows (y);
  46.  
  47.     if ((x_nr == y_nr && x_nr == 1)
  48.      || (x_nr != y_nr && ! prefer_column_vectors))
  49.       z = z.';
  50.     endif
  51.  
  52.   else
  53.     error ("cross: both x and y must be 3-dimensional vectors");
  54.   endif
  55.  
  56. endfunction
  57.