function pp=mkpp(breaks,coefs,d)
%MKPP Make piecewise polynomial.  
%   PP = MKPP(BREAKS,COEFS) puts together a piecewise polynomial PP from its
%   breaks and coefficients.  BREAKS must be a vector of length L+1 with
%   strictly increasing elements representing the start and end of each of L
%   intervals.  The matrix COEFS must be L-by-K with each row COEFS(i,:)
%   representing the coefficients of the order K polynomial on the interval
%   [BREAKS(i) ... BREAKS(i+1)].
%   Note: a K-th order polynomial has maximum index K-1:
%      C(1)*X^(K-1) + C(2)*X^(K-2) + ... + C(K-1)*X + C(K)
%   thus a cubic polynoimal with highest power 3 actually has order 4.
%
%   PP = MKPP(BREAKS,COEFS,D) denotes that the piecewise polynomial PP is
%   D-vector valued.  BREAKS must be an increasing vector of length L+1.
%   COEFS must be D-by-L-by-K with COEFS(r,i,:) the K coefficients of the r-th
%   component of the piecewise polynomial.
%
%   Examples:
%   These first two plots show the piecewise polynomials for the quadratic
%   1-(x/2-1)^2 = -x^2/4 + x shifted to the interval [-8 .. -4] from the origin
%   and for the quadratic (x/2-1)^2-1 = x^2/4 - x shifted to the interval
%   [-4 .. 0] from the origin.
%      subplot(2,2,1)
%      pp1 = mkpp([-8 -4],[-1/4 1 0]); xx1 = -8:0.1:-4;
%      plot(xx1,ppval(pp1,xx1),'k-')
%      subplot(2,2,2)
%      pp2 = mkpp([-4 -0],[1/4 -1 0]); xx2 = -4:0.1:0;
%      plot(xx2,ppval(pp2,xx2),'k-')
%      subplot(2,1,2)
%      pp = mkpp([-8 -4 0 4 8],[-1/4 1 0; 1/4 -1 0; -1/4 1 0; 1/4 -1 0]);
%      xx = -8:0.1:8;
%      plot(xx,ppval(pp,xx),'k-')
%      [breaks,coefs,l,k,d] = unmkpp(pp);
%      dpp = mkpp(breaks,repmat(k-1:-1:1,d*l,1).*coefs(:,1:k-1),d);
%      hold on, plot(xx,ppval(dpp,xx),'r-'), hold off
%   This last plot shows a piecewise polynomial constructed by alternating the
%   first two quadratic pieces over 4 intervals.  It also shows its first
%   derivative, which was constructed after breaking the piecewise polynomial
%   apart using UNMKPP.

%   See also UNMKPP, PPVAL, SPLINE.

%   Carl de Boor 7-2-86
%   Copyright 1984-2000 The MathWorks, Inc.
%   $Revision: 5.13 $  $Date: 2000/09/01 20:44:46 $

if nargin==2, d = 1; end
coefs=coefs(:).';dlk=length(coefs);l=length(breaks)-1;k=fix(dlk/(d*l)+100*eps);
if (k<=0)|(d*l*k~=dlk)
   error(['Number of polynomial pieces, ' int2str(l) ', is incompatible with number of coefficients, ' int2str(dlk) '.'])
else
   pp.form = 'pp';
   pp.breaks = reshape(breaks,1,l+1);
   pp.coefs = reshape(coefs,d*l,k);
   pp.pieces = l;
   pp.order = k;
   pp.dim = d;
end
