changeset 23550:613934236291

deconv.m: Overhaul function. * deconv.m: Rewrite docstring to have both calling forms of function and to use @var macro. Use variable names in error() statement. Use transpose operator built in to interpreter rather than permute function for performance. Only calculate second output argument if required. Add more input validation BIST tests.
author Rik <rik@octave.org>
date Tue, 06 Jun 2017 15:39:42 -0700
parents c8daadf7543b
children 500505e9279a
files scripts/polynomial/deconv.m
diffstat 1 files changed, 30 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/polynomial/deconv.m	Tue Jun 06 15:11:33 2017 -0700
+++ b/scripts/polynomial/deconv.m	Tue Jun 06 15:39:42 2017 -0700
@@ -17,11 +17,12 @@
 ## <http://www.gnu.org/licenses/>.
 
 ## -*- texinfo -*-
-## @deftypefn {} {} deconv (@var{y}, @var{a})
-## Deconvolve two vectors.
+## @deftypefn  {} {@var{b} =} deconv (@var{y}, @var{a})
+## @deftypefnx {} {[@var{b}, @var{r}] =} deconv (@var{y}, @var{a})
+## Deconvolve two vectors (polynomial division).
 ##
-## @code{[b, r] = deconv (y, a)} solves for @var{b} and @var{r} such that
-## @code{y = conv (a, b) + r}.
+## @code{[@var{b}, @var{r}] = deconv (@var{y}, @var{a})} solves for @var{b} and
+## @var{r} such that @code{@var{y} = conv (@var{a}, @var{b}) + @var{r}}.
 ##
 ## If @var{y} and @var{a} are polynomial coefficient vectors, @var{b} will
 ## contain the coefficients of the polynomial quotient and @var{r} will be
@@ -40,7 +41,12 @@
   endif
 
   if (! (isvector (y) && isvector (a)))
-    error ("deconv: both arguments must be vectors");
+    error ("deconv: Y and A must be vectors");
+  endif
+
+  ## Ensure A is oriented as Y.
+  if ((isrow (y) && iscolumn (a)) || (iscolumn (y) && isrow (a))) 
+    a = a.';
   endif
 
   la = length (a);
@@ -48,11 +54,6 @@
 
   lb = ly - la + 1;
 
-  ## Ensure A is oriented as Y.
-  if (diff (size (y)(1:2)) * diff (size (a)(1:2)) < 0)
-    a = permute (a, [2, 1]);
-  endif
-
   if (ly > la)
     x = zeros (size (y) - size (a) + 1);
     x(1) = 1;
@@ -63,19 +64,21 @@
     b = 0;
   endif
 
-  lc = la + length (b) - 1;
-  if (ly == lc)
-    r = y - conv (a, b);
-  else
-    ## Respect the orientation of Y"
-    if (rows (y) <= columns (y))
-      r = [(zeros (1, lc - ly)), y] - conv (a, b);
+  if (isargout (2))
+    lc = la + length (b) - 1;
+    if (ly == lc)
+      r = y - conv (a, b);
     else
-      r = [(zeros (lc - ly, 1)); y] - conv (a, b);
-    endif
-    if (ly < la)
-      ## Trim the remainder is equal to the length of Y.
-      r = r(end-(length(y)-1):end);
+      ## Respect the orientation of Y.
+      if (rows (y) <= columns (y))
+        r = [(zeros (1, lc - ly)), y] - conv (a, b);
+      else
+        r = [(zeros (lc - ly, 1)); y] - conv (a, b);
+      endif
+      if (ly < la)
+        ## Trim the remainder to be the length of Y.
+        r = r(end-(length(y)-1):end);
+      endif
     endif
   endif
 
@@ -109,5 +112,8 @@
 
 %!assert (deconv ((1:3)',[1, 1]), [1; 1])
 
-%!error [b, r] = deconv ([3, 6], [1, 2; 3, 4])
-%!error [b, r] = deconv ([3, 6; 1, 2], [1, 2, 3])
+## Test input validation
+%!error deconv (1)
+%!error deconv (1,2,3)
+%!error <Y .* must be vector> deconv ([3, 6], [1, 2; 3, 4])
+%!error <A must be vector> deconv ([3, 6], [1, 2; 3, 4])