Ask Your Question
0

Sympy Sagemath integration bug?

asked 2021-02-09 21:43:31 +0100

anonymous user

Anonymous

updated 2021-02-10 21:51:24 +0100

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[0], A21:0, U21:vals[1],b:1, v:vals[2], C11:1, W11:vals[3], C12:-2, W12:vals[4],d:3,r:vals[5], 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 flag offensive close merge delete

Comments

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...

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2021-02-10 09:11:24 +0100 )edit

@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

crazycats gravatar imagecrazycats ( 2021-02-10 21:50:46 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-02-11 00:27:31 +0100

slelievre gravatar image

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[0],
         A21: int(0),
         U21: vals[1],
         b: int(1),
         v: vals[2],
         C11: int(1),
         W11: vals[3],
         C12: int(-2),
         W12: vals[4],
         d: int(3),
         r: vals[5],
         mylambda: l}))
edit flag offensive delete link more

Comments

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

crazycats gravatar imagecrazycats ( 2021-02-15 19:53:28 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2021-02-09 21:43:31 +0100

Seen: 285 times

Last updated: Feb 11 '21