[SciPy-User] Heaviside function in vector form

Michael Sarahan msarahan at gmail.com
Sat Jul 5 23:26:58 EDT 2014


PS:

if abs(diff) < 50: #if x is close to Theta_syn

will also fail with this error if x is a vector.

The error message suggests any() or all(), which will return a single value
representing if any or all of the values are true (respectively) - you
might also consider min() or max() as sort of threshold settings, or a sum
or mean of the diff vector (it will be a vector if x is a vector).


On Sat, Jul 5, 2014 at 8:15 PM, Michael Sarahan <msarahan at gmail.com> wrote:

> when you say
>
> if x < Theta_syn:
>
> you are trying to figure out the truth value of a vector.  That's what
> Numpy is telling you.  It doesn't make sense - only the truth value of a
> single value makes sense for an if statement.  Instead, you probably want a
> sum or something similar.
>
> HTH.
> Mike
>
>
> On Sat, Jul 5, 2014 at 8:02 PM, Barrett B <barrett.n.b at gmail.com> wrote:
>
>> I am trying to code the Heaviside function in vector form. There are
>> several different versions of this, but essentially, f(x) = 1 whenever x >=
>> 0, and f(x) = 0 whenever x < 0. Here is what I have so far (keep in mind, x
>> is a vector here):
>>
>> =========
>>
>> def Heaviside(x):
>>     mult = -100.0; diff = mult*(x - Theta_syn);
>> #    print x
>>     if abs(diff) < 50: #if x is close to Theta_syn
>>         return 1.0/(1 + np.exp(diff))
>>     if x < Theta_syn:
>>         return 0
>>     return 1 #otherwise
>> #    return 1.0/(1 + np.exp(diff))
>>
>> =========
>>
>> which produces the following error:
>>
>> ValueError: The truth value of an array with more than one element is
>> ambiguous. Use a.any() or a.all()
>>
>> BTW, if I were to comment out every line except the first and last, it
>> runs fine.
>>
>> See what I'm trying to do? Basically, I want to check whether current
>> value of diff is not very large. But I don't know how to do this without
>> rewriting the code to include for loops, which is what I've been trying to
>> get away from all along?
>>
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User at scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20140705/a3245649/attachment.html>


More information about the SciPy-User mailing list