我在使用 SymPy nonlinsolve 时收到 NameError。在阅读了很多帖子后我认为可能与类型和/或语法有关,但我找不到确切原因,我几天前刚刚安装了 sympy,我的 python 版本是 Python 3.5.3(默认,2017 年 1 月 19 日, 14:11:04) [GCC 6.3.0 20170118] 在 Linux 上 提前致谢 维尔比约格
# Python 3 script , using SymPy library, parametrizatrion of the 3-sphere and rotations using quaternion multiplication
# python3 three_sphere.py
from sympy import *
def qmul(x0, x1, x2, x3, y0, y1, y2, y3):
z0 = x0*y0 - x1*y1 - x2*y2 - x3*y3
z1 = x0*y1 + x1*y0 + x2*y3 - x3*y2
z2 = x0*y2 - x1*y3 + x2*y0 + x3*y1
z3 = x0*y3 + x1*y2 - x2*y1 + x3*y0
return z0, z1, z2, z3
r1, s1, t1, r2, s2, t2 = symbols('r1, s1, t1, r2, s2, t2')
a0 = 2*r1/(1 + r1*r1 + s1*s1 + t1*t1)
a1 = 2*s1/(1 + r1*r1 + s1*s1 + t1*t1)
a2 = 2*t1/(1 + r1*r1 + s1*s1 + t1*t1)
a3 = (1 - r1*r1 - s1*s1 - t1*t1)/(1 + r1*r1 + s1*s1 + t1*t1)
b0 = 2*r2/(1 + r2*r2 + s2*s2 + t2*t2)
b1 = 2*s2/(1 + r2*r2 + s2*s2 + t2*t2)
b2 = 2*t2/(1 + r2*r2 + s2*s2 + t2*t2)
b3 = (1 - r2*r2 - s2*s2 - t2*t2)/(1 + r2*r2 + s2*s2 + t2*t2)
c0, c1, c2, c3 = qmul(a0, a1, a2, a3, b0, b1, b2, b3)
c0 = simplify(c0)
c1 = simplify(c1)
c2 = simplify(c2)
c3 = simplify(c3)
print(c0)
print(" ")
print(c1)
print(" ")
print(c2)
print(" ")
print(c3)
print(" ")
print(" ")
r3, s3, t3 = symbols('r3, s3, t3')
q0 = 2*r3/(1 + r3*r3 + s3*s3 + t3*t3)
q1 = 2*s3/(1 + r3*r3 + s3*s3 + t3*t3)
q2 = 2*t3/(1 + r3*r3 + s3*s3 + t3*t3)
q3 = (1 - r3*r3 - s3*s3 - t3*t3)/(1 + r3*r3 + s3*s3 + t3*t3)
#possibly syntax error here which causes NameError ??
soln = nonlinsolve([q0-c0, q1-c1, q2-c2, q3-c3], (r3, s3, t3))
# the idea is to have 4 equations : q0=c0, q1=c1. q2=c2. q3=c3 ; and solve for r3, s3 and t3 in terms of r1, s1, t1, r2, s2, t2
print(soln)
# http://docs.sympy.org/dev/tutorial/solvers.html
最佳答案
您正在使用没有 nonlinsolve
的 SymPy 版本。解决此问题的一种方法是更新 SymPy(从 1.1 开始的版本都有)。另一种是将nonlinsolve
替换为solve
。
不幸的是,两者都不会产生您的系统的解决方案。这是因为一般情况下,几个代数方程组没有明确的解,除非你真的很幸运。因此,尽管尝试了很长时间,nonlinsolve
和 solve
都没有成功。
关于python - SymPy nonlinsolve NameError :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42834136/