function pot = mk_pot_from_gaussian(bnode, bnet)
% MK_POT_FROM_GAUSSIAN Make a potential representing a Gaussian conditional distrib.
% pot = mk_pot_from_gaussian(bnode, bnet)

dps = intersect(bnode.parents, bnet.discrete_nodes);
cps = intersect(bnode.parents, bnet.cts_nodes);
fam = [bnode.parents bnode.self];
hfam = intersect(fam, bnet.hidden_nodes);
ofam = intersect(fam, bnet.observed_nodes);
cofam = intersect(ofam, bnet.cts_nodes);
chfam = intersect(hfam, bnet.cts_nodes);
cfam = intersect(fam, bnet.cts_nodes);
dfam = intersect(fam, bnet.discrete_nodes);
ns = bnet.node_sizes;

% Extract the params compatible with the observations (if any) on the discrete parents
dops = intersect(dps, ofam);
map = find_equiv_posns(dops, dps);
dpvals = bnet.values(dops);
index = mk_multi_index(length(dps), map, dpvals);
ns(bnet.observed_nodes) = 1;
dpsize = prod(ns(dps));
ss = bnode.self_size;
if isempty(dps)
  m = bnode.mean;
  C = bnode.cov;
else
  m = reshape(bnode.mean(:, index{:}), ss, dpsize);
  C = reshape(bnode.cov(:, :, index{:}), ss, ss, dpsize);
end
if ~isempty(cps)
  W = reshape(bnode.weights(:, :, index{:}), ss, bnode.cpsize, dpsize);
else
  W = [];
end

switch bnet.jtree.pot_type
  case 'd', 
    assert(subset(cfam, ofam));
    p = zeros(1,dpsize);
    self_val = bnet.values(bnode.self);
    if isempty(cps)
      for i=1:dpsize
	p(i) = gaussian_prob(self_val, m(:, i), C(:,:,i));
      end
    else
      cpvals = bnet.values(cps);
      for i=1:dpsize
	p(i) = gaussian_prob(self_val, m(:, i) + W(:,:,i)*cpvals(:), C(:,:,i));
      end
    end
    pot = mk_table(fam, ns(fam));
    pot.T = myreshape(p, pot.sizes);
    
  case 'g',
    assert(subset(dfam, ofam));
    ns(bnet.observed_nodes) = 0;
    pot = mk_gpot(fam, ns(fam), 'c');
    if ~isempty(W)
      [g, h, K] = gaussian_cpt_to_canonical(m, C, W);
    else
      [g, h, K] = gaussian_cpt_to_canonical(m, C, []);
    end
    if ~isempty(cofam)
      cvals = bnet.values(cofam);
      bs = bnet.blocks;
      [g, h, K] = enter_evidence_canonical(g, h, K, chfam, cofam, cfam, cvals, bs(cfam));
    end
    pot.g = g; pot.h = h; pot.K = K;

  case 'cg',
    ns(intersect(bnet.observed_nodes, bnet.cts_nodes)) = 0;
    ns(intersect(bnet.observed_nodes, bnet.discrete_nodes)) = 1;
    pot = mk_cgpot(dfam, cfam, ns, 'c');

    if 0
      assert(length(dps) <= 1); % for simplicity
      if ismember(dps, bnet.observed_nodes)
	vs = bnet.values(dps);
      else
	if isempty(dps)
	  vs = 1;
	else
	  vs = 1:ns(dps);
	end
      end
      for j=1:length(vs)
	v = vs(j);
	if ~isempty(W)
	  [g, h, K] = gaussian_cpt_to_canonical(m(:,v), C(:,:,v), W(:,:,v));
	else
	  [g, h, K] = gaussian_cpt_to_canonical(m(:,v), C(:,:,v), []);
	end
	if ~isempty(cofam)
	  cvals = bnet.values(cofam);
	  bs = bnet.blocks;
	  [g, h, K] = enter_evidence_canonical(g, h, K, chfam, cofam, cfam, cvals, bs(cfam));
	end
	pot.pot{j}.g = g; pot.pot{j}.h = h; pot.pot{j}.K = K;
      end
    end
  
    if 1
      for i=1:dpsize
	if ~isempty(W)
	  [g, h, K] = gaussian_cpt_to_canonical(m(:,i), C(:,:,i), W(:,:,i));
	else
	  [g, h, K] = gaussian_cpt_to_canonical(m(:,i), C(:,:,i), []);
	end
	if ~isempty(cofam)
	  cvals = bnet.values(cofam);
	  bs = bnet.blocks;
	  [g, h, K] = enter_evidence_canonical(g, h, K, chfam, cofam, cfam, cvals, bs(cfam));
	end
	pot.pot{i}.g = g; pot.pot{i}.h = h; pot.pot{i}.K = K;
      end
    end
  
  otherwise,
    error(['bad type ' bnet.jtree.pot_type]);
end
    
