SHARMILA VELAMUR
SHARMILA VELAMUR

Reputation: 13

How to add an "if otherwise" clause to evaluation in Sympy?

How would I implement the following in Sympy?

if x > y
    z = x - y
otherwise 
    Z = 0

I've looked at assumptions, but I am still lost.

Upvotes: 1

Views: 243

Answers (1)

user6764549
user6764549

Reputation:

Please see the examples on this page.

import sympy as s
x, y = s.symbols('x,y')

z = s.Piecewise( ( x-y, x > y), (0, True))

z.subs([(x,1),(y,2)])

z.subs([(x,2),(y,1)])

Upvotes: 2

Related Questions