Ejemplo n.º 1
0
def antideriv(i):
    ## CASE 1: i is a constant
    if isinstance(i, const):
        #3 => 3x^1
        return prod(i, make_pwr('x', 1.0))
    ## CASE 2: i is a pwr
    elif isinstance(i, pwr):
        #x^d => 1/(d+1) * x^(d+1)
        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):
            if d.get_val() == -1:
                return make_ln(make_absv(pwr(b, const(1.0))))
            else:
                r = const(d.get_val() + 1.0)
                return prod(quot(const(1.0), r), pwr(b, r))
        ## CASE 2.2: b is e
        elif is_e_const(b):  # e^(kx) => 1/k  * e(kx)
            if isinstance(d, prod):
                k = d.get_mult1()
                return prod(quot(const(1.0), k), i)
            else:
                raise Exception('antideriv: unknown case')
        # ## CASE 2.3: b is a sum
        elif isinstance(b, plus):  #(1+x)^-3
            r = const(d.get_val() + 1.0)
            if isinstance(d, const) and d.get_val() == -1:
                return make_ln(make_absv(b))
            elif isinstance(b.get_elt1(), prod):  #(3x+2)^4 => 1/3 * anti(
                if isinstance(d, const) and d.get_val() < 0:
                    return prod(quot(const(-1.0),
                                     b.get_elt1().get_mult1()), pwr(b, r))
                else:
                    return prod(
                        quot(const(1.0), prod(b.get_elt1().get_mult1(), r)),
                        pwr(b, r))
            else:
                return prod(quot(const(1.0), r), pwr(b, r))
        else:
            raise Exception('antideriv: unknown case')
    ### CASE 3: i is a sum, i.e., a plus object.
    elif isinstance(i, plus):
        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):
        return prod(i.get_mult1(), antideriv(i.get_mult2()))
    else:
        raise Exception('antideriv: unknown case')
Ejemplo n.º 2
0
def ln_deriv(p):
    #ln[g(x)] = 1/g(x) * g'(x)
    assert isinstance(p, ln)
    g = p.get_expr()
    if isinstance(g, prod):
        m1 = g.get_mult1()
        m2 = g.get_mult2()
        #(x)(x+1)
        #ln(x) + ln(x+1)
        if isinstance(m1, pwr) and isinstance(m2, pwr):
            return plus(quot(prod(m1.get_deg(), deriv(m1.get_base())), m1.get_base()),
                        quot(prod(m2.get_deg(), deriv(m2.get_base())), m2.get_base()))
        return plus(ln_deriv(make_ln(m1)), ln_deriv(make_ln(m2)))
    else:
        return prod(quot(const(1.0), g), deriv(g))
Ejemplo n.º 3
0
def problem_2_deriv():
    fexpr = make_prod(make_pwr('t', 2.0), make_ln(make_pwr('t', 1.0)))
    print(fexpr)
    dv = deriv(fexpr)
    print(dv)
    second_dv = deriv(dv)
    tof_second = tof(second_dv)
    print(tof_second(1.0))
Ejemplo n.º 4
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')
Ejemplo n.º 5
0
 def test_prob_01_ut_08(self):
     print('\n***** Problem 01: UT 08 ************')
     fex = make_ln(make_absv(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: 1.0/x
     err = 0.0001
     for i in range(1, 10):
         #print(drvf(i), gt(i))
         assert abs(gt(i) - drvf(i)) <= err
     print('Problem 01: UT 08: pass')
Ejemplo n.º 6
0
 def test_prob_01_ut_06(self):
     print('\n***** Problem 01: UT 06 ************')
     fex = make_prod(make_pwr('x', 1.0),
                     make_ln(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: 1.0 + math.log(x, math.e)
     err = 0.0001
     for i in range(1, 10):
         print(drvf(i), gt(i))
     assert abs(gt(i) - drvf(i)) <= err
     print('Problem 01: UT 06: pass')
Ejemplo n.º 7
0
def logdiff(p):
    # assert isinstance(p, prod)
    # if isinstance(p, prod):
    #     m1 = p.get_mult1()
    #     m2 = p.get_mult2()
    #
    #     if isinstance(m1, plus):#(x+1)(x+2)
    #         if isinstance(m2, plus):
    #             return prod(p, plus(prod(quot(const(1.0), m1),
    #                                  deriv(m1)),
    #                             prod(quot(const(1.0), m2),
    #                                  deriv(m2))))
    #     elif isinstance(m1, pwr):#x(x+1)(x+3)
    #         if isinstance(m2, prod):
    #             #return prod(, logdiff(m2))
    #             return prod(p, ln_deriv(make_ln(p)))

    return prod(p, ln_deriv(make_ln(p)))
Ejemplo n.º 8
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.º 9
0
def applyLog(expr):
    if isinstance(expr, quot):
        lnExpr1 = applyLog(expr.get_num())
        lnExpr2 = applyLog(expr.get_denom())
        minusPart = prod(const(-1.0), lnExpr2)
        return plus(lnExpr1, minusPart)
    if isinstance(expr, prod):
        if isinstance(expr.get_mult1(), const) and isinstance(
                expr.get_mult2(), pwr):
            return make_ln(expr)
        elif isinstance(expr.get_mult1(), pwr) and isinstance(
                expr.get_mult2(), const):
            return make_ln(expr)
        elif isinstance(expr.get_mult1(), const) and isinstance(
                expr.get_mult1(), const):
            return make_ln(expr)
        else:
            lnExpr1 = applyLog(expr.get_mult1())
            lnExpr2 = applyLog(expr.get_mult2())
            return make_plus(lnExpr1, lnExpr2)
    if isinstance(expr, plus):
        if isinstance(expr.get_elt1(), pwr):
            if isinstance(expr.get_elt2(), const):
                return make_ln(expr)
            if isinstance(expr.get_elt2(), pwr):
                return make_ln(expr)
            else:
                if isinstance(expr.get_elt2(), plus):
                    lnExpr = applyLog(expr.get_elt2())
                    return make_plus(make_ln(expr.get_elt1()), lnExpr)
                else:
                    return make_ln(expr)
        elif isinstance(expr.get_elt1(), const):
            if isinstance(expr.get_elt2(), const):
                return make_ln(expr)
            elif isinstance(expr.get_elt2(), pwr):
                return make_ln(expr)
            else:
                if isinstance(expr.get_elt2(), plus):
                    lnExpr = applyLog(expr.get_elt2())
                    return make_plus(make_ln(expr.get_elt1()), lnExpr)
                else:
                    return make_ln(expr)
        elif isinstance(expr.get_elt2(), const):
            if isinstance(expr.get_elt1(), const):
                return make_ln(expr)
            elif isinstance(expr.get_elt1(), pwr):
                return make_ln(expr)
            else:
                if isinstance(expr.get_elt1(), plus):
                    lnExpr = applyLog(expr.get_elt1())
                    return make_plus(lnExpr, make_ln(expr.get_elt2()))
                else:
                    return make_ln(expr)
        elif isinstance(expr.get_elt2(), pwr):
            if isinstance(expr.get_elt1(), const):
                return make_ln(expr)
            if isinstance(expr.get_elt1(), pwr):
                return make_ln(expr)
            else:
                if isinstance(expr.get_elt1(), plus):
                    lnExpr = applyLog(expr.get_elt1())
                    return make_plus(lnExpr, make_ln(expr.get_elt2()))
                else:
                    return make_ln(expr)
        else:
            lnExpr1 = applyLog(expr.get_elt1())
            lnExpr2 = applyLog(expr.get_elt2())
            return plus(lnExpr1, lnExpr2)
    elif isinstance(expr, pwr):
        if isinstance(expr.get_deg(), const):
            if expr.get_deg().get_val() == 1.0:
                if isinstance(expr.get_base(), var):
                    return make_ln(expr)
                else:
                    lnExpr = applyLog(expr.get_base())
                    return lnExpr
            else:
                if isinstance(expr.get_base(), var):
                    return make_prod(expr.get_deg(), make_ln(expr))
                else:
                    lnExpr = applyLog(expr.get_base())
                    return make_prod(expr.get_deg(), lnExpr)

        else:
            if isinstance(expr.get_base(), var):
                return make_prod(expr.get_deg(), make_ln(expr))
            else:
                lnExpr = applyLog(expr.get_base())
                return make_prod(expr.get_deg(), lnExpr)
    elif isinstance(expr, const):
        return make_ln(expr)
Ejemplo n.º 10
0
def logdiff(expr):
    newln = make_ln(expr)
    return prod(expr, deriv(newln))