Mercurial > octave
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 |
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 | 6 ## |
3922 | 7 ## This file is part of Octave. |
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 | 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 | 13 ## |
3922 | 14 ## Octave is distributed in the hope that it will be useful, but |
2540 | 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 | 18 ## |
2540 | 19 ## You should have received a copy of the GNU General Public License |
7016 | 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 | 22 |
3321 | 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 | 36 ## |
3321 | 37 ## @example |
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 | 42 ## @end group |
43 ## @end example | |
3883 | 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 | 46 ## @end deftypefn |
2540 | 47 |
5428 | 48 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
2540 | 49 ## Created: 15 October 1994 |
50 ## Adapted-By: jwe | |
51 | |
4890 | 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 | 54 if (nargin != 2 && nargin != 3) |
6046 | 55 print_usage (); |
4890 | 56 endif |
57 | |
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 | 61 ## Better yet, fix the calling code so that it uses conformant vectors. |
62 if (columns (x) == 1 && rows (y) == 1) | |
63 warning ("cross: taking cross product of column by row"); | |
64 y = y.'; | |
65 elseif (rows (x) == 1 && columns (y) == 1) | |
66 warning ("cross: taking cross product of row by column"); | |
67 x = x.'; | |
68 endif | |
2540 | 69 endif |
70 | |
4890 | 71 if (nargin == 2) |
6024 | 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 | 74 error ("cross: must have at least one dimension with 3 elements"); |
75 endif | |
76 else | |
9877 | 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 | 79 endif |
3883 | 80 endif |
3085 | 81 |
4890 | 82 nd = ndims (x); |
83 sz = size (x); | |
9877 | 84 idx2 = idx3 = idx1 = {':'}(ones (1, nd)); |
4890 | 85 idx1(dim) = 1; |
86 idx2(dim) = 2; | |
87 idx3(dim) = 3; | |
88 | |
6157 | 89 if (size_equal (x, y)) |
9877 | 90 x1 = x(idx1{:}); |
91 x2 = x(idx2{:}); | |
92 x3 = x(idx3{:}); | |
93 y1 = y(idx1{:}); | |
94 y2 = y(idx2{:}); | |
95 y3 = y(idx3{:}); | |
96 z = cat (dim, (x2.*y3 - x3.*y2), (x3.*y1 - x1.*y3), (x1.*y2 - x2.*y1)); | |
3085 | 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 | 99 endif |
100 | |
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 () |