function lam_msg = compute_lambda_msg_mux2(bnode, bnet, p)

n = bnode.self;
ns = bnet.node_sizes;
my_lambda = bnet.pearl.msg{n}.lambda(:);
pi_from_inputs = collect_pi_from_inputs_mux2(bnode, bnet);
[nrows ncols] = size(bnode.input);
X = bnode.self_size;

j = bnet.pearl.parent_index{n}(bnode.rgate); % rgate is my j'th parent
m = bnet.pearl.msg{n}.pi_from_parent{j};
pi_from_R = m(:);

j = bnet.pearl.parent_index{n}(bnode.cgate); % cgate is my j'th parent
m = bnet.pearl.msg{n}.pi_from_parent{j};
pi_from_C = m(:);

pi_from_RC = pi_from_R * pi_from_C';

switch p
  case bnode.cgate
    W = zeros(X, ncols);
    for c=1:ncols
      for x=1:X
	pi = reshape(pi_from_inputs(x,:,c), [nrows 1]);
	W(x,c) = sum(pi_from_R(:) .* pi);
      end
    end
  case bnode.rgate
    W = zeros(X, nrows);
    for r=1:nrows
      for x=1:X
	pi = reshape(pi_from_inputs(x,r,:), [ncols 1]);
	W(x,r) = sum(pi_from_C(:) .* pi);
      end
    end
  otherwise
    [r c] = find(p==bnode.input);  % p is input (r,c)
    W = zeros(X, X);
    for x=1:X
      for m=1:X
	pi = reshape(pi_from_inputs(x,:,:), [nrows ncols]);
	pi(r,c) = (x==m);
	W(x,m) = sum(pi(:) .* pi_from_RC(:));
      end
    end
end

lam_msg = W' * my_lambda;
