Mercurial > hg > octave-nkf
annotate scripts/polynomial/polyder.m @ 20727:4479d73eac72
Fix error when building annotation objects in gnuplot (bug #46035)
* __gnuplot_draw_figure__.m: ignore uicontextmenu objects.
author | Pantxo Diribarne <pantxo.diribarne@gmail.com> |
---|---|
date | Sat, 26 Sep 2015 11:26:18 +0200 |
parents | 83792dd9bcc1 |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
1 ## Copyright (C) 1994-2015 John W. Eaton |
2313 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
2313 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
1597 | 18 |
3458 | 19 ## -*- texinfo -*- |
12803
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
20 ## @deftypefn {Function File} {} polyder (@var{p}) |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
21 ## @deftypefnx {Function File} {[@var{k}] =} polyder (@var{a}, @var{b}) |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
22 ## @deftypefnx {Function File} {[@var{q}, @var{d}] =} polyder (@var{b}, @var{a}) |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
23 ## Return the coefficients of the derivative of the polynomial whose |
20375
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
24 ## coefficients are given by the vector @var{p}. |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
25 ## |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
26 ## If a pair of polynomials is given, return the derivative of the product |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
27 ## @math{@var{a}*@var{b}}. |
20391
aa36fb998a4d
maint: Remove unnecessary whitespace at end of lines.
Rik <rik@octave.org>
parents:
20375
diff
changeset
|
28 ## |
12803
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
29 ## If two inputs and two outputs are given, return the derivative of the |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
30 ## polynomial quotient @math{@var{b}/@var{a}}. The quotient numerator is |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
31 ## in @var{q} and the denominator in @var{d}. |
14104
614505385171
doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents:
12803
diff
changeset
|
32 ## @seealso{polyint, polyval, polyreduce} |
3458 | 33 ## @end deftypefn |
1597 | 34 |
12803
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
35 ## Author: Tony Richardson <arichard@stark.cc.oh.us> |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
36 ## Created: June 1994 |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
37 ## Adapted-By: jwe |
2314 | 38 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
39 function [q, d] = polyder (p, a) |
1597 | 40 |
12803
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
41 if (nargin == 1 || nargin == 2) |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
42 if (! isvector (p)) |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
43 error ("polyder: argument must be a vector"); |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
44 endif |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
45 if (nargin == 2) |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
46 if (! isvector (a)) |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
47 error ("polyder: argument must be a vector"); |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
48 endif |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
49 if (nargout == 1) |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
50 ## derivative of p*a returns a single polynomial |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
51 q = polyder (conv (p, a)); |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
52 else |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
53 ## derivative of p/a returns numerator and denominator |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
54 d = conv (a, a); |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
55 if (numel (p) == 1) |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
56 q = -p * polyder (a); |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
57 elseif (numel (a) == 1) |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
58 q = a * polyder (p); |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
59 else |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
60 q = conv (polyder (p), a) - conv (p, polyder (a)); |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
61 q = polyreduce (q); |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
62 endif |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
63 |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
64 ## remove common factors from numerator and denominator |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
65 x = polygcd (q, d); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
66 if (length (x) != 1) |
12803
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
67 q = deconv (q, x); |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
68 d = deconv (d, x); |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
69 endif |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
70 |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
71 ## move all the gain into the numerator |
20441
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20391
diff
changeset
|
72 q /= d(1); |
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20391
diff
changeset
|
73 d /= d(1); |
12803
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
74 endif |
5216 | 75 else |
12803
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
76 lp = numel (p); |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
77 if (lp == 1) |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
78 q = 0; |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
79 return; |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
80 elseif (lp == 0) |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
81 q = []; |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
82 return; |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
83 endif |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
84 |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
85 ## Force P to be a row vector. |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
86 p = p(:).'; |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
87 |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
88 q = p(1:(lp-1)) .* [(lp-1):-1:1]; |
5216 | 89 endif |
1597 | 90 else |
6046 | 91 print_usage (); |
1597 | 92 endif |
93 | |
94 endfunction | |
12803
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
95 |
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
96 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
97 %!assert (polyder ([1, 2, 3], [2, 2])) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
98 %!assert (polyder (13), 0) |
12803
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
99 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
100 %!error polyder ([]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
101 %!error polyder (1,2,3) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
102 %!error <argument must be a vector> polyder ([1, 2; 3, 4]) |
12803
b7a6a3644f3b
codesprint: Deprecate polyderiv.m
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
103 |