Mercurial > octave
annotate scripts/general/rot90.m @ 33639:65b1cf6aa60a bytecode-interpreter tip
maint: Merge default to bytecode-interpreter
author | Arun Giridhar <arungiridhar@gmail.com> |
---|---|
date | Mon, 03 Jun 2024 14:38:47 -0400 |
parents | 2e484f9f1f18 |
children |
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 ## |
32632
2e484f9f1f18
maint: update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
31706
diff
changeset
|
3 ## Copyright (C) 1993-2024 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
27898
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/>. |
2313 | 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 |
2313 | 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. |
2313 | 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. |
2313 | 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 ######################################################################## |
245 | 25 |
3369 | 26 ## -*- texinfo -*- |
30875
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
27 ## @deftypefn {} {@var{B} =} rot90 (@var{A}) |
5d3faba0342e
doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
28 ## @deftypefnx {} {@var{B} =} rot90 (@var{A}, @var{k}) |
19147
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
29 ## Rotate array by 90 degree increments. |
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
30 ## |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
31 ## Return a copy of @var{A} with the elements rotated counterclockwise in |
20158
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
32 ## 90-degree increments. |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
33 ## |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
34 ## The second argument is optional, and specifies how many 90-degree rotations |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
35 ## are to be applied (the default value is 1). Negative values of @var{k} |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
36 ## rotate the matrix in a clockwise direction. |
3369 | 37 ## For example, |
3426 | 38 ## |
3369 | 39 ## @example |
40 ## @group | |
41 ## rot90 ([1, 2; 3, 4], -1) | |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
42 ## @result{} 3 1 |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
43 ## 4 2 |
3369 | 44 ## @end group |
45 ## @end example | |
3426 | 46 ## |
3369 | 47 ## @noindent |
48 ## rotates the given matrix clockwise by 90 degrees. The following are all | |
49 ## equivalent statements: | |
3426 | 50 ## |
3369 | 51 ## @example |
52 ## @group | |
53 ## rot90 ([1, 2; 3, 4], -1) | |
54 ## rot90 ([1, 2; 3, 4], 3) | |
55 ## rot90 ([1, 2; 3, 4], 7) | |
56 ## @end group | |
57 ## @end example | |
4869 | 58 ## |
19147
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
59 ## The rotation is always performed on the plane of the first two dimensions, |
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
60 ## i.e., rows and columns. To perform a rotation on any other plane, use |
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
61 ## @code{rotdim}. |
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
62 ## |
19166
767103303974
doc: Re-order seealso references in rot90, rotdim.
Rik <rik@octave.org>
parents:
19147
diff
changeset
|
63 ## @seealso{rotdim, fliplr, flipud, flip} |
3369 | 64 ## @end deftypefn |
4 | 65 |
12862
a9d292ce5489
rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
66 function B = rot90 (A, k = 1) |
4869 | 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 (); |
4 | 70 endif |
71 | |
19147
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
72 if (! (isscalar (k) && isreal (k) && k == fix (k))) |
12862
a9d292ce5489
rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
73 error ("rot90: K must be a single real integer"); |
a9d292ce5489
rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
74 endif |
a9d292ce5489
rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
75 |
13287
5c22edebf2e8
rot90.m: Simplify function by using mod() rather than rem()
Rik <octave@nomad.inbox5.com>
parents:
12862
diff
changeset
|
76 k = mod (k, 4); |
19147
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
77 nd = ndims (A); |
12862
a9d292ce5489
rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
78 |
a9d292ce5489
rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
79 if (k == 0) |
a9d292ce5489
rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
80 B = A; |
a9d292ce5489
rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
81 elseif (k == 1) |
19147
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
82 B = flipud (permute (A, [2 1 3:1:nd])); |
12862
a9d292ce5489
rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
83 elseif (k == 2) |
19147
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
84 idx(1:nd) = {':'}; |
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
85 idx{1} = rows (A):-1:1; |
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
86 idx{2} = columns (A):-1:1; |
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
87 B = A(idx{:}); |
12862
a9d292ce5489
rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
88 elseif (k == 3) |
19147
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
89 B = permute (flipud (A), [2 1 3:1:nd]); |
12862
a9d292ce5489
rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
90 else |
a9d292ce5489
rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
91 error ("rot90: internal error!"); |
a9d292ce5489
rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
92 endif |
a9d292ce5489
rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
93 |
4 | 94 endfunction |
7411 | 95 |
12862
a9d292ce5489
rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
96 |
7411 | 97 %!test |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
98 %! x1 = [1, 2; 3, 4]; |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
99 %! x2 = [2, 4; 1, 3]; |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
100 %! x3 = [4, 3; 2, 1]; |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
101 %! x4 = [3, 1; 4, 2]; |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
102 %! |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
103 %! assert (rot90 (x1), x2); |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
104 %! assert (rot90 (x1, 2), x3); |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
105 %! assert (rot90 (x1, 3), x4); |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
106 %! assert (rot90 (x1, 4), x1); |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
107 %! assert (rot90 (x1, 5), x2); |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
108 %! assert (rot90 (x1, -1), x4); |
7411 | 109 |
19147
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
110 ## Test NDArrays |
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
111 %!test |
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
112 %! a(1:2,1:2,1) = [1 2; 3 4]; |
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
113 %! a(1:2,1:2,2) = [5 6; 7 8]; |
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
114 %! b(1:2,1:2,1) = [2 4; 1 3]; |
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
115 %! b(1:2,1:2,2) = [6 8; 5 7]; |
21580
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
116 %! assert (rot90 (a, 1), b); |
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
117 %! assert (rot90 (a, 2), rot90 (b, 1)); |
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
118 %! assert (rot90 (a, 3), rot90 (b, 2)); |
19147
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
119 |
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
120 %!test |
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
121 %! a = b = zeros (2, 2, 1, 2); |
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
122 %! a(1:2,1:2,:,1) = [1 2; 3 4]; |
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
123 %! a(1:2,1:2,:,2) = [5 6; 7 8]; |
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
124 %! b(1:2,1:2,:,1) = [2 4; 1 3]; |
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
125 %! b(1:2,1:2,:,2) = [6 8; 5 7]; |
21580
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
126 %! assert (rot90 (a, 1), b); |
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
127 %! assert (rot90 (a, 2), rot90 (b, 1)); |
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
128 %! assert (rot90 (a, 3), rot90 (b, 2)); |
19147
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
129 |
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
130 ## With non-square matrices |
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
131 %!test |
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
132 %! a = zeros (3, 2, 1, 2); |
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
133 %! b = zeros (2, 3, 1, 2); |
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
134 %! a(1:2,1:3,:,1) = [ 1 2 3; 4 5 6]; |
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
135 %! a(1:2,1:3,:,2) = [ 7 8 9; 10 11 12]; |
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
136 %! b(1:3,1:2,:,1) = [ 3 6; 2 5; 1 4]; |
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
137 %! b(1:3,1:2,:,2) = [ 9 12; 8 11; 7 10]; |
21580
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
138 %! assert (rot90 (a, 1), b); |
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
139 %! assert (rot90 (a, 2), rot90 (b, 1)); |
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
140 %! assert (rot90 (a, 3), rot90 (b, 2)); |
19147
8b2a919d24bc
rot90.m: add support for N-dimensional arrays.
Carnë Draug <carandraug@octave.org>
parents:
19126
diff
changeset
|
141 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
142 ## Test input validation |
28886
d8318c12d903
test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
143 %!error <Invalid call> rot90 () |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
144 %!error rot90 (1, ones (2)) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
145 %!error rot90 (1, 1.5) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
146 %!error rot90 (1, 1+i) |