Mercurial > hg > octave-nkf
annotate scripts/linear-algebra/cross.m @ 13011:1609dd300c4d
maint: add missing files for changeset edc5ec6e949b
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 26 Aug 2011 17:14:26 -0400 |
parents | 39ca02387a32 |
children | c5c94b63931f |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 1995-2011 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 | |
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 |
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 |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
2540 | 18 |
3321 | 19 ## -*- texinfo -*- |
9165
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
20 ## @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
|
21 ## @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
|
22 ## Compute the vector cross product of 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 ## |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
32 ## If @var{x} and @var{y} are matrices, the cross product is applied |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
33 ## 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
|
34 ## @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
|
35 ## the specified dimension. |
12546
39ca02387a32
Improve docstrings for a number of functions.
Rik <octave@nomad.inbox5.com>
parents:
12520
diff
changeset
|
36 ## @seealso{dot, curl, divergence} |
3321 | 37 ## @end deftypefn |
2540 | 38 |
5428 | 39 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
2540 | 40 ## Created: 15 October 1994 |
41 ## Adapted-By: jwe | |
42 | |
4890 | 43 function z = cross (x, y, dim) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
44 |
4890 | 45 if (nargin != 2 && nargin != 3) |
6046 | 46 print_usage (); |
4890 | 47 endif |
48 | |
49 if (ndims (x) < 3 && ndims (y) < 3 && nargin < 3) | |
5775 | 50 ## COMPATIBILITY -- opposite behaviour for cross(row,col) |
4890 | 51 ## Swap x and y in the assignments below to get the matlab behaviour. |
52 ## Better yet, fix the calling code so that it uses conformant vectors. | |
53 if (columns (x) == 1 && rows (y) == 1) | |
54 warning ("cross: taking cross product of column by row"); | |
55 y = y.'; | |
56 elseif (rows (x) == 1 && columns (y) == 1) | |
57 warning ("cross: taking cross product of row by column"); | |
58 x = x.'; | |
59 endif | |
2540 | 60 endif |
61 | |
4890 | 62 if (nargin == 2) |
6024 | 63 dim = find (size (x) == 3, 1); |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
64 if (isempty (dim)) |
4890 | 65 error ("cross: must have at least one dimension with 3 elements"); |
66 endif | |
67 else | |
9877 | 68 if (size (x, dim) != 3) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
69 error ("cross: dimension DIM must have 3 elements"); |
4890 | 70 endif |
3883 | 71 endif |
3085 | 72 |
4890 | 73 nd = ndims (x); |
74 sz = size (x); | |
9877 | 75 idx2 = idx3 = idx1 = {':'}(ones (1, nd)); |
4890 | 76 idx1(dim) = 1; |
77 idx2(dim) = 2; | |
78 idx3(dim) = 3; | |
79 | |
6157 | 80 if (size_equal (x, y)) |
9877 | 81 x1 = x(idx1{:}); |
82 x2 = x(idx2{:}); | |
83 x3 = x(idx3{:}); | |
84 y1 = y(idx1{:}); | |
85 y2 = y(idx2{:}); | |
86 y3 = y(idx3{:}); | |
87 z = cat (dim, (x2.*y3 - x3.*y2), (x3.*y1 - x1.*y3), (x1.*y2 - x2.*y1)); | |
3085 | 88 else |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
89 error ("cross: X and Y must have the same dimensions"); |
2540 | 90 endif |
91 | |
92 endfunction |