问题描述
为了计算导数和其他表达式,我使用了 sympy 包并说 T = sy.Symbol('T')
现在我已经计算了正确的表达式:
E= -T**2*F_deriv_T(T,rho)
哪里
def F_deriv_rho(T,rho):ret = 0对于范围内的 n(5):对于范围内的 m(4):内部= c[n,m]*g_rho_deriv_rho_np*g_T_npret += 内部返回 ret
看起来像这样:
F_deriv_rho: [0.0 7.76971e-5*T 0.0001553942*T**2*rhoT*(-5.14488e-5*log(rho) - 5.14488e-5)*log(T) + T*(1.22574e-5*log(rho)+1.22574e-5)*log(T) + T*(1.89488e-5*log(rho) + 1.89488e-5)*log(T) + T(2.29441e-5*log(rho) + 2.29441e-5)*log(T) + T*(7.49956)e-5*log(rho) + 7.49956e-5)*log(T)T**2*(-0.0001028976*rho*log(rho) - 5.14488e-5*rho)*log(T) + T**2*(2.45148e-5*rho*log(rho) + 1.22574e-5*rho)*log(T) + T**2*(3.78976e-5*rho*log(rho) + 1.89488e-5*rho)*log(T) + T**2*(4.58882e-5*rho*log(rho) + 2.29441e-5*rho)*log(T) + T**2*(0.0001499912*rho*log(rho) + 7.49956e 5*rho)*log(T)]代码>
使用 python 我想将 T(和 rho)作为一个符号更改为一个值.我怎么能这样做?
所以,我想创建 10 个数字,例如 T_def = np.arange(2000, 10000, 800)
并交换我所有的 sy.symbol(
T)
通过迭代我在数组中创建的 10 个值.
感谢您的帮助
我根据这篇文章找到了解决方案:
通过使用subs":
>>>从 sympy 导入符号>>>x, y = Symbol('x y')>>>f = x + y>>>f.subs({x:10, y: 20})>>>F30这里有更多关于这种事情的内容:http://docs.sympy.org/最新/教程/basic_operations.html
更快的方法是使用@Bjoern Dahlgren 建议的lamdify"
In order to calculate derivatives and other expressions I used the sympy package and said that T = sy.Symbol('T')
now that I have calculated the right expression:
E= -T**2*F_deriv_T(T,rho)
where
def F_deriv_rho(T,rho):
ret = 0
for n in range(5):
for m in range(4):
inner= c[n,m]*g_rho_deriv_rho_np*g_T_np
ret += inner
return ret
that looks like this:
F_deriv_rho: [0.0 7.76971e-5*T 0.0001553942*T**2*rhoT*(-5.14488e-5*log(rho) - 5.14488e-5)*log(T) + T*(1.22574e-5*log(rho)+1.22574e-5)*log(T) + T*(1.89488e-5*log(rho) + 1.89488e-5)*log(T) + T(2.29441e-5*log(rho) + 2.29441e-5)*log(T) + T*(7.49956e-5*log(rho) + 7.49956e-5)*log(T) T**2*(-0.0001028976*rho*log(rho) - 5.14488e-5*rho)*log(T) + T**2*(2.45148e-5*rho*log(rho) + 1.22574e-5*rho)*log(T) + T**2*(3.78976e-5*rho*log(rho) + 1.89488e-5*rho)*log(T) + T**2*(4.58882e-5*rho*log(rho) + 2.29441e-5*rho)*log(T) + T**2*(0.0001499912*rho*log(rho) + 7.49956e 5*rho)*log(T)]
with python I would like to change T (and rho) as a symbol to a value. How could I do that?
So, I would like to create 10 numbers like T_def = np.arange(2000, 10000, 800)
and exchange all my sy.symbol(
T)
by iterating through the 10 values I created in the array.
Thanks for your help
I have found the solution according to this post:
How to substitute multiple symbols in an expression in sympy?
by usings "subs":
>>> from sympy import Symbol
>>> x, y = Symbol('x y')
>>> f = x + y
>>> f.subs({x:10, y: 20})
>>> f
30
There's more for this kinda thing here: http://docs.sympy.org/latest/tutorial/basic_operations.html
EDIT: A faster way would be by using "lamdify" as suggested by @Bjoern Dahlgren
这篇关于python:更改符号变量并分配数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!