function pp=sp2pp(spline)
% SP2PP	Convert from B-form to pp-form.
%
%     pp = sp2pp(spline)
%
%  converts the  spline  to its  pp  representation on the interval
%                 [t(1),t(n+k)]

% C. de Boor / latest change: Aug.10, 1989
% Copyright (c) 1990-92 by Carl de Boor and The MathWorks, Inc.


%  Take apart the  spline

[t,a,n,k,d]=spbrk(spline);

%  and augment the knot sequence so that first and last knot each have
%  multiplicity  k .

index=find(diff(t)>0);addl=k-index(1);addr=index(length(index))-n;
if (addl>0|addr>0),
   t=[t(1)*ones(1,addl) t(:)' t(n+k)*ones(1,addr)];
   a=[zeros(d,addl) a zeros(d,addr)];
end

%  From this, generate the pp description. 

inter=find( diff(t)>0 );l=length(inter);
if (k>1)
   ones(d,1)*inter;dinter=ans(:);
   tx=ones(d*l,1)*[2-k:k-1]+dinter*ones(1,2*(k-1));tx(:)=t(tx);
   tx=tx-t(dinter)'*ones(1,2*(k-1));
   a=a(:);d*ones(d,1)*inter+[1-d:0]'*ones(1,l);dinter(:)=ans(:);
   b=d*ones(d*l,1)*[1-k:0]+dinter*ones(1,k);b(:)=a(b);
   c=sprpp(tx,b);
else,a(:,inter);c=ans(:);
end

%   put together the  pp

pp=ppmak([t(inter) t(inter(length(inter))+1)],c,d);
