On Sun, May 6, 2012 at 5:05 AM, Korrignu <[email protected]> wrote: > Hello, > > I'm a beginner with SymPy and I need to use an indicator function which > returns 1 when a condition holds and 0 otherwise. > > I have this kind of stuff : > mu*(0<a<1)+(1-mu)*(a>1) > > when I subsitute "mu" with a value, there is no problem, but when I > substitute "a" there is a problem with the multiplication between an > integer and a boolean > > What is the good way to do ? Is there an indicator function ? > Piecewise is what you need, I think:
>>> Piecewise((mu,0<a<1),(1-mu,a>1)) Piecewise((mu, a < 1), (-mu + 1, a > 1)).subs(a,.4) mu >>> Piecewise((mu,0<a<1),(1-mu,a>1)).subs(a,1.1) -mu + 1 >>> Piecewise((mu,0<a<1),(1-mu,a>1)).subs(a,1) Piecewise() note that you got nothing because neither condition was True. If you want a default value when the conditions aren't True then add that with the condiiton True: >>> Piecewise((mu,0<a<1),(1-mu,a>1),(42,True)).subs(a,1) 42 /c -- You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
