Mathematica 中的目标简化
我生成一般形式的非常长且复杂的分析表达式:
(...something not so complex...)(...ditto...)(...ditto...)...lots...
当我尝试使用 Simplify 时
,Mathematica 陷入停滞,我假设是因为它试图扩展括号和/或简化不同的括号。括号虽然包含长表达式,但很容易被 Mathematica 自己简化。有什么方法可以限制 Simplify
的范围一次到一个括号?
编辑:一些附加信息和进度。
因此,根据你们的建议,我现在开始使用类似
In[1]:= trouble = Log[(x + I y) (x - I y) + Sqrt[(a + I b) (a - I b)]];
In[2]:= Replace[trouble, form_ /; (Head[form] == Times) :> Simplify[form],{3}]
Out[2]= Log[Sqrt[a^2 + b^2] + (x - I y) (x + I y)]
“Changing Times
”的东西到合适的头,如 Plus
或 Power
来实现它可以非常准确地确定简化目标。不过,仍然存在的问题如下: Simplify
< /a> 仍将下降到比 Replace
,例如
In[3]:= Replace[trouble, form_ /; (Head[form] == Plus) :> Simplify[form], {1}]
Out[3]= Log[Sqrt[a^2 + b^2] + x^2 + y^2]
也简化了平方根。
我的计划是从下往上迭代使用 Replace
一次一个级别,但这显然会导致 Simplify 进行大量重复工作
并最终导致与我一开始经历的 Mathematica 陷入完全相同的困境。有没有办法将 Simplify
限制到一定程度(s)?
我意识到这种限制可能不会产生最佳结果,但这里的想法是得到“足够好”的东西。
I generate very long and complex analytic expressions of the general form:
(...something not so complex...)(...ditto...)(...ditto...)...lots...
When I try to use Simplify
, Mathematica grinds to a halt, I am assuming due to the fact that it tries to expand the brackets and or simplify across different brackets. The brackets, while containing long expressions, are easily simplified by Mathematica on their own. Is there some way I can limit the scope of Simplify
to a single bracket at a time?
Edit: Some additional info and progress.
So using the advice from you guys I have now started using something in the vein of
In[1]:= trouble = Log[(x + I y) (x - I y) + Sqrt[(a + I b) (a - I b)]];
In[2]:= Replace[trouble, form_ /; (Head[form] == Times) :> Simplify[form],{3}]
Out[2]= Log[Sqrt[a^2 + b^2] + (x - I y) (x + I y)]
Changing Times
to an appropriate head like Plus
or Power
makes it possible to target the simplification quite accurately. The problem / question that remains, though, is the following: Simplify
will still descend deeper than the level specified to Replace
, e.g.
In[3]:= Replace[trouble, form_ /; (Head[form] == Plus) :> Simplify[form], {1}]
Out[3]= Log[Sqrt[a^2 + b^2] + x^2 + y^2]
simplifies the square root as well.
My plan was to iteratively use Replace
from the bottom up one level at a time, but this clearly will result in vast amount of repeated work by Simplify
and ultimately result in the exact same bogging down of Mathematica I experienced in the outset. Is there a way to restrict Simplify
to a certain level(s)?
I realize that this sort of restriction may not produce optimal results, but the idea here is getting something that is "good enough".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有多种方法可以做到这一点,但这可能有点棘手,并且取决于实际表达式的结构。然而,通常括号中多项项的乘积会有头部
Times
,您可以使用FullForm
来验证这一点:您可以使用高阶函数 < a href="http://reference.wolfram.com/mathematica/ref/Map.html" rel="nofollow noreferrer">
Map
,其头部为Times< 的表达式/code> 与带有头
List
的表达式使用它的方式相同,这可以让您一次简化
表达式一项,如下所示:您可以使用如果您随后需要,
展开
结果展开括号。编辑添加:如果您想指定要简化的形式,可以使用
替换
或ReplaceAll
而不是Map
的亲戚之一。Replace
特别有用,因为它需要级别规范,允许您仅影响最顶层产品中的因素。作为一个简单的示例,请考虑以下情况:如果您不想简化依赖于
a
的因素。您可以这样做:仅更改了依赖于
b
的第二项。但需要记住的一件事是,某些转换是由Times
或Plus
自动完成的;例如,即使不使用Simplify
,a + a
也会变成2 a
。There are a number of ways you can do this, but it can be a little tricky and depends on the structure of your actual expression. However, usually a product of a number of terms in brackets will have the head
Times
, and you can useFullForm
to verify this:You can use the higher-order function
Map
with expressions with headTimes
the same way you use it with expressions with headList
, and that may allow you toSimplify
the expression one term at a time, like so:You can use
Expand
on the result if you need to subsequently expand out the brackets.EDIT to add: If you want to specify the forms that you do want to simplify, you can use
Replace
orReplaceAll
instead of one of the relatives ofMap
.Replace
is particularly useful because it takes a level specification, allowing you to only affect the factors in the topmost product. As a simple example, consider the following:If you don't want to simplify factors that depend on
a
. you can do this instead:Only the second term, which depends on
b
, has been changed. One thing to bear in mind though is that some transformations are done automatically byTimes
orPlus
; for instancea + a
will be turned into2 a
even without use ofSimplify
.我不敢苟同我的同事,因为使用
Map
将Simplify
应用于每个子表达式可能不会节省任何时间,因为它仍然会应用于每个子表达式。相反,请尝试MapAt
< /a>,如下:棘手的部分是确定位置规范。不过,如果您要简化的表达式位于第一级,那么它应该不会比我上面写的更困难。
现在,如果您仍然想简化一切,但希望保留一些结构,请尝试使用选项
排除表格
。过去,我曾经用来防止这种简化:Mathematica 似乎喜欢这种简化,所以我也这样做。
另外,不要忘记
Simplify
的第二个参数是用于假设的,并且可以极大地简化您的操作。努力将你的表达变成有用的形式。I beg to differ with my colleagues, in that using
Map
to applySimplify
to each subexpression may not save any time as it will still be applied to each one. Instead try,MapAt
, as follows:The tricky part is determining the position specification. Although, if the expression you want to simplify is at the first level, it shouldn't be any more difficult then what I've written above.
Now if you would still like to simplify everything, but you wish to preserve some structure, try using the option
ExcludedForms
. In the past, I've used to prevent this simplification:which Mathematica seems to like, so I do
Also, don't forget that the second parameter for
Simplify
is for assumptions, and can greatly ease your struggles in getting your expressions into a useful form.您应该尝试
地图
。一般来说,Map[foo, G[a, b, c, ...]] 给出 G[foo[a], foo[b], foo[c], .. .] 对于任何头
G
和任何表达式foo
,因此对于它给出的
Note,您可以表示
Map[foo, expr]
alsfoo /@ expr
如果您觉得这样更方便。You should try
Map
.In general,
Map[foo, G[a, b, c, ...]]
givesG[foo[a], foo[b], foo[c], ...]
for any headG
and any expressionfoo
, so forit gives
Note you can denote
Map[foo, expr]
alsfoo /@ expr
if you find that more convenient.