Пример #1
0
def Main():
    while True:
        usrInput = None
        if sys.version_info < (3,0):
            usrInput = raw_input("Input: ").lower()
        else:
            usrInput = input("Input: ").lower()
            
        if (usrInput == ""):
            break

        exp = ParseInput.ParseInput(usrInput)
        
        print("--------------")
        variable = None
        for var in exp.variables:
            if var.variable == 'x':
                variable = var
                break
        if variable == None:
            if len(exp.variables) >0:
                variable = exp.variables[0]
            else:
                variable = Expression.VariableElement('x')
                
        print("f("+str(variable)+") = "+ParseInput.blockToStr(exp,""))
        derivative = Derivative.differentiate(exp,variable,makeNice=True)
        print("f'("+str(variable)+") = "+ ParseInput.blockToStr(derivative, ""))
Пример #2
0
def ParseBlock(input, start_index, depth):
    global expression
    cur_block = Expression.ExpressionBlock()
    i = start_index
    var_last = False
    while i < len(input):
        char = input[i]
        if char in STARTBLOCK:
            block, endIndex = ParseBlock(input, i + 1, depth + 1)
            cur_block.append(block)
            i = endIndex
            var_last = False
        elif char in ENDBLOCK:
            if (depth == 0):
                warnings.warn("Klammern Falsch")
            return cur_block, i

        elif (Tools.IsNumber(char)):
            number, endIndex = getNumber(input, i)
            i = endIndex - 1
            if (var_last):
                cur_block.append(Expression.multiplyOperator)
            cur_block.append(Expression.NumberElement(number))
            if (i <= len(input) - 2):
                if (Tools.IsChar(input[i + 1])):
                    cur_block.append(Expression.multiplyOperator)

            var_last = False
        elif (Tools.IsOperator(char)):
            op = Tools.IsOperator(char)
            cur_block.append(op)
            var_last = False
        elif (Tools.IsChar(char)):
            word, endIndex = getWord(input, i)

            endVariables = endIndex + 1
            func = None

            vars = False

            # testet, ob die char-kette einen funktionennamen enthält
            for f in PredefinedObjects.functions:
                if f.name in word:
                    ind = word.index(f.name)
                    if func == None or len(f.name) > len(func.name):
                        endVariables = ind
                        func = f
                    #break

            if (
                    func == None or endVariables != i
            ):  # fügt alle variablen vor der function mit multiply operator hinzu
                for k in range(endVariables - i):
                    vars = True

                    var = word[k]

                    variableElement = None
                    for variableEl in expression.variables:
                        if variableEl.variable == var:
                            variableElement = variableEl
                            break
                    if (variableElement == None):
                        variableElement = Expression.VariableElement(var)
                        expression.variables.append(variableElement)

                    cur_block.append(variableElement)
                    if (k != endVariables - i - 1):
                        cur_block.append(Expression.multiplyOperator)
            var_last = vars
            if (func != None and vars):  # und ggf. auch die function
                cur_block.append(Expression.multiplyOperator)
                cur_block.append(func)
            elif (func != None):
                cur_block.append(func)
            """ if (func):
                cur_block.append(func)
            else: # produkt von variablen
                for k in range(len(word)):
                    var = word[k]
                    cur_block.append(VariableElement(var))
                    if (k!=(len(word)-1)):
                        cur_block.append(multiplyOperator)"""
            i = endIndex
        i += 1

    return cur_block, i