5

In Mathematica, how can I simplify expressions like a == b || a == -b into a^2 = b^2? Every function that I have tried (including Reduce, Simplify, and FullSimplify) does not do it.

Note that I want this to work for an arbitrary (polynomial) expressions a and b. As another example,

a == b || a == -b || a == i b || a == -i b

(for imaginary i) and

a^2 == b^2 || a^2 == -b^2

should both be simplified to a^4 == b^4.

Note: the solution should work at the logical level so as not to harm other unrelated logical cases. For example,

a == b || a == -b || c == d

should become

a^2 == b^2 || c == d.
Tyson Williams
  • 1,630
  • 15
  • 35

2 Answers2

11

Could convert set of possibilities to a product that must equal zero.

expr = a == b || a == -b || a == I*b || a == -I*b;
eqn = Apply[Times, Apply[Subtract, expr, 1]] == 0

Out[30]= (a - b)*(a - I*b)*(a + I*b)*(a + b) == 0

Now simplify that.

Simplify[eqn]

Out[32]= a^4 == b^4

Daniel Lichtblau

Daniel Lichtblau
  • 6,854
  • 1
  • 23
  • 30
7

The Boolean expression can be converted to the algebraic form as follows:

In[18]:= (a == b || a == -b || a == I b || a == -I b) /. {Or -> Times,
    Equal -> Subtract} // Expand

Out[18]= a^4 - b^4


EDIT

In response to making the transformation leave out parts in other variables, one can write Or transformation function, and use in Simplify:

In[41]:= Clear[OrCombine];
OrCombine[Verbatim[Or][e__Equal]] := Module[{g},
  g = GatherBy[{e}, Variables[Subtract @@ #] &];
  Apply[Or, 
   Function[
     h, ((Times @@ (h /. {Equal -> Subtract})) // Expand) == 0] /@ g]
  ]

In[43]:= OrCombine[(a == b || a == -b || a == I b || a == -I b || 
   c == d)]

Out[43]= a^4 - b^4 == 0 || c - d == 0

Alternatively:

In[40]:= Simplify[(a == b || a == -b || a == I b || a == -I b || 
   c == d), TransformationFunctions -> {Automatic, OrCombine}]

Out[40]= a^4 == b^4 || c == d
Sasha
  • 5,935
  • 1
  • 25
  • 33
  • Nice, similar to Daniel's but different too. The result isn't an equation anymore, though. – Sjoerd C. de Vries May 31 '11 at 16:46
  • I think Daniel and I offered essentially the same solutions, independently. Equality in mine is trivially obtained by equating the resulting polynomial to zero. – Sasha May 31 '11 at 16:52
  • 1
    Although this is a solution for the problem exactly as I stated it, the solution should not harm other, non-related cases. For example, `(a == b || a == -b || a == I b || a == -I b || c == d) /. {Or -> Times, Equal -> Subtract} // Expand` should be `a^4 == b^4 || c ==d`, not a^4 c - b^4 c - a^4 d + b^4 d`. – Tyson Williams May 31 '11 at 16:58
  • or at least something like `a^4 == b^4 || c == d`. – Tyson Williams May 31 '11 at 17:08
  • While both answers are good, I am picking this answer as the correct one because of the slight advantage that the result is still a logical expression. – Tyson Williams Jun 01 '11 at 13:03