局部问题与局部问题的结果不同?

2024-04-24 19:02:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个方程要解。我得到一个正确的结果与SymPy现场控制台,这是在他们的网站上提供。然而,当我试图在本地计算机上重现相同的结果时,我得到了一个ConditionSet,我无法求解这个方程。你知道吗

完全相同的方程不能局部求解。我不确定除法(但我正在导入__future__.division)、缺少乳胶库或其他一些设置是否有问题?你知道吗

以下是来自SymPy live的命令:

Python console for SymPy 1.0 (Python 2.7.12)

These commands were executed:
>>> from __future__ import division
>>> from sympy import *
>>> x, y, z, t = symbols('x y z t')
>>> k, m, n = symbols('k m n', integer=True)
>>> f, g, h = symbols('f g h', cls=Function)

Warning: this shell runs with SymPy 1.0 and so examples pulled from other documentation may provide unexpected results.
Documentation can be found at http://docs.sympy.org/1.0.

>>> a = Eq(309.07*(1+x)**(-1/12) + 309.07*(1+x)**(-2/12)+309.07*(1+x)**(-3/12) + 309.07*(1+x)**(-4/12) + 309.07*(1+x)**(-5/12) + 309.07*(1+x)**(-6/12) + 309.07*(1+x)**(-7/12), 1500)
>>> solveset(a, x, domain=S.Reals)

乳胶打印结果:

          {2.17102109654962} 

这里是本地python脚本:

from __future__ import division                                                                                                                                                             
from sympy import *                                                                                                                                                                         

init_printing()                                                                                                                                                                             

x, y, z, t = symbols('x y z t')                                                                                                                                                             
k, m, n = symbols('k m n', integer=True)                                                                                                                                                    
f, g, h = symbols('f g h', cls=Function)                                                                                                                                                    

a = Eq(309.07*(1+x)**(-1/12) + 309.07*(1+x)**(-2/12)+309.07*(1+x)**(-3/12) + 309.07*(1+x)**(-4/12) + 309.07*(1+x)**(-5/12) + 309.07*(1+x)**(-6/12) + 309.07*(1+x)**(-7/12), 1500)           
b = solveset(a, x, S.Reals)                                                                                                                                                                 

print b

以ASCII文本打印的结果:

ConditionSet(x, Eq(309.07*(x + 1)**(-0.583333333333333) + 309.07*(x + 1)**(-0.5) + 309.07*(x + 1)**(-0.416666666666667) + 309.07*(x + 1)**(-0.333333333333333) + 309.07*(x + 1)**(-0.25) + 309.07*(x + 1)**(-0.166666666666667) + 309.07*(x + 1)**(-0.0833333333333333) - 1500, 0), (-oo, oo))

我正在使用Python 2.7SymPy 1.0 如果你能帮我。。。你知道吗


Tags: fromimporttruefunctionfutureinteger乳胶division
1条回答
网友
1楼 · 发布于 2024-04-24 19:02:59

我不知道是什么导致了不同的行为,但是如果你告诉sympy把你的指数当作sympy整数,那么你会得到与sympy Live相同的结果:

from __future__ import division                                                                                                                                                             
from sympy import *                                                                                                                                                                         

init_printing()                                                                                                                                                                             

x, y, z, t = symbols('x y z t')                                                                                                                                                             
k, m, n = symbols('k m n', integer=True)                                                                                                                                                    
f, g, h = symbols('f g h', cls=Function)                                                                                                                                                    

a = Eq(309.07*(1+x)**(-1/Integer(12)) + 309.07*(1+x)**   (-2/Integer(12))+309.07*(1+x)**(-3/Integer(12)) + 309.07*(1+x)**(-4/Integer(12)) + 309.07*(1+x)**(-5/Integer(12)) + 309.07*(1+x)**(-6/Integer(12)) + 309.07*(1+x)**(-7/Integer(12)), 1500)           
b = solveset(a, x, S.Reals)                                                                                                                                                                 

print b

至于为什么python整数在本地安装中不被视为sympy整数,我不知道。在我看来,SymPy Live似乎覆盖了/操作符。要澄清这种行为,请尝试

print(type(1/12))

在您的本地安装和SymPy Live中。局部结果将是<class 'float'>,SymPy Live结果将是<class 'sympy.core.numbers.Rational'>。你知道吗

相关问题 更多 >