Ejemplo n.º 1
0
    def test_10(self):
        #(3x+2)^4
        print("****Unit Test 10********")
        fex0 = make_prod(make_const(3.0), make_pwr('x', 1.0))
        fex1 = make_plus(fex0, make_const(2.0))
        fex = make_pwr_expr(fex1, 4.0)
        print(fex)
        afex = antideriv(fex)
        assert not afex is None
        print(afex)
        afexf = tof(afex)
        err = 0.0001

        def gt(x):
            return (1.0 / 15) * ((3 * x + 2.0)**5)

        for i in range(1, 10):
            assert abs(afexf(i) - gt(i)) <= err
        fexf = tof(fex)
        assert not fexf is None
        fex2 = deriv(afex)
        assert not fex2 is None
        print(fex2)
        fex2f = tof(fex2)
        assert not fex2f is None
        for i in range(1, 1000):
            assert abs(fexf(i) - fex2f(i)) <= err
        print('Test 10:pass')
Ejemplo n.º 2
0
def test_04():
    ln1 = make_line_eq(make_var('y'), make_const(2.0))
    ln2 = make_line_eq(make_var('y'), make_plus(make_prod(make_const(2.0),
                                                                            make_pwr('x', 1.0)),
                                                                        make_const(-6.0)))
    print(line_intersection(ln1, ln2))
    print(line_intersection(ln2, ln1))
Ejemplo n.º 3
0
    def test_09(self):
        #3*(x+2)^-1 => 3*ln|x+2|
        print("****Unit Test 09********")
        fex0 = make_plus(make_pwr('x', 1.0), make_const(2.0))
        fex1 = make_pwr_expr(fex0, -1.0)
        fex = make_prod(make_const(3.0), fex1)
        print(fex)
        afex = antideriv(fex)
        print("antideriv: ", afex)
        err = 0.0001
        afexf = tof(afex)

        def gt(x):
            return 3.0 * math.log(abs(2.0 + x), math.e)

        for i in range(1, 101):
            assert abs(afexf(i) - gt(i)) <= err
        assert not afex is None
        print(afex)
        fexf = tof(fex)
        assert not fexf is None
        fex2 = deriv(afex)
        assert not fex2 is None
        print(fex2)
        fex2f = tof(fex2)
        assert not fex2f is None
        for i in range(1, 1000):
            assert abs(fexf(i) - fex2f(i)) <= err
        print('Test 09:pass')
Ejemplo n.º 4
0
 def test_prob_02_ut_01(self):
     print('\n***** Problem 02: UT 01 ************')
     fex = make_prod(make_pwr('x', 1.0),
                     make_prod(make_plus(make_pwr('x', 1.0),
                                         make_const(1.0)),
                               make_plus(make_pwr('x', 1.0),
                                         make_const(2.0))))
     drv = logdiff(fex)
     assert not drv is None
     print(drv)
     drvf = tof(drv)
     assert not drvf is None
     def gt_drvf(x):
         z = x*(x + 1.0)*(x + 2.0)
         z2 = (1.0/x + 1.0/(x + 1.0) + 1.0/(x + 2.0))
         return z * z2
     err = 0.0001
     for i in range(1, 10):
         #print(drvf(i), gt_drvf(i))
         assert abs(gt_drvf(i) - drvf(i)) <= err
     for i in range(-10, -1):
         if i == -1 or i == -2:
             continue
         #print(drvf(i), gt_drvf(i))
         assert abs(gt_drvf(i) - drvf(i)) <= err
     print('Problem 02: UT 01: pass')
Ejemplo n.º 5
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.º 6
0
    def test_03(self):
        #(x+11)/(x-3) ^3
        print('\n***** Test 03 ***********')
        q = make_quot(make_plus(make_pwr('x', 1.0), make_const(11.0)),
                      make_plus(make_pwr('x', 1.0), make_const(-3.0)))
        pex = make_pwr_expr(q, 3.0)
        print('-- function expression is:\n')
        print(pex)
        pexdrv = deriv(pex)
        assert not pexdrv is None
        print('\n - - derivative is:\n')
        print(pexdrv)
        pexdrvf = tof(pexdrv)
        assert not pexdrvf is None
        gt = lambda x: -42.0 * (((x + 11.0)**2) / ((x - 3.0)**4))
        err = 0.00001
        print('\n - -comparison with ground truth:\n')
        for i in range(10):
            if i != 3.0:
                print(pexdrvf(i), gt(i))
                #print("gt: ",gt(0))
                assert abs(pexdrvf(i) - gt(i)) <= err

    #print("pexdrvf(0): ",pexdrvf(0),"gt(i): ", gt(0))
        print('Test 03:pass')
Ejemplo n.º 7
0
def oil_disk_test():
    yt = make_prod(make_const(0.02 * math.pi), make_pwr('r', 2.0))
    print(yt)
    dydt = dydt_given_x_dxdt(yt, make_const(150.0), make_const(20.0))
    assert not dydt is None
    assert isinstance(dydt, const)
    print(dydt)
Ejemplo n.º 8
0
    def test_assign_03_prob_01_ut_04(self):
        print('\n***** Assign 03: Problem 01: Unit Test 04 *****')
        e1 = make_pwr('x', 2.0)
        e2 = make_plus(e1, make_prod(make_const(0.0), make_pwr('x', 1.0)))
        e3 = make_plus(e2, make_const(-1.0))
        e4 = make_pwr_expr(e3, 4.0)

        e5 = make_pwr('x', 2.0)
        e6 = make_plus(e5, make_prod(make_const(0.0), make_pwr('x', 1.0)))
        e7 = make_plus(e6, make_const(1.0))
        e8 = make_pwr_expr(e7, 5.0)

        e9 = make_prod(e4, e8)

        print(e9)
        e9f = tof(deriv(e9))
        assert not e9f is None
        err = 0.000001

        def drvf(x):
            return 2 * x * ((x**2 - 1.0)**3) * (
                (x**2 + 1.0)**4) * (9 * x**2 - 1.0)

        for i in range(10):
            #print(e9f(i), drvf(i))
            assert abs(e9f(i) - drvf(i)) <= err
        print('Assign 03: Problem 01: Unit Test 04: pass')
Ejemplo n.º 9
0
    def test_08(self):
        #(5x-7)^-2  => -1/5 * (5x-7)^-1
        print("****Unit Test 08********")
        fex1 = make_plus(make_prod(make_const(5.0), make_pwr('x', 1.0)),
                         make_const(-7.0))
        fex = make_pwr_expr(fex1, -2.0)
        print(fex)
        afex = antideriv(fex)
        assert not afex is None
        print("antideriv: ", afex)
        afexf = tof(afex)
        err = 0.0001

        def gt(x):
            return (-1.0 / 5.0) * ((5 * x - 7.0)**-1)

        for i in range(1, 100):
            assert abs(afexf(i) - gt(i)) <= err
        fexf = tof(fex)
        assert not fexf is None
        fex2 = deriv(afex)
        assert not fex2 is None
        print("deriv fex2: ", fex2)
        fex2f = tof(fex2)
        assert not fex2f is None
        for i in range(1, 100):
            print(fexf(i), " ", fex2f(i))
            assert abs(fexf(i) - fex2f(i)) <= err
        print('Test 08:pass')
Ejemplo n.º 10
0
def pwr_deriv(p):
    assert isinstance(p, pwr)
    b = p.get_base()
    d = p.get_deg()
    if isinstance(b, var):
        if isinstance(d, const):
            return prod(make_const(d.get_val()), pwr(b, make_const(d.get_val()-1)))
        else:
            raise Exception('pwr_deriv: case 1: ' + str(p))
    if isinstance(b, pwr):  # think this is (x^2 (^3))
        if isinstance(d, const):
           return prod(b.get_base(), prod(b.get_deg(), const))
        else:
            raise Exception('pwr_deriv: case 2: ' + str(p))
    elif isinstance(b, plus):  # (x+2)^3
        if isinstance(d, const):
            return prod(d, pwr(b, d.get_val()-1))
        else:
            raise Exception('pwr_deriv: case 3: ' + str(p))
    elif isinstance(b, prod):#(3x)^2 => (2*3*x)^(2-1)
        if isinstance(d, const):
            pwr( prod(d, prod(b.get_mult1(), b.get_mult2())), d.get_val()-1)
        else:
            raise Exception('pwr_deriv: case 4: ' + str(p))
    else:
        raise Exception('power_deriv: case 5: ' + str(p))
Ejemplo n.º 11
0
def test_09():  #y = 5; y = -x +6
    ln1 = make_line_eq(make_var('y'), make_const(5.0))
    ln2 = make_line_eq(
        make_var('y'),
        make_plus(make_prod(make_const(-1.0), make_pwr('x', 1.0)),
                  make_const(6.0)))
    print(line_intersection(ln1, ln2))
Ejemplo n.º 12
0
def nra_ut_00():
    ''' Approximating x^3 - x - 2. '''
    fexpr = make_pwr('x', 3.0)
    fexpr = make_plus(fexpr, make_prod(make_const(-1.0), make_pwr('x', 1.0)))
    fexpr = make_plus(fexpr, make_const(-2.0))
    # print('fexpr',str(fexpr))
    print(nra(fexpr, make_const(1.0), make_const(10)))
Ejemplo n.º 13
0
def arm_tumor_test():
    yt = make_prod(make_const(0.003 * math.pi), make_pwr('r', 3.0))
    print(yt)
    dydt = dydt_given_x_dxdt(yt, make_const(10.3), make_const(-1.75))
    assert not dydt is None
    assert isinstance(dydt, const)
    print(dydt)
Ejemplo n.º 14
0
def opt_prob_1c():
    f1 = lambda x, y: 3*x - 2*y


    #find corner points:
    ln1 = make_line_eq(make_plus(make_var('x'),make_var('y')), make_const(0.0))# x+y=0
    ln2 = make_line_eq(make_var('x'),make_var('y'))# x-y=0 => x=y
    ln3 = make_line_eq(make_plus(make_prod(make_const(-2.0), make_var('x')),make_prod(make_const(4.0), make_var('y'))), make_const(5.0))# -2x+4y=5


    p12 = line_intersection(ln1, ln2)
    p13 = line_intersection(ln1, ln3)

    p23 = line_intersection(ln2, ln3)


    possible_cps = [p12,p13,p23]

    corner_points = [pt for pt in possible_cps if -2*pt.get_x().get_val() + 4*pt.get_y().get_val() <= 5] # -2x+4y<=5
    corner_points = [pt for pt in corner_points if pt.get_x().get_val() + pt.get_y().get_val() >= 0] #x+y>=0
    corner_points = [pt for pt in corner_points if pt.get_x().get_val() - pt.get_y().get_val() <= 0] #x-y<=0

    # for pt in corner_points:
    #     print(pt.get_x().get_val(), pt.get_y().get_val())

    print '1c: ',maximize_obj_fun(f1, corner_points)
Ejemplo n.º 15
0
def opt_prob_1b():
    f1 = lambda x, y: x/2 + y

    #find corner points:
    ln1 = make_line_eq(make_var('x'), make_const(0.0))
    ln2 = make_line_eq(make_var('y'), make_const(2.0))

    ln3 = make_line_eq(make_var('x'), make_var('y'))#x=y
    ln4 = make_line_eq(make_plus(make_var('x'),make_var('y')), make_const(6.0))# x+y=6


    p12 = line_intersection(ln1, ln2)
    p13 = line_intersection(ln1, ln3)
    p14 = line_intersection(ln1, ln4)

    p23 = line_intersection(ln2, ln3)
    p24 = line_intersection(ln2, ln4)

    p34 = line_intersection(ln3, ln4)

    possible_cps = [p12,p13,p14,p23,p24,p34]

    # for pt in possible_cps:
    #     print pt

    corner_points = [pt for pt in possible_cps if pt.get_x().get_val() + pt.get_y().get_val() <= 6] #x+y <= 6
    corner_points = [pt for pt in corner_points if pt.get_x().get_val() >= pt.get_y().get_val()] #x>=y
    corner_points = [pt for pt in corner_points if pt.get_x().get_val() >= 0] #x>=0
    corner_points = [pt for pt in corner_points if pt.get_y().get_val() >= 2] #y>=2


    # for pt in corner_points:
    #     print(pt.get_x().get_val(), pt.get_y().get_val())

    print '1b: ',minimize_obj_fun(f1, corner_points)
Ejemplo n.º 16
0
def opt_prob_1a():
    f1 = lambda x, y: 2*x + y


    #find corner points:
    ln1 = make_line_eq(make_var('x'), make_const(1.0))
    ln2 = make_line_eq(make_var('y'), make_const(1.0))

    ln3 = make_line_eq(make_var('x'), make_const(5.0))
    ln4 = make_line_eq(make_var('y'), make_const(5.0))

    p12 = line_intersection(ln1, ln2)
    # p13 = line_intersection(ln1, ln3) #parallel lines
    p14 = line_intersection(ln1, ln4)

    p23 = line_intersection(ln2, ln3)
    # p24 = line_intersection(ln2, ln4) #parallel lines

    p34 = line_intersection(ln3, ln4)

    possible_cps = [p12,p14,p23,p34]

    corner_points = [pt for pt in possible_cps if pt.get_x().get_val() + pt.get_y().get_val() <= 6] #x+y <= 6

    # for pt in corner_points:
    #     print(pt.get_x().get_val(), pt.get_y().get_val())

    print '1a: ',maximize_obj_fun(f1, corner_points)
Ejemplo n.º 17
0
def plot_spread_of_news(p, k, tl, tu):
    assert isinstance(p, const) and isinstance(k, const)
    assert isinstance(tl, const) and isinstance(tu, const)

    expr = spread_of_news_model(p, k)
    news = tof(expr)

    expr_deriv = deriv(expr)
    news_deriv = tof(expr_deriv)

    min = tl.get_val()
    max = tu.get_val()

    xvals = np.linspace(min, max, 10000)
    yvals1 = np.array([news(x) for x in xvals])
    yvals2 = np.array([news_deriv(x) for x in xvals])

    fig1 = plt.figure(1)
    fig1.suptitle("Spread of News")
    plt.xlabel('t')
    plt.ylabel('snf and dsnf')
    ymax = max(np.max(yvals1), np.max(yvals2))
    ymin = min(np.min(yvals1), np.min(yvals2))
    plt.ylim([ymin, ymax])
    plt.xlim([min, max])
    plt.grid()
    plt.plot(xvals, yvals1, label='snf', c='r')
    plt.plot(xvals, yvals2, label='dnsf', c='b')
    plt.legend(loc='best')
    plt.show()

    if __name__ == '__main__':
        print("test 1: ")
        fex = percent_retention_model(make_const(1.0), make_const(1.0))
Ejemplo n.º 18
0
    def test_11(self):
        '''
        (x+1)^4 *(4x-1)^2 => (x+1)^4 *(4x-1)^2 * ( (4/(x+1) + 8/(4x-1))

        ln(x+1)^4
        '''
        print('*******Test 10********')
        fex1 = make_pwr_expr(make_plus(make_pwr('x', 1.0), make_const(1.0)), 4.0)
        fex2 = make_pwr_expr(make_plus(make_prod(make_const(4.0),
                                                 make_pwr('x', 1.0)),
                                       make_const(-1.0)), 2.0)
        fex = make_prod(fex1, fex2)
        print(fex)
        drv = logdiff(fex)
        assert not drv is None
        print(drv)
        drvf = tof(drv)
        assert not drvf is None
        def gt_drvf(x):
            z1 = ((x + 1.0) **4.0) * ((4*x - 1.0)** 2.0)
            z2 = (4.0/(x + 1.0)) + (8.0/ (4*x - 1.0))
            return z1 * z2

        err = 0.0001
        for i in range(1, 10):
            #print(drvf(i), gt_drvf(i))
            assert abs(gt_drvf(i) - drvf(i)) <= err
        print('Test 11: pass')
Ejemplo n.º 19
0
 def test_assgn_01_prob_02_ut_04(self):
     print('\n***** Assign 01: Unit Test 04 ************')
     fex1 = prod(mult1=make_const(2),
                 mult2=make_pwr('x', 2.0))
     fex2 = plus(elt1=fex1, elt2=make_const(2.0))
     graph_drv(fex2, [-10, 10], [-50.0, 50.0])
     print('Assign 01: Unit Test 04: pass')
Ejemplo n.º 20
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.º 21
0
    def test_assign_03_prob_01_ut_05(self):
        print('\n***** Assign 03: Problem 01: Unit Test 05 *****')
        e1 = make_plus(make_pwr('x', 1.0), make_const(1.0))

        e2 = make_pwr('x', 3.0)
        e3 = make_prod(make_const(0.0), make_pwr('x', 2.0))
        e4 = make_plus(e2, e3)
        e5 = make_prod(make_const(5.0), make_pwr('x', 1.0))
        e6 = make_plus(e4, e5)
        e7 = make_plus(e6, make_const(2.0))

        e8 = make_prod(e1, e7)
        # 1) print the expression we just constructed
        print('-- function expression is:\n')
        print(e8)
        # 2) differentiate and make sure that it not None
        drv = deriv(e8)
        assert not drv is None
        print('\n-- derivative is:\n')
        print(e8)
        # 3) convert drv into a function
        e8f = tof(drv)
        assert not e8f is None
        # steps 2) and 3) can be combined into tof(deriv(e6)).
        # 4) construct the ground truth function
        gt = lambda x: 4.0 * (x**3) + 3 * (x**2) + 10.0 * x + 7.0
        # 5) compare the ground gruth with what we got in
        # step 3) on an appropriate number range.
        print('\n--comparison with ground truth:\n')
        err = 0.00001
        for i in range(15):
            #print(e8f(i), gt(i))
            assert abs(e8f(i) - gt(i)) <= err
        print('Assign 03: Problem 01: Unit Test 05: pass')
Ejemplo n.º 22
0
def spread_of_news_model(p, k):
    assert isinstance(p, const) and isinstance(k, const)

    expr = prod(
        p,
        plus(make_const(-1.0), pwr(math.e, prod(make_const(-1.73), var("t")))))

    return expr
Ejemplo n.º 23
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.º 24
0
def test_07():
    ln1 = make_line_eq(make_var('y'), make_plus(make_prod(make_const(-1.0/5.0),
                                                                                make_pwr('x', 1.0)),
                                                            make_const(10.0)))
    ln2 = make_line_eq(make_var('y'), make_plus(make_prod(make_const(1.0/5.0),
                                                                                make_pwr('x', 1.0)),
                                                            make_const(5.0)))
    print(line_intersection(ln1, ln2))
Ejemplo n.º 25
0
def test_01():
    f1 = make_prod(make_const(3.0), make_pwr('x', 1.0))
    f2 = make_plus(f1, make_const(100.0))
    print f2
    z = find_poly_1_zeros(f2)
    f2f = tof(f2)
    assert f2f(z.get_val()) == 0.0
    print str(z)
    print 'true zero?', f2f(z.get_val()) == 0.0
Ejemplo n.º 26
0
 def test_05(self):
     print("****Unit Test 05********")
     eq = radioactive_decay(make_const(0.021), make_const(8.0), make_const(10.0))
     print(eq)
     eqf = tof(eq)
     assert not eqf is None
     err = 0.0001
     gt = lambda t: 8 * math.e ** (-0.021*t)
     assert abs(gt(10.0) - eqf(0)) <= err
     print("Unit Test 05: pass")
Ejemplo n.º 27
0
def test_11():
    ln1 = make_line_eq(make_var('x'), make_const(1.0))
    ln2 = make_line_eq(make_var('y'), make_prod(make_const(0.5),
                                                                make_pwr('x', 1.0)))
    print(line_intersection(ln1, ln2))
    ln3 = make_line_eq(make_var('y'), make_plus(make_prod(make_const(-3.0/4),
                                                                            make_pwr('x', 1.0)),
                                                        make_const(3.0)))
    print(line_intersection(ln1, ln3))
    print(line_intersection(ln2, ln3))
Ejemplo n.º 28
0
    def test_assign_03_prob_01_ut_06(self):
        print('\n***** Assign 03: Problem 01: Unit Test 06 *****')
        e1 = make_prod(make_const(2.0), make_pwr('x', 4.0))
        e2 = make_prod(make_const(-1.0), make_pwr('x', 1.0))
        e3 = make_plus(e1, e2)
        e4 = make_plus(e3, make_const(1.0))

        e5 = make_prod(make_const(-1.0), make_pwr('x', 5.0))
        e6 = make_prod(make_const(0.0), make_pwr('x', 4.0))
        e7 = make_plus(e5, e6)
        e8 = make_prod(make_const(0.0), make_pwr('x', 3.0))
        e9 = make_plus(e7, e8)
        e10 = make_prod(make_const(0.0), make_pwr('x', 2.0))
        e11 = make_plus(e9, e10)
        e12 = make_prod(make_const(0.0), make_pwr('x', 1.0))
        e13 = make_plus(e11, e12)
        e14 = make_plus(e13, make_const(1.0))

        e15 = make_prod(e4, e14)
        print('-- function expression is:\n')
        print(e15)
        drv = deriv(e15)
        assert not drv is None
        print('\n-- derivative is:\n')
        print(drv)
        e15f = tof(drv)
        assert not e15f is None
        gt = lambda x: -18.0 * (x**8) + 6.0 * (x**5) - 5.0 * (x**4) + 8.0 * (
            x**3) - 1.0
        err = 0.00001
        print('\n--comparison with ground truth:\n')
        for i in range(10):
            #print(e15f(i), gt(i))
            assert abs(e15f(i) - gt(i)) <= err
        print('Assign 03: Problem 01: Unit Test 06: pass')
Ejemplo n.º 29
0
def test_03():
    f1 = make_prod(make_const(1.0 / 3.0), make_pwr('x', 3.0))
    f2 = make_prod(make_const(-2.0), make_pwr('x', 2.0))
    f3 = make_prod(make_const(3.0), make_pwr('x', 1.0))
    f4 = make_plus(f1, f2)
    f5 = make_plus(f4, f3)
    poly = make_plus(f5, make_const(1.0))
    print 'f(x) = ', poly
    xtrma = loc_xtrm_1st_drv_test(poly)
    for i, j in xtrma:
        print i, str(j)
Ejemplo n.º 30
0
 def test_assign_03_prob_04_ut_03(self):
     print('\n***** Assign 03: Problem 04: Unit Test 03 *****')
     yt = make_prod(make_const(0.0075 * math.pi), make_pwr('r', 3.0))
     print(yt)
     dydt = dydt_given_x_dxdt(yt, make_const(5.5), make_const(-1.25))
     assert not dydt is None
     assert isinstance(dydt, const)
     print(dydt)
     err = 0.0001
     assert abs(dydt.get_val() + 2.67280812481) <= err
     print('Assign 03: Problem 04: Unit Test 03: pass')