annotate scripts/general/rot90.m @ 12862:a9d292ce5489

rot90.m: Put input validation first. Update tests to include input validation. * rot90.m: Move input validation to front of function. Use defaults in function call.
author Rik <octave@nomad.inbox5.com>
date Sun, 17 Jul 2011 18:14:41 -0700
parents 4d777e05d47c
children 5c22edebf2e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11523
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 11472
diff changeset
1 ## Copyright (C) 1993-2011 John W. Eaton
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
2 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
3 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
4 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
6 ## 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: 6046
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
8 ## your option) any later version.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
9 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
13 ## General Public License for more details.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
14 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
15 ## 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: 6046
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
17 ## <http://www.gnu.org/licenses/>.
245
16a24e76d6e0 [project @ 1993-12-03 02:00:15 by jwe]
jwe
parents: 4
diff changeset
18
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3238
diff changeset
19 ## -*- texinfo -*-
12639
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
20 ## @deftypefn {Function File} {} rot90 (@var{A})
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
21 ## @deftypefnx {Function File} {} rot90 (@var{A}, @var{k})
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 8920
diff changeset
22 ## Return a copy of @var{A} with the elements rotated counterclockwise in
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3238
diff changeset
23 ## 90-degree increments. The second argument is optional, and specifies
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3238
diff changeset
24 ## how many 90-degree rotations are to be applied (the default value is 1).
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 8920
diff changeset
25 ## Negative values of @var{k} rotate the matrix in a clockwise direction.
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3238
diff changeset
26 ## For example,
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3408
diff changeset
27 ##
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3238
diff changeset
28 ## @example
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3238
diff changeset
29 ## @group
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3238
diff changeset
30 ## rot90 ([1, 2; 3, 4], -1)
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3238
diff changeset
31 ## @result{} 3 1
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3238
diff changeset
32 ## 4 2
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3238
diff changeset
33 ## @end group
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3238
diff changeset
34 ## @end example
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3408
diff changeset
35 ##
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3238
diff changeset
36 ## @noindent
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3238
diff changeset
37 ## rotates the given matrix clockwise by 90 degrees. The following are all
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3238
diff changeset
38 ## equivalent statements:
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3408
diff changeset
39 ##
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3238
diff changeset
40 ## @example
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3238
diff changeset
41 ## @group
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3238
diff changeset
42 ## rot90 ([1, 2; 3, 4], -1)
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3238
diff changeset
43 ## rot90 ([1, 2; 3, 4], 3)
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3238
diff changeset
44 ## rot90 ([1, 2; 3, 4], 7)
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3238
diff changeset
45 ## @end group
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3238
diff changeset
46 ## @end example
4869
b92d59213e63 [project @ 2004-04-21 17:03:02 by jwe]
jwe
parents: 3456
diff changeset
47 ##
12639
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
48 ## Note that @code{rot90} only works with 2-D arrays. To rotate N-D arrays
4869
b92d59213e63 [project @ 2004-04-21 17:03:02 by jwe]
jwe
parents: 3456
diff changeset
49 ## use @code{rotdim} instead.
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5488
diff changeset
50 ## @seealso{rotdim, flipud, fliplr, flipdim}
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3238
diff changeset
51 ## @end deftypefn
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
52
2314
949ab8eba8bc [project @ 1996-07-12 03:58:02 by jwe]
jwe
parents: 2313
diff changeset
53 ## Author: jwe
949ab8eba8bc [project @ 1996-07-12 03:58:02 by jwe]
jwe
parents: 2313
diff changeset
54
12862
a9d292ce5489 rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
55 function B = rot90 (A, k = 1)
4869
b92d59213e63 [project @ 2004-04-21 17:03:02 by jwe]
jwe
parents: 3456
diff changeset
56
12862
a9d292ce5489 rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
57 if (nargin < 1 || nargin > 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
58 print_usage ();
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
59 endif
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
60
12862
a9d292ce5489 rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
61 if (ndims (A) > 2)
a9d292ce5489 rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
62 error ("rot90: A must be a 2-D array");
a9d292ce5489 rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
63 endif
a9d292ce5489 rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
64
a9d292ce5489 rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
65 if (! (isscalar (k) && isreal (k) && fix (k) == k))
a9d292ce5489 rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
66 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
67 endif
a9d292ce5489 rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
68
a9d292ce5489 rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
69 k = rem (k, 4);
a9d292ce5489 rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
70
a9d292ce5489 rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
71 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
72 k = k + 4;
a9d292ce5489 rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
73 endif
a9d292ce5489 rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
74
a9d292ce5489 rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
75 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
76 B = A;
a9d292ce5489 rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
77 elseif (k == 1)
a9d292ce5489 rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
78 B = flipud (A.');
a9d292ce5489 rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
79 elseif (k == 2)
a9d292ce5489 rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
80 B = flipud (fliplr (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 == 3)
a9d292ce5489 rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
82 B = (flipud (A)).';
a9d292ce5489 rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
83 else
a9d292ce5489 rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
84 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
85 endif
a9d292ce5489 rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
86
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
87 endfunction
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
88
12862
a9d292ce5489 rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
89
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
90 %!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
91 %! 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
92 %! 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
93 %! 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
94 %! 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
95 %!
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 8920
diff changeset
96 %! assert(rot90 (x1) == x2);
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 8920
diff changeset
97 %! assert(rot90 (x1, 2) == x3);
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 8920
diff changeset
98 %! assert(rot90 (x1, 3) == x4);
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 8920
diff changeset
99 %! assert(rot90 (x1, 4) == x1);
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 8920
diff changeset
100 %! assert(rot90 (x1, 5) == x2);
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 8920
diff changeset
101 %! assert(rot90 (x1, -1) == x4);
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
102
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 8920
diff changeset
103 %% Test input validation
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
104 %!error rot90 ();
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
105 %!error rot90 (1, 2, 3);
12862
a9d292ce5489 rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
106 %!error rot90 (1, ones(2));
a9d292ce5489 rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
107 %!error rot90 (1, 1.5);
a9d292ce5489 rot90.m: Put input validation first. Update tests to include input validation.
Rik <octave@nomad.inbox5.com>
parents: 12639
diff changeset
108 %!error rot90 (1, 1+i);