数学表达式中字符串替换类型的字符串操作
想象一下,
exp(49/200)+(x-49/200)
我想将任何不是加法或减法的操作作为函数“roundn”的参数传递 所以我的表情变成
roundn(exp(roundn(49/200, n)), n) + (x - roundn(49/200, n)
了我想要操纵的表情是这样的:
exp(49/200)+exp(49/200)*(x-49/200)+1/2*exp(49/200)*(x-49/200)^2+1/6*exp(49/200)*(x-49/200)^3+1/24*exp(49/200)*(x-49/200)^4+1/120*exp(49/200)*(x-49/200)^5+1/720*exp(49/200)*(x-49/200)^6+1/5040*exp(49/200)*(x-49/200)^7+1/40320*exp(49/200)*(x-49/200)^8+1/362880*exp(49/200)*(x-49/200)^9+1/3628800*exp(49/200)*(x-49/200)^10+1/39916800*exp(49/200)*(x-49/200)^11
Imagine something like
exp(49/200)+(x-49/200)
I want to pass as argument of the function "roundn" whatever operation that is not a addtion or a subtraction
So my expression became
roundn(exp(roundn(49/200, n)), n) + (x - roundn(49/200, n)
Well the expression I want to manipulate is this:
exp(49/200)+exp(49/200)*(x-49/200)+1/2*exp(49/200)*(x-49/200)^2+1/6*exp(49/200)*(x-49/200)^3+1/24*exp(49/200)*(x-49/200)^4+1/120*exp(49/200)*(x-49/200)^5+1/720*exp(49/200)*(x-49/200)^6+1/5040*exp(49/200)*(x-49/200)^7+1/40320*exp(49/200)*(x-49/200)^8+1/362880*exp(49/200)*(x-49/200)^9+1/3628800*exp(49/200)*(x-49/200)^10+1/39916800*exp(49/200)*(x-49/200)^11
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许你认为你想这样做,但你并不是真的想这样做。新 Python 用户通常认为他们需要对浮点数进行舍入,因为在计算时会得到意想不到的结果(例如 1.0/10 = 0.100000000000001)。我没有对表达式进行一些愚蠢的字符串替换,而是为
round(49/200,n)
创建了一个变量,并进行了一些格式清理。另外,exp(49/200) 不需要计算 13 次,只需计算一次并引用计算值即可。将 e 提高到四舍五入的数字几乎永远都不合适。同样,将四舍五入的数字提高到 11 次方。 (另请注意,在 Python 中,求幂运算符是
**
,而不是^
。)已编辑:
如果 S.Lott 没有建议代数简化,我就会保持原样。但是
* e_zz
可以从每个项中分解出来,从而给出更简单的(并且可能更快):Maybe you think you want to do this, but you don't really want to do this. New Pythoners usually think they need to round floating point numbers because when evaluated they get unexpected results (like 1.0/10 = 0.100000000000001). Rather than do some goofy string substitution on your expression, I just created a variable for
round(49/200,n)
, and did a little format cleanup. Alsoexp(49/200)
does not need to be evaluated 13 times, just do it once and refer to the computed value.Raising e to a rounded number is almost never appropriate. Likewise for raising a rounded number to the 11'th power. (Note also that in Python, the exponentiation operator is
**
, not^
.)Edited:
If S.Lott hadn't suggested the algebraic simplification, I would have left this as-is. But the
* e_zz
can be factored out of every term, giving the simpler (and probably faster):使用这个
http://sympy.org/
Use this
http://sympy.org/
我想知道这是否是您所需要的:
如果原始方程位于字符串变量
eq
中,您可以使用 replace 字符串方法:类似的表达式可以将
roundn
放在exp()
周围函数(这里可能需要一些漂亮的正则表达式)。I wonder whether this is what you need:
If you original equation is in the string variable
eq
you can create your new equations using the replace method of strings:and a similar expression could put
roundn
around theexp()
function (possibly need some nifty regex here).您可以将
p=re.compile(r'\d+/\d+')
的每个匹配项与 roundn 函数的输出相匹配。仅供参考,近似表达式中的这么多项将产生一个不一定非常接近实际结果的结果,具体取决于您四舍五入的位数。You could sub every match of say,
p=re.compile(r'\d+/\d+')
with the output of your roundn function. FYI, approximating so many terms in you expression will yield a result that is not necessarily very close to the actual result, depending on how many digits you round to.