annotate scripts/general/shift.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: 26376
diff changeset
2 ##
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
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: 26376
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: 26376
diff changeset
5 ##
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3369
diff changeset
6 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
7 ## This file is part of Octave.
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
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: 3457
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: 3369
diff changeset
13 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
14 ## Octave is distributed in the hope that it will be useful, but
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 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: 3369
diff changeset
18 ##
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 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: 6046
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/>.
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
22
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3263
diff changeset
23 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20158
diff changeset
24 ## @deftypefn {} {} shift (@var{x}, @var{b})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20158
diff changeset
25 ## @deftypefnx {} {} shift (@var{x}, @var{b}, @var{dim})
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3263
diff changeset
26 ## If @var{x} is a vector, perform a circular shift of length @var{b} of
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3263
diff changeset
27 ## the elements of @var{x}.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3369
diff changeset
28 ##
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3263
diff changeset
29 ## If @var{x} is a matrix, do the same for each column of @var{x}.
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
30 ##
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
31 ## If the optional @var{dim} argument is given, operate along this dimension.
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3263
diff changeset
32 ## @end deftypefn
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
33
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
34 ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at>
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
35 ## Created: 14 September 1994
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
36 ## Adapted-By: jwe
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
37
4862
a0997c4d1d54 [project @ 2004-04-15 21:14:43 by jwe]
jwe
parents: 4460
diff changeset
38 function y = shift (x, b, dim)
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
39
4862
a0997c4d1d54 [project @ 2004-04-15 21:14:43 by jwe]
jwe
parents: 4460
diff changeset
40 if (nargin != 2 && nargin != 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5568
diff changeset
41 print_usage ();
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
42 endif
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
43
13278
04edb15d7966 shift.m: Clean up code and add more tests
Rik <octave@nomad.inbox5.com>
parents: 12676
diff changeset
44 if (numel (x) < 1)
04edb15d7966 shift.m: Clean up code and add more tests
Rik <octave@nomad.inbox5.com>
parents: 12676
diff changeset
45 error ("shift: X must not be empty");
04edb15d7966 shift.m: Clean up code and add more tests
Rik <octave@nomad.inbox5.com>
parents: 12676
diff changeset
46 elseif (! (isscalar (b) && b == fix (b)))
10690
35adf2a71f3f Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
47 error ("shift: B must be an integer");
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
48 endif
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
49
4862
a0997c4d1d54 [project @ 2004-04-15 21:14:43 by jwe]
jwe
parents: 4460
diff changeset
50 nd = ndims (x);
a0997c4d1d54 [project @ 2004-04-15 21:14:43 by jwe]
jwe
parents: 4460
diff changeset
51 sz = size (x);
a0997c4d1d54 [project @ 2004-04-15 21:14:43 by jwe]
jwe
parents: 4460
diff changeset
52
a0997c4d1d54 [project @ 2004-04-15 21:14:43 by jwe]
jwe
parents: 4460
diff changeset
53 if (nargin == 3)
13278
04edb15d7966 shift.m: Clean up code and add more tests
Rik <octave@nomad.inbox5.com>
parents: 12676
diff changeset
54 if (!(isscalar (dim) && dim == fix (dim))
11149
fe3c3dfc07eb style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents: 10690
diff changeset
55 || !(1 <= dim && dim <= nd))
10690
35adf2a71f3f Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
56 error ("shift: DIM must be an integer and a valid dimension");
4862
a0997c4d1d54 [project @ 2004-04-15 21:14:43 by jwe]
jwe
parents: 4460
diff changeset
57 endif
a0997c4d1d54 [project @ 2004-04-15 21:14:43 by jwe]
jwe
parents: 4460
diff changeset
58 else
10690
35adf2a71f3f Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
59 ## Find the first non-singleton dimension.
12674
9493880928c8 Use common idiom in m-files for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
60 (dim = find (sz > 1, 1)) || (dim = 1);
4862
a0997c4d1d54 [project @ 2004-04-15 21:14:43 by jwe]
jwe
parents: 4460
diff changeset
61 endif
a0997c4d1d54 [project @ 2004-04-15 21:14:43 by jwe]
jwe
parents: 4460
diff changeset
62
13278
04edb15d7966 shift.m: Clean up code and add more tests
Rik <octave@nomad.inbox5.com>
parents: 12676
diff changeset
63 d = sz(dim);
4862
a0997c4d1d54 [project @ 2004-04-15 21:14:43 by jwe]
jwe
parents: 4460
diff changeset
64
12676
2783fa95cab7 Use common code idiom for creating cell array for indexing ND-arrays
Rik <octave@nomad.inbox5.com>
parents: 12674
diff changeset
65 idx = repmat ({':'}, nd, 1);
13278
04edb15d7966 shift.m: Clean up code and add more tests
Rik <octave@nomad.inbox5.com>
parents: 12676
diff changeset
66 if (b > 0)
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
67 b = rem (b, d);
7208
a730e47fda4d [project @ 2007-11-28 02:32:41 by jwe]
jwe
parents: 7017
diff changeset
68 idx{dim} = [d-b+1:d, 1:d-b];
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
69 elseif (b < 0)
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
70 b = rem (abs (b), d);
7208
a730e47fda4d [project @ 2007-11-28 02:32:41 by jwe]
jwe
parents: 7017
diff changeset
71 idx{dim} = [b+1:d, 1:b];
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
72 endif
13278
04edb15d7966 shift.m: Clean up code and add more tests
Rik <octave@nomad.inbox5.com>
parents: 12676
diff changeset
73
7208
a730e47fda4d [project @ 2007-11-28 02:32:41 by jwe]
jwe
parents: 7017
diff changeset
74 y = x(idx{:});
4369
3192d1c40195 [project @ 2003-03-18 09:13:46 by jwe]
jwe
parents: 4030
diff changeset
75
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
76 endfunction
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
77
13278
04edb15d7966 shift.m: Clean up code and add more tests
Rik <octave@nomad.inbox5.com>
parents: 12676
diff changeset
78
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
79 %!test
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
80 %! a = [1, 2, 3];
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
81 %! b = [4, 5, 6];
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
82 %! c = [7, 8, 9];
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
83 %!
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
84 %! r = [a, b, c];
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
85 %! m = [a; b; c];
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
86 %!
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
87 %! assert (shift (r, 0), r);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
88 %! assert (shift (r, 3), [c, a, b]);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
89 %! assert (shift (r, -6), [c, a, b]);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
90 %! assert (shift (r, -3), [b, c, a]);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
91 %! assert (shift (m, 1), [c; a; b]);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
92 %! assert (shift (m, -2), [c; a; b]);
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
93
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
94 ## Test input validation
13278
04edb15d7966 shift.m: Clean up code and add more tests
Rik <octave@nomad.inbox5.com>
parents: 12676
diff changeset
95 %!error shift ()
04edb15d7966 shift.m: Clean up code and add more tests
Rik <octave@nomad.inbox5.com>
parents: 12676
diff changeset
96 %!error shift (1, 2, 3, 4)
04edb15d7966 shift.m: Clean up code and add more tests
Rik <octave@nomad.inbox5.com>
parents: 12676
diff changeset
97 %!error shift ([], 1)
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
98 %!error shift (ones (2), ones (2))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
99 %!error shift (ones (2), 1.5)
13296
27da11c63d98 shift.m: Add validation tests for dimension argument
Rik <octave@nomad.inbox5.com>
parents: 13292
diff changeset
100 %!error shift (1, 1, 1.5)
27da11c63d98 shift.m: Add validation tests for dimension argument
Rik <octave@nomad.inbox5.com>
parents: 13292
diff changeset
101 %!error shift (1, 1, 0)
27da11c63d98 shift.m: Add validation tests for dimension argument
Rik <octave@nomad.inbox5.com>
parents: 13292
diff changeset
102 %!error shift (1, 1, 3)