关于“智能"的问题在数学中替换 [英] Question on "smart" replacing in mathematica

查看:23
本文介绍了关于“智能"的问题在数学中替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何告诉 mathematica 巧妙地进行这种替换?(或者我如何更聪明地告诉 mathematica 做我想做的事)

How do I tell mathematica to do this replacement smartly? (or how do I get smarter at telling mathematica to do what i want)

expr = b + c d + ec + 2 a;
expr /. a + b :> 1

Out = 2 a + b + c d + ec

我希望答案是a + cd + ec + 1.在有人建议之前,我不想做 a :>1 - b,因为出于审美目的,我希望在我的等式中同时包含 ab,只要 a+b= 1 不能简化.

I expect the answer to be a + cd + ec + 1. And before someone suggests, I don't want to do a :> 1 - b, because for aesthetic purposes, I'd like to have both a and b in my equation as long as the a+b = 1 simplification cannot be made.

此外,如何让它替换 1-b-b+1-1+b 的所有实例, b-1 分别与 a-a ,反之亦然?

In addition, how do I get it to replace all instances of 1-b, -b+1 or -1+b, b-1 with a or -a respectively and vice versa?

这是这部分的一个例子:

Here's an example for this part:

expr = b + c (1 - a) + (-1 + b)(a - 1) + (1 -a -b) d + 2 a

推荐答案

您可以使用自定义版本的 FullSimplify,方法是向 FullSimplify 提供您自己的转换并让它计算出来出细节:

You can use a customised version of FullSimplify by supplying your own transformations to FullSimplify and let it figure out the details:

In[1]:= MySimplify[expr_,equivs_]:= FullSimplify[expr,
          TransformationFunctions ->
            Prepend[
              Function[x,x-#]&/@Flatten@Map[{#,-#}&,equivs/.Equal->Subtract],
              Automatic
            ]
        ]
In[2]:= MySimplify[2a+b+c*d+e*c, {a+b==1}]
Out[2]= a + c(d + e) + 1

equivs/.Equal->Subtract 将给定的方程变成等于 0 的表达式(例如 a+b==1 -> a+b-1).Flatten@Map[{#,-#}&, ] 然后构造否定版本并将它们展平成一个列表.函数[x,x-#]&/@ 将零表达式转换为函数,该函数从稍后由 提供给它们的 (x) 中减去零表达式 (#)完全简化.

equivs/.Equal->Subtract turns given equations into expressions equal to zero (e.g. a+b==1 -> a+b-1). Flatten@Map[{#,-#}&, ] then constructs also negated versions and flattens them into a single list. Function[x,x-#]& /@ turns the zero expressions into functions, which subtract the zero expressions (the #) from what is later given to them (x) by FullSimplify.

如果您对简单的想法与 FullSimplify 的默认设置不同,则可能还需要为 FullSimplify 指定您自己的 ComplexityFunctioncode>ComplexityFunction(大致相当于LeafCount),例如:

It may be necessary to specify your own ComplexityFunction for FullSimplify, too, if your idea of simple differs from FullSimplify's default ComplexityFunction (which is roughly equivalent to LeafCount), e.g.:

MySimplify[expr_, equivs_] := FullSimplify[expr,
  TransformationFunctions ->
    Prepend[
      Function[x,x-#]&/@Flatten@Map[{#,-#}&,equivs/.Equal->Subtract],
      Automatic
    ],
  ComplexityFunction -> (
    1000 LeafCount[#] + 
    Composition[
      Total,Flatten,Map[ArrayDepth[#]#&,#]&,CoefficientArrays
    ][#] &
  )
]

在您的示例中,默认的 ComplexityFunction 工作正常.

In your example case, the default ComplexityFunction works fine, though.

这篇关于关于“智能"的问题在数学中替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆