annotate scripts/general/shift.m @ 31200:075443476dfb stable

nextpow2.m: Add bug number tag to regression BIST tests (bug #62947) * nextpow2.m: Add '<*62947>' to BIST tests for bug #62947.
author Rik <rik@octave.org>
date Wed, 24 Aug 2022 08:55:14 -0700
parents 796f54d4ddbf
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 ##
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
3 ## Copyright (C) 1995-2022 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/>.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3369
diff changeset
7 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
8 ## This file is part of Octave.
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
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
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
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.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3369
diff changeset
14 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3457
diff changeset
15 ## 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
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.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3369
diff changeset
19 ##
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
20 ## 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
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 ########################################################################
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
25
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3263
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20158
diff changeset
27 ## @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
28 ## @deftypefnx {} {} shift (@var{x}, @var{b}, @var{dim})
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3263
diff changeset
29 ## 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
30 ## the elements of @var{x}.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3369
diff changeset
31 ##
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3263
diff changeset
32 ## 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
33 ##
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
34 ## 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
35 ## @end deftypefn
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
36
4862
a0997c4d1d54 [project @ 2004-04-15 21:14:43 by jwe]
jwe
parents: 4460
diff changeset
37 function y = shift (x, b, dim)
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
38
28789
28de41192f3c Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents: 27985
diff changeset
39 if (nargin < 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5568
diff changeset
40 print_usage ();
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
41 endif
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
42
13278
04edb15d7966 shift.m: Clean up code and add more tests
Rik <octave@nomad.inbox5.com>
parents: 12676
diff changeset
43 if (numel (x) < 1)
04edb15d7966 shift.m: Clean up code and add more tests
Rik <octave@nomad.inbox5.com>
parents: 12676
diff changeset
44 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
45 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
46 error ("shift: B must be an integer");
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
47 endif
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
48
4862
a0997c4d1d54 [project @ 2004-04-15 21:14:43 by jwe]
jwe
parents: 4460
diff changeset
49 nd = ndims (x);
a0997c4d1d54 [project @ 2004-04-15 21:14:43 by jwe]
jwe
parents: 4460
diff changeset
50 sz = size (x);
a0997c4d1d54 [project @ 2004-04-15 21:14:43 by jwe]
jwe
parents: 4460
diff changeset
51
a0997c4d1d54 [project @ 2004-04-15 21:14:43 by jwe]
jwe
parents: 4460
diff changeset
52 if (nargin == 3)
13278
04edb15d7966 shift.m: Clean up code and add more tests
Rik <octave@nomad.inbox5.com>
parents: 12676
diff changeset
53 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
54 || !(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
55 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
56 endif
a0997c4d1d54 [project @ 2004-04-15 21:14:43 by jwe]
jwe
parents: 4460
diff changeset
57 else
10690
35adf2a71f3f Use common code block to find first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
58 ## 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
59 (dim = find (sz > 1, 1)) || (dim = 1);
4862
a0997c4d1d54 [project @ 2004-04-15 21:14:43 by jwe]
jwe
parents: 4460
diff changeset
60 endif
a0997c4d1d54 [project @ 2004-04-15 21:14:43 by jwe]
jwe
parents: 4460
diff changeset
61
13278
04edb15d7966 shift.m: Clean up code and add more tests
Rik <octave@nomad.inbox5.com>
parents: 12676
diff changeset
62 d = sz(dim);
4862
a0997c4d1d54 [project @ 2004-04-15 21:14:43 by jwe]
jwe
parents: 4460
diff changeset
63
12676
2783fa95cab7 Use common code idiom for creating cell array for indexing ND-arrays
Rik <octave@nomad.inbox5.com>
parents: 12674
diff changeset
64 idx = repmat ({':'}, nd, 1);
13278
04edb15d7966 shift.m: Clean up code and add more tests
Rik <octave@nomad.inbox5.com>
parents: 12676
diff changeset
65 if (b > 0)
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
66 b = rem (b, d);
7208
a730e47fda4d [project @ 2007-11-28 02:32:41 by jwe]
jwe
parents: 7017
diff changeset
67 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
68 elseif (b < 0)
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
69 b = rem (abs (b), d);
7208
a730e47fda4d [project @ 2007-11-28 02:32:41 by jwe]
jwe
parents: 7017
diff changeset
70 idx{dim} = [b+1:d, 1:b];
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
71 endif
13278
04edb15d7966 shift.m: Clean up code and add more tests
Rik <octave@nomad.inbox5.com>
parents: 12676
diff changeset
72
7208
a730e47fda4d [project @ 2007-11-28 02:32:41 by jwe]
jwe
parents: 7017
diff changeset
73 y = x(idx{:});
4369
3192d1c40195 [project @ 2003-03-18 09:13:46 by jwe]
jwe
parents: 4030
diff changeset
74
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
75 endfunction
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
76
13278
04edb15d7966 shift.m: Clean up code and add more tests
Rik <octave@nomad.inbox5.com>
parents: 12676
diff changeset
77
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
78 %!test
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
79 %! a = [1, 2, 3];
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
80 %! b = [4, 5, 6];
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
81 %! c = [7, 8, 9];
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
82 %!
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
83 %! r = [a, b, c];
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
84 %! m = [a; b; c];
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
85 %!
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
86 %! 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
87 %! 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
88 %! 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
89 %! 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
90 %! 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
91 %! assert (shift (m, -2), [c; a; b]);
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7208
diff changeset
92
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
93 ## 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
94 %!error <Invalid call> shift ()
d8318c12d903 test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents: 28789
diff changeset
95 %!error <Invalid call> shift (1)
d8318c12d903 test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents: 28789
diff changeset
96 %!error <X must not be empty> shift ([], 1)
d8318c12d903 test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents: 28789
diff changeset
97 %!error <B must be an integer> shift (ones (2), ones (2))
d8318c12d903 test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents: 28789
diff changeset
98 %!error <B must be an integer> shift (ones (2), 1.5)
d8318c12d903 test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents: 28789
diff changeset
99 %!error <DIM must be an integer> shift (1, 1, 1.5)
d8318c12d903 test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents: 28789
diff changeset
100 %!error <DIM must be .* a valid dimension> shift (1, 1, 0)
d8318c12d903 test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents: 28789
diff changeset
101 %!error <DIM must be .* a valid dimension> shift (1, 1, 3)