简体   繁体   English

使用 sympy 重新排列方程式?

[英]Using sympy to rearrange equation?

Im trying to rearrange a supply curve equation for calculation the price elasticy.我试图重新安排供应曲线方程来计算价格弹性。

The equation is log P = -2 + 1.7 * log Q .等式是log P = -2 + 1.7 * log Q

Im trying to rearrange the equation so that log Q is in terms of log P. Is there a way sympy can handle these rearrangements?我正在尝试重新排列方程式,以便 log Q 是 log P 的形式。sympy 有没有办法处理这些重新排列?

p, q = sp.symbols('p, q', real=True, positive=True)

eq = sp.Eq(-2+1.7*sp.log(q))

sp.solve(eq,sp.log(p))

Using sympy solve will take care of the arrangements.使用 sympy solve 将负责安排。 The equations just must be set up properly and the function parameters must be correct.方程式必须正确设置并且 function 参数必须正确。

import sympy as sp

p, q = sp.symbols('p, q', real=True, positive=True)
eq = sp.Eq(-2+1.7*sp.log(q), p) # set equation = to p
ans = sp.solve(eq,q) #solve equation for q in terms of p

This solution simply saves your equation to eq and solves eq for q so the answer is in terms of only p.此解决方案只是将您的方程式保存为 eq 并求解 eq 以获得 q,因此答案仅以 p 为单位。 In the end ans holds the value:最后 ans 持有的价值:

[3.24290842192454*exp(0.588235294117647*p)]

I highly reccomend reading this website before coming to SO.我强烈建议在访问 SO 之前先阅读网站。

Although you showed log P in the written text, you did not include it in the code.虽然您在书面文本中显示了log P ,但您没有将其包含在代码中。 If you do so, you can then request that solve isolate log(q) for you:如果这样做,您可以请求为您solve isolate log(q)问题:

>>> eq = Eq(log(p), -2 + 10*log(q)/17)
>>> solve(eq, log(q))
[17*log(p)/10 + 17/5]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM