]> Creatis software - CreaPhase.git/blob - octave_packages/m/polynomial/polyder.m
update packages
[CreaPhase.git] / octave_packages / m / polynomial / polyder.m
1 ## Copyright (C) 1994-2012 John W. Eaton
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Function File} {} polyder (@var{p})
21 ## @deftypefnx {Function File} {[@var{k}] =} polyder (@var{a}, @var{b})
22 ## @deftypefnx {Function File} {[@var{q}, @var{d}] =} polyder (@var{b}, @var{a})
23 ## Return the coefficients of the derivative of the polynomial whose
24 ## coefficients are given by the vector @var{p}.  If a pair of polynomials
25 ## is given, return the derivative of the product @math{@var{a}*@var{b}}.
26 ## If two inputs and two outputs are given, return the derivative of the
27 ## polynomial quotient @math{@var{b}/@var{a}}.  The quotient numerator is
28 ## in @var{q} and the denominator in @var{d}.
29 ## @seealso{polyint, polyval, polyreduce}
30 ## @end deftypefn
31
32 ## Author: Tony Richardson <arichard@stark.cc.oh.us>
33 ## Created: June 1994
34 ## Adapted-By: jwe
35
36 function [q, d] = polyder (p, a)
37
38   if (nargin == 1 || nargin == 2)
39     if (! isvector (p))
40       error ("polyder: argument must be a vector");
41     endif
42     if (nargin == 2)
43       if (! isvector (a))
44         error ("polyder: argument must be a vector");
45       endif
46       if (nargout == 1)
47         ## derivative of p*a returns a single polynomial
48         q = polyder (conv (p, a));
49       else
50         ## derivative of p/a returns numerator and denominator
51         d = conv (a, a);
52         if (numel (p) == 1)
53           q = -p * polyder (a);
54         elseif (numel (a) == 1)
55           q = a * polyder (p);
56         else
57           q = conv (polyder (p), a) - conv (p, polyder (a));
58           q = polyreduce (q);
59         endif
60
61         ## remove common factors from numerator and denominator
62         x = polygcd (q, d);
63         if (length(x) != 1)
64           q = deconv (q, x);
65           d = deconv (d, x);
66         endif
67
68         ## move all the gain into the numerator
69         q = q/d(1);
70         d = d/d(1);
71       endif
72     else
73       lp = numel (p);
74       if (lp == 1)
75         q = 0;
76         return;
77       elseif (lp == 0)
78         q = [];
79         return;
80       endif
81
82       ## Force P to be a row vector.
83       p = p(:).';
84
85       q = p(1:(lp-1)) .* [(lp-1):-1:1];
86     endif
87   else
88     print_usage ();
89   endif
90
91 endfunction
92
93
94 %!assert(all (all (polyder ([1, 2, 3]) == [2, 2])));
95 %!assert(polyder (13) == 0);
96
97 %!error polyder ([]);
98 %!error polyder ([1, 2; 3, 4]);
99