changeset 10690:35adf2a71f3f

Use common code block to find first non-singleton dimension.
author Rik <octave@nomad.inbox5.com>
date Tue, 08 Jun 2010 22:09:25 -0700
parents 6622772a0add
children e0ba186b242b
files scripts/ChangeLog scripts/general/cumtrapz.m scripts/general/postpad.m scripts/general/prepad.m scripts/general/shift.m scripts/general/trapz.m scripts/signal/unwrap.m
diffstat 7 files changed, 65 insertions(+), 78 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Tue Jun 08 21:59:16 2010 -0700
+++ b/scripts/ChangeLog	Tue Jun 08 22:09:25 2010 -0700
@@ -1,3 +1,8 @@
+2010-06-03  Rik <octave@nomad.inbox5.com>
+
+        * general/cumtrapz.m, general/postpad.m, general/prepad.m, 
+        general/shift.m, general/trapz.m, signal/unwrap.m: Use common 
+        method to find first non-singleton dimension.
 2010-06-03  Rik <octave@nomad.inbox5.com>
 
         * general/rotdim.m: Modify function to use same variable names
--- a/scripts/general/cumtrapz.m	Tue Jun 08 21:59:16 2010 -0700
+++ b/scripts/general/cumtrapz.m	Tue Jun 08 22:09:25 2010 -0700
@@ -17,14 +17,14 @@
 ## <http://www.gnu.org/licenses/>.
 
 ## -*- texinfo -*-
-## @deftypefn {Function File} {@var{z} =} cumtrapz (@var{y})
+## @deftypefn  {Function File} {@var{z} =} cumtrapz (@var{y})
 ## @deftypefnx {Function File} {@var{z} =} cumtrapz (@var{x}, @var{y})
 ## @deftypefnx {Function File} {@var{z} =} cumtrapz (@dots{}, @var{dim})
 ## 
 ## Cumulative numerical integration using trapezoidal method.
 ## @code{cumtrapz (@var{y})} computes the cumulative integral of the 
 ## @var{y} along the first non-singleton dimension.  If the argument 
-## @var{x} is omitted a equally spaced vector is assumed.  @code{cumtrapz 
+## @var{x} is omitted an equally spaced vector is assumed.  @code{cumtrapz 
 ## (@var{x}, @var{y})} evaluates the cumulative integral with respect 
 ## to @var{x}.
 ##  
@@ -50,8 +50,7 @@
   if (nargin == 3)
     have_x = true;
     have_dim = true;
-  endif
-  if (nargin == 2)
+  elseif (nargin == 2)
     if (! size_equal (x, y) && isscalar (y))
       dim = y;
       have_dim = true;
@@ -61,19 +60,15 @@
   endif
 
   if (! have_dim)
-    ## Find the first singleton dimension.
-    dim = 0;
-    while (dim < nd && sz(dim+1) == 1)
-      dim++;
-    endwhile
-    dim++;
-    if (dim > nd)
+    ## Find the first non-singleton dimension.
+    dim = find (sz > 1, 1);
+    if (isempty (dim))
       dim = 1;
     endif
   else
     dim = floor (dim);
-    if (dim < 1 || dim > nd)
-      error ("cumtrapz: invalid dimension along which to sort");
+    if (! (isscalar (dim) && 1 <= dim && dim <= nd))
+      error ("cumtrapz: invalid dimension DIM");
     endif
   endif
 
@@ -90,7 +85,7 @@
     z = 0.5 * cumsum (x(idx1{:}) + x(idx2{:}), dim);
   else
     if (! size_equal (x, y))
-      error ("cumtrapz: x and y must have same shape");
+      error ("cumtrapz: X and Y must have the same shape");
     endif
     z = 0.5 * cumsum ((x(idx1{:}) - x(idx2{:})) .* 
                       (y(idx1{:}) + y(idx2{:})), dim);
--- a/scripts/general/postpad.m	Tue Jun 08 21:59:16 2010 -0700
+++ b/scripts/general/postpad.m	Tue Jun 08 22:09:25 2010 -0700
@@ -18,7 +18,7 @@
 ## <http://www.gnu.org/licenses/>.
 
 ## -*- texinfo -*-
-## @deftypefn {Function File} {} postpad (@var{x}, @var{l}, @var{c})
+## @deftypefn  {Function File} {} postpad (@var{x}, @var{l}, @var{c})
 ## @deftypefnx {Function File} {} postpad (@var{x}, @var{l}, @var{c}, @var{dim})
 ## @seealso{prepad, resize}
 ## @end deftypefn
@@ -43,18 +43,15 @@
   nd = ndims (x);
   sz = size (x);
   if (nargin < 4)
-    %% Find the first non-singleton dimension
-    dim  = 1;
-    while (dim < nd + 1 && sz (dim) == 1)
-      dim = dim + 1;
-    endwhile
-    if (dim > nd)
+    ## Find the first non-singleton dimension
+    dim = find (sz > 1, 1);
+    if (isempty (dim))
       dim = 1;
     endif
   else
-    if (! (isscalar (dim) && dim == round (dim)) && dim > 0 && 
-        dim < (nd + 1))
-      error ("postpad: dim must be an integer and valid dimension");
+    if (!(isscalar (dim) && dim == fix (dim)) || 
+        !(1 <= dim && dim <= nd))
+      error ("postpad: DIM must be an integer and a valid dimension");
     endif
   endif
 
--- a/scripts/general/prepad.m	Tue Jun 08 21:59:16 2010 -0700
+++ b/scripts/general/prepad.m	Tue Jun 08 22:09:25 2010 -0700
@@ -54,18 +54,15 @@
   nd = ndims (x);
   sz = size (x);
   if (nargin < 4)
-    %% Find the first non-singleton dimension
-    dim  = 1;
-    while (dim < nd + 1 && sz (dim) == 1)
-      dim = dim + 1;
-    endwhile
-    if (dim > nd)
+    ## Find the first non-singleton dimension
+    dim = find (sz > 1, 1);
+    if (isempty (dim))
       dim = 1;
     endif
   else
-    if (! (isscalar (dim) && dim == round (dim)) && dim > 0 && 
-        dim < (nd + 1))
-      error ("prepad: dim must be an integer and valid dimension");
+    if (!(isscalar (dim) && dim == fix (dim)) ||
+        !(1 <= dimm && dim <= nd))
+      error ("prepad: DIM must be an integer and a valid dimension");
     endif
   endif
 
--- a/scripts/general/shift.m	Tue Jun 08 21:59:16 2010 -0700
+++ b/scripts/general/shift.m	Tue Jun 08 22:09:25 2010 -0700
@@ -18,14 +18,14 @@
 ## <http://www.gnu.org/licenses/>.
 
 ## -*- texinfo -*-
-## @deftypefn {Function File} {} shift (@var{x}, @var{b})
+## @deftypefn  {Function File} {} shift (@var{x}, @var{b})
 ## @deftypefnx {Function File} {} shift (@var{x}, @var{b}, @var{dim})
 ## If @var{x} is a vector, perform a circular shift of length @var{b} of
 ## the elements of @var{x}.
 ##
 ## If @var{x} is a matrix, do the same for each column of @var{x}.
 ## If the optional @var{dim} argument is given, operate along this 
-## dimension
+## dimension.
 ## @end deftypefn
 
 ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at>
@@ -39,30 +39,27 @@
   endif
 
   if (! (isscalar (b) && b == round (b)))
-    error ("shift: b must be an integer");
+    error ("shift: B must be an integer");
   endif
 
   nd = ndims (x);
   sz = size (x);
 
   if (nargin == 3)
-    if (! (isscalar (dim) && dim == round (dim)) && dim > 0 && 
-        dim < (nd + 1))
-      error ("shift: dim must be an integer and valid dimension");
+    if (!(isscalar (dim) && dim == round (dim)) || 
+        !(1 <= dim && dim <= nd))
+      error ("shift: DIM must be an integer and a valid dimension");
     endif
   else
-    ## Find the first non-singleton dimension
-    dim  = 1;
-    while (dim < nd + 1 && sz (dim) == 1)
-      dim = dim + 1;
-    endwhile
-    if (dim > nd)
+    ## Find the first non-singleton dimension.
+    dim = find (sz > 1, 1);
+    if (isempty (dim))
       dim = 1;
     endif
   endif
 
   if (numel (x) < 1)
-    error ("shift: x must not be empty");
+    error ("shift: X must not be empty");
   endif
 
   d = sz (dim);
--- a/scripts/general/trapz.m	Tue Jun 08 21:59:16 2010 -0700
+++ b/scripts/general/trapz.m	Tue Jun 08 22:09:25 2010 -0700
@@ -17,7 +17,7 @@
 ## <http://www.gnu.org/licenses/>.
 
 ## -*- texinfo -*-
-## @deftypefn {Function File} {@var{z} =} trapz (@var{y})
+## @deftypefn {Function File}  {@var{z} =} trapz (@var{y})
 ## @deftypefnx {Function File} {@var{z} =} trapz (@var{x}, @var{y})
 ## @deftypefnx {Function File} {@var{z} =} trapz (@dots{}, @var{dim})
 ## 
@@ -37,7 +37,6 @@
 
 function z = trapz (x, y, dim)
         
-
   if (nargin < 1) || (nargin > 3)
     print_usage ();
   endif
@@ -61,19 +60,15 @@
   endif
 
   if (! have_dim)
-    ## Find the first singleton dimension.
-    dim = 0;
-    while (dim < nd && sz(dim+1) == 1)
-      dim++;
-    endwhile
-    dim++;
-    if (dim > nd)
+    ## Find the first non-singleton dimension.
+    dim = find (sz > 1, 1);
+    if (isempty (dim))
       dim = 1;
     endif
   else
     dim = floor (dim);
     if (dim < 1 || dim > nd)
-      error ("trapz: invalid dimension along which to sort");
+      error ("trapz: invalid dimension DIM along which to sort");
     endif
   endif
  
--- a/scripts/signal/unwrap.m	Tue Jun 08 21:59:16 2010 -0700
+++ b/scripts/signal/unwrap.m	Tue Jun 08 22:09:25 2010 -0700
@@ -17,14 +17,15 @@
 ## <http://www.gnu.org/licenses/>.
 
 ## -*- texinfo -*-
-## @deftypefn {Function File} {@var{b} =} unwrap (@var{a}, @var{tol}, @var{dim})
+## @deftypefn  {Function File} {@var{b} =} unwrap (@var{a})
+## @deftypefnx {Function File} {@var{b} =} unwrap (@var{a}, @var{tol})
+## @deftypefnx {Function File} {@var{b} =} unwrap (@var{a}, @var{tol}, @var{dim})
 ## 
 ## Unwrap radian phases by adding multiples of 2*pi as appropriate to
 ## remove jumps greater than @var{tol}.  @var{tol} defaults to pi.
 ##
-## Unwrap will unwrap along the first non-singleton dimension of
-## @var{a}, unless the optional argument @var{dim} is given, in 
-## which case the data will be unwrapped along this dimension
+## Unwrap will work along the the dimension @var{dim}.  If @var{dim}
+## is unspecified it defaults to the first non-singleton dimension.
 ## @end deftypefn
 
 ## Author: Bill Lash <lash@tellabs.com>
@@ -35,23 +36,8 @@
     print_usage ();
   endif
 
-  nd = ndims (a);
-  sz = size (a);
-
-  if (nargin == 3)
-    if (! (isscalar (dim) && dim == round (dim)) && dim > 0 && 
-        dim < (nd + 1))
-      error ("unwrap: dim must be an integer and valid dimension");
-    endif
-  else
-    ## Find the first non-singleton dimension
-    dim  = 1;
-    while (dim < nd + 1 && sz(dim) == 1)
-      dim = dim + 1;
-    endwhile
-    if (dim > nd)
-      dim = 1;
-    endif
+  if (!isnumeric(a))
+    error ("unwrap: A must be a numeric matrix or vector");
   endif
 
   if (nargin < 2 || isempty (tol))
@@ -60,7 +46,22 @@
 
   ## Don't let anyone use a negative value for TOL.
   tol = abs (tol);
-  
+
+  nd = ndims (a);
+  sz = size (a);
+  if (nargin == 3)
+    if (!(isscalar (dim) && dim == fix (dim)) || 
+        !(1 <= dim && dim <= nd))
+      error ("unwrap: DIM must be an integer and a valid dimension");
+    endif
+  else
+    ## Find the first non-singleton dimension
+    dim = find (sz > 1, 1);
+    if (isempty (dim))
+      dim = 1;
+    endif
+  endif
+
   rng = 2*pi;
   m = sz(dim);