Manish Manish - 3 months ago 7
Python Question

How do I define a conditional function using sympy?

I want to be able to define an expression which takes all the values of variable where it is defined and evaluates the expression as 0 when it is not defined.
Similar to this: -

import numpy as np
import sympy as sp

def expr(k1, k2):
x, y =sp.symbols('x y')
if x == k1 :
fn = 0
else:
fn = np.divide(1,(x-k1)*(y-k2))
return fn, x, y

f,x, y = expr(1,2)
print(f)
fx = f.subs({x:1,y:4})
print(fx)


So how is the equality or conditionality going to be checked once the function has been defined?

fn = 1/ (x-1)(y-2); How to set it as 0 for x=1 or y=2?

Answer

You should define a function inside your function and then return it. Like this:

import numpy as np
import sympy as sp

def expr(k1, k2):
    x, y =sp.symbols('x y')
    def fn(x, y):
        if x==k1:
            return 0
        else:
            return np.divide(1, (x-k1)*(y-k2))
    return fn, x, y


f, x, y = expr(1, 2)
print(f(x, y))
print(f(1, 4))

EDIT:

Here is one way to use sp.lambdify as asked in the comments:

x_dot = 1 / ((x - 1) * (y - 2))
f = lambda a, b : 0 if a==1 or b==2 else sp.lambdify((x,y), xdot, "numpy")(a,b)

Another option is to use sp.subs

f = lambda a, b: 0 if a==1 or b==2 else float(x_dot.subs({x:a, y:b}))
Comments