

def simplify(expr, ratio=1.7, measure=count_ops, fu=False):
    '\n    Simplifies the given expression.\n\n    Simplification is not a well defined term and the exact strategies\n    this function tries can change in the future versions of SymPy. If\n    your algorithm relies on "simplification" (whatever it is), try to\n    determine what you need exactly  -  is it powsimp()?, radsimp()?,\n    together()?, logcombine()?, or something else? And use this particular\n    function directly, because those are well defined and thus your algorithm\n    will be robust.\n\n    Nonetheless, especially for interactive use, or when you don\'t know\n    anything about the structure of the expression, simplify() tries to apply\n    intelligent heuristics to make the input expression "simpler".  For\n    example:\n\n    >>> from sympy import simplify, cos, sin\n    >>> from sympy.abc import x, y\n    >>> a = (x + x**2)/(x*sin(y)**2 + x*cos(y)**2)\n    >>> a\n    (x**2 + x)/(x*sin(y)**2 + x*cos(y)**2)\n    >>> simplify(a)\n    x + 1\n\n    Note that we could have obtained the same result by using specific\n    simplification functions:\n\n    >>> from sympy import trigsimp, cancel\n    >>> trigsimp(a)\n    (x**2 + x)/x\n    >>> cancel(_)\n    x + 1\n\n    In some cases, applying :func:`simplify` may actually result in some more\n    complicated expression. The default ``ratio=1.7`` prevents more extreme\n    cases: if (result length)/(input length) > ratio, then input is returned\n    unmodified.  The ``measure`` parameter lets you specify the function used\n    to determine how complex an expression is.  The function should take a\n    single argument as an expression and return a number such that if\n    expression ``a`` is more complex than expression ``b``, then\n    ``measure(a) > measure(b)``.  The default measure function is\n    :func:`count_ops`, which returns the total number of operations in the\n    expression.\n\n    For example, if ``ratio=1``, ``simplify`` output can\'t be longer\n    than input.\n\n    ::\n\n        >>> from sympy import sqrt, simplify, count_ops, oo\n        >>> root = 1/(sqrt(2)+3)\n\n    Since ``simplify(root)`` would result in a slightly longer expression,\n    root is returned unchanged instead::\n\n       >>> simplify(root, ratio=1) == root\n       True\n\n    If ``ratio=oo``, simplify will be applied anyway::\n\n        >>> count_ops(simplify(root, ratio=oo)) > count_ops(root)\n        True\n\n    Note that the shortest expression is not necessary the simplest, so\n    setting ``ratio`` to 1 may not be a good idea.\n    Heuristically, the default value ``ratio=1.7`` seems like a reasonable\n    choice.\n\n    You can easily define your own measure function based on what you feel\n    should represent the "size" or "complexity" of the input expression.  Note\n    that some choices, such as ``lambda expr: len(str(expr))`` may appear to be\n    good metrics, but have other problems (in this case, the measure function\n    may slow down simplify too much for very large expressions).  If you don\'t\n    know what a good metric would be, the default, ``count_ops``, is a good\n    one.\n\n    For example:\n\n    >>> from sympy import symbols, log\n    >>> a, b = symbols(\'a b\', positive=True)\n    >>> g = log(a) + log(b) + log(a)*log(1/b)\n    >>> h = simplify(g)\n    >>> h\n    log(a*b**(-log(a) + 1))\n    >>> count_ops(g)\n    8\n    >>> count_ops(h)\n    5\n\n    So you can see that ``h`` is simpler than ``g`` using the count_ops metric.\n    However, we may not like how ``simplify`` (in this case, using\n    ``logcombine``) has created the ``b**(log(1/a) + 1)`` term.  A simple way\n    to reduce this would be to give more weight to powers as operations in\n    ``count_ops``.  We can do this by using the ``visual=True`` option:\n\n    >>> print(count_ops(g, visual=True))\n    2*ADD + DIV + 4*LOG + MUL\n    >>> print(count_ops(h, visual=True))\n    2*LOG + MUL + POW + SUB\n\n    >>> from sympy import Symbol, S\n    >>> def my_measure(expr):\n    ...     POW = Symbol(\'POW\')\n    ...     # Discourage powers by giving POW a weight of 10\n    ...     count = count_ops(expr, visual=True).subs(POW, 10)\n    ...     # Every other operation gets a weight of 1 (the default)\n    ...     count = count.replace(Symbol, type(S.One))\n    ...     return count\n    >>> my_measure(g)\n    8\n    >>> my_measure(h)\n    14\n    >>> 15./8 > 1.7 # 1.7 is the default ratio\n    True\n    >>> simplify(g, measure=my_measure)\n    -log(a)*log(b) + log(a) + log(b)\n\n    Note that because ``simplify()`` internally tries many different\n    simplification strategies and then compares them using the measure\n    function, we get a completely different result that is still different\n    from the input expression by doing this.\n    '
    expr = sympify(expr)
    try:
        return expr._eval_simplify(ratio=ratio, measure=measure)
    except AttributeError:
        pass
    original_expr = expr = signsimp(expr)
    from sympy.simplify.hyperexpand import hyperexpand
    from sympy.functions.special.bessel import BesselBase
    from sympy import Sum, Product
    if ((not isinstance(expr, Basic)) or (not expr.args)):
        return expr
    if (not isinstance(expr, (Add, Mul, Pow, ExpBase))):
        if (isinstance(expr, Function) and hasattr(expr, 'inverse')):
            if ((len(expr.args) == 1) and (len(expr.args[0].args) == 1) and isinstance(expr.args[0], expr.inverse(argindex=1))):
                return simplify(expr.args[0].args[0], ratio=ratio, measure=measure, fu=fu)
        return expr.func(*[simplify(x, ratio=ratio, measure=measure, fu=fu) for x in expr.args])

    def shorter(*choices):
        'Return the choice that has the fewest ops. In case of a tie,\n        the expression listed first is selected.'
        if (not has_variety(choices)):
            return choices[0]
        return min(choices, key=measure)
    expr = bottom_up(expr, (lambda w: w.normal()))
    expr = Mul(*powsimp(short).as_content_primitive())
    _e = cancel(expr)
    expr1 = shorter(_e, _mexpand(_e).cancel())
    expr2 = shorter(together(expr, deep=True), together(expr1, deep=True))
    if (ratio is S.Infinity):
        expr = expr2
    else:
        expr = shorter(expr2, expr1, expr)
    if (not isinstance(expr, Basic)):
        return expr
    expr = factor_terms(expr, sign=False)
    expr = hyperexpand(expr)
    expr = piecewise_fold(expr)
    if expr.has(BesselBase):
        expr = besselsimp(expr)
    if ((expr.has(TrigonometricFunction) and (not fu)) or expr.has(HyperbolicFunction)):
        expr = trigsimp(expr, deep=True)
    if expr.has(log):
        expr = shorter(expand_log(expr, deep=True), logcombine(expr))
    if expr.has(CombinatorialFunction, gamma):
        expr = combsimp(expr)
    if expr.has(Sum):
        expr = sum_simplify(expr)
    if expr.has(Product):
        expr = product_simplify(expr)
    short = shorter(powsimp(expr, combine='exp', deep=True), powsimp(expr), expr)
    short = shorter(short, factor_terms(short), expand_power_exp(expand_mul(short)))
    if short.has(TrigonometricFunction, HyperbolicFunction, ExpBase):
        short = exptrigsimp(short, simplify=False)
    hollow_mul = Transform((lambda x: Mul(*x.args)), (lambda x: (x.is_Mul and (len(x.args) == 2) and x.args[0].is_Number and x.args[1].is_Add and x.is_commutative)))
    expr = short.xreplace(hollow_mul)
    (numer, denom) = expr.as_numer_denom()
    if denom.is_Add:
        (n, d) = fraction(radsimp((1 / denom), symbolic=False, max_terms=1))
        if (n is not S.One):
            expr = ((numer * n).expand() / d)
    if expr.could_extract_minus_sign():
        (n, d) = fraction(expr)
        if (d != 0):
            expr = signsimp(((- n) / (- d)))
    if (measure(expr) > (ratio * measure(original_expr))):
        expr = original_expr
    return expr

dataset/ETHPy150Open sympy/sympy/sympy/simplify/simplify.py simplify/VarMisuse@340/2416 `expr`->`short`