function B = extend_domain(A, Adom, Asizes, Bdom, Bsizes)
% EXTEND_DOMAIN Extend the domain of an array.
% B = extend_domain_table(A, Adom, Asizes, Bdom, Bsizes)
%
% A is the array with domain Adom and sizes Asizes.
% Bdom is the desired domain, with sizes Bsizes.
% e.g, Adom = [1 3], Asizes = [2 4], Bdom = [1 2 3 4], Bsizes = [2 1 4 5],
% so B(i,j,k,l) = A(i,k) for i in [1,2], j in [1,1], k in [1,4], l in [1,5]

if isequal(size(A), [1 1]) % a scalar
  B = A * myones(Bsizes);
  return;
end

map = find_equiv_posns(Adom, Bdom);
n1 = length(Bdom);
sz = ones(1, n1);
sz(map) = Asizes;
if length(sz)==1
  sz = [sz 1];
end
B = reshape(A, sz); % add dimensions for the stuff not in A
sz = Bsizes;
sz(map) = 1; % don't replicate along A's dimensions
if length(sz)==1
  sz = [sz 1];
end
B = repmat(B, sz(:)');
