function pc=ppcut(pp,bounds)
% PPCUT	Cut down a pp to a given interval.
%
%      pc = ppcut(pp,bounds)
%
%  returns the restriction of  pp  to the interval  [bounds(1) .. bounds(2)]

% C. de Boor / latest change: Feb.25, 1989
% C. de Boor / latest change: May 27, 1991  interval notation changed
% Copyright (c) 1990-92 by Carl de Boor and The MathWorks, Inc.

xl=bounds(1);xr=bounds(2);if xl>xr,xl=xr;xr=bounds(1);end

[breaks,coefs,l,k,d]=ppbrk(pp);

%  the first pol. piece is  jl , the one responsible for argument  xl 

jl=l;index=find(breaks(2:l)>xl);        % note that the resulting  index  is 
if (~isempty(index)), jl=index(1); end  % shifted down by one because of
                                        % break(2: ...

%  if xl ~= breaks(jl), recenter the pol.coeffs.
x=xl-breaks(jl);
if x ~= 0,
   a=coefs(d*jl+[1-d:0],:);
   for ii=k:-1:2;
      for i=2:ii;
         a(:,i)=x*a(:,i-1)+a(:,i);
      end
   end
   coefs(d*jl+[1-d:0],:)=a;
end

%  the last pol. piece is  jr , the one responsible for argument  xr .
jr=l;index=find(breaks(2:l+1)>=xr);      % note that the resulting  index  is 
if (~isempty(index)), jr=index(1); end  % shifted down by one because of
                                        % break(2: ...

%  put together the cut-down  pp
pc=ppmak([xl breaks(jl+1:jr) xr], coefs(d*(jl-1)+[1:d*(jr-jl+1)],:),d);
