为什么在Sympy中`pprint()`它不能用于for循环?

时间:2022-08-29 18:06:54

This is my code

这是我的代码

 14 def sum(output):
 15     result = 0
 16     for x, w in zip(output[0], output[1]):
 17         result+=w*np.exp(-((((b-a)/2.0)*x)+((a+b)/2.0))**2)
 18         pprint(w*exp(-((((b-a)/2.0)*x)+((a+b)/2.0))**2))
 19     return ((b-a)/2.0)*result

For this code if i call a function sum my output on terminal will print:

对于此代码,如果我调用函数sum,我的终端输出将打印:

0.548543700179284
0.6
0.692267362730138
0.0
0.252818105473090
0.6

but if

但如果

 14 def sum(output):
 15     result = 0
 16     for x, w in zip(output[0], output[1]):
 17         result+=w*np.exp(-((((b-a)/2.0)*x)+((a+b)/2.0))**2)
 18     return ((b-a)/2.0)*result
 19
 20 pprint(w*np.exp(-((((b-a)/2.0)*x)+((a+b)/2.0))**2))

It will print a beautiful equation(I mean pprint() it's work!).

它将打印一个漂亮的等式(我的意思是pprint()它的工作!)。

Why the first code can not print a beautiful equation like the second code?

为什么第一个代码不能像第二个代码那样打印漂亮的等式?

1 个解决方案

#1


1  

It is hard to say without more context, but probably because w and x are both defined both inside and outside of your function sum().

没有更多的上下文很难说,但可能因为w和x都在函数sum()的内部和外部定义。

Also note that the two lines are not the same:

还要注意两条线不一样:

pprint(w*exp(-((((b-a)/2.0)*x)+((a+b)/2.0))**2))
pprint(w*np.exp(-((((b-a)/2.0)*x)+((a+b)/2.0))**2))

#1


1  

It is hard to say without more context, but probably because w and x are both defined both inside and outside of your function sum().

没有更多的上下文很难说,但可能因为w和x都在函数sum()的内部和外部定义。

Also note that the two lines are not the same:

还要注意两条线不一样:

pprint(w*exp(-((((b-a)/2.0)*x)+((a+b)/2.0))**2))
pprint(w*np.exp(-((((b-a)/2.0)*x)+((a+b)/2.0))**2))