Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Mathematica, how to simplify expressions like a == +/- b into a^2 == b^2?

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.
like image 633
Tyson Williams Avatar asked May 31 '11 16:05

Tyson Williams


People also ask

How do you simplify Mathematica?

The simplify command finds the simplest form of an equation. Simplify[expr,assum] does simplification using assumptions. Expand[expr,patt] leaves unexpanded any parts of expr that are free of the pattern patt.

What does == mean in Mathematica?

Equal (==)—Wolfram Language Documentation.


2 Answers

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

like image 103
Daniel Lichtblau Avatar answered Sep 28 '22 19:09

Daniel Lichtblau


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
like image 42
Sasha Avatar answered Sep 28 '22 19:09

Sasha