Ejemplo n.º 1
0
def quickSimplify(workspace):
    """Dynamic simplifier for simplifying expression as it is being typed

    Arguments:
        workspace {QtWidgets.QWidget} -- main layout

    Returns:
        qSolution/log {string} -- quick solution or error log
        enableInteraction {bool} -- if False disables 'visma'(interaction) button
    """
    # FIXME: Crashes for some cases. Find and fix.
    logger.setLogName('qsolver')
    logger.setLevel(0)
    qSolution = ""
    strIn = workspace.textedit.toPlainText()
    cleanInput = removeSpaces(strIn)
    if ';' in cleanInput:
        return "", True, True
    terms = getTerms(cleanInput)
    normalizedTerms = normalize(terms)
    symTokens = tokenizeSymbols(normalizedTerms)
    normalizedTerms, symTokens = removeUnary(normalizedTerms, symTokens)
    if checkEquation(normalizedTerms, symTokens) is True and strIn != "":
        if symTokens and symTokens[-1] is not False:
            tokens = getToken(normalizedTerms, symTokens)
            tokens = tokens.tokens
            lhs, rhs = getLHSandRHS(tokens)
            _, solutionType = checkTypes(lhs, rhs)
            lhs, rhs = getLHSandRHS(tokens)
            if solutionType == 'expression':
                _, _, _, equationTokens, _ = simplify(tokens)
                qSolution = r'$ ' + '= \ '
            else:
                _, _, _, _, equationTokens, _ = simplifyEquation(lhs, rhs)
                qSolution = r'$ ' + '\Rightarrow \ '
            qSolution += tokensToLatex(equationTokens[-1]) + ' $'
            # workspace.eqToks = equationTokens
            # plot(workspace)
            return qSolution, True, False
        elif symTokens:
            log = "Invalid Expression"
            workspace.logBox.append(logger.error(log))
            return log, False, _
        else:
            log = ""
            workspace.logBox.append(logger.error(log))
            return log, False, _
    else:
        log = ""
        if strIn != "":
            _, log = checkEquation(normalizedTerms, symTokens)
            workspace.logBox.append(logger.error(log))
        return log, False, _
Ejemplo n.º 2
0
def quickSimplify(workspace):
    """Dynamic simplifier for simplifying expression as it is being typed

    Arguments:
        workspace {QtWidgets.QWidget} -- main layout

    Returns:
        qSolution/log {string} -- quick solution or error log
    """
    # FIXME: Crashes for some cases. Find and fix.
    qSolution = ""
    input = workspace.textedit.toPlainText()
    cleanInput = removeSpaces(input)
    terms = getTerms(cleanInput)
    normalizedTerms = normalize(terms)
    symTokens = tokenizeSymbols(normalizedTerms)
    terms, symTokens = removeUnary(normalizedTerms, symTokens)
    if checkEquation(normalizedTerms, symTokens) is True and input != "":
        if symTokens[-1] is not False:
            tokens = getToken(normalizedTerms, symTokens)
            tokens = tokens.tokens
            lhs, rhs = getLHSandRHS(tokens)
            _, solutionType = checkTypes(lhs, rhs)
            if solutionType == 'expression':
                _, _, _, equationTokens, _ = simplify(tokens)
                qSolution = r'$ ' + '= \ '
            else:
                _, _, _, _, equationTokens, _ = simplifyEquation(lhs, rhs)
                qSolution = r'$ ' + '\Rightarrow \ '
            qSolution += tokensToLatex(equationTokens[-1]) + ' $'
            # workspace.eqToks = equationTokens
            # plot(workspace)
            return qSolution
        else:
            log = "Invalid Expression"
            return log
    else:
        log = ""
        if input != "":
            _, log = checkEquation(normalizedTerms, symTokens)
        return log
Ejemplo n.º 3
0
def test_getTerms():

    assert getTerms("1 + 2 * 3 - sqrt(2) / 5") == [
        '1', '+', '2', '*', '3', '-', 'sqrt', '(', '2', ')', '/', '5'
    ]
    assert getTerms("x + x^2*y + y^2 + y/z = -z") == [
        'x', '+', 'x', '^', '2', '*', 'y', '+', 'y', '^', '2', '+', 'y', '/',
        'z', '=', '-', 'z'
    ]

    assert getTerms("sin^2(x) + cos^2(x) = 1") == [
        'sin', '^', '2', '(', 'x', ')', '+', 'cos', '^', '2', '(', 'x', ')',
        '=', '1'
    ]
    assert getTerms("1 + tan^2(x) = sec^2(x)") == [
        '1', '+', 'tan', '^', '2', '(', 'x', ')', '=', 'sec', '^', '2', '(',
        'x', ')'
    ]
    assert getTerms("1 + cot^2(x) = csc^2(x)") == [
        '1', '+', 'cot', '^', '2', '(', 'x', ')', '=', 'csc', '^', '2', '(',
        'x', ')'
    ]

    assert getTerms("cosh^2(x)-sinh^2(x)=1") == [
        'cosh', '^', '2', '(', 'x', ')', '-', 'sinh', '^', '2', '(', 'x', ')',
        '=', '1'
    ]
    assert getTerms("1 - tanh^2(x) = sech^2(x)") == [
        '1', '-', 'tanh', '^', '2', '(', 'x', ')', '=', 'sech', '^', '2', '(',
        'x', ')'
    ]
    assert getTerms("coth^2(x)-csch^2(x)=1") == [
        'coth', '^', '2', '(', 'x', ')', '-', 'csch', '^', '2', '(', 'x', ')',
        '=', '1'
    ]

    assert getTerms("e = 2.71828") == ['exp', '=', '2.71828']
    assert getTerms("log_10(100) = 2") == [
        'log_', '10', '(', '100', ')', '=', '2'
    ]
    assert getTerms("ln(e) = 1") == ['ln', '(', 'exp', ')', '=', '1']
    assert getTerms("e^(i*pi)=1") == [
        'exp', '^', '(', 'iota', '*', 'pi', ')', '=', '1'
    ]

    assert getTerms("a = b") == ['a', '=', 'b']
    assert getTerms("a < b") == ['a', '<', 'b']
    assert getTerms("a > b") == ['a', '>', 'b']
    assert getTerms("a <= b") == ['a', '<=', 'b']
    assert getTerms("a >= b") == ['a', '>=', 'b']

    assert getTerms("[1,0;0,1]") == [
        '[', '1', ',', '0', ';', '0', ',', '1', ']'
    ]
    assert getTerms("2*[2,3;2,3]+[1,2;1,2]") == [
        '2', '*', '[', '2', ',', '3', ';', '2', ',', '3', ']', '+', '[', '1',
        ',', '2', ';', '1', ',', '2', ']'
    ]