刻意简化分数指数 [英] Deliberately simplifying fractional exponents

查看:23
本文介绍了刻意简化分数指数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个涉及分数指数的表达式,我想将其转化为可识别的多项式,以便 sympy 求解.如有必要,我可以使用 Rational 编写指数,但无法使其工作.

我能做什么?

<预><代码>>>>从 sympy 导入 *>>>var('d x')(d, x)>>>(0.125567*(d + 0.04) - d**2.25*(2.51327*d + 6.72929)).subs(d,x**4)0.125567*x**4 - (2.51327*x**4 + 6.72929)*(x**4)**2.25 + 0.00502268

解决方案

SymPy 不会组合指数,除非它知道这样做是安全的.对于复数,只有整数指数才是安全的.因为我们不知道 x 是实数还是复数,所以指数没有合并.

即使对于实数 x,(x**4)**(9/4) 也与 x**9 不同(考虑负 x).如果 x 被声明为实数,使用 x = Symbol('x', real=True),则 (x**4)**Rational(9,4) 正确返回 x**8*Abs(x) 而不是 x**9.

如果 x 被声明为正数,x = Symbol('x', positive=True),则 (x**4)**Rational(9, 4) 返回 x**9.

<小时>

在 SymPy 中使用有理数的浮点表示是不可取的,尤其是作为指数.这就是我将 2.25 替换为上面的 Rational(9, 4) 的原因.对于 2.25,当 x 为实数时,结果为 Abs(x)**9.0,如果 x 为正数,结果为 x**9.0.小数点表示这些是浮点数;所以后续的操作将有浮点结果而不是符号结果.例如(x 声明为正):

<预><代码>>>>解决((x**4)**Rational(9, 4) - 2)[2**(1/9)]>>>解决((x**4)**2.25 - 2)[1.08005973889231]

I have an expression involving fractional exponents that I want to make into a polynomial recognisable to sympy for solution. I could, if necessary, write the exponents using Rational but can't make that work.

What can I do?

>>> from sympy import *
>>> var('d x')
(d, x)
>>> (0.125567*(d + 0.04) - d**2.25*(2.51327*d + 6.72929)).subs(d,x**4)
0.125567*x**4 - (2.51327*x**4 + 6.72929)*(x**4)**2.25 + 0.00502268

解决方案

SymPy does not combine exponents unless it knows it is safe to do so. For complex numbers it's only safe with integer exponents. Since we don't know if x is real or complex, the exponents are not combined.

Even for real x, (x**4)**(9/4) is not the same as x**9 (consider negative x). If x is declared real, using x = Symbol('x', real=True), then (x**4)**Rational(9, 4) correctly returns x**8*Abs(x) instead of x**9.

If x is declared positive, x = Symbol('x', positive=True), then (x**4)**Rational(9, 4) returns x**9.


It is not advisable to use floating point representation of rational numbers in SymPy, especially as exponents. This is why I replaced 2.25 by Rational(9, 4) above. With 2.25, the result is Abs(x)**9.0 when x is real, and x**9.0 if x is declared positive. The decimal dot indicates these are floating point numbers; so subsequent manipulations will have floating-point results instead of symbolic ones. For example (with x declared positive):

>>> solve((x**4)**Rational(9, 4) - 2)
[2**(1/9)]
>>> solve((x**4)**2.25 - 2)
[1.08005973889231]

这篇关于刻意简化分数指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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