Mercurial > hg > octave-nkf
annotate scripts/general/circshift.m @ 18126:d76f790b4eec gui-release
enable do_braindead_shortcircuit_evaluation by default and deprecate
* octave.cc (maximum_braindamage): Don't call
Fdo_brainded_shortcircuit_evaluation.
* pt-exp.h (tree_expression::mark_braindead_shortcircuit): Eliminate
file name argument.
* pt-binop.h, pt-binop.cc
(tree_binary_expression::mark_braindead_shortcircuit): Likewise.
* oct-parse.in.yy (if_cmd_list1, elseif_clause, loop_command):
Eliminate argument from call to mark_braindead_shortcircuit.
* pt-binop.h, pt-binop.cc (Vdo_braindead_shortcircuit_evaluation):
Initialize to true.
(tree_binary_expression::matlab_style_short_circuit_warning): New function.
(tree_binary_expression::rvalue1): Call
matlab_style_short_circuit_warning if short circuit evaluation occurs.
(Fdo_braindead_shortcircuit_evaluation): Display deprecated warning.
Delete tests for do_braindead_shortcircuit_evaluation.
(tree_binary_expression::braindead_shortcircuit_warning_issued): New
member variable.
* NEWS: Mention change in default value and deprecated function.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 11 Dec 2013 20:51:22 -0500 |
parents | d63878346099 |
children | 4197fc428c7d |
rev | line source |
---|---|
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
14363
diff
changeset
|
1 ## Copyright (C) 2004-2013 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}) |
12575
d0b799dafede
Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents:
12351
diff
changeset
|
21 ## Circularly shift the values of the array @var{x}. @var{n} must be |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
22 ## a vector of integers no longer than the number of dimensions in |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
23 ## @var{x}. The values of @var{n} can be either positive or negative, |
4894 | 24 ## which determines the direction in which the values or @var{x} are |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
25 ## 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
|
26 ## dimension of @var{x} will not be shifted. For example: |
4894 | 27 ## |
28 ## @example | |
29 ## @group | |
6671 | 30 ## x = [1, 2, 3; 4, 5, 6; 7, 8, 9]; |
4894 | 31 ## circshift (x, 1) |
32 ## @result{} 7, 8, 9 | |
33 ## 1, 2, 3 | |
34 ## 4, 5, 6 | |
35 ## circshift (x, -2) | |
36 ## @result{} 7, 8, 9 | |
37 ## 1, 2, 3 | |
38 ## 4, 5, 6 | |
39 ## circshift (x, [0,1]) | |
40 ## @result{} 3, 1, 2 | |
41 ## 6, 4, 5 | |
42 ## 9, 7, 8 | |
43 ## @end group | |
44 ## @end example | |
5642 | 45 ## @seealso {permute, ipermute, shiftdim} |
4894 | 46 ## @end deftypefn |
47 | |
4895 | 48 function y = circshift (x, n) |
4894 | 49 |
13277
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
50 if (nargin != 2) |
6046 | 51 print_usage (); |
4894 | 52 endif |
13277
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
53 |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
54 if (isempty (x)) |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
55 y = x; |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
56 return; |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
57 endif |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
58 |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
59 nd = ndims (x); |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
60 sz = size (x); |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
61 |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
62 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
|
63 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
|
64 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
|
65 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
|
66 endif |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
67 |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
68 idx = repmat ({':'}, 1, nd); |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
69 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
|
70 b = n(i); |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
71 d = sz(i); |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
72 if (b > 0) |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
73 b = rem (b, d); |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
74 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
|
75 elseif (b < 0) |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
76 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
|
77 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
|
78 endif |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
79 endfor |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
80 |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
81 y = x(idx{:}); |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
82 |
4894 | 83 endfunction |
7614
52f90c7adac6
Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
84 |
13277
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
85 |
7614
52f90c7adac6
Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
86 %!shared x |
52f90c7adac6
Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
87 %! 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
|
88 |
52f90c7adac6
Avoid infinite loop in circshift for infinite matrices
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
89 %!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
|
90 %!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
|
91 %!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
|
92 %!assert (circshift ([], 1), []) |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
93 |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
94 %!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
|
95 %!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
|
96 |
13277
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
97 %% Test input validation |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
98 %!error circshift () |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
99 %!error circshift (1) |
9f2e568123ea
circshift.m: Recode to do away with some for loops.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
100 %!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
|
101 %!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
|
102 %!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
|
103 %!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
|
104 |