function u=proj(x,v,y)
%
% u=proj(x,v,y); 
% Projects piecewise constant function v with
% discontinuities at y onto 
% piecewise constant function u with discontinuities at x.
%
n=length(x);
l=length(y);
if (l==0),
	u=v(1)*ones(size(x)-1);
else
  u=zeros(1,n-1);
  j=1;
  h=x(1);
  ns=y(j);
  while (ns<h),
    j=j+1;
    ns=y(j);
  end;
  for i=1:n-1,
    dx=x(i+1)-h;
    while (ns<x(i+1)),
      u(i)=u(i)+v(j)*(y(j)-h)/dx;
      h=ns;
      j=j+1;
      if ((j<=l)&&(j>0)),
        ns=y(j);
      else
        ns=x(n)+1;
      end;
    end;
    u(i)=u(i)+v(j)*(x(i+1)-h)/dx;
    h=x(i+1);
  end;
end;

