Zach Thomas Zach Thomas - 1 month ago 7
Python Question

Why am I getting an empty list as my output using solve command?

I am trying to solve an equation using sympy's solve command but my output is an empty list [ ]. The only reason I think this could be happening is because there is no solution but I doubt that is the reason. Anyone know why I am not getting an answer? Thanks!

from sympy import *

class WaterModel:

def fp_requirement(self, Ws0, Wp0, Wg0):
greyW = 60.0
potW = 126.0
rainW = 17.05

self.Ws0 = Ws0
self.Wp0 = Wp0
self.Wg0 = Wg0

self.fp = var('fp')

filt_greyW = self.fp*greyW
dWg = self.Wg0 - greyW + (1 - self.fp)*greyW + rainW
dWp = self.Wp0 - potW + filt_greyW
F = self.Ws0 + dWp + dWg
self.fp = solve(F,self.fp)

return self.fp

a = WaterModel()
fp = a.fp_requirement(1500, 100, 100)


I tried adding a few tracing statements to your function, just above the call to solve.

    print "dWg\t", dWg, type(dWg)
    print "dWp\t", dWp, type(dWp)
    print "F\t", F, type(F)
    print "self.fp\t", self.fp
    self.fp = solve(F,self.fp)


dWg 117.050000000000 - 60.0000000000000*fp <class 'sympy.core.add.Add'>
dWp -26.0000000000000 + 60.0000000000000*fp <class 'sympy.core.add.Add'>
F   1591.05000000000 <class 'sympy.core.numbers.Real'>
self.fp fp

If I'm reading this correctly, your function evaluates the expressions rather than maintaining the symbolic nature of F. Thus, when you issue the solve directive, you're trying to solve a constant for the variable fp. That's why you get no solutions.

Ah, ha! There it is!

1500 + 117 - 26 - 60*fp + 60*fp => 1591

With fp out of the equation, there are no solutions.