function mpnew = mpshift(mp,newcenter)
%MPSHIFT Shift to a different center
%
%        MPNEW = MPVAL(MP,NEWCENTER)
%
% Recenters the mp-form at the given NEWCENTER. 
%
% See MPMAK for details concerning the mp-form 
%   p(x) = sum_a (x-center)^a (|a| choose a) coefs(:,nn(a))
%

% cb: 01aug97, 9aug97; revert to v4 13sep97
%   Copyright (c) 1997 by C. de Boor

[coefs,d,k,center,dimpjd,mm] = mpbrk(mp);
[df,nc] = size(coefs);

if size(newcenter)~=[d,1]
   error(['NEWCENTER should be of size [',int2str(d),',1].'])
end
%v5x = reshape(newcenter-center,[1,d,1]);
%v5
%v5for i=0:k-1
%v5   for j=k-1:-1:i 
%v5      ranj = dimpjd(j+1)+1:dimpjd(j+2); lj = length(ranj);
%v5      coefs(:,ranj) = coefs(:,ranj) + ...
%v5        reshape(sum(reshape(coefs(:,dimpjd(j+2)+mm(:,ranj)),[df,d,lj]) ...
%v5                           .*x(ones(df,1),:,ones(1,lj)),2),[df,lj]);
%v5   end
%v5end
x = newcenter-center;
for ff=1:df
   for i=0:k-1
      for j=k-1:-1:i 
         ranj = dimpjd(j+1)+1:dimpjd(j+2); lj = length(ranj);
         coefs(ff,ranj) = coefs(ff,ranj) + ...
           sum(reshape(coefs(ff,dimpjd(j+2)+mm(:,ranj)),d,lj) ...
                              .*x(:,ones(1,lj)));
      end
   end
end

mpnew = mpmak(coefs,d,k,newcenter,dimpjd,mm);
