[英]Sympy polynomials with `mpfr` coefficients?
I want to use Sympy's polynomials, but I also want to use higher-precision coefficients. 我想使用Sympy的多项式,但我也想使用更高的系数。
Just Doing It seems to give me polynomials with sympy.core.numbers.float
coefficients. 只是在做这似乎给了我多项式与
sympy.core.numbers.float
系数。
import sympy
from sympy import Poly
from sympy.abc import x
from gmpy2 import mpfr, get_context
get_context().precision = 150
#float64 can't tell this from 1.0
one_and_change = mpfr('1.0000000000000000000000000000000000001')
#mpfr('1.0000000000000000000000000000000000001000000005',150)
p = [one_and_change]
px = Poly(p, x)
print(px)
# Poly(1.0, x, domain='RR')
print(px.is_one)
# True
print(type(px.all_coeffs()[0]))
# <class 'sympy.core.numbers.Float'>
I've also tried sympy.mpmath.mpf
, with the same results. 我也尝试了
sympy.mpmath.mpf
,结果相同。
This also didn't work:[ 1 ] 这也不起作用:[ 1 ]
domain = sympy.polys.domains.realfield.RealField(150)
px = Poly(p, x, domain=domain)
print(type(px.all_coeffs()[0]))
# <class 'sympy.core.numbers.Float'>
There are a few obstacles: 有一些障碍:
gmpy.mpfr
has no ._sympy_
method, so it will be converted to float in an intermediate step. gmpy.mpfr
没有._sympy_
方法,因此它将在中间步骤中转换为float。 sympy.Poly
, by default, uses sympy.polys.domain.RR
for floating point coefficients, and will use it to convert. sympy.Poly
默认情况下,将sympy.polys.domain.RR
用于浮点系数,并将用于转换。 RR
, when it loads, uses 53 as its precision, and ignores mpmath.mp.precision
. RR
加载时使用53作为其精度,并忽略mpmath.mp.precision
。 RR
uses RR.precision
and ignores the argument's precision. RR
使用RR.precision
并忽略参数的精度。 Solution: 解:
sympy.Float
, which has extended precision). sympy.Float
,具有扩展的精度)。 sympy.polys.domain.RR._context.prec = my_precision
. sympy.polys.domain.RR._context.prec = my_precision
。 domain='EX'
, which doesn't do conversion. domain='EX'
,不进行转换。 sympy.polys.domains.realfield.RealField(150)
). sympy.polys.domains.realfield.RealField(150)
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.