[SciPy-User] Heaviside function in vector form

Barrett B barrett.n.b at gmail.com
Sat Jul 5 23:37:12 EDT 2014


Michael Sarahan <msarahan <at> gmail.com> writes:

> 
> 
> PS:if abs(diff) < 50: #if x is close to Theta_synwill 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 sayif 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
> 

Yeah, I discovered that.

For context, here's the function that's calling Heaviside(x):

=====

def f(X, t):
    N = len(X)/3
    V = X[:N]; n = X[N:2*N]; S = X[2*N:3*N]
        
    #Equations     
    m_inf_E = 1/(1 + np.exp((E_half_m - V)/k_m))
    n_inf_E = 1/(1 + np.exp((E_half_n - V)/k_n))
    S_inf_E = 1/(1 + np.exp((E_half_S - V)/k_S))
        
    I_Ca = g_Ca * m_inf_E * (V - E_Ca)
    I_K = g_K * n * (V - E_K)
    I_S = g_S * S * (V - E_S)
        
    dV = (-I_Ca - I_K - I_S)/tau
    dV += g_inh*(E_inh - V)*Heaviside(np.dot(V, connex))/tau
    dn = (n_inf_E - n)/tau
    dS = (S_inf_E - S)/tau_S
    return np.concatenate((dV, dn, dS))

=====

(plz pretend not to notice the two lines to establish dV, lol)

The problem is that when x << Theta_syn in the Heaviside function,
np.exp(diff) is astronomically large. My whole thought process was to say
that on any given run, if x is nowhere close to Theta_syn, then Heaviside(x)
should just return 1 or 0 depending on which side we're talking about.
Simple enough, but the trick becomes:

(1) How to do this when x is a vector;
(2) How to avoid using a for loop, which would slow things down.

That is where I am stuck.




More information about the SciPy-User mailing list