Question

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:

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!).

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

Was it helpful?

Solution

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().

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))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top