2017-10-19 4 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 7, Python 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을 키 w의 모든 항목 값별로 정렬하려고 시도하지만이 값은 서로 비교할 수 없습니다.

왜 그럴까요? 그것들은 "관계형"값이기 때문에 그것들 안에있는 변수의 값에 의존한다는 것을 의미합니다. 확인할 수 있습니다 :

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

-3*h + x 또는 다른 방법으로 주위보다 -h + x 큰가요? 뭐, hx이 무엇인지에 따라 달라지며, SymPy가 이러한 값의 순서를 결정할 수 없으므로 오류가 발생합니다.