Two polynomials multiply incorrectly only on my computer
I am using Docker Desktop on Windows 11 to run Sagemath with Macaulay package. The polynomials "F[0]" and "xd" below multiply into "dat" incorrectly (when compared to the correct result on another computer.)
I tried to obtain a minimal example illustrating my problem:
Source Code
import numpy as np
import sympy as sp
import itertools
alpha_2, alpha_01, x_0, x_1, x_2 = var('alpha_2 alpha_01 x_0 x_1 x_2')
y=np.array([x_0, x_1, x_2])
f=np.zeros(shape=(3),dtype=object)
f[0]=alpha_01*(x_0 + x_1 + x_2 )*(x_1 + x_2 ) + alpha_2*x_1*x_2 + (x_0*x_1 + x_0*x_2 + x_1*x_2 )*alpha_01 + 3*(x_0 + x_1 + x_2)^2
f[1]=alpha_01*(x_0 + x_1 + x_2 )*(x_0 + x_2 ) + alpha_2*x_0*x_2 + (x_0*x_1 + x_0*x_2 + x_1*x_2 )*alpha_01 + 3*(x_0 + x_1 + x_2)^2
f[2]=alpha_01*(x_0 + x_1 + x_2 )*(x_0 + x_1 ) + alpha_2*x_0*x_1 + (x_0*x_1 + x_0*x_2 + x_1*x_2 )*alpha_01 + 3*(x_0 + x_1 + x_2)^2
K=[4,1,1]
list2=[0,1,2]
F=np.zeros(shape=(3),dtype=object)
for p in range(3):
b=list2[p]
g=(f[b])^K[b]/y[b]^(2*K[b])*(1/K[b])
F[p]=(-g.subs({x_0:1})).simplify() #F[i]=\hat{f}_i
A=sp.zeros(int(2),int(2))
for i, j in itertools.product(range(2),range(2)):
A[i,j]=diff( F[i+1], y[list2[j+1]] )
xd=det(A)
xd=1*xd #turns type <class 'sympy.core.add.Add'> into <class 'sage.symbolic.expression.Expression'>
#https://doc.sagemath.org/html/en/reference/calculus/sage/calculus/test_sympy.html
print("F[0]=",F[0].expand())
print( " ---------------------------------- " )
print("xd=",xd.expand())
dat=F[0]*xd
dat=expand(dat)
print( " ---------------------------------- " )
print("DAT=F[0]*xd=",dat)
This program works fine on another computer. The result I get on my computer has a summand "-alpha_01^6x_1^11x_2^3". In a correct expansion of "dat", the summand "...+ -alpha_01^6x_1^11x_2^3+..." must not appear.
Note: I tried using polynomial rings instead of symbolic variables, but differentiation operation is not defined for polynomial rings.
It appears to be an issue with symbolic calculation. However, there seems to be no point to deal with symbolic expressions here, while you in fact are working with polynomial/rational functions. I therefore suggest to replace
alpha_2, alpha_01, x_0, x_1, x_2 = var('alpha_2 alpha_01 x_0 x_1 x_2')
withR.<alpha_2, alpha_01, x_0, x_1, x_2> = QQ[]
and remove latersimplify()
andexpand()
calls as redundant. The issue should go away.Look at
As you recommended, I started with rational functions, and using derivatives and determinants that are already defined, I was able to obtain "dat" correctly! However, to obtain the coefficient of 1/x_1, I still need to import sympy and convert "dat" into sympy.core.mul.Mul, which makes me wonder if there is an easier way. To me, it seems like symbolic expressions are better in some respects and polynomial functions are better in other respects and it is possible to convert them into one another, e.g. via sympify().
I've added an answer with more details,in particular explaining how to obtain the coefficient of
1/x_1
. In fact, you don't neednumpy
andsympy
here - Sage can handle everything more efficiently and in consistent manner.