Ejemplo n.º 1
0
def problem_3_tangent():
    fex = make_e_expr(make_pwr('x', 1.0))
    print("f(x)= ", fex)
    drv = deriv(fex)
    print("f'(x)= ", drv)
    drvf = tof(drv)
    print("f'(-1)= ", drvf(-1))
Ejemplo n.º 2
0
    def test_06(self):
        #x^-3+7e^(5x)+4*x^-1 => (1/-2)*(x^-2)+0 +(7*(1/5)*e(5x)+ (4*ln|x|)
        print("****Unit Test 06********")
        fex1 = make_pwr('x', -3.0)
        fex2 = make_prod(
            make_const(7.0),
            make_e_expr(make_prod(make_const(5.0), make_pwr('x', 1.0))))
        fex3 = make_prod(make_const(4.0), make_pwr('x', -1.0))
        fex4 = make_plus(fex1, fex2)
        fex = make_plus(fex4, fex3)
        print(fex)
        afex = antideriv(fex)
        assert not afex is None

        def gt(x):
            v1 = -0.5 * (x**(-2.0))
            v2 = (7.0 / 5.0) * (math.e**(5.0 * x))
            v3 = 4.0 * (math.log(abs(x), math.e))
            return v1 + v2 + v3

        afexf = tof(afex)
        assert not afexf is None
        err = 0.001
        for i in range(1, 10):
            print(afexf(i), gt(i))
            assert abs(afexf(i) - gt(i)) <= err * gt(i)
        print("Unit Test 06 pass")
Ejemplo n.º 3
0
def solve_pdeq(k1, k2):
    #k1*y' = k2*y
    assert isinstance(k1, const)
    assert isinstance(k2, const)
    return make_prod(
        make_const(1.0),
        make_e_expr(make_prod(make_quot(k2, k1), make_pwr('t', 1.0))))
Ejemplo n.º 4
0
def spread_of_news_model(p, k):
    assert isinstance(p, const) and isinstance(k, const)
    #t0 = 4
    #t1 = ?
    #p = .5 * P => P(1 - e^(-k*4)) = .5*P (where t=4) => k = -0.25*ln(0.5) ~= 1.73 => f(t) = P(1 - e^(-1.73*t))
    #p1 = .9 * P (p0 and p1 given)

    #we can now compute t1
    #0.9*P = P(1 - e^(-1.73*t)) => t ~= 13.3

    #generally
    #f(t) = P(1-e^(-kt)) we know that f(0) = 0
    #where p0 is initial knowing pop at time t0
    #and p1 is pop at time t1

    #return f(t)
    #where f(t) = p*(1 - e^(-k*t))
    #where k = (1/t0)*ln(p0)
    #so return p*(1 - e^(( -( (1/t0)*ln(p0) ) )*t)) #if given p0 and t0
    #we are given k so:
    #return p*(1 - e^(-k*t))

    return prod(
        p,
        plus(
            1,
            prod(const(-1.0),
                 make_e_expr(prod(prod(const(-1), k), make_pwr('t', 1))))))
Ejemplo n.º 5
0
def spread_of_disease_model(p, t0, p0, t1, p1):
    assert isinstance(p, const) and isinstance(t0, const)
    assert isinstance(p0, const) and isinstance(t1, const)

    #f(t) = P/(1+B*e^(-c*t))
    #p = 500,000
    #let t0 = 0 such that f(t0) = f(0)
    #this would make t1 = delta_t
    #where delta_t = t1 - t0
    #f(0) = p0 = 200 = 500,000/(1+B*e^0) = 500,000/(1+B) => B = 2499
    #generally: B = (p-p0)/p0
    #f(1) = p1 = 500 = 500,000/(1+2499*e^(-c*1)) => c = .92
    #generally: c = -ln((p-p1)/(p1*B))/t

    #so we get f(t) = p/(1+((p-p0)/p0)*e^(-(-ln((p-p1)/(p1*((p-p0)/p0))))*t))
    #or f(t) = p/(1+((p-p0)/p0)*e^((ln((p-p1)/(p1*((p-p0)/p0)))/(t1-t0)*t))
    #return f(t)
    return quot(
        p,
        plus(
            const(1.0),
            prod(
                quot(plus(p, prod(const(-1.0), p0)), p0),
                make_e_expr(
                    ln(
                        prod(
                            quot(
                                quot(
                                    plus(p, prod(const(-1.0), p1)),
                                    prod(
                                        p1,
                                        quot(plus(p, prod(const(-1.0), p0)),
                                             p0))),
                                plus(t1, prod(const(-1.0), t0))),
                            make_pwr('t', 1)))))))
Ejemplo n.º 6
0
def gt21_02(x):
    ''' ground truth for 2nd taylor of fexpr2_01. '''
    fexpr2_01 = make_prod(make_pwr('x', 1.0), make_e_expr(make_pwr('x', 1.0)))
    f0 = tof(fexpr2_01)
    f1 = tof(deriv(fexpr2_01))
    f2 = tof(deriv(deriv(fexpr2_01)))
    return f0(2.0) + f1(2.0) * (x - 2.0) + (f2(2.0) / 2) * (x - 2.0)**2
Ejemplo n.º 7
0
def radioactive_decay(lmbda, p0, t):
    assert isinstance(lmbda, const)
    assert isinstance(p0, const)
    assert isinstance(t, const)

    #exponential decay is P(t) = p0 * e^(-lambda * t)
    return prod(p0, make_e_expr(prod(lmbda, make_pwr('t', 1))))
Ejemplo n.º 8
0
def test_05():
    fexpr2_01 = make_prod(make_pwr('x', 1.0), make_e_expr(make_pwr('x', 1.0)))
    print(gt21_02(2.001))
    fex = taylor_poly(fexpr2_01, make_const(2.001), make_const(2))
    print(fex)
    fex_tof = tof(fex)
    print(fex_tof(2.001))
Ejemplo n.º 9
0
def plant_growth_model(m, t1, x1, t2, x2):
    assert isinstance(m, const) and isinstance(t1, const)
    assert isinstance(x1, const) and isinstance(x2, const)
    assert isinstance(x2, const)

    #B,M,k
    #M is 1000 (maximum population)
    #B is 9 (which is (M-x1)/x1)
    #k is 0.00037 (which is (ln((m-x2)/(x2*B))/(-M*(t2-t1))) )

    #f(t) = M / (1+B*e^(-Mkt)
    #or f(t) = m / (1+((m-x1)/x1)*e^(-Mkt))
    return quot(
        m,
        plus(
            const(1.0),
            prod(
                quot(plus(m, prod(const(-1.0), x1)), x1),
                make_e_expr(
                    prod(
                        prod(
                            prod(const(-1.0), m),
                            quot(
                                ln(
                                    plus(m, prod(const(-1.0), x2)),
                                    prod(
                                        x2,
                                        quot(plus(m, prod(const(-1.0), x1)),
                                             x1))),
                                prod(prod(const(-1.0), m),
                                     plus(t2, prod(const(-1.0), t1))))),
                        make_pwr('t', 1))))))
Ejemplo n.º 10
0
def percent_retention_model(lmbda, a):
    assert isinstance(lmbda, const)
    assert isinstance(a, const)

    #r(t) = (100 - a)*e^(-lmda*t) + a
    return plus(
        prod(plus(const(100.0), prod(const(-1.0), a)),
             make_e_expr(prod(prod(const(-1.0), lmbda), make_pwr('t', 1)))), a)
Ejemplo n.º 11
0
def nra_ut_10():
    ''' Approximating e^(5-x) = 10 - x. '''
    fexpr = make_e_expr(
        make_plus(make_const(5.0),
                  make_prod(make_const(-1.0), make_pwr('x', 1.0))))
    fexpr = make_plus(fexpr, make_pwr('x', 1.0))
    fexpr = make_plus(fexpr, make_const(-10.0))
    print(nra(fexpr, make_const(1.0), make_const(10000)))
Ejemplo n.º 12
0
def solve_pdeq_with_init_cond(y0, k):
    assert isinstance(y0, const)
    assert isinstance(k, const)
    #y` = ky
    #y(0) = P_0
    #and
    #y = P(t) = y(0)*e^(kt) = P_0*e^(kt)
    #return y0 * e^(k*x)
    return prod(y0, make_e_expr(prod(k, make_pwr('x', 1.0))))
Ejemplo n.º 13
0
def spread_of_news_model(p, k):
    assert isinstance(p, const) and isinstance(k, const)

    expon = const(-1.0 * k.get_val())
    return make_prod(
        p,
        make_plus(
            const(1.0),
            make_prod(const(-1.0),
                      make_e_expr(make_prod(expon, make_pwr('t', 1.0))))))
Ejemplo n.º 14
0
def find_growth_model(p0, t, n):
    assert isinstance(p0, const)
    assert isinstance(t, const)
    assert isinstance(n, const)

    #p0 is C
    #n  is k
    #t  is t

    return prod(p0, make_e_expr(prod(quot(ln(n), t), make_pwr('t', 1))))
Ejemplo n.º 15
0
def percent_retention_model(lmbda, a):
    #r(t) = (100-a)e^(-lmbda*t) + a
    assert isinstance(lmbda, const)
    assert isinstance(a, const)

    left = const(100.0 - a.get_val())
    right = make_e_expr(
        make_prod(const(-1.0 * lmbda.get_val()), make_pwr('t', 1.0)))

    return make_plus(make_prod(left, right), a)
Ejemplo n.º 16
0
def problem_1_decay():
    fex = make_e_expr(make_prod(const(-0.021), make_pwr('x', 1.0)))
    print(fex)
    P0 = const(8.0)
    expr, decay_const = fun1(fex, P0)

    print("f(x)= ", expr, "lambda=", decay_const)
    remaining = fun2(expr, 3.0)
    print("remaining= ", remaining)
    remaining_after_n_years = fun3(expr, 33.0, 40.0)
    print("after n years", remaining_after_n_years)
Ejemplo n.º 17
0
def solve_pdeq(k1, k2):
    assert isinstance(k1, const)
    assert isinstance(k2, const)
    #k1*y` = k2*y
    #y` = (k2/k1) * y
    #let k2/k1 = k
    #y` = k*y
    #therefore y = C*e^(kt) and y` = k*C*e^(kt) or y` = (k2/k1)*C*e^(kt)
    #I am going to let C = 1/k
    #so we get:
    #return e^((k2/k1)*x)
    # return prod(quot(k2,k1), make_e_expr(prod(quot(k2,k1),make_pwr('x',1.0)))) #C = 1
    return make_e_expr(prod(quot(k2, k1), make_pwr('x', 1.0)))  #C = 1/k
Ejemplo n.º 18
0
 def test_prob_01_ut_02(self):
     print('\n***** Problem 01: UT 02 ************')
     fex = make_e_expr(make_plus(make_pwr('x', 2.0),
                                 make_const(-1.0)))
     print(fex)
     drv = deriv(fex)
     assert not drv is None
     print(drv)
     drvf = tof(drv)
     assert not drvf is None
     gt = lambda x: 2*x*(math.e**(x**2 - 1.0))
     err = 0.0001
     for i in range(10):
         #print(drvf(i), gt(i))
         assert abs(gt(i) - drvf(i)) <= err
     print('Problem 01: UT 02: pass')
Ejemplo n.º 19
0
def spread_of_disease_model(p, t0, p0, t1, p1):
    assert isinstance(p, const) and isinstance(t0, const)
    assert isinstance(p0, const) and isinstance(t1, const)
    # find B
    B = const(((p.get_val() / p0.get_val()) - 1.0) / math.e**(t0.get_val()))
    x = const(((p.get_val() / p1.get_val()) - 1.0) / B.get_val())

    #find k
    k = const(math.log(x.get_val()) / (-1.0 * p.get_val() * t1.get_val()))

    #simplify exponent before creating it
    expon = const(-1.0 * p.get_val() * k.get_val())

    bottom = make_plus(
        make_const(1.0),
        make_prod(B, make_e_expr(make_prod(expon, make_pwr('t', 1.0)))))
    return make_quot(p, bottom)
Ejemplo n.º 20
0
 def test_prob_01_ut_01(self):
     print('\n***** Problem 01: UT 01 ************')
     fex = make_e_expr(make_prod(make_const(5.0),
                                 make_pwr('x', 1.0)))
     print(fex)
     drv = deriv(fex)
     assert not drv is None
     print(drv)
     drvf = tof(drv)
     assert not drvf is None
     gt = lambda x: 5.0*(math.e**(5.0*x))
     err = 0.0001
     for i in range(10):
         #print(drvf(i), gt(i))
         # the numbers get pretty large pretty fast.
         assert abs(gt(i) - drvf(i)) <= err
     print('Problem 01: UT 01: pass')
Ejemplo n.º 21
0
    def test_02(self):
        #e^(-2x)=> -0.5e^(-2x)
        print("****Unit Test 02********")
        fex = make_e_expr(make_prod(make_const(-2.0), make_pwr('x', 1.0)))
        print(fex)
        afex = antideriv(fex)
        assert not afex is None

        def gt(x):
            return (-0.5) * (math.e**(-2.0 * x))

        afexf = tof(afex)
        assert not afexf is None
        err = 0.0001
        for i in range(0, 101):
            assert abs(afexf(i) - gt(i)) <= err
        print(afex)
        print("Unit Test 02: pass")
Ejemplo n.º 22
0
def plant_growth_model(m, t1, x1, t2, x2):
    assert isinstance(m, const) and isinstance(t1, const)
    assert isinstance(x1, const) and isinstance(x2, const)
    assert isinstance(x2, const)

    b = (m.get_val() / x1.get_val()) - 1
    k = (np.log(((m.get_val() / x2.get_val()) - 1.0) /
                b)) / (-1 * m.get_val() * t2.get_val())

    expr = quot(
        m,
        plus(
            const(1.0),
            prod(
                const(b),
                make_e_expr(
                    prod(const(m.get_val() * -1),
                         prod(const(k), pwr(var("t"), const(1.0))))))))
    return expr
Ejemplo n.º 23
0
 def test_prob_01_ut_03(self):
     print('\n***** Problem 01: UT 03 ************')
     fex1 = make_quot(make_const(-1.0), make_pwr('x', 1.0))
     fex2 = make_e_expr(make_plus(make_pwr('x', 1.0), fex1))
     print(fex2)
     drv = deriv(fex2)
     assert not drv is None
     print(drv)
     drvf = tof(drv)
     assert not drvf is None
     def gt_drvf(x):
         d = (x - 1.0/x)
         return (math.e**d)*(1.0 + 1.0/(x**2))
     err = 0.0001
     for i in range(1, 10):
         #print drvf(i), gt_drvf(i)
         # the numbers are too large.
         assert abs(gt_drvf(i) - drvf(i)) <= err
     print('Problem 01: UT 03: pass')
Ejemplo n.º 24
0
def plant_growth_model(m, t0, h0, t1, h1):
    assert isinstance(m, const) and isinstance(t0, const)
    assert isinstance(h0, const) and isinstance(t1, const)
    assert isinstance(h1, const)

    t_new = abs(t0.get_val() - t1.get_val())

    B = const((m.get_val() / h0.get_val()) - 1)

    k = const(
        math.log(((m.get_val() / h1.get_val()) - 1) / B.get_val()) /
        (-1.0 * m.get_val() * t_new))

    # # simplify exponent before creating it
    expon = const(-1.0 * m.get_val() * k.get_val())

    bottom = make_plus(
        make_const(1.0),
        make_prod(B, make_e_expr(make_prod(expon, make_pwr('t', 1.0)))))
    return make_quot(m, bottom)
Ejemplo n.º 25
0
 def test_prob_01_ut_07(self):
     print('\n***** Problem 01: UT 07 ************')
     fex0 = make_prod(make_pwr('x', 1.0),
                      make_e_expr(make_pwr('x', 1.0)))
     fex = make_ln(fex0)
     print(fex)
     drv = deriv(fex)
     assert not drv is None
     print(drv)
     drvf = tof(drv)
     assert not drvf is None
     gt = lambda x: (x + 1.0)/x
     err = 0.0001
     for i in range(1, 10):
         #print(drvf(i), gt(i))
         assert abs(gt(i) - drvf(i)) <= err
     for i in range(-10, -1):
         #print(drvf(i), gt(i))
         assert abs(gt(i) - drvf(i)) <= 0.001
     print('Problem 01: UT 07: pass')
Ejemplo n.º 26
0
 def test_prob_01_ut_04(self):
     print('\n***** Problem 01: UT 04 ************')
     n = make_prod(make_const(3.0),
                   make_e_expr(make_prod(make_const(2.0),
                                         make_pwr('x', 1.0))))                               
     d = make_plus(make_const(1.0), make_pwr('x', 2.0))
     fex = make_quot(n, d)
     print(fex)
     drv = deriv(fex)
     assert not drv is None
     print(drv)
     drvf = tof(drv)
     assert not drvf is None
     def gt_drvf(x):
         n = 6.0*(math.e**(2.0*x))*(x**2 - x + 1.0)
         d = (1 + x**2)**2
         return n/d
     for i in range(-10, 10):
         #print drvf(i), gt_drvf(i)
         # the numbers get pretty large.
         assert abs(gt_drvf(i) - drvf(i)) <= 0.001
     print('Problem 01: UT 04: pass')
Ejemplo n.º 27
0
def solve_pdeq_with_init_cond(y0, k):
    # solve y'=3y y(0) =1
    assert isinstance(y0, const)
    assert isinstance(k, const)
    return make_prod(y0, make_e_expr(make_prod(k, make_pwr('t', 1.0))))
Ejemplo n.º 28
0
def find_growth_model(p0, t, n):
    assert isinstance(p0, const)
    assert isinstance(t, const)
    assert isinstance(n, const)
    k = math.log(n.get_val() / p0.get_val()) / t.get_val()
    return make_prod(p0, make_e_expr(make_prod(const(k), make_pwr('t', 1.0))))
Ejemplo n.º 29
0
def radioactive_decay(lmbda, p0, t):
    assert isinstance(lmbda, const)
    assert isinstance(p0, const)
    assert isinstance(t, const)
    return make_prod(p0,
                     make_e_expr(make_prod(make_prod(const(-1.0), lmbda), t)))
Ejemplo n.º 30
0
def nra_ut_05():
    ''' Approximating e^-x = x^2. '''
    fexpr = make_e_expr(make_prod(make_const(-1.0), make_pwr('x', 1.0)))
    fexpr = make_plus(fexpr, make_prod(make_const(-1.0), make_pwr('x', 2.0)))
    print(nra(fexpr, make_const(1.0), make_const(10000)))