Mercurial > hg > octave-lyh
annotate scripts/linear-algebra/cross.m @ 9245:16f53d29049f
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 22 May 2009 10:46:00 -0400 |
parents | 8c71a86c4bf4 |
children | cac3b4e5035b |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1995, 1996, 1997, 1999, 2000, 2002, 2004, 2005, 2006, |
9245 | 2 ## 2007, 2009 Kurt Hornik |
3426 | 3 ## |
3922 | 4 ## This file is part of Octave. |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
3426 | 10 ## |
3922 | 11 ## Octave is distributed in the hope that it will be useful, but |
2540 | 12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 14 ## General Public License for more details. |
15 ## | |
2540 | 16 ## You should have received a copy of the GNU General Public License |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
2540 | 19 |
3321 | 20 ## -*- texinfo -*- |
9165
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
21 ## @deftypefn {Function File} {} cross (@var{x}, @var{y}) |
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
22 ## @deftypefnx {Function File} {} cross (@var{x}, @var{y}, @var{dim}) |
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
23 ## Compute the vector cross product of two 3-dimensional vectors |
3418 | 24 ## @var{x} and @var{y}. |
25 ## | |
3321 | 26 ## @example |
27 ## @group | |
28 ## cross ([1,1,0], [0,1,1]) | |
29 ## @result{} [ 1; -1; 1 ] | |
30 ## @end group | |
31 ## @end example | |
3883 | 32 ## |
4890 | 33 ## If @var{x} and @var{y} are matrices, the cross product is applied |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
34 ## along the first dimension with 3 elements. The optional argument |
9165
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
35 ## @var{dim} forces the cross product to be calculated along |
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
36 ## the specified dimension. |
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
37 ## @seealso{dot} |
3321 | 38 ## @end deftypefn |
2540 | 39 |
5428 | 40 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
2540 | 41 ## Created: 15 October 1994 |
42 ## Adapted-By: jwe | |
43 | |
4890 | 44 function z = cross (x, y, dim) |
3883 | 45 |
4890 | 46 if (nargin != 2 && nargin != 3) |
6046 | 47 print_usage (); |
4890 | 48 endif |
49 | |
50 if (ndims (x) < 3 && ndims (y) < 3 && nargin < 3) | |
5775 | 51 ## COMPATIBILITY -- opposite behaviour for cross(row,col) |
4890 | 52 ## Swap x and y in the assignments below to get the matlab behaviour. |
53 ## Better yet, fix the calling code so that it uses conformant vectors. | |
54 if (columns (x) == 1 && rows (y) == 1) | |
55 warning ("cross: taking cross product of column by row"); | |
56 y = y.'; | |
57 elseif (rows (x) == 1 && columns (y) == 1) | |
58 warning ("cross: taking cross product of row by column"); | |
59 x = x.'; | |
60 endif | |
2540 | 61 endif |
62 | |
4890 | 63 if (nargin == 2) |
6024 | 64 dim = find (size (x) == 3, 1); |
4890 | 65 if (isempty (dim)) |
66 error ("cross: must have at least one dimension with 3 elements"); | |
67 endif | |
68 else | |
69 if (size (x) != 3) | |
70 error ("cross: dimension dim must have 3 elements"); | |
71 endif | |
3883 | 72 endif |
3085 | 73 |
4890 | 74 nd = ndims (x); |
75 sz = size (x); | |
5108 | 76 idx1 = cell (1, nd); |
4890 | 77 for i = 1:nd |
78 idx1{i} = 1:sz(i); | |
79 endfor | |
80 idx2 = idx3 = idx1; | |
81 idx1(dim) = 1; | |
82 idx2(dim) = 2; | |
83 idx3(dim) = 3; | |
84 | |
6157 | 85 if (size_equal (x, y)) |
4890 | 86 z = cat (dim, |
87 (x(idx2{:}) .* y(idx3{:}) - x(idx3{:}) .* y(idx2{:})), | |
88 (x(idx3{:}) .* y(idx1{:}) - x(idx1{:}) .* y(idx3{:})), | |
89 (x(idx1{:}) .* y(idx2{:}) - x(idx2{:}) .* y(idx1{:}))); | |
3085 | 90 else |
3883 | 91 error ("cross: x and y must have the same dimensions"); |
2540 | 92 endif |
93 | |
94 endfunction |