# Sympy Sagemath integration bug? Anonymous

The following piece of code (never mind what it does) that uses sympy runs fine:

# some code...
import numpy as np
import sympy as sp
import matplotlib.pyplot as plt

phi_1_NN, phi_2_NN = sp.symbols('general_NN test', cls=sp.Function)
A11,A21,b,C11,C12,d = sp.symbols('A11 A21 b C11  C12 d')
U11,U21,v,W11,W12,r = sp.symbols('U11 U21 v W11 W12 r')
mylambda = sp.symbols('lambda')
x = sp.symbols('x')
phi_1_NN =(C11*A11**3+C12*A21**3)*x**3 + (3*b*C11*A11**2+3*b*C12*A21**2)*x**2 + (3*b**2*C11*A11+3*b**2*C12*A21)*x+b**3*C11+b**3*C12+d
sp.poly(phi_1_NN,x)
phi_2_NN =(W11*U11**3+W12*U21**3)*x**3 + (3*v*W11*U11**2+3*v*W12*U21**2)*x**2 + (3*v**2*W11*U11+3*v**2*W12*U21)*x+v**3*W11+v**3*W12+r
sp.poly(phi_2_NN,x)
final_expr = mylambda *phi_1_NN+ (mylambda -1)*phi_2_NN
sp.poly(final_expr,x)
arr_linspace = []
for i in range(6):
arr_linspace.append(np.linspace(int(-2.), int(2.), int(4.)))
mesh = np.meshgrid(*arr_linspace, indexing='ij')

arr_flat = []
for m in mesh:
arr_flat.append(m.flatten('C'))
mat_high_dim = np.stack((np.array(arr_flat)),axis = -1)


But when I run in the next cell this code, Sagemath throws a sympy error. But when I run the piece of code at home, offline, instead of the CoCalc Server, it runs fine.

# UNTIL HERE EVERYTHING RUNS FINE
# BUT IF I RUN THE CODE BELOW IN MY MACHINE AT HOME IT WORKS, BUT IF I RUN IT IN SAGE, A SYMPY ERROR IS RETURNED (EVEN THOUGH AT HOME IT RUNS FINE)
l = sp.symbols('l')
rhs_arr = []
for vals in mat_high_dim:
vals = list(map(sp.sympify,vals))
rhs_arr.append(final_expr.subs({A11:1, U11:vals, A21:0, U21:vals,b:1, v:vals, C11:1, W11:vals, C12:-2, W12:vals,d:3,r:vals, mylambda:l}))


Is this some kind of bug that Sagemath has, not having integrated sympy properly? (I tried everything to get Sagemath to work, changed the software environment to different versions of Ubuntu, bought a CoCalc licence; nothing worked)

edit retag close merge delete

What bloody kind of fucking sympy error do you get ?

There is an error message FOR A REASON... Asking for help and not giving it is requiring your potential helpers to solve a riddle before even starting to attack the real problem...

@EmmanuelCharpentier Sorry for creating such a strong emotional reaction. The error message by sympy was "ValueError: 1 is not an integer" - unfortunately something that didn't help me at all, so I figured there was no use in including it. Here is a link to the traceback (which is really): https://we.tl/t-JECpwPI0qp

Sort by » oldest newest most voted

This is likely a case of SymPy expecting Python integers and getting Sage integers.

Are you running this in both cases in a Jupyter sheet?

Is it using the SageMath Jupyter kernel in both cases?

Is it using the same version of SageMath?

Does it help to force some of the integers to be Python integers as below?

l = sp.symbols('l')
rhs_arr = []
for vals in mat_high_dim:
vals = list(map(sp.sympify, vals))
rhs_arr.append(final_expr.subs(
{A11: int(1),
U11: vals,
A21: int(0),
U21: vals,
b: int(1),
v: vals,
C11: int(1),
W11: vals,
C12: int(-2),
W12: vals,
d: int(3),
r: vals,
mylambda: l}))

more

Not really :(. The problem are expression such as vals. If I remove those than everything runs fine. I tried sympifying everything, but that didn't work either.