function [resF,resB]=fastCumConv(X,first,last)
% [resF,resB]=fastCumConv(X,first,last)
%
% Cumulative convolution of all the rows of X. Retains
% only the first size(X,2) components.
% if first and last are specified, the forward process begins
% with first and the backword process begins with last.

M=size(X,1);N=size(X,2);
resF=zeros(M,N);resB=zeros(M,N);

if nargin>1,
    tmp=ifft(fft(X(1,:),2*N) .* fft(first,2*N));
    resF(1,:)=tmp(1:N);
    tmp=ifft(fft(X(M,:),2*N) .* fft(last,2*N));
    resB(M,:)=tmp(1:N);   
else
    resF(1,:)=X(1,:);resB(end,:)=X(end,:);
end

for i=2:size(X,1),
    tmp=ifft(fft(X(i,:),2*N) .* fft(resF(i-1,:),2*N));
    resF(i,:)=tmp(1:N);
    tmp=ifft(fft(X(M-i+1,:),2*N) .* fft(resB(M-i+2,:),2*N));
    resB(M-i+1,:)=tmp(1:N);
end