英文:
Simplify an expression with the given substitution of another expression
问题
假设一个表达式 expr2=expr3(expr1*expr2)+expr2...
如何进行简化或替换,以便可以用单个字母 A
替代 expr1
?
from sympy import Function
f = Function('f')
from sympy import *
x, y = symbols('x y', real=True)
A, B, C = symbols('A B C')
a = 1/f(x)
b = (1/f(x)).diff(x)
c = (1/f(x)).diff(x, 2)
func = 1/f(x)*(1-f(x)**2*y)
expr = func.diff(x, 2)
其中
expr
(-2*y*(f(x)*Derivative(f(x), (x, 2)) + Derivative(f(x), x)**2) + 4*y*Derivative(f(x), x)**2 + (y*f(x)**2 - 1)*(Derivative(f(x), (x, 2)) - 2*Derivative(f(x), x)**2/f(x))/f(x))/f(x)
显然,expr
包含表达式 c
c
(-Derivative(f(x), (x, 2)) + 2*Derivative(f(x), x)**2/f(x))/f(x)**2
然而,当我尝试以下操作时
expr.subs(c, C)
expr.expand().subs(c, C)
expr.simplify().subs(c, C)
expr.expand().simplify().subs(c, C)
这些尝试都没有成功。
如何使用给定的表达式替换来简化表达式?
英文:
Suppose an expression expr2=expr3(expr1*expr2)+expr2...
How to make a simplification or substitution such that expr1
can be replaced by a single letter A
?
from sympy import Function
f= Function('f')
from sympy import *
x,y=symbols('x y',real=True)
A,B,C=symbols("A B C")
a=1/f(x)
b=(1/f(x)).diff(x)
c=(1/f(x)).diff(x,2)
func=1/f(x)*(1-f(x)**2*y)
expr=func.diff(x,2)
where
expr
(-2*y*(f(x)*Derivative(f(x), (x, 2)) + Derivative(f(x), x)**2) + 4*y*Derivative(f(x), x)**2 + (y*f(x)**2 - 1)*(Derivative(f(x), (x, 2)) - 2*Derivative(f(x), x)**2/f(x))/f(x))/f(x)
Clearly, expr
contain the expression c
c
(-Derivative(f(x), (x, 2)) + 2*Derivative(f(x), x)**2/f(x))/f(x)**2
However, when I tried
expr.subs(c,C)
expr.expand().subs(c,C)
expr.simplify().subs(c,C)
expr.expand().simplify().subs(c,C)
None of those attempts worked.
How to simplify an expression with the given substitution of another expression?
答案1
得分: 1
以下是代码的中文翻译:
from sympy import *
init_printing()
f = Function('f')
x, y = symbols('x y', real=True)
A, B, C = symbols("A B C")
a = 1 / f(x)
b = (1 / f(x)).diff(x)
c = (1 / f(x)).diff(x, 2)
func = 1 / f(x) * (1 - f(x) ** 2 * y)
expr = func.diff(x, 2)
expr
让我们看看 c
:
c
显然,expr
不包含 c
,不直接包含。您可以使用 expr.has(c)
来验证,它会返回 False。我们可以看到 c
的分子(带有反号),但分母应用于不同的表达式。这就是为什么 SymPy 在 expr
中看不到 c
:它们在结构上不同。但我们可以处理这个问题。
num, den = fraction(c)
num, den
现在,我们寻找 -num
并用 C * den
替换它:
new_expr = expr.subs(-num, C * den)
new_expr
您可以再进一步:new_expr
是 1/f(x)
与一个加法的乘法。因此,我们可以应用乘法的分配性质:
new_expr2 = Add(*[1 / f(x) * a for a in new_expr.args[1].args])
new_expr2
在这里,您可以验证 new_expr2.equals(new_expr)
。
英文:
from sympy import *
init_printing()
f= Function('f')
x,y=symbols('x y',real=True)
A,B,C=symbols("A B C")
a=1/f(x)
b=(1/f(x)).diff(x)
c=(1/f(x)).diff(x,2)
func=1/f(x)*(1-f(x)**2*y)
expr=func.diff(x,2)
expr
Let's look at c
:
c
Clearly, expr
doesn't contain c
, not directly. You can verify it with expr.has(c)
which returns False. We can spot the numerator of c
(with the sign flipped), but the denominator is applied to different expressions. That's why SymPy doesn't see c
into expr
: they are structurally different. But we can work on that.
num, den = fraction(c)
num, den
Now, we look for -num
and replace it with C * den
:
new_expr = expr.subs(-num, C * den)
new_expr
You can go a step further: new_expr
is a multiplication between 1/f(x)
and an addition. Hence, we can apply the distributive property of multiplication:
new_expr2 = Add(*[1 / f(x) * a for a in new_expr.args[1].args])
new_expr2
Here, you can verify that new_expr2.equals(new_expr)
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论