0

I have been successful solving for an unknown variable with solve()
Code here:

x = 1000  
y = Symbol('y')  
w = 2.84 * x > x + y, 1.7 * y > x + y  
solve(w)  

Now I am trying to get the same result from a Pandas dataframe, unsuccessfully though.

df = pd.DataFrame({'A': pd.Series([2.84]),'B': pd.Series([1.7])})  
A = Symbol('A')  
B = Symbol('B')
C = 1000
y = Symbol('y')  
expr = A * C > C + y, B * y > C + y  
f = lambdify([A,B],expr, modules="sympy")
f(df['A'],df['B'])  

I get the error - NameError: name 'y' is not defined
Which is weird because 'y' is what I'm searching for.

Josef
  • 41
  • 3

1 Answers1

0

The lambdify function does not solve for anything, it only evaluates. To evaluate your expr, it needs to know A, B, and y. You have not provided y, hence the error.

If the goal is to determine y from A and B, you should first get a formula for y in terms of A and B, presumably using the output of solve. Then lambdify that formula.

As it happens, solve can't solve multivariable inequalities. Even if it could, there would be a range of possible values of y, and lambdify needs something concrete to evaluate. To get somewhere with this, I replaced > with =, solved the equations, and returned the average of these (they are the upper and lower bounds for y, assuming B > 1 as in your example).

A = Symbol('A')  
B = Symbol('B')
C = 1000
y = Symbol('y')  
expr = (A * C > C + y, B * y > C + y)
upper = solve(expr[0].replace(Gt, Eq), y, dict=True)[0][y]
lower = solve(expr[1].replace(Gt, Eq), y, dict=True)[0][y]
f = lambdify((A, B), (upper+lower)/2)

Now f is a lambda with two arguments, which returns the midpoint of the interval of possible values of y.

import pandas as pd
df = pd.DataFrame({'A': pd.Series([2.84, 3.24]),'B': pd.Series([1.7, 2.12])}) 
f(df['A'], df['B'])

returns

0    1634.285714
1    1566.428571
dtype: float64