2017-10-19 8 views
1

私は今、SymPyを学んでいます。ここで私が得た問題です:Sympy TypeError:sympyを使用しているときにRelationalの真偽値を判断できません

x = symbols('x',real=True) 
h = symbols('h',real=True) 
f = symbols('f',cls=Function)  
sym_dexpr = f_diff.subs(f(x), x*exp(-x**2)).doit() 
f_diff = f(x).diff(x,1) 
expr_diff = as_finite_diff(f_diff, [x, x-h,x-2*h,x-3*h]) 
w=Wild('w') 
c=Wild('c') 
patterns = [arg.match(c*f(w)) for arg in expr_diff.args] 
coefficients = [t[c] for t in sorted(patterns, key=lambda t:t[w])] 
print(coefficients) 

は、しかし、私はエラー以下だ:私はWindows 7Python 3.5.2Anaconda 3を使用しています

TypeError Traceback (most recent call last) in() ----> 1 coefficients = [t[c] for t in sorted(patterns, key=lambda t:t[w])] 2 print(coefficients)

C:\Program Files\Anaconda3\lib\site-packages\sympy\core\relational.py in nonzero(self) 193 194 def nonzero(self): --> 195 raise TypeError("cannot determine truth value of Relational") 196 197 bool = nonzero

TypeError: cannot determine truth value of Relational

ありがとうございます。

答えて

0

問題はpatternsで実行する並べ替えです。

sorted(patterns, key=lambda t:t[w])patternsのすべてのアイテムの値によってソートされた場合、これらの値は互いに比較できません。

なぜですか?それらが「リレーショナル」値であるため、変数の値に依存することを意味します。

>>> [t[w] for t in patterns] 
[-h + x, -3*h + x, -2*h + x, x] 

-h + x-3*h + x以上の周りに他の方法である:チェックができますか?よく、それはhxであり、SymPyはこれらの値の順序を判断できないため、エラーになります。

関連する問題