Example #1
0
def condassign(b, c, d):
    """
    Returns the result of (b>0)? c : d 

    Use instead of if statements to force taping both c and d
    expressions. Uses type(b) to decide if ad.condexp_gt() should be
    called, so make sure this an ad type if you are generating a tape.
    """
    if type(b) == ad.cppad_.a_float:
        try:
            return ad.condexp_gt(b, ad.ad(0.), c, d)
        except:
            # Had to catch all because could not find exception name
            if type(c) != ad.cppad_.a_float:
                c = ad.ad(c)
            if type(d) != ad.cppad_.a_float:
                d = ad.ad(d)
            return ad.condexp_gt(b, ad.ad(0.), c, d)
    else:
        if b > 0.:
            #print('a',end='')
            return c
        else:
            #print('b',end='')
            return d
Example #2
0
def condassign(b, c, d):
    """
    Returns the result of (b>0)? c : d 

    Use instead of if statements to force taping both c and d
    expressions. Uses type(b) to decide if ad.condexp_gt() should be
    called, so make sure this an ad type if you are generating a tape.
    """
    if type(b) == ad.cppad_.a_float:
        try:
            return ad.condexp_gt(b, ad.ad(0.), c, d)
        except:
            # Had to catch all because could not find exception name
            if type(c) != ad.cppad_.a_float:
                c = ad.ad(c)
            if type(d) != ad.cppad_.a_float:
                d = ad.ad(d)
            return ad.condexp_gt(b, ad.ad(0.), c, d)
    else:
        if b > 0.:
            #print('a',end='')
            return c
        else:
            #print('b',end='')
            return d
Example #3
0
def ode_function(num_step, age_local, all_local, scipy=False):
    global age, incidence, remission, excess, all_cause
    global susceptible, condition

    if scipy == False:
        N = len(age_local)
        age = age_local
        all_cause = all_local
        susceptible = pycppad.ad(numpy.zeros(N))
        condition = pycppad.ad(numpy.zeros(N))
        incidence = .00 * numpy.ones(N)
        remission = .00 * numpy.ones(N)
        excess = .00 * numpy.ones(N)
        s0 = 0.
        c0 = 0.
        x = numpy.hstack((incidence, remission, excess, s0, c0))
        x = pycppad.independent(x)
        incidence = x[(0 * N):(1 * N)]
        remission = x[(1 * N):(2 * N)]
        excess = x[(2 * N):(3 * N)]
        s0 = x[3 * N]
        c0 = x[3 * N + 1]
        ode_integrate(N, num_step, s0, c0)
        y = numpy.hstack((susceptible, condition))
        fun = pycppad.adfun(x, y)

        return fun

    if scipy == True:
        res = integrate.solve_ivp(fun=odefun,
                                  t_span=(age[0], age[-1]),
                                  y0=[s0, c0],
                                  method='RK45',
                                  t_eval=age)

        print(res.message)
        s = res.y[0, :]
        c = res.y[1, :]

        return s, c
Example #4
0
def safe_exp(x):
    """
    Same as exp() except when x is greater than threshold. It has
    continuous derivatives.
    """
    threshold = 50.
    c = np.exp(x)
    d = np.exp(threshold) * (x - threshold + 1.)
    if type(x) == ad.cppad_.a_float:
        return ad.condexp_lt(x, ad.ad(threshold), c, d)
    else:
        if x < threshold:
            return c
        else:
            return d
Example #5
0
def safe_exp(x):
    """
    Same as exp() except when x is greater than threshold. It has
    continuous derivatives.
    """    
    threshold = 50.
    c = np.exp(x)
    d = np.exp(threshold) * (x - threshold + 1.)
    if type(x) == ad.cppad_.a_float:
        return ad.condexp_lt(x, ad.ad(threshold), c, d)
    else:
        if x < threshold:
            return c
        else:
            return d
Example #6
0
def ode_function(num_step, age_local, all_local):
    global age, incidence, remission, excess, all_cause
    global susceptible, condition
    N = len(age_local)
    age = age_local
    all_cause = all_local
    susceptible = pycppad.ad(numpy.zeros(N))
    condition = pycppad.ad(numpy.zeros(N))
    incidence = .00 * numpy.ones(N)
    remission = .00 * numpy.ones(N)
    excess = .00 * numpy.ones(N)
    s0 = 0.
    c0 = 0.
    x = numpy.hstack((incidence, remission, excess, s0, c0))
    x = pycppad.independent(x)
    incidence = x[(0 * N):(1 * N)]
    remission = x[(1 * N):(2 * N)]
    excess = x[(2 * N):(3 * N)]
    s0 = x[3 * N]
    c0 = x[3 * N + 1]
    ode_integrate(N, num_step, s0, c0)
    y = numpy.hstack((susceptible, condition))
    fun = pycppad.adfun(x, y)
    return fun
Example #7
0
def ode_function(num_step, age_local, all_local) :
	global age, incidence, remission, excess, all_cause
	global susceptible, condition
	N            = len( age_local )
	age          = age_local
	all_cause    = all_local
	susceptible  = pycppad.ad( numpy.zeros(N) )
	condition    = pycppad.ad( numpy.zeros(N) )
	incidence    = .00 * numpy.ones(N)
	remission    = .00 * numpy.ones(N)
	excess       = .00 * numpy.ones(N)
	s0           = 0.
	c0           = 0.
	x            = numpy.hstack( (incidence, remission, excess, s0, c0) )
	x            = pycppad.independent( x )
	incidence    = x[(0*N):(1*N)]
	remission    = x[(1*N):(2*N)]
	excess       = x[(2*N):(3*N)]
	s0           = x[3*N]
	c0           = x[3*N+1]
	ode_integrate(N, num_step, s0, c0)
	y            = numpy.hstack( (susceptible, condition) )
	fun          = pycppad.adfun(x, y)
	return fun
Example #8
0
	def oscillator(self, x):
		'''
		The actual van der pol oscillator differential equations
		'''
		lx = x.size
		l = lx/2
		res = pcad.ad(np.zeros(np.shape(x)))
		'''position calc \dot{x} = y '''
		res[l:] = x[:l]
		xa = np.tile(x[l:],(l,1))
		dx = xa - xa.transpose()
		'''velocity calc \dot{y} = m*(1-x^2)*y-w^2*x+A.dx'''
		res[:l] = (self.mu*(1-x[l:]*x[l:])*x[:l]-self.w*self.w*x[l:] +
					np.dot(self.A,dx.T).diagonal())
		return res