Mercurial > hg > octave-nkf
annotate scripts/general/circshift.m @ 20798:128414587af2
don't print additional error message in argument list evaluation
* pt-arg-list.cc (tree_argument_list::convert_to_const_vector):
Don't call error for for failed argument evaluation.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 09 Oct 2015 16:52:49 -0400 |
parents | 7503499a252b |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
1 ## Copyright (C) 2004-2015 David Bateman |
4894 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
4894 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
4894 | 18 |
19 ## -*- texinfo -*- | |
6547 | 20 ## @deftypefn {Function File} {@var{y} =} circshift (@var{x}, @var{n}) |
20368
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
21 ## Circularly shift the values of the array @var{x}. |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
22 ## |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
23 ## @var{n} must be a vector of integers no longer than the number of |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
24 ## dimensions in @var{x}. The values of @var{n} can be either positive or |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
25 ## negative, which determines the direction in which the values or @var{x} |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
26 ## are shifted. If an element of @var{n} is zero, then the corresponding |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
27 ## dimension of @var{x} will not be shifted. For example: |
4894 | 28 ## |
29 ## @example | |
30 ## @group | |
6671 | 31 ## x = [1, 2, 3; 4, 5, 6; 7, 8, 9]; |
4894 | 32 ## circshift (x, 1) |
33 ## @result{} 7, 8, 9 | |
34 ## 1, 2, 3 | |
35 ## 4, 5, 6 | |
36 ## circshift (x, -2) | |
37 ## @result{} 7, 8, 9 | |
38 ## 1, 2, 3 | |
39 ## 4, 5, 6 | |
40 ## circshift (x, [0,1]) | |
41 ## @result{} 3, 1, 2 | |
42 ## 6, 4, 5 | |
43 ## 9, 7, 8 | |
44 ## @end group | |
45 ## @end example | |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
46 ## @seealso{permute, ipermute, shiftdim} |
4894 | 47 ## @end deftypefn |
48 | |
4895 | 49 function y = circshift (x, n) |
4894 | 50 |
13277
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
51 if (nargin != 2) |
6046 | 52 print_usage (); |
4894 | 53 endif |
13277
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
54 |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
55 if (isempty (x)) |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
56 y = x; |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
57 return; |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
58 endif |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
59 |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
60 nd = ndims (x); |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
61 sz = size (x); |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
62 |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
63 if (! isvector (n) || length (n) > nd) |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
64 error ("circshift: N must be a vector, no longer than the number of dimension in X"); |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
65 elseif (any (n != fix (n))) |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
66 error ("circshift: all values of N must be integers"); |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
67 endif |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
68 |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
69 idx = repmat ({':'}, 1, nd); |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
70 for i = 1:length (n); |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
71 b = n(i); |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
72 d = sz(i); |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
73 if (b > 0) |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
74 b = rem (b, d); |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
75 idx{i} = [d-b+1:d, 1:d-b]; |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
76 elseif (b < 0) |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
77 b = rem (abs (b), d); |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
78 idx{i} = [b+1:d, 1:b]; |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
79 endif |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
80 endfor |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
81 |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
82 y = x(idx{:}); |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
83 |
4894 | 84 endfunction |
7614
52f90c7adac6
Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
85 |
13277
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
86 |
7614
52f90c7adac6
Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
87 %!shared x |
52f90c7adac6
Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
88 %! x = [1, 2, 3; 4, 5, 6; 7, 8, 9]; |
52f90c7adac6
Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
89 |
52f90c7adac6
Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
90 %!assert (circshift (x, 1), [7, 8, 9; 1, 2, 3; 4, 5, 6]) |
52f90c7adac6
Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
91 %!assert (circshift (x, -2), [7, 8, 9; 1, 2, 3; 4, 5, 6]) |
13277
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
92 %!assert (circshift (x, [0, 1]), [3, 1, 2; 6, 4, 5; 9, 7, 8]) |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
93 %!assert (circshift ([], 1), []) |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
94 |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
95 %!assert (circshift (eye (3), 1), circshift (eye (3), 1)) |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
96 %!assert (circshift (eye (3), 1), [0,0,1;1,0,0;0,1,0]) |
12351
ca1190196d26
PermMatrix.cc (operator*): fix mixed row/column case
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
97 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
98 ## Test input validation |
13277
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
99 %!error circshift () |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
100 %!error circshift (1) |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
101 %!error circshift (1,2,3) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
102 %!error circshift (1, ones (2,2)) |
13277
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
103 %!error circshift (1, [1 2 3]) |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
104 %!error circshift (1, 1.5) |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
105 |