Ask Your Question

Revision history [back]

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.

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.

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.