Did I find the right examples for you? yes no      Crawl my project      Python Jobs

All Samples(113)  |  Call(107)  |  Derive(0)  |  Import(6)
Find a simple representation for a number or, if there are free symbols or
if rational=True, then replace Floats with their Rational equivalents. If
no change is made and rational is not False then Floats will at least be
converted to Rationals.

For numerical expressions, a simple formula that numerically matches the
given numerical expression is sought (and the input should be possible
to evalf to a precision of at least 30 digits).

Optionally, a list of (rationally independent) constants to(more...)

        def nsimplify(expr, constants=[], tolerance=None, full=False, rational=None):
    """
    Find a simple representation for a number or, if there are free symbols or
    if rational=True, then replace Floats with their Rational equivalents. If
    no change is made and rational is not False then Floats will at least be
    converted to Rationals.

    For numerical expressions, a simple formula that numerically matches the
    given numerical expression is sought (and the input should be possible
    to evalf to a precision of at least 30 digits).

    Optionally, a list of (rationally independent) constants to
    include in the formula may be given.

    A lower tolerance may be set to find less exact matches. If no tolerance
    is given then the least precise value will set the tolerance (e.g. Floats
    default to 15 digits of precision, so would be tolerance=10**-15).

    With full=True, a more extensive search is performed
    (this is useful to find simpler numbers when the tolerance
    is set low).

    Examples
    ========

    >>> from sympy import nsimplify, sqrt, GoldenRatio, exp, I, exp, pi
    >>> nsimplify(4/(1+sqrt(5)), [GoldenRatio])
    -2 + 2*GoldenRatio
    >>> nsimplify((1/(exp(3*pi*I/5)+1)))
    1/2 - I*sqrt(sqrt(5)/10 + 1/4)
    >>> nsimplify(I**I, [pi])
    exp(-pi/2)
    >>> nsimplify(pi, tolerance=0.01)
    22/7

    See Also
    ========
    sympy.core.function.nfloat

    """
    expr = sympify(expr)
    if rational or expr.free_symbols:
        return _real_to_rational(expr, tolerance)

    # SymPy's default tolerance for Rationals is 15; other numbers may have
    # lower tolerances set, so use them to pick the largest tolerance if None
    # was given
    if tolerance is None:
        tolerance = 10**-min([15] +
             [mpmath.libmp.libmpf.prec_to_dps(n._prec)
             for n in expr.atoms(Float)])
    # XXX should prec be set independent of tolerance or should it be computed
    # from tolerance?
    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()

    # safety check to make sure that this evaluated to a number
    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:
            # We'll be happy with low precision if a simple fraction
            if not (tolerance or full):
                mpmath.mp.dps = 15
                rat = mpmath.findpoly(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:  # don't let x become 0
                raise ValueError
            if expr.is_bounded is False and not xv in [mpmath.inf, mpmath.ninf]:
                raise ValueError
            return expr
        finally:
            # even though there are returns above, this is executed
            # before leaving
            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 there was a change or rational is explicitly not wanted
    # return the value, else return the Rational representation
    if rv != expr or rational is False:
        return rv
    return _real_to_rational(expr)
        


src/s/y/sympy-HEAD/sympy/simplify/tests/test_simplify.py   sympy(Download)
from sympy import (
    acos, Add, atan, besselsimp, binomial, collect, collect_const, combsimp,
    cos, cosh, cot, coth, count_ops, Derivative, diff, Dummy, E, Eq, erf, exp,
    exp_polar, expand, exptrigsimp, factor, factorial, FallingFactorial, Float,
    fraction, Function, gamma, GoldenRatio, hyper, hyper, hypersimp, I,
def test_nsimplify():
    x = Symbol("x")
    assert nsimplify(0) == 0
    assert nsimplify(-1) == -1
    assert nsimplify(1) == 1
    assert nsimplify(1 + x) == 1 + x

src/s/y/sympy-0.7.5/sympy/simplify/tests/test_simplify.py   sympy(Download)
from sympy import (
    acos, Add, atan, besselsimp, binomial, collect, collect_const, combsimp,
    cos, cosh, cot, coth, count_ops, Derivative, diff, Dummy, E, Eq, erf, exp,
    exp_polar, expand, exptrigsimp, factor, factorial, FallingFactorial, Float,
    fraction, Function, gamma, GoldenRatio, hyper, hyper, hypersimp, I,
def test_nsimplify():
    x = Symbol("x")
    assert nsimplify(0) == 0
    assert nsimplify(-1) == -1
    assert nsimplify(1) == 1
    assert nsimplify(1 + x) == 1 + x

src/s/y/sympy-polys-HEAD/sympy/simplify/tests/test_simplify.py   sympy-polys(Download)
from sympy import Symbol, symbols, together, hypersimp, factorial, binomial, \
        collect, Function, powsimp, separate, sin, exp, Rational, fraction, \
        simplify, trigsimp, cos, tan, cot, log, ratsimp, Matrix, pi, integrate, \
        solve, nsimplify, GoldenRatio, sqrt, E, I, sympify, atan, Derivative, \
        S, diff, oo, Eq, Integer, gamma, acos, Integral, logcombine, separatevars
def test_nsimplify():
    x = Symbol("x")
    assert nsimplify(0) == 0
    assert nsimplify(-1) == -1
    assert nsimplify(1) == 1
    assert nsimplify(1+x) == 1+x

src/s/y/sympy-HEAD/sympy/utilities/randtest.py   sympy(Download)
import random
 
from sympy import I, nsimplify, Tuple, Symbol
from sympy.core.compatibility import is_sequence, as_int
 
    if not rational:
        return A + I*B
    return nsimplify(A, rational=True) + I*nsimplify(B, rational=True)
 
 

src/s/y/sympy-HEAD/sympy/core/tests/test_expr.py   sympy(Download)
from __future__ import division
 
from sympy import (Add, Basic, S, Symbol, Wild, Float, Integer, Rational, I,
    sin, cos, tan, exp, log, nan, oo, sqrt, symbols, Integral, sympify,
    WildFunction, Poly, Function, Derivative, Number, pi, NumberSymbol, zoo,
def test_action_verbs():
    assert nsimplify((1/(exp(3*pi*x/5) + 1))) == \
        (1/(exp(3*pi*x/5) + 1)).nsimplify()
    assert ratsimp(1/x + 1/y) == (1/x + 1/y).ratsimp()
    assert trigsimp(log(x), deep=True) == (log(x)).trigsimp(deep=True)

src/s/y/sympy-HEAD/sympy/core/tests/test_wester.py   sympy(Download)
"""
 
from sympy import (Rational, symbols, factorial, sqrt, log, exp, oo, product,
    binomial, rf, pi, gamma, igcd, factorint, nsimplify, radsimp, combsimp,
    npartitions, totient, primerange, factor, simplify, gcd, resultant, expand,
def test_C14():
    assert nsimplify(sqrt(2*sqrt(3) + 4)) == 1 + sqrt(3)
 
 
def test_C15():
    test = nsimplify(sqrt(14 + 3*sqrt(3 + 2*sqrt(5 - 12*sqrt(3 - 2*sqrt(2))))))
def test_C17():
    test = nsimplify((sqrt(3) + sqrt(2)) / (sqrt(3) - sqrt(2)))
    good = 5 + 2*sqrt(6)
    assert test == good
 
 
def test_C18():
    assert nsimplify(sqrt(-2 + sqrt(-5)) * sqrt(-2 - sqrt(-5))) == 3