Looking for an Indicator function

691 views
Skip to first unread message

Korrignu

unread,
May 5, 2012, 7:20:39 PM5/5/12
to sy...@googlegroups.com
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 ?

Thanks,

Regards

K.

signature.asc

Chris Smith

unread,
May 5, 2012, 9:21:05 PM5/5/12
to sy...@googlegroups.com
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

Aaron Meurer

unread,
May 5, 2012, 11:19:18 PM5/5/12
to sy...@googlegroups.com
You should not write 0 < a < 1. Due to limitations in Python, this
will not work (see the docstring of GreaterThan). Instead, write
And(0 < a, a < 1).

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
>

Korrignu

unread,
May 6, 2012, 4:14:17 AM5/6/12
to sy...@googlegroups.com
On 05/06/2012 03:21 AM, Chris Smith wrote:
> On Sun, May 6, 2012 at 5:05 AM, Korrignu <korr...@gmail.com> 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()
>
> 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)
>
I tried Piecewise, and this behavior is my problem. I tried this :

def indicator(self,condition) :
"""Returns 1 if bool 0 otherwise"""
return Piecewise((1,type(condition)==bool and
condition),(0,type(condition)==bool and not condition), (condition, True))

to return 1 if condition holds, 0 if it doesn't hold and return
condition if condition can't be evaluated due to Symbols. So when I have
an expression build with this, there are no more piecewise function in
it, and if I do a substitution, it applies on "condition" and not on
Piecewise and it fails.

In fact, I think I need a function which is really executed as soon as
condition can be evaluated as boolean value and not before.

I home my explanation was clear.

Thx

Regards

K.

signature.asc
Reply all
Reply to author
Forward
0 new messages