annotate scripts/linear-algebra/cross.m @ 27919:1891570abac8

update Octave Project Developers copyright for the new year In files that have the "Octave Project Developers" copyright notice, update for 2020.
author John W. Eaton <jwe@octave.org>
date Mon, 06 Jan 2020 22:29:51 -0500
parents b442ec6dda5c
children bd51beb6205e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27919
1891570abac8 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 27918
diff changeset
1 ## Copyright (C) 1995-2020 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26600
diff changeset
2 ##
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26600
diff changeset
3 ## See the file COPYRIGHT.md in the top-level directory of this distribution
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26600
diff changeset
4 ## or <https://octave.org/COPYRIGHT.html/>.
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26600
diff changeset
5 ##
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3418
diff changeset
6 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3883
diff changeset
7 ## This file is part of Octave.
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3883
diff changeset
8 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
9 ## Octave is free software: you can redistribute it and/or modify it
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3883
diff changeset
10 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
11 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
12 ## (at your option) any later version.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3418
diff changeset
13 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3883
diff changeset
14 ## 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
15 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
16 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## GNU General Public License for more details.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3418
diff changeset
18 ##
2540
c3d634d49ce4 [project @ 1996-11-20 00:20:12 by jwe]
jwe
parents:
diff changeset
19 ## 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
20 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
21 ## <https://www.gnu.org/licenses/>.
2540
c3d634d49ce4 [project @ 1996-11-20 00:20:12 by jwe]
jwe
parents:
diff changeset
22
3321
6923abb04e16 [project @ 1999-10-26 18:15:30 by jwe]
jwe
parents: 3238
diff changeset
23 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20160
diff changeset
24 ## @deftypefn {} {} cross (@var{x}, @var{y})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20160
diff changeset
25 ## @deftypefnx {} {} cross (@var{x}, @var{y}, @var{dim})
20160
03b9d17a2d95 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
26 ## Compute the vector cross product of two 3-dimensional vectors @var{x} and
03b9d17a2d95 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
27 ## @var{y}.
03b9d17a2d95 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
28 ##
03b9d17a2d95 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
29 ## If @var{x} and @var{y} are matrices, the cross product is applied along the
03b9d17a2d95 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
30 ## first dimension with three elements.
03b9d17a2d95 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
31 ##
03b9d17a2d95 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
32 ## The optional argument @var{dim} forces the cross product to be calculated
03b9d17a2d95 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
33 ## along the specified dimension.
03b9d17a2d95 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
34 ##
03b9d17a2d95 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
35 ## Example Code:
3418
ca92c9d3f882 [project @ 2000-01-12 03:07:47 by jwe]
jwe
parents: 3321
diff changeset
36 ##
3321
6923abb04e16 [project @ 1999-10-26 18:15:30 by jwe]
jwe
parents: 3238
diff changeset
37 ## @example
6923abb04e16 [project @ 1999-10-26 18:15:30 by jwe]
jwe
parents: 3238
diff changeset
38 ## @group
26600
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
39 ## cross ([1, 1, 0], [0, 1, 1])
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
40 ## @result{}
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
41 ## 1 -1 1
3321
6923abb04e16 [project @ 1999-10-26 18:15:30 by jwe]
jwe
parents: 3238
diff changeset
42 ## @end group
6923abb04e16 [project @ 1999-10-26 18:15:30 by jwe]
jwe
parents: 3238
diff changeset
43 ## @end example
3883
69b6bd271277 [project @ 2002-04-02 21:05:10 by jwe]
jwe
parents: 3426
diff changeset
44 ##
12546
39ca02387a32 Improve docstrings for a number of functions.
Rik <octave@nomad.inbox5.com>
parents: 12520
diff changeset
45 ## @seealso{dot, curl, divergence}
3321
6923abb04e16 [project @ 1999-10-26 18:15:30 by jwe]
jwe
parents: 3238
diff changeset
46 ## @end deftypefn
2540
c3d634d49ce4 [project @ 1996-11-20 00:20:12 by jwe]
jwe
parents:
diff changeset
47
5428
2a16423e4aa0 [project @ 2005-08-23 18:38:27 by jwe]
jwe
parents: 5307
diff changeset
48 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>
2540
c3d634d49ce4 [project @ 1996-11-20 00:20:12 by jwe]
jwe
parents:
diff changeset
49 ## Created: 15 October 1994
c3d634d49ce4 [project @ 1996-11-20 00:20:12 by jwe]
jwe
parents:
diff changeset
50 ## Adapted-By: jwe
c3d634d49ce4 [project @ 1996-11-20 00:20:12 by jwe]
jwe
parents:
diff changeset
51
4890
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
52 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
53
4890
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
54 if (nargin != 2 && nargin != 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 6024
diff changeset
55 print_usage ();
4890
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
56 endif
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
57
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
58 if (ndims (x) < 3 && ndims (y) < 3 && nargin < 3)
21308
c53bfd6d8e08 maint: Use American spelling for "behavior".
Rik <rik@octave.org>
parents: 20852
diff changeset
59 ## COMPATIBILITY -- opposite behavior for cross(row,col)
c53bfd6d8e08 maint: Use American spelling for "behavior".
Rik <rik@octave.org>
parents: 20852
diff changeset
60 ## Swap x and y in the assignments below to get the matlab behavior.
4890
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
61 ## 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
62 if (columns (x) == 1 && rows (y) == 1)
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
63 warning ("cross: taking cross product of column by row");
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
64 y = y.';
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
65 elseif (rows (x) == 1 && columns (y) == 1)
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
66 warning ("cross: taking cross product of row by column");
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
67 x = x.';
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
68 endif
2540
c3d634d49ce4 [project @ 1996-11-20 00:20:12 by jwe]
jwe
parents:
diff changeset
69 endif
c3d634d49ce4 [project @ 1996-11-20 00:20:12 by jwe]
jwe
parents:
diff changeset
70
4890
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
71 if (nargin == 2)
6024
500d884ae373 [project @ 2006-10-03 14:27:33 by jwe]
jwe
parents: 5775
diff changeset
72 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
73 if (isempty (dim))
4890
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
74 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
75 endif
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
76 else
9877
cac3b4e5035b cse in cross
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
77 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
78 error ("cross: dimension DIM must have 3 elements");
4890
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
79 endif
3883
69b6bd271277 [project @ 2002-04-02 21:05:10 by jwe]
jwe
parents: 3426
diff changeset
80 endif
3085
e6d14959bea9 [project @ 1997-09-19 22:06:12 by jwe]
jwe
parents: 2540
diff changeset
81
4890
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
82 nd = ndims (x);
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
83 sz = size (x);
9877
cac3b4e5035b cse in cross
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
84 idx2 = idx3 = idx1 = {':'}(ones (1, nd));
4890
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
85 idx1(dim) = 1;
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
86 idx2(dim) = 2;
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
87 idx3(dim) = 3;
e7da90a1cc11 [project @ 2004-05-06 20:36:29 by jwe]
jwe
parents: 3922
diff changeset
88
6157
045038e0108a [project @ 2006-11-13 22:22:53 by jwe]
jwe
parents: 6046
diff changeset
89 if (size_equal (x, y))
9877
cac3b4e5035b cse in cross
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
90 x1 = x(idx1{:});
cac3b4e5035b cse in cross
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
91 x2 = x(idx2{:});
cac3b4e5035b cse in cross
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
92 x3 = x(idx3{:});
cac3b4e5035b cse in cross
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
93 y1 = y(idx1{:});
cac3b4e5035b cse in cross
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
94 y2 = y(idx2{:});
cac3b4e5035b cse in cross
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
95 y3 = y(idx3{:});
cac3b4e5035b cse in cross
Jaroslav Hajek <highegg@gmail.com>
parents: 9245
diff changeset
96 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
97 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
98 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
99 endif
c3d634d49ce4 [project @ 1996-11-20 00:20:12 by jwe]
jwe
parents:
diff changeset
100
c3d634d49ce4 [project @ 1996-11-20 00:20:12 by jwe]
jwe
parents:
diff changeset
101 endfunction
13048
c5c94b63931f codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents: 12546
diff changeset
102
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
103
13048
c5c94b63931f codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents: 12546
diff changeset
104 %!test
c5c94b63931f codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents: 12546
diff changeset
105 %! x = [1 0 0];
c5c94b63931f codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents: 12546
diff changeset
106 %! y = [0 1 0];
c5c94b63931f codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents: 12546
diff changeset
107 %! r = [0 0 1];
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
108 %! assert (cross (x, y), r, 2e-8);
13048
c5c94b63931f codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents: 12546
diff changeset
109
c5c94b63931f codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents: 12546
diff changeset
110 %!test
c5c94b63931f codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents: 12546
diff changeset
111 %! x = [1 2 3];
c5c94b63931f codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents: 12546
diff changeset
112 %! y = [4 5 6];
c5c94b63931f codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents: 12546
diff changeset
113 %! r = [(2*6-3*5) (3*4-1*6) (1*5-2*4)];
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
114 %! assert (cross (x, y), r, 2e-8);
13048
c5c94b63931f codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents: 12546
diff changeset
115
c5c94b63931f codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents: 12546
diff changeset
116 %!test
c5c94b63931f codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents: 12546
diff changeset
117 %! x = [1 0 0; 0 1 0; 0 0 1];
c5c94b63931f codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents: 12546
diff changeset
118 %! y = [0 1 0; 0 0 1; 1 0 0];
c5c94b63931f codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents: 12546
diff changeset
119 %! r = [0 0 1; 1 0 0; 0 1 0];
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
120 %! assert (cross (x, y, 2), r, 2e-8);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
121 %! assert (cross (x, y, 1), -r, 2e-8);
13048
c5c94b63931f codesprint: linear algebra tests: cross, housh, planerot, qzhess, rref
Roman Belov <romblv@gmail.com>
parents: 12546
diff changeset
122
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
123 %!error cross (0,0)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
124 %!error cross ()