Пример #1
0
def multiplyVariables(variable1, variable2, coeff):

    variable = Variable()
    variable.value = []
    variable.value.extend(variable1.value)
    variable.power = []
    variable.power.extend(variable1.power)
    if isNumber(variable1.coefficient):
        variable.coefficient = float(variable1.coefficient)
    elif isinstance(variable1.coefficient, Function):
        variable.coefficient = evaluateConstant(variable1.coefficient)
    else:
        variable.coefficient = variable1.coefficient
    for j, var in enumerate(variable.value):
        found = False
        for k, var2 in enumerate(variable2.value):
            if var == var2:
                if isNumber(variable.power[j]) and isNumber(variable2.power[k]):
                    variable.power[j] += variable2.power[k]
                    found = True
                    break
        if not found:
            variable.value.append(variable2.value[j])
            variable.power.append(variable2.power[j])
    variable.coefficient *= variable2.coefficient
    variable.coefficient *= coeff
    # removeScopes.append(tokens[i].scope)
    # removeScopes.append(tokens[i+1].scope)
    return variable
Пример #2
0
 def __mul__(self, other):
     if other.isZero():
         return other
     elif self.isZero():
         return self
     elif isinstance(other, Constant):
         const = Constant(self.calculate() * other.calculate())
         return const
     elif isinstance(other, Variable):
         variable = Variable()
         variable.coefficient = self.calculate() * other.coefficient
         variable.value.extend(other.value)
         variable.power.extend(other.power)
         self = variable
         return variable
     elif isinstance(other, Expression):
         if other.power == 1:
             other.tokens[0] = self * other.tokens[0]
             for i, var in enumerate(other.tokens):
                 if other.tokens[i-1].value == '+' or other.tokens[i-1].value == '-':
                     other.tokens[i] = self * var
         else:
             if isinstance(other.power, Constant) or isinstance(other.power, int) or isinstance(other.power, float):
                 self = self ** (-1 * other.power)
                 for i, var in enumerate(other.tokens):
                     if other.tokens[i - 1].value == '+' or other.tokens[i - 1].value == '-':
                         other.tokens[i] = self * var
             else:
                 other.coefficient = self * other.coefficient
     else:
         other.coefficient = self.calculate() * other.coefficient
     return other
Пример #3
0
def integrateTokens(funclist, wrtVar):
    """Integrates given tokens wrt given variable

    Arguments:
        funclist {list} -- list of function 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 isfunctionOf
            intFunc.append(func)
        else:
            newfunc = []
            commentsNew[0] += r"$" + r"\int \ " + r"( " + func.__str__(
            ) + ")" + r" d" + wrtVar + r"$"
            funcCopy = copy.deepcopy(func)
            if wrtVar in funcCopy.functionOf():
                if isinstance(funcCopy, Variable):
                    log = False
                    funcCopy, log = funcCopy.integrate(wrtVar)
                    if log:
                        commentsNew[0] += r"$" + r"= " + funcCopy[0].__str__(
                        ) + r"*" + funcCopy[2].__str__() + r"\ ;\ " + r"$"
                        newfunc.extend(funcCopy)
                    else:
                        commentsNew[0] += r"$" + r"= " + funcCopy.__str__(
                        ) + r"\ ;\ " + r"$"
                        newfunc.append(funcCopy)
                elif isinstance(funcCopy, Trigonometric):
                    funcCopy = funcCopy.integrate(wrtVar)
                    newfunc.append(funcCopy)
                    commentsNew[0] += r"$" + r"= " + funcCopy.__str__(
                    ) + r"\ ;\ " + r"$"
            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"$" + r"= " + funcCopy.__str__(
                ) + r"\ ;\ " + r"$"
            intFunc.extend(newfunc)
    animNew.extend(intFunc)
    return intFunc, animNew, commentsNew
Пример #4
0
def multiplyVariableConstant(constant, variable, coeff):

    variable1 = Variable()
    variable1.value = []
    variable1.value.extend(variable.value)
    variable1.power = []
    variable1.power.extend(variable.power)
    if isNumber(variable.coefficient):
        variable1.coefficient = float(variable.coefficient)
    elif isinstance(variable.coefficient, Function):
        variable1.coefficient = evaluateConstant(variable.coefficient)
    else:
        variable.coefficient = variable1.coefficient

    variable1.coefficient *= evaluateConstant(constant)
    variable1.coefficient *= coeff
    # removeScopes.append(tokens[i].scope)
    # removeScopes.append(tokens[i-1].scope)
    return variable1
Пример #5
0
def division_constantVariable(constant, variable, coeff):

    variable1 = Variable()
    variable1.coefficient = (evaluateConstant(
        constant) / variable.coefficient) * coeff
    variable1.value = []
    variable1.power = []
    for i, var in enumerate(variable):
        variable1.value.append(var)
        variable1.power.append(-variable.power[i])
    return variable1
Пример #6
0
def expressionDivision(variables, tokens):

    removeScopes = []
    comments = []
    for i, token in enumerate(tokens):
        if isinstance(token, Binary):
            if token.value in ['/']:
                prev = False
                nxt = False
                if i != 0:
                    if tokens[i - 1].__class__ in [Variable, Constant]:
                        prev = True
                if i + 1 < len(tokens):
                    if tokens[i + 1].__class__ in [Variable, Constant]:
                        nxt = True
                if nxt and prev:
                    if isinstance(tokens[i + 1], Constant) and isinstance(tokens[i - 1], Constant):
                        comments.append("Dividing " + r"$" + tokens[i - 1].__str__() + r"$" + " by " + r"$" + tokens[i + 1].__str__() + r"$")
                        no_1 = False
                        no_2 = False
                        if isNumber(tokens[i - 1].value):
                            no_1 = True
                        if isNumber(tokens[i + 1].value):
                            no_2 = True
                        if no_1 and no_2:
                            tokens[i + 1].value = evaluateConstant(
                                tokens[i - 1]) / evaluateConstant(tokens[i + 1])
                            tokens[i + 1].power = 1
                            removeScopes.append(tokens[i].scope)
                            removeScopes.append(tokens[i - 1].scope)
                        elif no_1 and not no_2:
                            value = tokens[i - 1].value
                            power = tokens[i - 1].power
                            tokens[i - 1].value = [value]
                            tokens[i - 1].power = [power]
                            for val in tokens[i + 1].value:
                                tokens[i - 1].value.append(val)
                            for pows in tokens[i + 1].power:
                                tokens[i - 1].power.append(-pows)
                            removeScopes.append(tokens[i].scope)
                            removeScopes.append(tokens[i + 1].scope)
                        elif not no_1 and no_2:
                            tokens[i - 1].value.append(tokens[i + 1].value)
                            tokens[i - 1].power.append(-tokens[i + 1].power)
                            removeScopes.append(tokens[i].scope)
                            removeScopes.append(tokens[i + 1].scope)
                        elif not no_1 and not no_2:
                            for vals in tokens[i + 1].value:
                                tokens[i - 1].value.append(vals)
                            for pows in tokens[i + 1].power:
                                tokens[i - 1].power.append(pows)
                            removeScopes.append(tokens[i].scope)
                            removeScopes.append(tokens[i + 1].scope)
                        return variables, tokens, removeScopes, comments

                    elif isinstance(tokens[i + 1], Variable) and isinstance(tokens[i - 1], Variable):
                        comments.append("Dividing " + r"$" + tokens[i - 1].__str__() + r"$" + " by " + r"$" + tokens[i + 1].__str__() + r"$")
                        for j, var in enumerate(tokens[i + 1].value):
                            found = False
                            for k, var2 in enumerate(tokens[i - 1].value):
                                tokens[i-1].coefficient /= tokens[i+1].coefficient
                                if var == var2:
                                    if isNumber(tokens[i + 1].power[j]) and isNumber(tokens[i - 1].power[k]):
                                        tokens[i - 1].power[k] -= tokens[i + 1].power[j]
                                        if tokens[i - 1].power[k] == 0:
                                            del tokens[i - 1].power[k]
                                            del tokens[i - 1].value[k]
                                        found = True
                                        break
                            if not found:
                                tokens[i - 1].value.append(tokens[i + 1].value[j])
                                tokens[i - 1].power.append(-tokens[i + 1].power[j])

                            if len(tokens[i - 1].value) == 0:
                                constant = Constant()
                                constant.scope = tokens[i - 1].scope
                                constant.power = 1
                                constant.value = tokens[i - 1].coefficient
                                tokens[i - 1] = constant
                            removeScopes.append(tokens[i].scope)
                            removeScopes.append(tokens[i + 1].scope)
                        return variables, tokens, removeScopes, comments

                    elif (isinstance(tokens[i + 1], Variable) and isinstance(tokens[i - 1], Constant)):
                        comments.append("Dividing " + r"$" + tokens[i - 1].__str__() + r"$" + " by " + r"$" + tokens[i + 1].__str__() + r"$")
                        val = evaluateConstant(tokens[i - 1])
                        scope = tokens[i - 1].scope
                        tokens[i - 1] = Variable()
                        tokens[i - 1].value = tokens[i + 1].value
                        tokens[i - 1].coefficient = val / \
                            tokens[i + 1].coefficient
                        tokens[i - 1].power = []
                        tokens[i - 1].scope = scope
                        for pows in tokens[i + 1].power:
                            tokens[i - 1].power.append(-pows)

                        removeScopes.append(tokens[i].scope)
                        removeScopes.append(tokens[i + 1].scope)
                        return variables, tokens, removeScopes, comments

                    elif (isinstance(tokens[i - 1], Variable) and isinstance(tokens[i + 1], Constant)):
                        comments.append("Dividing " + r"$" + tokens[i - 1].__str__() + r"$" + " by " + r"$" + tokens[i + 1].__str__() + r"$")
                        tokens[i - 1].coefficient /= evaluateConstant(tokens[i + 1])
                        removeScopes.append(tokens[i].scope)
                        removeScopes.append(tokens[i + 1].scope)
                        return variables, tokens, removeScopes, comments
    return variables, tokens, removeScopes, comments
Пример #7
0
def getLevelVariables(tokens):
    """Returns tokens of Function class from a list of function tokens

    Arguments:
        tokens {list} -- list of function tokens

    Returns:
        variables {list} -- list of tokens of Function class(Variable/Constant)
    """
    variables = []
    for i, term in enumerate(tokens):
        if isinstance(term, Variable):
            skip = False
            for var in variables:
                if var.value == term.value and var.power[0] == term.power:
                    var.power.append(term.power)
                    var.scope.append(term.scope)
                    var.coefficient.append(term.coefficient)
                    if i != 0 and isinstance(tokens[i - 1], Binary):
                        var.before.append(tokens[i - 1].value)
                        var.beforeScope.append(tokens[i - 1].scope)
                    else:
                        var.before.append('')
                        var.beforeScope.append('')
                    if i + 1 < len(tokens) and isinstance(tokens[i + 1], Binary):
                        var.after.append(tokens[i + 1].value)
                        var.afterScope.append(tokens[i + 1].scope)
                    else:
                        var.after.append('')
                        var.afterScope.append('')
                    skip = True
                    break
            if not skip:
                variable = Variable()
                variable.value = term.value
                variable.scope = [term.scope]
                variable.power = []
                variable.coefficient = []
                variable.before = []
                variable.beforeScope = []
                variable.after = []
                variable.afterScope = []
                variable.power.append(term.power)
                variable.coefficient.append(term.coefficient)
                if i != 0 and isinstance(tokens[i - 1], Binary):
                    variable.before.append(tokens[i - 1].value)
                    variable.beforeScope.append(tokens[i - 1].scope)
                else:
                    variable.before.append('')
                    variable.beforeScope.append('')
                if i + 1 < len(tokens) and isinstance(tokens[i + 1], Binary):
                    variable.after.append(tokens[i + 1].value)
                    variable.afterScope.append(tokens[i + 1].scope)
                else:
                    variable.after.append('')
                    variable.afterScope.append('')
                variables.append(variable)
        elif isinstance(term, Constant):
            skip = False
            for var in variables:
                if isinstance(var.value, list) and isNumber(var.value[0]):
                    if var.power[0] == term.power:
                        var.value.append(term.value)
                        var.power.append(term.power)
                        var.scope.append(term.scope)
                        if i != 0 and isinstance(tokens[i - 1], Binary):
                            var.before.append(tokens[i - 1].value)
                            var.beforeScope.append(tokens[i - 1].scope)
                        else:
                            var.before.append('')
                            var.beforeScope.append('')
                        if i + 1 < len(tokens) and isinstance(tokens[i + 1], Binary):
                            var.after.append(tokens[i + 1].value)
                            var.afterScope.append(tokens[i + 1].scope)
                        else:
                            var.after.append('')
                            var.afterScope.append('')
                        skip = True
                        break
            if not skip:
                variable = Constant()
                variable.power = []
                if isinstance(term.value, list):
                    variable.value = [evaluateConstant(term)]
                    variable.power.append(1)
                else:
                    variable.value = [term.value]
                    variable.power.append(term.power)
                variable.scope = [term.scope]
                variable.before = []
                variable.beforeScope = []
                variable.after = []
                variable.afterScope = []
                if i != 0 and isinstance(tokens[i - 1], Binary):
                    variable.before.append(tokens[i - 1].value)
                    variable.beforeScope.append(tokens[i - 1].scope)
                else:
                    variable.before.append('')
                    variable.beforeScope.append('')
                if i + 1 < len(tokens) and isinstance(tokens[i + 1], Binary):
                    variable.after.append(tokens[i + 1].value)
                    variable.afterScope.append(tokens[i + 1].scope)
                else:
                    variable.after.append('')
                    variable.afterScope.append('')
                variables.append(variable)
        elif isinstance(term, Expression):
            var = getLevelVariables(term.tokens)
            retType, val = extractExpression(var)
            if retType == "expression":
                variable = Expression()
                variable.value = val
                variable.tokens = term.tokens
                variables.append(variable)
            elif retType == "constant":
                skip = False
                for var in variables:
                    if isinstance(var.value, list) and isNumber(var.value[0]):
                        if var.power == val.power:
                            var.value.append(val.value)
                            var.power.append(val.power)
                            var.scope.append(val.scope)
                            var.before.append(val.before)
                            var.beforeScope.append(val.beforeScope)
                            var.after.append(val.after)
                            var.afterScope.append(val.afterScope)
                            skip = True
                            break
                if not skip:
                    var = Constant()
                    var.value = [val.value]
                    var.power = [val.power]
                    var.scope = [val.scope]
                    var.before = [val.before]
                    var.beforeScope = [val.beforeScope]
                    var.after = []
                    var.afterScope = ['']
                    variables.append(var)
            elif retType == "variable":
                skip = False
                for var in variables:
                    if var.value == val.value:
                        var.power.append(val.power)
                        var.before.append('')
                        var.beforeScope.append('')
                        var.after.append('')
                        var.afterScope.append('')
                        var.coefficient.append(val.coefficient)
                        var.scope.append(val.scope)
                        skip = True
                        break
                if not skip:
                    var = Constant()
                    var.coefficient = [val.coefficient]
                    var.value = val.value
                    var.power = [val.power]
                    var.scope = [val.scope]
                    var.before = ['']
                    var.beforeScope = ['']
                    var.after = ['']
                    var.afterScope = ['']
                    variables.append(var)
            elif retType == "mixed":
                for v in val:
                    if isinstance(v, Variable):
                        skip = False
                        for var in variables:
                            if var.value == v.value:
                                var.power.extend(v.power)
                                var.before.extend(v.before)
                                var.beforeScope.extend(v.beforeScope)
                                var.after.extend(v.after)
                                var.afterScope.extend(v.afterScope)
                                var.coefficient.extend(v.coefficient)
                                var.scope.extend(v.scope)
                                skip = True
                                break
                        if not skip:
                            var = Constant()
                            var.coefficient = [v.coefficient]
                            var.value = v.value
                            var.power = [v.power]
                            var.scope = [v.scope]
                            var.before = [v.before]
                            var.beforeScope = [v.beforeScope]
                            var.after = [v.after]
                            var.afterScope = [v.afterScope]
                            variables.append(var)
                    elif isinstance(v, Constant):
                        for var in variables:
                            if isinstance(var.value, list) and isNumber(var.value[0]):
                                if var.power == v.power:
                                    var.value.extend(v.value)
                                    var.power.extend(v.power)
                                    var.before.extend(v.before)
                                    var.beforeScope.extend(
                                        v.beforeScope)
                                    var.after.extend(v.after)
                                    var.afterScope.extend(v.afterScope)
                                    var.coefficient.extend(v.coefficient)
                                    var.scope.extend(v.scope)
                                    skip = True
                                    break
                        if not skip:
                            var = Constant()
                            var.coefficient = [v.coefficient]
                            var.value = [v.value]
                            var.power = [v.power]
                            var.scope = [v.scope]
                            var.before = [v.before]
                            var.beforeScope = [v.beforeScope]
                            var.after = [v.after]
                            var.afterScope = [v.afterScope]
                            variables.append(var)
                    elif isinstance(v, Expression):
                        variables.append(v)
    return variables
Пример #8
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