Consider the following polynomial expression.
Numerical_Precision =200
xt= Symbol("xt" , real=True)
xt_b=-0.121
test_exp = xt**2+xt +1
The question is how to expand this polynomial expression with high accuracy.
method_1= test_exp.subs(xt, xt+xt_b).expand().evalf(Numerical_Precision )
#display(method_1)
method_2= test_exp.subs(xt, xt+xt_b).evalf(Numerical_Precision ).expand() # in each case, more preciece
#display(method_2)
#display(method_1-method_2)
At xt_b=0.121
, the correct constant term ought to be 1.1356411
, and
#float 1.135641
#method_1 1.1356409999999999005382278483011759817600250244140625
#method_2 1.135640999999999995587529610929777858362092781876032636587657044524579674771302961744368076324462890625
At xt_b=-0.121
, the correct constant term ought to be 0.893641
, and
#float 0.8936409999999999
#method_1 0.89364100000000001866595766841783188283443450927734375
#method_2 0.893641000000000002692956968530779717073335457657282636587657044524579674771302961744368076324462890625
It appeared that there’s no control over the precision when using the .expand()
method, and in each case, the .evalf(Numerical_Precision ).expand()
allowed high precision computation.
However, the former implied a potential reduction of precision at .expand()
method, and I wonder if there’s a way to increase the precision.
In a further investigation, at the Numerical_Precision =200
and xt_b=-0.121
, the accuracy for the more accurate method_2
had already deviate at 0.893641000000000002692
the 19
th digit. The increase of Numerical_Precision
for method_2
seemed to have been limited by the .expand()
, where at Numerical_Precision =20
, the constant term for method_2
had already been 0.89364100000000000269
.
Is there a way to increase the precision of .expand()
method in sympy? I tried .expand(Numerical_Precision)
but it didn’t affect the result.