Sympy TypeError:使用sympy

时间:2017-10-19 04:50:45

标签: python-3.x sympy

我现在正在学习SymPy。这是我遇到的问题:

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)

但是我遇到了错误

  

TypeError Traceback(最近一次调用   最后)in()   ----> 1个系数= [t [c]表示排序中的t(模式,键= lambda t:t [w])]         2打印(系数)

     

C:\ Program Files \ Anaconda3 \ lib \ site-packages \ sympy \ core \ relational.py   在非零(个体经营)       193       194 def 非零(个体经营):    - > 195引发TypeError(“无法确定Relational的真值”)       196       197 bool = 非零

     

TypeError :无法确定Relational的真值

我正在使用Windows 7Python 3.5.2Anaconda 3

谢谢。

1 个答案:

答案 0 :(得分:1)

问题是您在// create the CSVReader CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), '~'); // create the DocumentBuilder which will create our xml documents DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); // create the transoformer which will make the xml ready for storing TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); // loop through the 4 rows for (int i = 0; i < 4; i++) { // read one row from the csv String [] line = reader.readNext() // create an xml document Document doc = docBuilder.newDocument(); Element rootElement = doc.createElement("data-"+i); doc.appendChild(rootElement); // iterate over the entries in the row for (String s : line) { Element element = doc.createElement("element"); element.appendChild(doc.createTextNode(s)); rootElement.appendChild(element); } // finally save it: DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File("file_"+i+".xml")); transformer.transform(source, result); System.out.println("File " + i + " saved!"); } 上执行的排序。

patterns尝试返回按sorted(patterns, key=lambda t:t[w])的每个项目值排序的patterns,但这些值无法相互比较。

为什么?因为他们是&#34;关系&#34;值,意味着它们依赖于变量的值。让我们检查:

w

>>> [t[w] for t in patterns] [-h + x, -3*h + x, -2*h + x, x] 大于-h + x还是相反?好吧,这取决于-3*h + xh是什么,并且由于SymPy无法确定这些值的顺序,因此会出错。