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