与Sympy的符号矩阵区分

时间:2016-06-08 01:42:36

标签: sympy differentiation

我正在使用Python35测试https://zulko.wordpress.com/2012/04/15/symbolic-matrix-differentiation-with-sympy/,我收到了这个错误:

Traceback (most recent call last):
  File "D:\Program Files\JetBrains\PyCharm 2016.1\helpers\pydev\pydev_run_in_console.py", line 71, in <module>
    globals = run_file(file, None, None)
  File "D:\Program Files\JetBrains\PyCharm 2016.1\helpers\pydev\pydev_run_in_console.py", line 31, in run_file
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "D:\Program Files\JetBrains\PyCharm 2016.1\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "D:/programs/python/wcf/test2/my.py", line 134, in <module>
    matPrint(  expand( expand( matDiff(H,X) ) ) )
  File "D:/programs/python/wcf/test2/my.py", line 64, in matDiff
    return  MATRIX_DIFF_RULES[expr.__class__](expr,symbols)
  File "D:/programs/python/wcf/test2/my.py", line 54, in <lambda>
    Mul :  lambda e,s : Mul(matDiff(e.args[0],s),Mul(*e.args[1:]))
  File "D:/programs/python/wcf/test2/my.py", line 64, in matDiff
    return  MATRIX_DIFF_RULES[expr.__class__](expr,symbols)
  File "D:/programs/python/wcf/test2/my.py", line 52, in <lambda>
    Symbol : lambda e, s: d(e) if (e in s) else 0,
TypeError: argument of type 'Symbol' is not iterable

1 个答案:

答案 0 :(得分:3)

我的猜测是,代码是针对旧版本的SymPy编写的,您可以在其中编写类似x in sin(x)的内容。现在,这是不允许的,您必须使用has。所以替换应该是

Symbol : lambda e, s: d(e) if s.has(e) else 0,