7
$\begingroup$

How to simplify list of rules? For example,

{{a -> 0},{a -> 0, b -> 0}, {a -> 0, c -> 0}}

should return {{a->0}} since $(a=0)||(a=0\&\&b=0)||(a=0\&\&c=0)=(a=0)$.

$\endgroup$
0

2 Answers 2

9
$\begingroup$

To explain the other answerer's response, what you need to do is convert the rules into logical expressions, as a Rule in the Wolfram Language just turns the left hand side into the right hand side. So the first step is to make all the Rules into Equals:

rules = {{a -> 0},{a -> 0, b -> 0}, {a -> 0, c -> 0}};

rules2 = ReplaceAll[rules, Rule -> Equal]
(*{{a == 0}, {a == 0, b == 0}, {a == 0, c == 0}}*)

ReplaceAll takes anything matching a given pattern, in this case Rule, and replaces it with another expression, in this case Equal

Then you need to make this a logical And expression, like this:

rules3 = Apply[And, rules2, {1}]
(*{a == 0, a == 0 && b == 0, a == 0 && c == 0}*)

Apply changes the head of the second argument to the first argument. The third argument defines which level it happens on

Then you need to make the list into a logical Or expression:

rules4 = Apply[Or, rules3]
(*a == 0 || (a == 0 && b == 0) || (a == 0 && c == 0)*)

Finally you use LogicalExpand to convert this statement into it's simplest form:

LogicalExpand[rules4]
(*a == 0*)

The shorthand posted previously is from the shorthands of ReplaceAll and Apply.

@@ means Apply

@@@ means Apply at level one

/. means ReplaceAll

// is suffix notation, so it does the right hand side on the left hand side.

Put together this makes the short version:

Or @@ And @@@ rules /. Rule -> Equal // LogicalExpand
$\endgroup$
2
  • 1
    $\begingroup$ Great explanation. You could maybe even add something about the order with which the shorthands are evaluated. By experience, I know it's evaluated as such ((Or @@ (And @@@ rules)) /. Rule -> Equal) // LogicalExpand but I couldn't "prove" it. $\endgroup$
    – anderstood
    Nov 16, 2016 at 18:09
  • 2
    $\begingroup$ @anderstood See when is f@g not the same as f[g]? $\endgroup$
    – corey979
    Nov 16, 2016 at 18:27
10
$\begingroup$
Or @@ And @@@ r /. Rule -> Equal // LogicalExpand
$\endgroup$
2
  • $\begingroup$ If you want to have a rule in the end, you have to replace it back after the expression is simplified like this: {Or @@ And @@@ r /. Rule -> Equal // LogicalExpand} /. Equal -> Rule $\endgroup$
    – Stitch
    Nov 16, 2016 at 22:13
  • $\begingroup$ @Stitch yep, there are also double brackets but I decided that should be enough and OP should be able to adjust it for specific needs. $\endgroup$
    – Kuba
    Nov 17, 2016 at 6:41

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.