Mercurial > octave-nkf
annotate scripts/linear-algebra/cross.m @ 10549:95c3e38098bf
Untabify .m scripts
author | Rik <code@nomad.inbox5.com> |
---|---|
date | Fri, 23 Apr 2010 11:28:50 -0700 |
parents | cac3b4e5035b |
children | 1740012184f9 |
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) |
10549 | 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 | |
9877 | 69 if (size (x, dim) != 3) |
4890 | 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); | |
9877 | 76 idx2 = idx3 = idx1 = {':'}(ones (1, nd)); |
4890 | 77 idx1(dim) = 1; |
78 idx2(dim) = 2; | |
79 idx3(dim) = 3; | |
80 | |
6157 | 81 if (size_equal (x, y)) |
9877 | 82 x1 = x(idx1{:}); |
83 x2 = x(idx2{:}); | |
84 x3 = x(idx3{:}); | |
85 y1 = y(idx1{:}); | |
86 y2 = y(idx2{:}); | |
87 y3 = y(idx3{:}); | |
88 z = cat (dim, (x2.*y3 - x3.*y2), (x3.*y1 - x1.*y3), (x1.*y2 - x2.*y1)); | |
3085 | 89 else |
3883 | 90 error ("cross: x and y must have the same dimensions"); |
2540 | 91 endif |
92 | |
93 endfunction |