在Mathematica中,如何将a == b || a == -b
等表达式简化为a^2 = b^2
?我尝试过的每个函数(包括Reduce,Simplify和FullSimplify)都没有这样做。
请注意,我希望这适用于任意(多项式)表达式a
和b
。另一个例子,
a == b || a == -b || a == i b || a == -i b
(假想的i
)和
a^2 == b^2 || a^2 == -b^2
应该简化为a^4 == b^4
。
注意:解决方案应该在逻辑级别工作,以免损害其他不相关的逻辑案例。例如,
a == b || a == -b || c == d
应该成为
a^2 == b^2 || c == d.
答案 0 :(得分:11)
可以将一组可能性转换为必须等于零的产品。
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
现在简化一下。
Simplify[eqn]
Out[32]= a^4 == b^4
Daniel Lichtblau
答案 1 :(得分:7)
布尔表达式可以转换为代数形式,如下所示:
In[18]:= (a == b || a == -b || a == I b || a == -I b) /. {Or -> Times,
Equal -> Subtract} // Expand
Out[18]= a^4 - b^4
为了使转换在其他变量中省略部分,可以编写Or
转换函数,并在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
可替换地:
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