Trashold functions


In [1]:
# initialize environment
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import sympy as sy
x = np.linspace(-5.0,5.0,1000)
dx = x[1]-x[0]

def clamp( x, xmin, xmax):
    x[x<xmin] = xmin
    x[x>xmax] = xmax
    return x
    
def deriv(f):
    return (f[1:]-f[:-1])/dx

def plotFunc(func):
    fs   = func (x)
    dfs  = deriv(fs)
    ddfs = deriv(dfs)
    plt.subplot(311); plt.plot( x,fs)
    plt.subplot(312); plt.plot( x[1:]-0.5*dx, dfs  )
    plt.subplot(313); plt.plot( x[2:]    -dx, ddfs )

Sigmoide derivs


In [6]:
def softmax_exp(x):
    expx = np.exp(x)
    return x*expx/(1+expx)

def softmax_sqrt(x):
    return 0.5*( np.sqrt( 1 + x*x ) + x )

def smoothstep_sqrt(x):
    return 0.5*( x/np.sqrt( 1 + x*x ) + 1 )

plt.figure(figsize=(6,15))
plotFunc( softmax_sqrt )
plotFunc( smoothstep_sqrt )

plt.subplot(311); plt.ylim(-2.0,2.0); plt.grid()
plt.subplot(312); plt.ylim(-2.0,2.0); plt.grid()
plt.subplot(313); plt.ylim(-4.0,4.0); plt.grid()


Plynominal Smoothstep

from /home/prokop/Dropbox/MyDevSW/Python/_math/FunctionApproximation/TreshholdFunctions.py


In [ ]:
def stepP3(x):
    # integral of (1-x)*x shifted
    x = clamp(x.copy(),0.0,1.0)
    return (3-2*x)*x**2 

def stepP5a(x):
    # double integrel of stepP3:  0.5 - (3-2*x)*x**2
    x = clamp(x.copy(),0.0,1.0)
    return (-x**4/4 + x**5/10 + x**2/4)*10 

def stepP5b(x):
    # integral of (x**2)*((1-x)**2)
    x = clamp(x.copy(),0.0,1.0)
    return (x**3/3 - x**4/2 + x**5/5)*30  

def stepP5c(x):
    # integral of  
    x = clamp(x.copy(),0.0,1.0)
    return x - (2.0/3)*x**3 + x**5/5  

def stepLorenz(x, w=1.0):
    x = clamp(x.copy(),0.0,1.0)
    return w/(w+x**2) - w/(w+(1-x)**2)

plt.figure(figsize=(6,15))

plotFunc(stepP3 )
plotFunc(stepP5a)
plotFunc(stepP5b)

plt.subplot(311); plt.xlim(-1.0,2.0); plt.grid()
plt.subplot(312); plt.xlim(-1.0,2.0); plt.grid()
plt.subplot(313); plt.xlim(-1.0,2.0); plt.grid()

In [ ]:


In [ ]:


In [ ]: