예제 #1
0
def simplify_expr(expr):
    """
    Return a simplified version of the expression.
    """
    tree = AST.strip_parse(expr)
    simplify_ast = _simplify_ast(tree)
    return AST.ast2str(simplify_ast)
예제 #2
0
def extract_comps(expr):
    """
    Extract all comparisons from the expression.
    """
    comps_found = []
    _extract_comps_ast(AST.strip_parse(expr), comps_found)
    comps_found = [AST.ast2str(ast) for ast in comps_found]
    return set(comps_found)
예제 #3
0
def extract_funcs(expr):
    """
    Return a Set of the functions used in an expression.
    The elements of the Set are ('function_name', #arguments).
    """
    funcs_found = []
    _extract_funcs_ast(AST.strip_parse(expr), funcs_found)
    return set(funcs_found)
예제 #4
0
def expr2TeX(expr, name_dict={}):
    """
    Return a TeX version of a python math expression.

    name_dict: A dictionary mapping variable names used in the expression to
        preferred TeX expressions.
    """
    ast = AST.strip_parse(expr)
    return _ast2TeX(ast, name_dict=name_dict)
예제 #5
0
def extract_vars(expr):
    """
    Return a Set of the variables used in an expression.
    """
    try:
        return extract_vars_cache[expr]
    except KeyError:
        vars_found = []
        _extract_vars_ast(AST.strip_parse(expr), vars_found)
        vars_found = [AST.ast2str(ast) for ast in vars_found]
        result = set(vars_found)
        extract_vars_cache[expr] = result
        return result
예제 #6
0
def diff_expr(expr, wrt):
    """
    Return the derivative of the expression with respect to a given variable.
    """
    logger.debug('Taking derivative of %s wrt %s' % (expr, wrt))
    key = '%s__derivWRT__%s' % (expr, wrt)
    if key in __deriv_saved:
        deriv = __deriv_saved[key]
        logger.debug('Found saved result %s.' % deriv)
        return deriv

    ast = AST.strip_parse(expr)
    deriv = _diff_ast(ast, wrt)
    deriv = Simplify._simplify_ast(deriv)
    deriv = AST.ast2str(deriv)
    __deriv_saved[key] = deriv
    logger.debug('Computed result %s.' % deriv)
    return deriv
예제 #7
0
def dict2TeX(d, name_dict, lhs_form='%s', split_terms=False, simpleTeX=False):
    lines = []
    for lhs, rhs in list(d.items()):
        if split_terms:
            ast = AST.strip_parse(rhs)
            pos, neg = [], []
            AST._collect_pos_neg(ast, pos, neg)
            try:
                lhsTeX = lhs_form % expr2TeX(lhs, name_dict=name_dict)
            except TypeError:
                lhsTeX = lhs_form
            rhsTeX = _ast2TeX(pos[0], name_dict=name_dict)
            lines.append(r'$ %s $ &=& $ %s $\\' % (lhsTeX, rhsTeX))

            for term in pos[1:]:
                TeXed = _ast2TeX(term, name_dict=name_dict)
                lines.append(r' & & $ + \, %s $\\' % TeXed)
            for term in neg:
                TeXed = _ast2TeX(term, name_dict=name_dict)
                lines.append(r' & & $ - \, %s $\\' % TeXed)
        else:
            lhsTeX = lhs_form % expr2TeX(lhs, name_dict=name_dict)
            rhsTeX = expr2TeX(rhs, name_dict=name_dict)
            lines.append(r'$ %s $ & = & $ %s $\\' % (lhsTeX, rhsTeX))

        if not simpleTeX:
            # Force a space between TeX'd entries
            lines[-1] = '%s[5mm]' % lines[-1]

    all = os.linesep.join(lines)

    if not simpleTeX:
        all = all.replace(r'\frac{', r'\tabfrac{')
        # This makes the fractions look much nicer in the tabular output. See
        #  http://www.texnik.de/table/table.phtml#fractions
        lines = [
            r'\providecommand{\tabfrac}[2]{%',
            r'   \setlength{\fboxrule}{0pt}%', r'   \fbox{$\frac{#1}{#2}$}}',
            r'\begin{longtable}{lll}'
        ] + [all] + [r'\end{longtable}']
        all = os.linesep.join(lines)

    return all