Mercurial > hg > octave-lyh
diff scripts/polynomial/polyderiv.m @ 5216:5ed60b8b1ac4
[project @ 2005-03-16 19:51:39 by jwe]
author | jwe |
---|---|
date | Wed, 16 Mar 2005 19:51:46 +0000 |
parents | 8eaef366ab43 |
children | e88886a6934d |
line wrap: on
line diff
--- a/scripts/polynomial/polyderiv.m +++ b/scripts/polynomial/polyderiv.m @@ -19,39 +19,77 @@ ## -*- texinfo -*- ## @deftypefn {Function File} {} polyderiv (@var{c}) +## @deftypefnx {Function File} {[@var{q}] =} polyder (@var{b}, @var{a}) +## @deftypefnx {Function File} {[@var{q}, @var{r}] =} polyder (@var{b}, @var{a}) ## Return the coefficients of the derivative of the polynomial whose -## coefficients are given by vector @var{c}. +## coefficients are given by vector @var{c}. If a pair of polynomials +## is given @var{b} and @var{a}, the derivative of the product is +## returned in @var{q}, or the quotient numerator in @var{q} and the +## quotient denominator in @var{r}. ## @end deftypefn -## ## @seealso{poly, polyinteg, polyreduce, roots, conv, deconv, residue, -## filter, polyval, and polyvalm} +## filter, polygcd, polyval, and polyvalm} ## Author: Tony Richardson <arichard@stark.cc.oh.us> ## Created: June 1994 ## Adapted-By: jwe - -function q = polyderiv (p) +## Paul Kienzle <pkienzle@kienzle.powernet.co.uk> +## handle b/a and b*a - if (nargin != 1) - usage ("polyderiv (vector)"); +function [q, r] = polyderiv (p, a) + + if (nargin < 1 || nargin > 3) + usage ("q=polyderiv(p) or q=polyderiv(b,a) or [q, r]=polyderiv(b,a)"); endif if (! isvector (p)) error ("polyderiv: argument must be a vector"); endif - lp = numel (p); - if (lp == 1) - q = 0; - return; - elseif (lp == 0) - q = []; - return; - end + if (nargin == 2) + if (! isvector (a)) + error ("polyderiv: argument must be a vector"); + endif + if (nargout == 1) + ## derivative of p*a returns a single polynomial + q = polyderiv(conv(p,a)); + else + ## derivative of p/a returns numerator and denominator + r = conv(a, a); + if numel(p) == 1 + q = -p * polyderiv(a); + elseif numel(a) == 1 + q = a * polyderiv(p); + else + q = conv(polyderiv(p),a) - conv(p,polyderiv(a)); + q = polyreduce(q); + endif - ## Force P to be a row vector. - p = p(:).'; + ## remove common factors from numerator and denominator + x = polygcd(q,r); + if length(x)!=1 + q=deconv(q,x); + r=deconv(r,x); + endif - q = p(1:(lp-1)) .* [(lp-1):-1:1]; + ## move all the gain into the numerator + q=q/r(1); + r=r/r(1); + endif + else + lp = numel (p); + if (lp == 1) + q = 0; + return; + elseif (lp == 0) + q = []; + return; + end + + ## Force P to be a row vector. + p = p(:).'; + + q = p (1:(lp-1)) .* [(lp-1):-1:1]; + endif endfunction