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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
1 ## Copyright (C) 1995, 1996, 1997, 1999, 2000, 2002, 2004, 2005, 2006,
9245
16f53d29049f update copyright notices
John W. Eaton <jwe@octave.org>
parents: 9165
diff changeset
2 ## 2007, 2009 Kurt Hornik
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3418
diff changeset
3 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3883
diff changeset
4 ## This file is part of Octave.
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3883
diff changeset
5 ##
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3883
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3883
diff changeset
7 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6157
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6157
diff changeset
9 ## your option) any later version.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3418
diff changeset
10 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3883
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
2540
c3d634d49ce4 [project @ 1996-11-20 00:20:12 by jwe]
jwe
parents:
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
c3d634d49ce4 [project @ 1996-11-20 00:20:12 by jwe]
jwe
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3418
diff changeset
14 ## General Public License for more details.
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3418
diff changeset
15 ##
2540
c3d634d49ce4 [project @ 1996-11-20 00:20:12 by jwe]
jwe
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6157
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6157
diff changeset
18 ## <http://www.gnu.org/licenses/>.
2540
c3d634d49ce4 [project @ 1996-11-20 00:20:12 by jwe]
jwe
parents:
diff changeset
19
3321
6923abb04e16 [project @ 1999-10-26 18:15:30 by jwe]
jwe
parents: 3238
diff changeset
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
ca92c9d3f882 [project @ 2000-01-12 03:07:47 by jwe]
jwe
parents: 3321
diff changeset
24 ## @var{x} and @var{y}.
ca92c9d3f882 [project @ 2000-01-12 03:07:47 by jwe]
jwe
parents: 3321
diff changeset
25 ##
3321
6923abb04e16 [project @ 1999-10-26 18:15:30 by jwe]
jwe
parents: 3238
diff changeset
26 ## @example
6923abb04e16 [project @ 1999-10-26 18:15:30 by jwe]
jwe
parents: 3238
diff changeset
27 ## @group
6923abb04e16 [project @ 1999-10-26 18:15:30 by jwe]
jwe
parents: 3238
diff changeset
28 ## cross ([1,1,0], [0,1,1])
6923abb04e16 [project @ 1999-10-26 18:15:30 by jwe]
jwe
parents: 3238
diff changeset
29 ## @result{} [ 1; -1; 1 ]
6923abb04e16 [project @ 1999-10-26 18:15:30 by jwe]
jwe
parents: 3238
diff changeset
30 ## @end group
6923abb04e16 [project @ 1999-10-26 18:15:30 by jwe]
jwe
parents: 3238
diff changeset
31 ## @end example
3883
69b6bd271277 [project @ 2002-04-02 21:05:10 by jwe]
jwe
parents: 3426
diff changeset
32 ##
4890
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
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
6923abb04e16 [project @ 1999-10-26 18:15:30 by jwe]
jwe
parents: 3238
diff changeset
38 ## @end deftypefn
2540
c3d634d49ce4 [project @ 1996-11-20 00:20:12 by jwe]
jwe
parents:
diff changeset
39
5428
2a16423e4aa0 [project @ 2005-08-23 18:38:27 by jwe]
jwe
parents: 5307
diff changeset
40 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>
2540
c3d634d49ce4 [project @ 1996-11-20 00:20:12 by jwe]
jwe
parents:
diff changeset
41 ## Created: 15 October 1994
c3d634d49ce4 [project @ 1996-11-20 00:20:12 by jwe]
jwe
parents:
diff changeset
42 ## Adapted-By: jwe
c3d634d49ce4 [project @ 1996-11-20 00:20:12 by jwe]
jwe
parents:
diff changeset
43
4890
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
44 function z = cross (x, y, dim)
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9877
diff changeset
45
4890
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
46 if (nargin != 2 && nargin != 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 6024
diff changeset
47 print_usage ();
4890
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
48 endif
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
49
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
50 if (ndims (x) < 3 && ndims (y) < 3 && nargin < 3)
5775
ace8d8d26933 [project @ 2006-04-24 19:13:06 by jwe]
jwe
parents: 5448
diff changeset
51 ## COMPATIBILITY -- opposite behaviour for cross(row,col)
4890
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
52 ## Swap x and y in the assignments below to get the matlab behaviour.
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
53 ## Better yet, fix the calling code so that it uses conformant vectors.
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
54 if (columns (x) == 1 && rows (y) == 1)
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
55 warning ("cross: taking cross product of column by row");
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
56 y = y.';
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
57 elseif (rows (x) == 1 && columns (y) == 1)
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
58 warning ("cross: taking cross product of row by column");
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
59 x = x.';
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
60 endif
2540
c3d634d49ce4 [project @ 1996-11-20 00:20:12 by jwe]
jwe
parents:
diff changeset
61 endif
c3d634d49ce4 [project @ 1996-11-20 00:20:12 by jwe]
jwe
parents:
diff changeset
62
4890
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
63 if (nargin == 2)
6024
500d884ae373 [project @ 2006-10-03 14:27:33 by jwe]
jwe
parents: 5775
diff changeset
64 dim = find (size (x) == 3, 1);
4890
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
65 if (isempty (dim))
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
66 error ("cross: must have at least one dimension with 3 elements");
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
67 endif
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
68 else
9877
cac3b4e5035b cse in cross
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
69 if (size (x, dim) != 3)
4890
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
70 error ("cross: dimension dim must have 3 elements");
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
71 endif
3883
69b6bd271277 [project @ 2002-04-02 21:05:10 by jwe]
jwe
parents: 3426
diff changeset
72 endif
3085
e6d14959bea9 [project @ 1997-09-19 22:06:12 by jwe]
jwe
parents: 2540
diff changeset
73
4890
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
74 nd = ndims (x);
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
75 sz = size (x);
9877
cac3b4e5035b cse in cross
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
76 idx2 = idx3 = idx1 = {':'}(ones (1, nd));
4890
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
77 idx1(dim) = 1;
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
78 idx2(dim) = 2;
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
79 idx3(dim) = 3;
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
80
6157
045038e0108a [project @ 2006-11-13 22:22:53 by jwe]
jwe
parents: 6046
diff changeset
81 if (size_equal (x, y))
9877
cac3b4e5035b cse in cross
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
82 x1 = x(idx1{:});
cac3b4e5035b cse in cross
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
83 x2 = x(idx2{:});
cac3b4e5035b cse in cross
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
84 x3 = x(idx3{:});
cac3b4e5035b cse in cross
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
85 y1 = y(idx1{:});
cac3b4e5035b cse in cross
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
86 y2 = y(idx2{:});
cac3b4e5035b cse in cross
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
87 y3 = y(idx3{:});
cac3b4e5035b cse in cross
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
88 z = cat (dim, (x2.*y3 - x3.*y2), (x3.*y1 - x1.*y3), (x1.*y2 - x2.*y1));
3085
e6d14959bea9 [project @ 1997-09-19 22:06:12 by jwe]
jwe
parents: 2540
diff changeset
89 else
3883
69b6bd271277 [project @ 2002-04-02 21:05:10 by jwe]
jwe
parents: 3426
diff changeset
90 error ("cross: x and y must have the same dimensions");
2540
c3d634d49ce4 [project @ 1996-11-20 00:20:12 by jwe]
jwe
parents:
diff changeset
91 endif
c3d634d49ce4 [project @ 1996-11-20 00:20:12 by jwe]
jwe
parents:
diff changeset
92
c3d634d49ce4 [project @ 1996-11-20 00:20:12 by jwe]
jwe
parents:
diff changeset
93 endfunction