简体   繁体   English

具有mpfr系数的对称多项式?

[英]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
  • When converting, RR uses RR.precision and ignores the argument's precision. 转换时, RR使用RR.precision并忽略参数的精度。

Solution: 解:

  1. Coefficients must be a Sympy type (eg sympy.Float , which has extended precision). 系数必须是Sympy类型(例如sympy.Float ,具有扩展的精度)。
  2. For the domain, one of the following: 对于域,请使用以下之一:
    • Set sympy.polys.domain.RR._context.prec = my_precision . 设置sympy.polys.domain.RR._context.prec = my_precision
    • Pass domain='EX' , which doesn't do conversion. 通过domain='EX' ,不进行转换。
    • Pass in a custom domain (such as sympy.polys.domains.realfield.RealField(150) ). 传递自定义域(例如sympy.polys.domains.realfield.RealField(150) )。

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

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