Mercurial > octave
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 |
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 | 7 ## |
3922 | 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 |
3922 | 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 | 14 ## |
3922 | 15 ## Octave is distributed in the hope that it will be useful, but |
2539 | 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 | 19 ## |
2539 | 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 ######################################################################## |
2539 | 25 |
3369 | 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 | 29 ## If @var{x} is a vector, perform a circular shift of length @var{b} of |
30 ## the elements of @var{x}. | |
3426 | 31 ## |
3369 | 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 | 35 ## @end deftypefn |
2539 | 36 |
4862 | 37 function y = shift (x, b, dim) |
2539 | 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 | 40 print_usage (); |
2539 | 41 endif |
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 | 47 endif |
48 | |
4862 | 49 nd = ndims (x); |
50 sz = size (x); | |
51 | |
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 | 56 endif |
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 | 60 endif |
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 | 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 | 66 b = rem (b, d); |
7208 | 67 idx{dim} = [d-b+1:d, 1:d-b]; |
5568 | 68 elseif (b < 0) |
69 b = rem (abs (b), d); | |
7208 | 70 idx{dim} = [b+1:d, 1:b]; |
5568 | 71 endif |
13278
04edb15d7966
shift.m: Clean up code and add more tests
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
72 |
7208 | 73 y = x(idx{:}); |
4369 | 74 |
7411 | 75 endfunction |
2539 | 76 |
13278
04edb15d7966
shift.m: Clean up code and add more tests
Rik <octave@nomad.inbox5.com>
parents:
12676
diff
changeset
|
77 |
7411 | 78 %!test |
79 %! a = [1, 2, 3]; | |
80 %! b = [4, 5, 6]; | |
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 | 83 %! r = [a, b, c]; |
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 | 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) |