Johannes Gerer
Johannes Gerer

Reputation: 25818

SymPy Comparison and Conditional

What is the SymPy equivalent of the Mathematica function: f[x_]:=If[x==Infinity,1,2]?

If tried without success:

lambdify(x,Piecewise((1, <expr> ),(2,True))

where <expr> is one of

1)

 Eq(x,oo)

2)

 simplify(x)==oo

3)

 Eq(x+1,x)

Upvotes: 1

Views: 1020

Answers (1)

asmeurer
asmeurer

Reputation: 91470

The correct expression should be Piecewise((1, Eq(x, 0)), (2, True)). == does structural comparison and does not create a symbolic object (see http://docs.sympy.org/latest/tutorial/gotchas.html#equals-signs).

This works for me

In [3]: f = lambdify(x, Piecewise((1, Eq(x, 0)), (2, True)))

In [4]: f(0)
Out[4]: 1

In [5]: f(1)
Out[5]: 2

Upvotes: 1

Related Questions