sympy解决错误不可用类型:' list'

时间:2018-03-30 17:01:11

标签: python sympy

我试图用同情来解决一组方程式 看来xv1, yv1, zv1 = v 正确拆包。
我没有看到错误代码所指的列表。

def vectPerpend(v):
    import sympy
    'find vector in x_y plane perpendicular to the given vector'
    xv2, yv2 = sympy.symbols ("xv2,yv2")
    xv1, yv1, zv1 = v

    return sympy.solve ([xv2 ** 2 + yv2 ** 2 == 1,
                         xv1 * xv2 + yv1 * yv2 == 0], [xv2, yv2], dict=True)

print (vectPerpend ([3, 5, 1]))

我收到此错误:

  File "C:\Python36\lib\site-packages\sympy\solvers\inequalities.py", line 671, in reduce_inequalities
    symbols = (set(symbols) or gens) & gens
TypeError: unhashable type: 'list'



`**UPDATE**: the problem was that equations were presented as enequalities.
Changing from xv2 ** 2 + yv2 ** 2 == 1 to xv2 ** 2 + yv2 ** 2 - 1
     and from xv1 * xv2 + yv1 * yv2 == 0 to xv1 * xv2 + yv1 * yv2   
fixed the error.`

1 个答案:

答案 0 :(得分:3)

错误与您记下系统的方式相同

[xv2 ** 2 + yv2 ** 2 == 1, xv1 * xv2 + yv1 * yv2 == 0],

两个表达式立即评估为False。所以你要求SymPy解决系统问题

[False, False] 

当然不是故意的,并导致奇怪的错误消息。

使用Eq(xv2 ** 2 + yv2 ** 2, 1)来表达等式,或者(更简单地)将=替换为-以创建左右两侧的差异。传递给solve的表达式被理解为等于零。

sympy.solve ([xv2 ** 2 + yv2 ** 2 - 1,
              xv1 * xv2 + yv1 * yv2], [xv2, yv2], dict=True)

返回

[{xv2: -5*sqrt(34)/34, yv2: 3*sqrt(34)/34},
 {xv2: 5*sqrt(34)/34, yv2: -3*sqrt(34)/34}]