考虑一下这个简单的MWE:
from sympy.solvers.diophantine import diophantine
from sympy import symbols
x, y, z = symbols("x, y, z", integer=True)
diophantine(x*(2*x + 3*y - z))这一产出如下:
[(t_0, t_1, 2*t_0 + 3*t_1), (0, n1, n2)]--如果我想要创建这些解决方案的实例,例如,我希望能够将整数值替换为t_0和t_1。
我试过了。
diof = diophantine(x*(2*x + 3*y - z))
list(diof)[0][0].subs(t_0, 0)但这给了
NameError: name 't_0' is not defined发布于 2021-10-06 19:29:28
t_0不被定义为Python变量。您可以通过几种不同的方式创建Python变量:
from sympy import symbols
t_0 = symbols("t_0")或者,您可以将从SymPy计算中生成的符号捕获到Python变量中。有一种方法:
diofl = list(diof) # Assuming that diof is as you had defined it.
t_0 = diofl[1][0] # Presuming that this is the part of diofl
# that contains the correct symbol. Print
# diofl[1][0] to be sure!然后,您可以使用t_0作为subs()方法的参数。
https://stackoverflow.com/questions/69471307
复制相似问题