% cumsum - cumsum for sequences
%
% cumsum :: seq [[N,M]], D:natural -> seq [[N,M]].
%
% cumulative sum taken along Dth dimension of the array
function y=cumsum(x,n)
	if length(size(x))>2
		error('cumsum not supported for >2 dim array sequences');
	end

	switch n
		case 1
			y=scanl(@cs1,zeros(1,size(x,2)),x);
		case 2
			y=scanl(@cs2,zeros(size(x,1),1),x);
		case 3
			error('cumsum(.,3) not supported for sequences');
	end

	function w=cs1(s,z), w=row(cumsum([s(end,:);z],1),2:size(z,1)+1); end
	function w=cs2(s,z), w=col(cumsum([s(:,end),z],2),2:size(z,2)+1); end
end
