2
$\begingroup$

Oftentimes, the output of Reduce is quite difficult to read because it has too many && and || included in it.

For example the output of Reduce[expr]//Simplify for a particular expr I had was:

(ϵc == 
0 && ((ex == 0 && 
   ey == 0 && (ez ϵb != 
      0 || (ez != 0 && ϵb == 0))) || (ey != 
    0 && ϵb != 
    0 && (nr == Sqrt[ey ϵa + ex ϵb]/Sqrt[ey] || 
     nr + Sqrt[ey ϵa + ex ϵb]/Sqrt[ey] == 
      0) && (ex - I ey == 0 || ex + I ey == 0)) || (ez != 
    0 && ϵb == 
    0 && (nr == Sqrt[ϵa] || 
     nr + Sqrt[ϵa] == 0)))) || (ez == 
0 && ((ϵb == 
    0 && (nr == Sqrt[ϵa] || 
     nr + Sqrt[ϵa] == 0) && (ex != 0 || 
     ey != 0)) || (ey != 0 && ϵb != 
    0 && (nr == Sqrt[ey ϵa + ex ϵb]/Sqrt[ey] || 
     nr + Sqrt[ey ϵa + ex ϵb]/Sqrt[ey] == 
      0) && (ex - I ey == 0 || ex + I ey == 0))))

I was thinking it would be easier to read this if it were displayed as a tree. However TreeForm[%] returns: Messy Tree which is not much better.

Annother question suggested truncating the tree, in which case you get: enter image description here

Also not great.

What I would like is to split up the tree by logic symbols, but to have the leaves of the tree be the individual equations.

How can I get the tree to terminate at equations?

As in I'd want expressions that look like nr == Sqrt[ea] to be connected in a tree of Ands and Ors. It's difficult because the number of levels may be different for different parts of the tree. Any ideas?

$\endgroup$

2 Answers 2

8
$\begingroup$

There are plenty of new functionalities for manipulating and styling trees in v13 (some of them present already in v12.3).

For example:

exprTree[expr_] := 
 RulesTree[
  TreeRules[ExpressionTree[expr]] /. (e : (Except[And | Or] -> __)) :>
     TreeExpression[RulesTree[e]], 
  TreeLayout -> {"LayeredDigraphEmbedding", "Orientation" -> Left}, 
  TreeElementStyle -> {TreeCases[And] -> LightRed, 
    TreeCases[Or] -> LightBlue, TreeCases[_Equal] -> LightGreen, 
    TreeCases[_Unequal] -> LightYellow}, AspectRatio -> 1]

exprTree[expr]

enter image description here

$\endgroup$
4
$\begingroup$
expr//.{eq_Equal:>ToString[eq,StandardForm],neq_Unequal:>ToString[neq,StandardForm]}

enter image description here


@Domen has provided an answer based on tree manipulation. Here my idea is to "atomize" the equations. Besides the ToString method, we can also atomize the equations with some undocumented functions.

c_wrap /; System`Private`HoldNotValidQ[c] := (
    System`Private`HoldSetValid[c];
    System`Private`HoldSetNoEntry[c]
);

get[wrap[data_]]:=
    data;

wrap/:MakeBoxes[c_wrap,format_]:=
    With[{data=get[c]},MakeBoxes[data,format]];
expr/.{eq:_Equal|_Unequal:>wrap[eq]}//ExpressionTree

enter image description here

This method does not work for TreeForm comparing with ToString since my format definition wrap/:... does not handle tree forms.

$\endgroup$

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .