如何根据 sympy 中的其他表达式重写表达式 [英] How to rewrite an expression in terms of an other expression in sympy

查看:37
本文介绍了如何根据 sympy 中的其他表达式重写表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是在问如何根据给定的变量求解方程(如在这个

A.rewrite(B)

A 和 B 可能是相当复杂的表达式.作为参考,这是我的真实案例:

import sympy as spsp.init_printing(use_unicode=True)t, w, r = sp.symbols('t w r')S = sp.Function('S')(t)V = (S-w*(1+r)**t)/(((1+r)**t)-1)伏

St = -(r + 1)**t*(w - S)*sp.log(r + 1)/((r + 1)**t - 1)英石

一旦我用 V 来写 St,我应该能够简化得到

St = rS(t)+rV

但我无法在 SymPy 中做到这一点.

解决方案

首先要注意,当你做类似的事情时

a,b,c = sp.symbols('a b c')A = a+b+cB = a+c

变量 AB不是 Sympy 可以理解和操作的新 Sympy 符号,而是 Sympy 表达式的别名a+b+ca+c 分别.因此,A.subs(a+c,B)A.subs(a+c,a+c) 本质上是一样的,当然,无意义的.你明白为什么 A.rewrite(B) 也没有用了.

我认为像 expr.subs({complicated_mutlivariable_formula: new_variable}) 这样的调用在 Sympy 中不起作用.做你想做的一种方法是首先解决方程 complicated_mutlivariable_formula = new_variable 关于旧"变量之一,并假设存在唯一的解决方案,使用 subs() 替换此变量.

将此方法应用于第二个示例:

# sympy 符号 A 将用于表示表达式 VA = sp.symbols('A')# 求解关于 w 的方程 V==A,它具有作为 A 函数的唯一解w_A = sp.solve(sp.Eq(V,A), w)[0]# 现在替换 wSt.subs({w:w_A}).simplify()

EDIT: I am not asking how to solve an equation in terms of a given variable (as in this supposed duplicated question), but how to represent an expression in terms of an other one, as specified in the question. I believe it is the "duplicated" question to have a misleading title.

I am very new with SymPy. I have an expression that, once expressed in terms to an other expression, should become very nice. The problem is that I don't know how to "force" to express the original expression in terms of the other one.

This is a basic example:

import sympy as sp
sp.init_printing(use_unicode=True)
a,b,c =  sp.symbols('a b c')
A = a+b+c
B = a+c
C = A.subs(a+c,B) #  Expected/wanted: C = B+b
C

A.rewrite(B)

A and B could be rather complex expressions. For reference, this is my real-case scenario:

import sympy as sp
sp.init_printing(use_unicode=True)
t, w, r = sp.symbols('t w r')
S = sp.Function('S')(t)
V = (S-w*(1+r)**t)/(((1+r)**t)-1)
V

St = -(r + 1)**t*(w - S)*sp.log(r + 1)/((r + 1)**t - 1)
St 

Once I write St in terms of V, I should be able to simplify to get just

St = rS(t)+rV

But I am unable to do it in SymPy.

解决方案

First note that when you do something like

a,b,c =  sp.symbols('a b c')
A = a+b+c
B = a+c

variables A, B are not new Sympy symbols that Sympy can understand and operate on, rather, they are aliases for the Sympy expressions a+b+c and a+c, respectively. Therefore, A.subs(a+c,B) is essentially the same as A.subs(a+c,a+c), which is, of course, meaningless. You get the idea of why A.rewrite(B) is also of no use.

I do not think that calls like expr.subs({complicated_mutlivariable_formula: new_variable}) work in Sympy. One way to do what you want is to first solve the equation complicated_mutlivariable_formula = new_variable with respect to one of the "old" variables, and, assuming a unique solution exist, use subs() to substitute this variable.

Applying this approach for the second example:

# sympy Symbol A will be used to represent expression V
A = sp.symbols('A') 

# Solve the equation V==A with respect to w, which has a unique solution as a function of A
w_A = sp.solve(sp.Eq(V,A), w)[0] 

# Now substitute w 
St.subs({w:w_A}).simplify()

这篇关于如何根据 sympy 中的其他表达式重写表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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