'Is there a way to specify x!=y in a piecewise function in python?
I am sort of new in this whole word, and today I wanted to try and do some maths on jupyter notebook. I wanted to solve this function: enter image description here
I am using jupyter notebook, and modules like sympy, numpy and matplotlib, but I don't know how to represent the x!=-y.
For example, if instead of x!=-y, the function was x>y I will do:
x, y = sp.symbols('x y', real=True)
g = sp.Piecewise(((x*y**4 - x**4*y)/(x**3+y**3), (x>y)), (0, True))
G = sp.Lambda((x,y), g)
display(G(x,y))
But if I type x!=-y, it doesn't work (it doesn't display the whole piecewise function, it only displays the 'first part' of the piecewise funtion), and I don't know how to solve it.
Thanks!!!
Solution 1:[1]
With SymPy we can only create equality with Eq and non-equality with Ne. That's because in SymPy == and != are reserved for structural comparison of symbolic expressions.
So, in your example you would have to do:
x, y = symbols('x y', real=True)
g = Piecewise(((x*y**4 - x**4*y)/(x**3+y**3), Ne(x, -y)), (0, True))
display(g)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Davide_sd |
