

def nsimplify(expr, constants=[], tolerance=None, full=False, rational=None):
    '\n    Find a simple representation for a number or, if there are free symbols or\n    if rational=True, then replace Floats with their Rational equivalents. If\n    no change is made and rational is not False then Floats will at least be\n    converted to Rationals.\n\n    For numerical expressions, a simple formula that numerically matches the\n    given numerical expression is sought (and the input should be possible\n    to evalf to a precision of at least 30 digits).\n\n    Optionally, a list of (rationally independent) constants to\n    include in the formula may be given.\n\n    A lower tolerance may be set to find less exact matches. If no tolerance\n    is given then the least precise value will set the tolerance (e.g. Floats\n    default to 15 digits of precision, so would be tolerance=10**-15).\n\n    With full=True, a more extensive search is performed\n    (this is useful to find simpler numbers when the tolerance\n    is set low).\n\n    Examples\n    ========\n\n    >>> from sympy import nsimplify, sqrt, GoldenRatio, exp, I, exp, pi\n    >>> nsimplify(4/(1+sqrt(5)), [GoldenRatio])\n    -2 + 2*GoldenRatio\n    >>> nsimplify((1/(exp(3*pi*I/5)+1)))\n    1/2 - I*sqrt(sqrt(5)/10 + 1/4)\n    >>> nsimplify(I**I, [pi])\n    exp(-pi/2)\n    >>> nsimplify(pi, tolerance=0.01)\n    22/7\n\n    See Also\n    ========\n    sympy.core.function.nfloat\n\n    '
    try:
        return sympify(as_int(expr))
    except (TypeError, ValueError):
        pass
    expr = sympify(expr).xreplace({
        Float('inf'): S.Infinity,
        Float('-inf'): S.NegativeInfinity,
    })
    if ((expr is S.Infinity) or (expr is S.NegativeInfinity)):
        return expr
    if (rational or expr.free_symbols):
        return _real_to_rational(expr, tolerance)
    if (tolerance is None):
        tolerance = (10 ** (- min(([15] + [mpmath.libmp.libmpf.prec_to_dps(n._prec) for n in expr.atoms(Float)]))))
    prec = 30
    bprec = int((prec * 3.33))
    constants_dict = {
        
    }
    for constant in constants:
        constant = sympify(constant)
        v = constant.evalf(prec)
        if (not v.is_Float):
            raise ValueError('constants must be real-valued')
        constants_dict[str(constant)] = v._to_mpmath(bprec)
    exprval = expr.evalf(prec, chop=True)
    (re, im) = exprval.as_real_imag()
    if (not (re.is_Number and im.is_Number)):
        return expr

    def nsimplify_real(x):
        orig = mpmath.mp.dps
        xv = x._to_mpmath(bprec)
        try:
            if (not (tolerance or full)):
                mpmath.mp.dps = 15
                rat = mpmath.pslq([xv, 1])
                if (rat is not None):
                    return Rational((- int(rat[1])), int(rat[0]))
            mpmath.mp.dps = prec
            newexpr = mpmath.identify(xv, constants=constants_dict, tol=tolerance, full=full)
            if (not newexpr):
                raise ValueError
            if full:
                newexpr = newexpr[0]
            expr = sympify(newexpr)
            if (x and (not expr)):
                raise ValueError
            if ((expr.is_finite is False) and (not (xv in [mpmath.inf, mpmath.ninf]))):
                raise ValueError
            return expr
        finally:
            mpmath.mp.dps = orig
    try:
        if re:
            re = nsimplify_real(re)
        if im:
            im = nsimplify_real(im)
    except ValueError:
        if (rational is None):
            return _real_to_rational(expr)
        return expr
    rv = (re + (im * S.ImaginaryUnit))
    if ((rv != expr) or (rational is False)):
        return rv
    return _real_to_rational(expr)

dataset/ETHPy150Open sympy/sympy/sympy/simplify/simplify.py nsimplify/original