changeset 26155:8be616a0b9aa

circshift.m: shift first non-singleton dimension if DIM not provided (bug #53178). * circshift.m: Document that first non-singular dimension is used when DIM is not provided. Add another example of code to docstring, and add comments explaining all four examples. In input validation, detect when DIM is not specified and find the first non-singleton dimension. Add BIST tests for bug #53178.
author Rik <rik@octave.org>
date Sat, 01 Dec 2018 02:34:00 -0800
parents af655eb15cdc
children e2626799445f
files scripts/general/circshift.m
diffstat 1 files changed, 35 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/general/circshift.m	Fri Nov 30 15:53:02 2018 -0800
+++ b/scripts/general/circshift.m	Sat Dec 01 02:34:00 2018 -0800
@@ -25,7 +25,8 @@
 ## in @var{x}.  The values of @var{n} can be either positive or negative, which
 ## determines the direction in which the values of @var{x} are shifted.  If an
 ## element of @var{n} is zero, then the corresponding dimension of @var{x} will
-## not be shifted.
+## not be shifted.  If @var{n} is a scalar and no @var{dim} is specified then
+## the shift is applied to the first non-singular dimension.
 ##
 ## If a scalar @var{dim} is given then operate along the specified dimension.
 ## In this case @var{n} must be a scalar as well.
@@ -33,21 +34,25 @@
 ## Examples:
 ##
 ## @example
-## @group
-## x = [1, 2, 3; 4, 5, 6; 7, 8, 9];
-## circshift (x, 1)
-## @result{}  7, 8, 9
-##     1, 2, 3
-##     4, 5, 6
-## circshift (x, -2)
-## @result{}  7, 8, 9
-##     1, 2, 3
-##     4, 5, 6
-## circshift (x, [0,1])
-## @result{}  3, 1, 2
-##     6, 4, 5
-##     9, 7, 8
-## @end group
+## x = [1, 2, 3;
+##      4, 5, 6;
+##      7, 8, 9];
+## circshift (x, 1)      # positive shift on rows (1st non-singular dim)
+##  @result{}  7, 8, 9
+##      1, 2, 3
+##      4, 5, 6
+## circshift (x, -2)     # negative shift on rows (1st non-singular dim)
+##  @result{}  7, 8, 9
+##      1, 2, 3
+##      4, 5, 6
+## circshift (x, [0,1])  # no shift of rows, shift columns by 1 (2nd dimension)
+##  @result{}  3, 1, 2
+##      6, 4, 5
+##      9, 7, 8
+## circshift (x, 1, 2)   # shift columns (2nd dimension)
+##  @result{}  3, 1, 2
+##      6, 4, 5
+##      9, 7, 8
 ## @end example
 ## @seealso{permute, ipermute, shiftdim}
 ## @end deftypefn
@@ -63,16 +68,22 @@
     return;
   endif
 
-  if (nargin == 3)
-    if (! isscalar (n))
+  nd = ndims (x);
+  sz = size (x);
+
+  if (nargin == 2)
+    if (isscalar (n))
+      ## Find the first non-singleton dimension.
+      (dim = find (sz > 1, 1)) || (dim = 1);
+      n = [zeros(1, dim-1), n];
+    endif
+  elseif (nargin == 3)
+    if ( ! isscalar (n))
       error ("circshift: N must be a scalar when DIM is also specified");
     endif
     n = [zeros(1, dim-1), n];
   endif
 
-  nd = ndims (x);
-  sz = size (x);
-
   if (! isvector (n) || length (n) > nd)
     error ("circshift: N must be a vector, no longer than the number of dimensions in X");
   elseif (any (n != fix (n)))
@@ -111,6 +122,9 @@
 %!assert (circshift (x, -2, 1), [7, 8, 9; 1, 2, 3; 4, 5, 6])
 %!assert (circshift (x, 1, 2), [3, 1, 2; 6, 4, 5; 9, 7, 8])
 
+%!test <*53178> assert (circshift (1:4, 1), [4 1 2 3])
+%!test <*53178> assert (circshift (1:4, 1, 1), 1:4)
+
 ## Test input validation
 %!error circshift ()
 %!error circshift (1)