3
$\begingroup$

I have outputs which are in the form of nested Piecewise functions and I want to represent these diagrammatically. There are multiple levels in general. A simple example with two levels is:

Piecewise[{{Piecewise[{{f = a, y > 0}, {f = b, y <= 0}}], z > 0}, {f = c, z <= 0}}]

where y and z are Reals. I would like to express this result as a tree diagram (or something similar), where vertices would be labeled by the conditions and leaves would be the values of f. How can I write a function which can turn this output into such a diagram?

$\endgroup$
  • $\begingroup$ Have you tried TreeForm? $\endgroup$ – bill s Oct 25 '16 at 15:24
  • $\begingroup$ TreeForm looks like it will be useful. Thank you @bill s $\endgroup$ – JohnF Oct 25 '16 at 18:42
2
$\begingroup$

You might also want to use PiecewiseExpand

p = Piecewise[{{Piecewise[{{f = a, y > 0}, {f = b, y <= 0}}], z > 0}, 
  {f = c, z <= 0}}];

p2 = p // PiecewiseExpand;

p == p2 // Simplify

(*  True  *)

p2 // TreeForm

enter image description here

$\endgroup$
1
$\begingroup$
p = Piecewise[{{Piecewise[{{f = a, y > 0}, 
              {f = b, y <= 0}}], z > 0}, {f = c, z <= 0}}];
TreeForm[p]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.