Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Making sympy function with 2 variables

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

>Solution :

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading