7

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)$.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
anderstood
  • 14,301
  • 2
  • 29
  • 80

2 Answers2

10
Or @@ And @@@ r /. Rule -> Equal // LogicalExpand
Kuba
  • 136,707
  • 13
  • 279
  • 740
  • 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 – Stitch Nov 16 '16 at 22:13
  • @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. – Kuba Nov 17 '16 at 06:41
9

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
lowriniak
  • 2,412
  • 10
  • 15