function CPT = mk_CPT_from_bool_fn(n, formula)
% CPT_FROM_BOOL_FN Make a CPT equivalent to the specified boolean formula
% CPT = CPT_from_bool_fn(n, formula)
%
% 'formula' should be a string which can be evaluated to give the value of the output.
% The names of the inputs (parents) are p(1) ... p(n).
% e.g., formula = 'p(1) & ~p(2)'

bits = fliplr(bintab(n)); % all n bit vectors, with the left most column toggling fastest (LSB)
CPT = zeros(2^n,2);
for i=1:2^n
  p = bits(i,:);
  x = eval(formula);
  if x==0
    CPT(i,:) = [1.0 0.0];
  else
    CPT(i,:) = [0.0 1.0];
  end
end
CPT = reshape(CPT, twos(1,n+1));
