3175
|
1 ## Copyright (C) 1998 John W. Eaton |
3426
|
2 ## |
3922
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
3426
|
9 ## |
3922
|
10 ## Octave is distributed in the hope that it will be useful, but |
3175
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
13 ## General Public License for more details. |
|
14 ## |
3175
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
3175
|
18 |
3449
|
19 ## -*- texinfo -*- |
4890
|
20 ## @deftypefn {Function File} {} dot (@var{x}, @var{y}, @var{dim}) |
|
21 ## Computes the dot product of two vectors. If @var{x} and @var{y} |
|
22 ## are matrices, calculate the dot-product along the first |
|
23 ## non-singleton dimension. If the optional argument @var{dim} is |
|
24 ## given, calculate the dot-product along this dimension. |
3449
|
25 ## @end deftypefn |
3175
|
26 |
|
27 ## Author: jwe |
|
28 |
4890
|
29 function z = dot (x, y, dim) |
3426
|
30 |
4890
|
31 if (nargin != 2 && nargin != 3) |
6046
|
32 print_usage (); |
3175
|
33 endif |
|
34 |
4890
|
35 if (nargin < 3) |
|
36 if isvector (x) |
|
37 x = x(:); |
|
38 endif |
|
39 if isvector (y) |
|
40 y = y(:); |
3175
|
41 endif |
6157
|
42 if (! size_equal (x, y)) |
4890
|
43 error ("dot: sizes of arguments must match") |
|
44 endif |
|
45 z = sum(x .* y); |
3175
|
46 else |
6157
|
47 if (! size_equal (x, y)) |
4890
|
48 error ("dot: sizes of arguments must match") |
|
49 endif |
|
50 z = sum(x .* y, dim); |
3175
|
51 endif |
|
52 |
|
53 endfunction |