Beispiel #1
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')
Beispiel #2
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')
Beispiel #3
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')
Beispiel #4
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')
Beispiel #5
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')
Beispiel #6
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')
Beispiel #7
0
def nra_ut_15():
    f1 = make_prod(
        const(60.0),
        make_quot(
            make_plus(
                const(1.0),
                make_prod(
                    const(-1.0),
                    make_pwr_expr(make_plus(const(1.0), make_pwr('x', 1.0)),
                                  const(-6)))), make_pwr('x', 1.0)))
    f2 = make_prod(
        const(995.0),
        make_pwr_expr(make_plus(const(1.0), make_pwr('x', 1.0)), const(-6.5)))
    fsum = make_plus(f1, f2)
    fexpr = make_plus(fsum, const(-970))
    approx = nra(fexpr, const(0.03), const(1000000))
    print(approx)
Beispiel #8
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()
 def test_prob_02_ut_03(self):
     print('\n***** Problem 02: UT 03 ************')
     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)
     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
     for i in range(0, 20):
         #print(drvf(i), gt_drvf(i))
         assert abs(gt_drvf(i) - drvf(i)) <= 0.001
     print('Problem 02: UT 03: pass')
Beispiel #10
0
 def test_prob_01_ut_05(self):
     print('\n***** Problem 01: UT 05 ************')
     fex = make_pwr_expr(make_ln(make_pwr('x', 1.0)), 5.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.log(x, math.e)**4))/x
     err = 0.0001
     for i in range(1, 5):
         print(drvf(i), gt(i))
         assert abs(gt(i) - drvf(i)) <= err
     print('Problem 01: UT 05: pass')
Beispiel #11
0
 def test_assign_03_prob_01_ut_10(self):
     print('\n***** Assign 03: Problem 01: Unit Test 10 *****')
     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))
             assert abs(pexdrvf(i) - gt(i)) <= 0.001
     print('Assign 03: Problem 01: Unit Test 10: pass')
Beispiel #12
0
def taylor_poly(fexpr, a, n):
    assert isinstance(a, const)
    assert isinstance(n, const)

    tof_exp = tof(fexpr)
    ex = fexpr
    result = const(tof_exp(a.get_val()))

    for i in range(1, int(n.get_val())):
        drv = deriv(ex)
        drv_tof = tof(drv)

        inside = const(drv_tof(a.get_val()) / math.factorial(i))
        x = make_plus(make_pwr('x', 1.0), make_prod(const(-1.0), a))

        pw = make_pwr_expr(x, i)

        result = make_plus(result, make_prod(inside, pw))
        ex = drv

    return result
Beispiel #13
0
def antideriv(i):
    ## CASE 1: i is a constant
    if isinstance(i, const):
        return prod(i, make_pwr('x', 1.0))
    ## CASE 2: i is a pwr
    elif isinstance(i, pwr):
        b = i.get_base()
        d = i.get_deg()
        ## CASE 2.1: b is var and d is constant.
        if isinstance(b, var) and isinstance(d, const):
            #(b^(d+1))/(d+1)
            if d.get_val() == -1.0:
                return ln(make_pwr('x', 1.0))
            return quot(pwr(b, const(d.get_val() + 1.0)),
                        const(d.get_val() + 1.0))
        ## CASE 2.2: b is e
        elif is_e_const(b):
            if isinstance(d, const) or isinstance(d, pwr) or isinstance(
                    d, var) or isinstance(d, prod):
                if isinstance(d, const):
                    return prod(const(b.get_value()**d.getvalue()), var('x'))
                elif isinstance(d, var):  #e^x
                    return i
                elif isinstance(d, pwr) and d.get_deg() == 1.0:  #e^x^1
                    return i
                elif isinstance(d, prod):
                    left = d.get_mult1()
                    right = d.get_mult2()
                    if isinstance(left, var) or (isinstance(
                            left, pwr) and left.get_deg().get_val() == 1.0):
                        # e^xa == e^ax => (1/a) * e^ax
                        return prod(quot(const(1.0), right), i)
                    elif isinstance(right, var) or (isinstance(
                            right, pwr) and right.get_deg().get_val() == 1.0):
                        # e^ax => (1/a) * e^ax
                        return prod(quot(const(1.0), left), i)
                    else:
                        raise Exception('e^(unknown expression)')
                else:
                    raise Exception(
                        'antideriv: unknown case -- did not implement substitution'
                    )
        ## CASE 2.3: b is a sum
        elif isinstance(b, plus):
            #(x+y)^d => ((x+y)^(d+1))/(d+1)
            if isinstance(d, const):
                if d.get_val() == -1.0:
                    return prod(pwr(deriv(b), const(d.get_val() + 1.0)), ln(b))
                    # return prod(pwr(deriv(b),const(d.get_val()+1.0)) , quot(make_pwr_expr(b,const(d.get_val()+1.0)),const(d.get_val()+1.0)))
                else:
                    # return prod(pwr(deriv(b),const(d.get_val()+1.0)) , quot(make_pwr_expr(b,const(d.get_val()+1.0)),const(d.get_val()+1.0)))
                    # return prod(make_pwr_expr(prod(deriv(b),const(d.get_val()+1.0)), -1.0) , quot(make_pwr_expr(b,const(d.get_val()+1.0)),const(d.get_val()+1.0)))

                    # formula from this page (https://www.quora.com/How-do-I-find-anti-derivative-of-ax+b-2)
                    return prod(
                        make_pwr_expr(prod(deriv(b), const(d.get_val() + 1.0)),
                                      -1.0),
                        make_pwr_expr(b, const(d.get_val() + 1.0)))
            else:
                raise Exception('antideriv: unknown case')
        else:
            raise Exception('antideriv: unknown case')
    ### CASE 3: i is a sum, i.e., a plus object.
    elif isinstance(i, plus):
        # S(n+m) => S(n) + S(m)
        return plus(antideriv(i.get_elt1()), antideriv(i.get_elt2()))

    ### CASE 4: is is a product, i.e., prod object,
    ### where the 1st element is a constant.
    elif isinstance(i, prod):
        left = i.get_mult1()
        right = i.get_mult2()
        if isinstance(left, const):
            return prod(left, antideriv(right))
        elif isinstance(right, const):
            return prod(right, antideriv(left))
        else:
            raise Exception(
                'antideriv: unknown case -- did not implement special rules (like substitution)'
            )
    else:
        raise Exception('antideriv: unknown case' + str(type(i)) + str(i))