Example #1
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)
Example #2
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')
Example #3
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')
Example #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')
Example #5
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')
Example #6
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))))
Example #7
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")
Example #8
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))
Example #9
0
def nra_ut_13():
    #approximate a zero for x^3-3x^2+14x-2
    f1 = make_plus(make_pwr('x', 3.0),
                   make_prod(const(-3.0), make_pwr('x', 2.0)))
    f2 = make_plus(make_prod(const(14.0), make_pwr('x', 1.0)), const(-2.0))
    fexpr = make_plus(f1, f2)
    approx = nra(fexpr, const(1.0), const(1000000))
    print(approx)
Example #10
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))
Example #11
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)
Example #12
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))))))
Example #13
0
def max_rev_test():
    print("***Max Revenue Test 01 *****")
    e1 = make_prod( make_const(1.0/12.0), make_pwr('x', 2.0))
    e2 = make_prod(make_const(-10.0), make_pwr('x', 1.0))
    sum1 = make_plus(e1, e2)
    demand_expr = make_plus(sum1, make_const(300.0))
    num_units, rev, price = maximize_revenue(demand_expr, constraint=lambda x: 0 <= x <= 60)
    print('x = ', num_units.get_val())
    print("rev= ", rev.get_val())
    print('price = ', price.get_val())
    print("Max Revenue Test: pass")
Example #14
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)
Example #15
0
def test_05():  #y = x; y = 2x; y = 3x-10
    ln1 = make_line_eq(make_var('y'), make_pwr('x', 1.0))
    ln2 = make_line_eq(make_var('y'),
                       make_prod(make_const(2.0), make_pwr('x', 1.0)))
    ln3 = make_line_eq(
        make_var('y'),
        make_plus(make_prod(make_const(3.0), make_pwr('x', 1.0)),
                  make_const(-10.0)))
    print(get_line_coeffs(ln1))
    print(get_line_coeffs(ln2))
    print(get_line_coeffs(ln3))
Example #16
0
def test_02():
    f0 = make_prod(make_const(0.5), make_pwr('x', 2.0))
    f1 = make_prod(make_const(6.0), make_pwr('x', 1.0))
    f2 = make_plus(f0, f1)
    poly = make_plus(f2, make_const(0.0))
    print '+++', poly
    zeros = find_poly_2_zeros(poly)
    for c in zeros:
        print c
    pf = tof(poly)
    for c in zeros:
        assert abs(pf(c.get_val()) - 0.0) <= 0.0001
    print 'True zeros!'
Example #17
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')
Example #18
0
def problem_1_deriv():  # still has problems

    # f(x) =(x+1)(2x+1)(3x+1) /(4x+1)^.5
    fex1 = make_plus(make_pwr('x', 1.0), const(1.0))
    fex2 = make_plus(make_prod(const(2.0), make_pwr('x', 1.0)), const(1.0))
    fex3 = make_plus(make_prod(const(3.0), make_pwr('x', 1.0)), const(1.0))
    fex4 = make_prod(fex1, fex2)

    fex5 = make_prod(fex4, fex3)

    fex6 = make_pwr_expr(
        make_plus(make_prod(const(4.0), make_pwr('x', 1.0)), const(1.0)), 0.5)
    fex = make_quot(fex5, fex6)
    print(fex)
    drv = deriv(fex)
    print('drv: ', drv)
    drvf = tof(drv)

    def gt_drvf(x):
        n = (60.0 * x**3) + (84 * x**2) + 34 * x + 4
        d = (4 * x + 1)**(3 / 2)
        return n / d

    for i in range(1, 10):
        print(drvf(i), gt_drvf(i))
        assert abs(gt_drvf(i) - drvf(i)) <= 0.001

    assert drv is not None
    print(drv)

    # zeros = find_poly_2_zeros(drv)
    # print(zeros)

    f1 = tof(fex)
    f2 = tof(deriv(fex))

    xvals = np.linspace(1, 5, 10000)
    yvals1 = np.array([f1(x) for x in xvals])
    yvals2 = np.array([f2(x) for x in xvals])
    fig1 = plt.figure(1)
    fig1.suptitle('Graph')
    plt.xlabel('x')
    plt.ylabel('y')
    plt.ylim()
    plt.xlim()
    plt.grid()
    plt.plot(xvals, yvals1, label=fex.__str__(), c='r')
    plt.plot(xvals, yvals2, label=deriv(fex).__str__(), c='g')
    plt.legend(loc='best')
    plt.show()
Example #19
0
def test_05():
    ln1 = make_line_eq(make_var('y'), make_pwr('x', 1.0))
    ln2 = make_line_eq(make_var('y'), make_prod(make_const(2.0),
                                                                 make_pwr('x', 1.0)))
    ln3 = make_line_eq(make_var('y'), make_plus(make_prod(make_const(3.0),
                                                                            make_pwr('x', 1.0)),
                                                                    make_const(-10.0)))
    print(get_line_coeffs(ln1))
    print(get_line_coeffs(ln2))
    print(get_line_coeffs(ln3))

    print(line_intersection(ln1, ln2))
    print(line_intersection(ln2, ln3))
    print(line_intersection(ln1, ln3))
Example #20
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)
Example #21
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')
Example #22
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))
Example #23
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))
Example #24
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
Example #25
0
def test_11():
    f1 = make_prod(make_const(-1.0), make_pwr('x', 3.0))
    f2 = make_prod(make_const(8.5), make_pwr('x', 2.0))
    f3 = make_prod(make_const(0.0), make_pwr('x', 0.0))
    f4 = make_plus(f1, f2)
    f5 = make_plus(f4, f3)
    f6 = make_plus(f5, make_const(100.0))
    print(f6)

    ips = find_infl_pnts(f6)
    err = 0.0001
    assert len(ips) == 1
    ip = ips[0]
    # assert abs(ip.get_x().get_val() - 1.0) <= err
    # assert abs(ip.get_y().get_val() - 3.0) <= err
    print("inflection points: ", ip)
Example #26
0
def test_12():
    ln1 = make_line_eq(make_var('x'), make_const(0.0))
    ln2 = make_line_eq(make_var('y'), make_const(0.0))
    ln3 = make_line_eq(make_var('y'), make_plus(make_prod(make_const(-4.0/3),
                                                                            make_pwr('x', 1.0)),
                                                        make_const(160.0)))
    ln4 = make_line_eq(make_var('y'), make_plus(make_prod(make_const(-0.5),
                                                                            make_pwr('x', 1.0)),
                                                        make_const(120.0)))
    print(ln1)
    print(ln3)
    print(line_intersection(ln1, ln3))
    print(ln2)
    print(ln3)
    print(line_intersection(ln2, ln3))
    print(line_intersection(ln3, ln4))
Example #27
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)))
Example #28
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))
Example #29
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')
Example #30
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)