2540
|
1 ## Copyright (C) 1995, 1996 Kurt Hornik |
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 |
2540
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
3426
|
9 ## |
3922
|
10 ## Octave is distributed in the hope that it will be useful, but |
2540
|
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 ## |
2540
|
15 ## You should have received a copy of the GNU General Public License |
3922
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
2540
|
19 |
3321
|
20 ## -*- texinfo -*- |
4890
|
21 ## @deftypefn {Function File} {} cross (@var{x}, @var{y}, @var{dim}) |
2540
|
22 ## Computes the vector cross product of the two 3-dimensional vectors |
3418
|
23 ## @var{x} and @var{y}. |
|
24 ## |
3321
|
25 ## @example |
|
26 ## @group |
|
27 ## cross ([1,1,0], [0,1,1]) |
|
28 ## @result{} [ 1; -1; 1 ] |
|
29 ## @end group |
|
30 ## @end example |
3883
|
31 ## |
4890
|
32 ## If @var{x} and @var{y} are matrices, the cross product is applied |
|
33 ## along the first dimension with 3 elements. The optional argument |
|
34 ## @var{dim} is used to force the cross product to be calculated along |
5448
|
35 ## the dimension defined by @var{dim}. |
3321
|
36 ## @end deftypefn |
2540
|
37 |
5428
|
38 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
2540
|
39 ## Created: 15 October 1994 |
|
40 ## Adapted-By: jwe |
|
41 |
4890
|
42 function z = cross (x, y, dim) |
3883
|
43 |
4890
|
44 if (nargin != 2 && nargin != 3) |
|
45 usage ("cross (x, y, dim)"); |
|
46 endif |
|
47 |
|
48 if (ndims (x) < 3 && ndims (y) < 3 && nargin < 3) |
5775
|
49 ## COMPATIBILITY -- opposite behaviour for cross(row,col) |
4890
|
50 ## Swap x and y in the assignments below to get the matlab behaviour. |
|
51 ## Better yet, fix the calling code so that it uses conformant vectors. |
|
52 if (columns (x) == 1 && rows (y) == 1) |
|
53 warning ("cross: taking cross product of column by row"); |
|
54 y = y.'; |
|
55 elseif (rows (x) == 1 && columns (y) == 1) |
|
56 warning ("cross: taking cross product of row by column"); |
|
57 x = x.'; |
|
58 endif |
2540
|
59 endif |
|
60 |
4890
|
61 if (nargin == 2) |
6024
|
62 dim = find (size (x) == 3, 1); |
4890
|
63 if (isempty (dim)) |
|
64 error ("cross: must have at least one dimension with 3 elements"); |
|
65 endif |
|
66 else |
|
67 if (size (x) != 3) |
|
68 error ("cross: dimension dim must have 3 elements"); |
|
69 endif |
3883
|
70 endif |
3085
|
71 |
4890
|
72 nd = ndims (x); |
|
73 sz = size (x); |
5108
|
74 idx1 = cell (1, nd); |
4890
|
75 for i = 1:nd |
|
76 idx1{i} = 1:sz(i); |
|
77 endfor |
|
78 idx2 = idx3 = idx1; |
|
79 idx1(dim) = 1; |
|
80 idx2(dim) = 2; |
|
81 idx3(dim) = 3; |
|
82 |
|
83 if (size (x) == size (y)) |
|
84 z = cat (dim, |
|
85 (x(idx2{:}) .* y(idx3{:}) - x(idx3{:}) .* y(idx2{:})), |
|
86 (x(idx3{:}) .* y(idx1{:}) - x(idx1{:}) .* y(idx3{:})), |
|
87 (x(idx1{:}) .* y(idx2{:}) - x(idx2{:}) .* y(idx1{:}))); |
3085
|
88 else |
3883
|
89 error ("cross: x and y must have the same dimensions"); |
2540
|
90 endif |
|
91 |
|
92 endfunction |