]> Creatis software - CreaPhase.git/blob - octave_packages/m/polynomial/ppder.m
update packages
[CreaPhase.git] / octave_packages / m / polynomial / ppder.m
1 ## Copyright (C) 2008-2012 VZLU Prague, a.s., Czech Republic
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or
8 ## (at your option) any later version.
9 ##
10 ## This program is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ## GNU General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with this software; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Function File} {ppd =} ppder (pp)
21 ## @deftypefnx {Function File} {ppd =} ppder (pp, m)
22 ## Compute the piecewise @var{m}-th derivative of a piecewise polynomial
23 ## struct @var{pp}.  If @var{m} is omitted the first derivative is calculated.
24 ## @seealso{mkpp, ppval, ppint}
25 ## @end deftypefn
26
27 function ppd = ppder (pp, m)
28
29   if ((nargin < 1) || (nargin > 2))
30     print_usage ();
31   elseif (nargin == 1)
32     m = 1;
33   endif
34
35   if (! (isstruct (pp) && strcmp (pp.form, "pp")))
36     error ("ppder: PP must be a structure");
37   endif
38
39   [x, p, n, k, d] = unmkpp (pp);
40
41   if (k - m <= 0)
42     x = [x(1) x(end)];
43     pd = zeros (prod (d), 1);
44   else
45     f = k : -1 : 1;
46     ff = bincoeff (f, m + 1) .* factorial (m + 1) ./ f;
47     k -= m;
48     pd = p(:,1:k) * diag (ff(1:k));
49   endif
50
51   ppd = mkpp (x, pd, d);
52 endfunction
53
54 %!shared x,y,pp,ppd
55 %! x=0:8;y=[x.^2;x.^3+1];pp=spline(x,y);
56 %! ppd=ppder(pp);
57 %!assert(ppval(ppd,x),[2*x;3*x.^2],1e-14)
58 %!assert(ppd.order,3)
59 %! ppd=ppder(pp,2);
60 %!assert(ppval(ppd,x),[2*ones(size(x));6*x],1e-14)
61 %!assert(ppd.order,2)
62 %! ppd=ppder(pp,3);
63 %!assert(ppd.order,1)
64 %!assert(ppd.pieces,8)
65 %!assert(size(ppd.coefs),[16,1])
66 %! ppd=ppder(pp,4);
67 %!assert(ppd.order,1)
68 %!assert(ppd.pieces,1)
69 %!assert(size(ppd.coefs),[2,1])
70 %!assert(ppval(ppd,x),zeros(size(y)),1e-14)