Making sympy function with 2 variables

Question:

I´m trying to do a piecewise function with sympy, but I can´t. It doesn´t the piecewise function.
I want a simple funtion when (x,y) != (0,0) and a when (x,y) = (0,0)
Can someone help me ?

Thank you so much

I tried this code:

f_exp = sp.Piecewise((((x*y)*(x**2 - y**2)) / (x**2 + y**2), (x,y) != (0,0)), (a, (x,y) == (0,0)))
f = sp.Lambda((x,y), f_exp) # Creamos la función
display(f)
Asked By: Adrián

||

Answers:

You can’t write (x,y) != (0,0) because Python intercept that codes before Sympy, and it performs the comparison: in this case, clearly the tuple (x, y) is different from (0, 0).

Hence, you’d have to manually construct the relationship. Here are two ways to achieve it:

f_exp = Piecewise((((x*y)*(x**2 - y**2)) / (x**2 + y**2), And(Ne(x, 0), Ne(y, 0))), (a, True))
f_exp = Piecewise((a, Eq(x, 0) & Eq(y, 0)), (((x*y)*(x**2 - y**2)) / (x**2 + y**2), True))
Answered By: Davide_sd