Mercurial > hg > octave-nkf
comparison scripts/general/shift.m @ 13278:04edb15d7966
shift.m: Clean up code and add more tests
* shift.m: Shift input validation to front of function. Add more
tests for input validation. Tweak algorithm to avoid one unecessary
calculation when the shift is 0 and add test for that.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 05 Oct 2011 12:00:31 -0700 |
parents | 2783fa95cab7 |
children | c479e3d0311f |
comparison
equal
deleted
inserted
replaced
13277:9f2e568123ea | 13278:04edb15d7966 |
---|---|
35 | 35 |
36 if (nargin != 2 && nargin != 3) | 36 if (nargin != 2 && nargin != 3) |
37 print_usage (); | 37 print_usage (); |
38 endif | 38 endif |
39 | 39 |
40 if (! (isscalar (b) && b == round (b))) | 40 if (numel (x) < 1) |
41 error ("shift: X must not be empty"); | |
42 elseif (! (isscalar (b) && b == fix (b))) | |
41 error ("shift: B must be an integer"); | 43 error ("shift: B must be an integer"); |
42 endif | 44 endif |
43 | 45 |
44 nd = ndims (x); | 46 nd = ndims (x); |
45 sz = size (x); | 47 sz = size (x); |
46 | 48 |
47 if (nargin == 3) | 49 if (nargin == 3) |
48 if (!(isscalar (dim) && dim == round (dim)) | 50 if (!(isscalar (dim) && dim == fix (dim)) |
49 || !(1 <= dim && dim <= nd)) | 51 || !(1 <= dim && dim <= nd)) |
50 error ("shift: DIM must be an integer and a valid dimension"); | 52 error ("shift: DIM must be an integer and a valid dimension"); |
51 endif | 53 endif |
52 else | 54 else |
53 ## Find the first non-singleton dimension. | 55 ## Find the first non-singleton dimension. |
54 (dim = find (sz > 1, 1)) || (dim = 1); | 56 (dim = find (sz > 1, 1)) || (dim = 1); |
55 endif | 57 endif |
56 | 58 |
57 if (numel (x) < 1) | 59 d = sz(dim); |
58 error ("shift: X must not be empty"); | |
59 endif | |
60 | |
61 d = sz (dim); | |
62 | 60 |
63 idx = repmat ({':'}, nd, 1); | 61 idx = repmat ({':'}, nd, 1); |
64 if (b >= 0) | 62 if (b > 0) |
65 b = rem (b, d); | 63 b = rem (b, d); |
66 idx{dim} = [d-b+1:d, 1:d-b]; | 64 idx{dim} = [d-b+1:d, 1:d-b]; |
67 elseif (b < 0) | 65 elseif (b < 0) |
68 b = rem (abs (b), d); | 66 b = rem (abs (b), d); |
69 idx{dim} = [b+1:d, 1:b]; | 67 idx{dim} = [b+1:d, 1:b]; |
70 endif | 68 endif |
69 | |
71 y = x(idx{:}); | 70 y = x(idx{:}); |
72 | 71 |
73 endfunction | 72 endfunction |
73 | |
74 | 74 |
75 %!test | 75 %!test |
76 %! a = [1, 2, 3]; | 76 %! a = [1, 2, 3]; |
77 %! b = [4, 5, 6]; | 77 %! b = [4, 5, 6]; |
78 %! c = [7, 8, 9]; | 78 %! c = [7, 8, 9]; |
79 %! | 79 %! |
80 %! r = [a, b, c]; | 80 %! r = [a, b, c]; |
81 %! m = [a; b; c]; | 81 %! m = [a; b; c]; |
82 %! | 82 %! |
83 %! assert((shift (r, 3) == [c, a, b] | 83 %! assert(shift (r, 0), r); |
84 %! && shift (r, -6) == [c, a, b] | 84 %! assert(shift (r, 3), [c, a, b]); |
85 %! && shift (r, -3) == [b, c, a] | 85 %! assert(shift (r, -6), [c, a, b]); |
86 %! && shift (m, 1) == [c; a; b] | 86 %! assert(shift (r, -3), [b, c, a]); |
87 %! && shift (m, -2) == [c; a; b])); | 87 %! assert(shift (m, 1), [c; a; b]); |
88 %! assert(shift (m, -2), [c; a; b]); | |
88 | 89 |
89 %!error shift (); | 90 %% Test input validation |
91 %!error shift () | |
92 %!error shift (1, 2, 3, 4) | |
93 %!error shift ([], 1) | |
94 %!error shift (ones(2), ones(2)) | |
95 %!error kurtosis (ones(2), 1.5) | |
96 %!error kurtosis (ones(2), 0) | |
97 %!error kurtosis (ones(2), 3) | |
90 | 98 |
91 %!error shift (1, 2, 3, 4); | |
92 |