Mercurial > octave
annotate scripts/general/rotdim.m @ 29359:7854d5752dd2
maint: merge stable to default.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 10 Feb 2021 10:10:40 -0500 |
parents | d8318c12d903 0a5b15007766 |
children | 796f54d4ddbf |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
29358
0a5b15007766
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27923
diff
changeset
|
3 ## Copyright (C) 2004-2021 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
5012 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
5012 | 11 ## 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
|
12 ## 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
|
13 ## (at your option) any later version. |
5012 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
5012 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
7016 | 21 ## 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
|
22 ## <https://www.gnu.org/licenses/>. |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
5012 | 25 |
26 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20231
diff
changeset
|
27 ## @deftypefn {} {} rotdim (@var{x}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20231
diff
changeset
|
28 ## @deftypefnx {} {} rotdim (@var{x}, @var{n}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20231
diff
changeset
|
29 ## @deftypefnx {} {} rotdim (@var{x}, @var{n}, @var{plane}) |
5012 | 30 ## Return a copy of @var{x} with the elements rotated counterclockwise in |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
31 ## 90-degree increments. |
20158
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
32 ## |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
33 ## The second argument @var{n} is optional, and specifies how many 90-degree |
20158
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
34 ## rotations are to be applied (the default value is 1). Negative values of |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
35 ## @var{n} rotate the matrix in a clockwise direction. |
5012 | 36 ## |
20158
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
37 ## The third argument is also optional and defines the plane of the rotation. |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
38 ## If present, @var{plane} is a two element vector containing two different |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
39 ## valid dimensions of the matrix. When @var{plane} is not given the first two |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
40 ## non-singleton dimensions are used. |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
41 ## |
5012 | 42 ## For example, |
43 ## | |
44 ## @example | |
45 ## @group | |
46 ## rotdim ([1, 2; 3, 4], -1, [1, 2]) | |
47 ## @result{} 3 1 | |
48 ## 4 2 | |
49 ## @end group | |
50 ## @end example | |
51 ## | |
52 ## @noindent | |
53 ## rotates the given matrix clockwise by 90 degrees. The following are all | |
54 ## equivalent statements: | |
55 ## | |
56 ## @example | |
57 ## @group | |
5487 | 58 ## rotdim ([1, 2; 3, 4], -1, [1, 2]) |
59 ## rotdim ([1, 2; 3, 4], 3, [1, 2]) | |
60 ## rotdim ([1, 2; 3, 4], 7, [1, 2]) | |
5012 | 61 ## @end group |
62 ## @end example | |
19166
767103303974
doc: Re-order seealso references in rot90, rotdim.
Rik <rik@octave.org>
parents:
19126
diff
changeset
|
63 ## @seealso{rot90, fliplr, flipud, flip} |
5012 | 64 ## @end deftypefn |
65 | |
10689
6622772a0add
rotdim.m: Modify function to use same variable names as documentation
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
66 function y = rotdim (x, n, plane) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
67 |
28789
28de41192f3c
Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
68 if (nargin < 1) |
6046 | 69 print_usage (); |
5012 | 70 endif |
71 | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
72 if (nargin > 1 && ! isempty (n)) |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
73 if (! isscalar (n) || ! isreal (n) || fix (n) != n) |
10689
6622772a0add
rotdim.m: Modify function to use same variable names as documentation
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
74 error ("rotdim: N must be a scalar integer"); |
5012 | 75 endif |
76 else | |
10689
6622772a0add
rotdim.m: Modify function to use same variable names as documentation
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
77 n = 1; |
5012 | 78 endif |
79 | |
80 nd = ndims (x); | |
81 sz = size (x); | |
82 if (nargin < 3) | |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
83 if (nd > 2) |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
84 ## Find the first two non-singleton dimension. |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
85 plane = []; |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
86 dim = 0; |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
87 while (dim < nd) |
20231
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
88 dim += 1; |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
89 if (sz (dim) != 1) |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
90 plane = [plane, dim]; |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
91 if (length (plane) == 2) |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
92 break; |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
93 endif |
10549 | 94 endif |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
95 endwhile |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
96 if (length (plane) < 1) |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
97 plane = [1, 2]; |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
98 elseif (length (plane) < 2) |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
99 plane = [1, plane]; |
5012 | 100 endif |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
101 else |
5012 | 102 plane = [1, 2]; |
103 endif | |
104 else | |
105 if (! (isvector (plane) && length (plane) == 2 | |
13279
984359717d71
Use common code idiom for checking whether a double value is an integer.
Rik <octave@nomad.inbox5.com>
parents:
12795
diff
changeset
|
106 && all (plane == fix (plane)) && all (plane > 0) |
10549 | 107 && all (plane < (nd + 1)) && plane(1) != plane(2))) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10689
diff
changeset
|
108 error ("rotdim: PLANE must be a 2 element integer vector defining a valid PLANE"); |
5012 | 109 endif |
110 endif | |
111 | |
10689
6622772a0add
rotdim.m: Modify function to use same variable names as documentation
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
112 n = rem (n, 4); |
6622772a0add
rotdim.m: Modify function to use same variable names as documentation
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
113 if (n < 0) |
20231
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
114 n += 4; |
5012 | 115 endif |
10689
6622772a0add
rotdim.m: Modify function to use same variable names as documentation
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
116 if (n == 0) |
5012 | 117 y = x; |
10689
6622772a0add
rotdim.m: Modify function to use same variable names as documentation
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
118 elseif (n == 2) |
19126
995df67fc912
Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents:
18857
diff
changeset
|
119 y = flip (flip (x, plane(1)), plane(2)); |
10689
6622772a0add
rotdim.m: Modify function to use same variable names as documentation
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
120 elseif (n == 1 || n == 3) |
5012 | 121 perm = 1:nd; |
122 perm(plane(1)) = plane(2); | |
123 perm(plane(2)) = plane(1); | |
124 y = permute (x, perm); | |
10689
6622772a0add
rotdim.m: Modify function to use same variable names as documentation
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
125 if (n == 1) |
19126
995df67fc912
Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents:
18857
diff
changeset
|
126 y = flip (y, min (plane)); |
5012 | 127 else |
19126
995df67fc912
Flip arrays - ND support for fliplr and flipud, and replace flipdim with flip.
Carnë Draug <carandraug+dev@gmail.com>
parents:
18857
diff
changeset
|
128 y = flip (y, max (plane)); |
5012 | 129 endif |
130 else | |
131 error ("rotdim: internal error!"); | |
132 endif | |
133 | |
134 endfunction | |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
135 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
136 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
137 %!shared r, rr |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
138 %! r = [1,2,3]; rr = [3,2,1]; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
139 %!assert (rotdim (r, 0), r) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
140 %!assert (rotdim (r, 1), rr') |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
141 %!assert (rotdim (r, 2), rr) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
142 %!assert (rotdim (r, 3), r') |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
143 %!assert (rotdim (r, 3), rotdim (r, -1)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
144 %!assert (rotdim (r, 1), rotdim (r)) |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
145 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
146 %!shared c, cr |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
147 %! c = [1;2;3]; cr = [3;2;1]; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
148 %!assert (rotdim (c, 0), c) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
149 %!assert (rotdim (c, 1), c') |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
150 %!assert (rotdim (c, 2), cr) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
151 %!assert (rotdim (c, 3), cr') |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
152 %!assert (rotdim (c, 3), rotdim (c, -1)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
153 %!assert (rotdim (c, 1), rotdim (c)) |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
154 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
155 %!shared m |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
156 %! m = [1,2;3,4]; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
157 %!assert (rotdim (m, 0), m) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
158 %!assert (rotdim (m, 1), [2,4;1,3]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
159 %!assert (rotdim (m, 2), [4,3;2,1]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
160 %!assert (rotdim (m, 3), [3,1;4,2]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
161 %!assert (rotdim (m, 3), rotdim (m, -1)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
162 %!assert (rotdim (m, 1), rotdim (m)) |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12639
diff
changeset
|
163 |
18857
7bbe3658c5ef
maint: Use "FIXME:" coding convention in m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
164 ## FIXME: We need tests for multidimensional arrays |
7bbe3658c5ef
maint: Use "FIXME:" coding convention in m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
165 ## and different values of PLANE. |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
166 |
28886
d8318c12d903
test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
167 %!error <Invalid call> rotdim () |