function T1 = marginalize_table(T2, onto)
% MARGINALIZE_TABLE Marginalize a table onto a smaller domain.
% function T1 = marginalize_table(T2, onto)

node_sizes = sparse(1, max(T2.domain));
node_sizes(T2.domain) = T2.sizes;
T1 = mk_table(onto, node_sizes(onto));
sum_over = setdiff(T2.domain, T1.domain);
S = find_equiv_posns(sum_over, T2.domain);
%if isvector(T2.T)
%  T2.T = T2.T(:); % so summing over dimension 1 collapses T2 to a scalar
%end
T1.T = sumv(T2.T, S);

