home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / linear-algebra / dot.m < prev    next >
Text File  |  1999-03-09  |  1KB  |  50 lines

  1. ## Copyright (C) 1998 John W. Eaton
  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. ## usage:  dot (x, y)
  18. ##
  19. ## Computes the dot product of two vectors.
  20.  
  21. ## Author: jwe
  22.  
  23. function z = dot (x, y)
  24.   
  25.   if (nargin != 2)
  26.     usage ("dot (x, y)");
  27.   endif
  28.  
  29.   if (is_vector (x) && is_vector (y) && length (x) == length (y))
  30.     [x_nr, x_nc] = size (x);
  31.     [y_nr, y_nc] = size (y);
  32.     if (x_nr == 1)
  33.       if (y_nr == 1)
  34.     z = x * y.';
  35.       else
  36.     z = x * y;
  37.       endif
  38.     else
  39.       if (y_nr == 1)
  40.     z = y * x;
  41.       else
  42.     z = y.' * x;
  43.       endif
  44.     endif
  45.   else
  46.     error ("dot: both arguments must be vectors of the same length");
  47.   endif
  48.  
  49. endfunction
  50.