Beispiel #1
0
def differentiateTokens(funclist, wrtVar):
    """Differentiates given tokens wrt given variable

    Arguments:
        funclist {list} -- list of function tokens
        wrtVar {string} -- with respect to variable

    Returns:
        diffFunc {list} -- list of differentiated tokens
        animNew {list} -- equation tokens for step-by-step
        commentsNew {list} -- comments for step-by-step
    """
    diffFunc = []
    animNew = []
    commentsNew = ["Differentiating with respect to " + r"$" + wrtVar + r"$" + "\n"]
    for func in funclist:
        if isinstance(func, Operator):
            diffFunc.append(func)
        else:
            newfunc = []
            while(isinstance(func, Function)):
                commentsNew[0] += r"$" + "\\frac{d}{d" + wrtVar + "} ( " + func.__str__() + ")" + r"$"
                funcCopy = copy.deepcopy(func)
                if wrtVar in funcCopy.functionOf():
                    if not isinstance(funcCopy, Constant):
                        for i, var in enumerate(funcCopy.value):
                            if var == wrtVar:
                                funcCopy.coefficient *= funcCopy.power[i]
                                funcCopy.power[i] -= 1
                                if(funcCopy.power[i] == 0):
                                    del funcCopy.power[i]
                                    del funcCopy.value[i]
                                    if funcCopy.value == []:
                                        funcCopy.__class__ = Constant
                                        funcCopy.value = funcCopy.coefficient
                                        funcCopy.coefficient = 1
                                        funcCopy.power = 1
                        commentsNew[0] += r"$" + r"= " + funcCopy.__str__() + r"\ ;\ " + r"$"
                        newfunc.append(funcCopy)
                    func.differentiate()
                    if not(isinstance(func, Constant) and func.value == 1):
                        newfunc.append(func)
                else:
                    funcCopy = (Zero())
                    newfunc.append(funcCopy)
                    commentsNew[0] += r"$" + r"= " + funcCopy.__str__() + r"\ ;\ " + r"$"

                if func.operand is None:
                    break
                else:
                    func = func.operand
                    if isinstance(func, Constant):
                        diffFunc = Zero()
                        break

            diffFunc.extend(newfunc)

    animNew.extend(diffFunc)
    return diffFunc, animNew, commentsNew
Beispiel #2
0
def differentiateTokens(funclist, wrtVar):
    """Differentiates given tokens wrt given variable

    Arguments:
        funclist {list} -- list of function tokens
        wrtVar {string} -- with respect to variable

    Returns:
        diffFunc {list} -- list of differentiated tokens
        animNew {list} -- equation tokens for step-by-step
        commentsNew {list} -- comments for step-by-step
    """
    diffFunc = []
    animNew = []
    commentsNew = ["Differentiating with respect to " + r"$" + wrtVar + r"$" + "\n"]
    for func in funclist:
        if isinstance(func, Operator):
            diffFunc.append(func)
        else:
            newExpression = Expression()
            newfunc = []
            while(isinstance(func, Function)):
                commentsNew[0] += r"$" + "\\frac{d}{d" + wrtVar + "} ( " + func.__str__() + ")" + r"$"
                funcCopy = copy.deepcopy(func)
                if wrtVar in funcCopy.functionOf():
                    if isinstance(funcCopy, Trigonometric) or isinstance(funcCopy, Logarithm) or isinstance(funcCopy, Variable) or isinstance(funcCopy, Exponential):
                        funcCopy = funcCopy.differentiate(wrtVar)
                        newfunc.append(funcCopy)
                        commentsNew[0] += r"$" + r"= " + funcCopy.__str__() + r"\ ;\ " + r"$"
                else:
                    funcCopy = Zero()
                    newfunc.append(funcCopy)
                    commentsNew[0] += r"$" + r"= " + funcCopy.__str__() + r"\ ;\ " + r"$"
                newfunc.append(Multiply())
                if func.operand is None:
                    break
                else:
                    func = func.operand
                    if isinstance(func, Constant):
                        diffFunc = Zero()
                        break
            newfunc.pop()
            newExpression.tokens = newfunc
            diffFunc.extend([newExpression])
    animNew.extend(diffFunc)
    return diffFunc, animNew, commentsNew
Beispiel #3
0
def integrateTokens(funclist, wrtVar):
    """Integrates given tokens wrt given variable

    Arguments:
        funclist {list} -- list of funtion tokens
        wrtVar {string} -- with respect to variable

    Returns:
        intFunc {list} -- list of integrated tokens
        animNew {list} -- equation tokens for step-by-step
        commentsNew {list} -- comments for step-by-step
    """
    intFunc = []
    animNew = []
    commentsNew = [
        "Integrating with respect to " + r"$" + wrtVar + r"$" + "\n"
    ]
    for func in funclist:
        if isinstance(func, Operator):  # add isFuntionOf
            intFunc.append(func)
        else:
            newfunc = []
            while (isinstance(func, Function)):
                commentsNew[0] += r"$" + "\int \ " + "( " + func.__str__(
                ) + ")" + " d" + wrtVar + r"$"
                funcCopy = copy.deepcopy(func)
                if wrtVar in funcCopy.functionOf():
                    if not isinstance(funcCopy, Constant):
                        for i, var in enumerate(funcCopy.value):
                            log = False
                            if var == wrtVar:
                                if (funcCopy.power[i] == -1):
                                    log = True
                                    funcLog = Logarithm()
                                    funcLog.operand = Variable()
                                    funcLog.operand.coefficient = 1
                                    funcLog.operand.value.append(
                                        funcCopy.value[i])
                                    funcLog.operand.power.append(1)
                                    del funcCopy.power[i]
                                    del funcCopy.value[i]
                                    if funcCopy.value == []:
                                        funcCopy.__class__ = Constant
                                        funcCopy.value = funcCopy.coefficient
                                        funcCopy.coefficient = 1
                                        funcCopy.power = 1
                                    funcCopy = [funcCopy]
                                    funcJoin = Binary()
                                    funcJoin.value = '*'
                                    funcCopy.append(funcJoin)
                                    funcCopy.append(funcLog)
                                else:
                                    funcCopy.power[i] += 1
                                    funcCopy.coefficient /= funcCopy.power[i]

                        if log:
                            commentsNew[0] += r"$" + "= " + funcCopy[0].__str__(
                            ) + "*" + funcCopy[2].__str__() + "\ ;\ " + r"$"
                            newfunc.extend(funcCopy)
                        else:
                            commentsNew[0] += r"$" + "= " + funcCopy.__str__(
                            ) + "\ ;\ " + r"$"
                            newfunc.append(funcCopy)
                else:
                    if isinstance(funcCopy, Variable):
                        funcCopy.value.append(wrtVar)
                        funcCopy.power.append(1)
                    if isinstance(funcCopy, Constant):
                        coeff = funcCopy.value
                        funcCopy = Variable()
                        funcCopy.coefficient = coeff
                        funcCopy.value.append(wrtVar)
                        funcCopy.power.append(1)
                    newfunc.append(funcCopy)
                    commentsNew[0] += r"$" + "= " + funcCopy.__str__(
                    ) + "\ ;\ " + r"$"

                if func.operand is None:
                    break
                else:
                    func = func.operand
                    if isinstance(func, Constant):
                        intFunc = Zero()
                        break

            intFunc.extend(newfunc)

    animNew.extend(intFunc)
    return intFunc, animNew, commentsNew