PTTHomps
PTTHomps

Reputation: 1499

Can't get SymPy to numerically evaluate results of solving a system of equations

***** EDIT *****

Adding a simplified example to show the problem more clearly. Original post below the edit.

I'm trying to get the big mass of ugly code in the original post to produce a simple numerical result. In this reduced example, I have three equations, EqA, EqB, and EqC. EqA and EqB both take numeric inputs. EqC then takes the output from EqA and adds it to the output from EqB. After doing a solve() and an evalf(), I expected to receive a value of 11 as the result, but it just gives me an expression. How would I properly evaluate a system of equations like this?

from sympy import *
from sympy.printing.mathml import print_mathml
init_printing()

Fa,Fb,a,b,c,d,T = symbols('F_a F_b a b c d T')
EqA = Eq(a+b,Fa)
print(EqA)
EqB = Eq(c+d,Fb)
print(EqB)
EqC = Eq(Fa + Fb,T)
print(EqC)
results = (solve([EqA,EqB,EqC],T))
print(results)
print('printing results')
print(results[T].evalf(subs={a:1,b:3,c:3,d:4}))

This produces the following output:

-F_a + a + b
-F_b + c + d
F_a + F_b - T
{T: F_a + F_b}
printing results
F_a + F_b

The expected output for the evalf() statement is 11.

***** /EDIT *****

Can't seem to get this code to produce any numerical output. I'm trying to build up a more complex equation by breaking it into smaller pieces, to make troubleshooting easier. The desired output is to solve for T. However, when I run the solver and then try to evalf() on the output, it just pumps the same expression back out at me. The behavior is identical to when I fail to define all necessary numerical inputs, but I think I have all the relevant ones defined in the evalf() call. Any ideas as to what I'm doing wrong?

from sympy import *
init_printing()

periodOfInterest  = symbols('P') #constant
modDensity = symbols('D_m') # result of calculation
floorDensity = symbols('D_f') # result of calculation
distanceTraveledPerPickMod = symbols('x_m')  # result of calculation
distanceTraveledPerPickFloor  = symbols('x_f') # result of calculation
travelTimePerPickMod = symbols('t_modTravel') # result of calculation
travelTimePerPickFloor = symbols('t_floorTravel') # result of calculation
timePerPickMod = symbols('t_totalMod') # result of calculation
timePerPickFloor = symbols('t_totalFloor') # result of calculation
T = symbols('Total_picks') # result of calculation
unitsMod = symbols('U_m') #constant
zonesMod = symbols('Z_m') #constant
pathLengthMod = symbols('L_m') #constant
travelRate = symbols('R_p') #constant
pickTime = symbols('k_p') #constant
unitsFloor = symbols('U_f') #constant
zonesFloor = symbols('Z_f') #constant
pathLengthFloor = symbols('L_f') #constant
floorPickers = symbols('N_floor') #constant
modPickers = symbols('N_mod') #constant



modDensityEq = Eq(unitsMod/zonesMod , modDensity)
floorDensityEq = Eq(unitsFloor/zonesFloor , floorDensity)
distanceTraveledPerPickModEq = Eq(pathLengthMod/modDensity , distanceTraveledPerPickMod)
distanceTraveledPerPickFloorEq = Eq(pathLengthFloor/floorDensity , distanceTraveledPerPickFloor)
travelTimePerPickModEq = Eq(distanceTraveledPerPickMod/travelRate , travelTimePerPickMod)
travelTimePerPickFloorEq = Eq(distanceTraveledPerPickFloor/travelRate , travelTimePerPickFloor)
timePerPickModEq = Eq(travelTimePerPickMod+pickTime , timePerPickMod)
timePerPickFloorEq = Eq(travelTimePerPickFloor + pickTime , timePerPickFloor)
totalPicksEq = Eq(floorPickers*periodOfInterest/timePerPickFloor + modPickers*periodOfInterest/timePerPickMod, T)
results = (solve([totalPicksEq,timePerPickFloorEq,timePerPickModEq,travelTimePerPickFloorEq,
       travelTimePerPickModEq,distanceTraveledPerPickModEq,distanceTraveledPerPickFloorEq, 
       floorDensityEq,modDensityEq],T))
(results)

results[T]



(results[T]).evalf(subs={
        periodOfInterest:60*60,
        unitsMod:5000*2/3,
        zonesMod:4,
        pathLengthMod:3000,
        travelRate:1.34,
        pickTime:10,
        unitsFloor:5000/3,
        zonesFloor:2, 
        pathLengthFloor:3000, 
        floorPickers:15,
        modPickers:35 
    })

This produces the following output:

 N_floor*P/t_totalFloor + N_mod*P/t_totalMod

Upvotes: 0

Views: 1202

Answers (2)

smichr
smichr

Reputation: 18939

What you want is for SymPy to update all the pertinent variables and give you the result: you want the equations you have defined and the values for a - d to be simultaneously true. The way to do this is give the values as additional simultaneous equations which must be true:

>>> solve((EqA,EqB,EqC,Eq(a,1),Eq(b,3),Eq(c,3),Eq(d,4)))
{c: 3, F_a: 4, a: 1, d: 4, F_b: 7, b: 3, T: 11}

And this shows your T=11 value that you were seeking.

Upvotes: 1

moorepants
moorepants

Reputation: 1869

How about:

results[T].subs({floorPickers: 15, modPickers: 35, periodOfInterest: 60*60, timePerPickMod: 1.0, timePerPickFloor: 1.0})

Upvotes: 0

Related Questions