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.
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.
Equal (==)—Wolfram Language Documentation.
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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With