<commit_before>from sympy.utilities.pytest import XFAIL, raises
from sympy import (symbols, lambdify, sqrt, sin, cos, pi, atan, Rational, Float,
        Matrix, Lambda, exp, Integral, oo, I)
from sympy.printing.lambdarepr import LambdaPrinter
from sympy import mpmath
from sympy.utilities.lambdify import implemented_function
import math, sympy

x,y,z = symbols('x,y,z')

#================== Test different arguments ==============
def test_no_args():
    f = lambdify([], 1)
    try:
        f(-1)
        assert False
    except TypeError:
        pass
    assert f() == 1

def test_single_arg():
    f = lambdify(x, 2*x)
    assert f(1) == 2

def test_list_args():
    f = lambdify([x,y], x+y)
    assert f(1,2) == 3

def test_str_args():
    f = lambdify('x,y,z', 'z,y,x')
    assert f(3,2,1) == (1,2,3)
    assert f(1.0,2.0,3.0) == (3.0,2.0,1.0)
    # make sure correct number of args required
    try:
        f(0)
        assert False
    except TypeError:
        pass

def test_own_namespace():
    myfunc = lambda x:1
    f = lambdify(x, sin(x), {"sin":myfunc})
    assert f(0.1) == 1
    assert f(100) == 1

def test_own_module():
    f = lambdify(x, sin(x), math)
    assert f(0)==0.0
    f = lambdify(x, sympy.ceiling(x), math)
    try:
        f(4.5)
        assert False
    except NameError:
        pass

def test_bad_args():
    try:
        # no vargs given
        f = lambdify(1)
        assert False
    except TypeError:
        pass
    try:
        # same with vector exprs
        f = lambdify([1,2])
        assert False
    except TypeError:
        pass

def test_atoms():
    # Non-Symbol atoms should not be pulled out from the expression namespace
    f = lambdify(x, pi + x, {"pi": 3.14})
    assert f(0) == 3.14
    f = lambdify(x, I + x, {"I": 1j})
    assert f(1) == 1 + 1j

#================== Test different modules ================

# high precision output of sin(0.2*pi) is used to detect if precision is lost unwanted

def test_sympy_lambda():
    dps = mpmath.mp.dps
    mpmath.mp.dps = 50
    try:
        sin02 = mpmath.mpf("0.19866933079506121545941262711838975037020672954020")
        f = lambdify(x, sin(x), "sympy")
        assert f(x) == sin(x)
        prec = 1e-15
        assert -prec < f(Rational(1,5)).evalf() - Float(str(sin02)) < prec
        try:
            # arctan is in numpy module and should not be available
            f = lambdify(x, arctan(x), "sympy")
            assert False
        except NameError:
            pass
    finally:
        mpmath.mp.dps = dps

def test_math_lambda():
    dps = mpmath.mp.dps
    mpmath.mp.dps = 50
    try:
        sin02 = mpmath.mpf("0.19866933079506121545941262711838975037020672954020")
        f = lambdify(x, sin(x), "math")
        prec = 1e-15
        assert -prec < f(0.2) - sin02 < prec
        try:
            f(x) # if this succeeds, it can't be a python math function
            assert False
        except ValueError:
            pass
    finally:
        mpmath.mp.dps = dps

def test_mpmath_lambda():
    dps = mpmath.mp.dps
    mpmath.mp.dps = 50
    try:
        sin02 = mpmath.mpf("0.19866933079506121545941262711838975037020672954020")
        f = lambdify(x, sin(x), "mpmath")
        prec = 1e-49 # mpmath precision is around 50 decimal places
        assert -prec < f(mpmath.mpf("0.2")) - sin02 < prec
        try:
            f(x) # if this succeeds, it can't be a mpmath function
            assert False
        except TypeError:
            pass
    finally:
        mpmath.mp.dps = dps

@XFAIL
def test_number_precision():
    dps = mpmath.mp.dps
    mpmath.mp.dps = 50
    try:
        sin02 = mpmath.mpf("0.19866933079506121545941262711838975037020672954020")
        f = lambdify(x, sin02, "mpmath")
        prec = 1e-49 # mpmath precision is around 50 decimal places
        assert -prec < f(0) - sin02 < prec
    finally:
        mpmath.mp.dps = dps

#================== Test Translations =====================
# We can only check if all translated functions are valid. It has to be checked
# by hand if they are complete.

def test_math_transl():
    from sympy.utilities.lambdify import MATH_TRANSLATIONS
    for sym, mat in MATH_TRANSLATIONS.iteritems():
        assert sym in sympy.__dict__
        assert mat in math.__dict__

def test_mpmath_transl():
    from sympy.utilities.lambdify import MPMATH_TRANSLATIONS
    for sym, mat in MPMATH_TRANSLATIONS.iteritems():
        assert sym in sympy.__dict__ or sym == 'Matrix'
        assert mat in mpmath.__dict__

#================== Test some functions ===================
def test_exponentiation():
    f = lambdify(x, x**2)
    assert f(-1) == 1
    assert f(0) == 0
    assert f(1) == 1
    assert f(-2) == 4
    assert f(2) == 4
    assert f(2.5) == 6.25

def test_sqrt():
    f = lambdify(x, sqrt(x))
    assert f(0) == 0.0
    assert f(1) == 1.0
    assert f(4) == 2.0
    assert abs(f(2) - 1.414) < 0.001<commit_msg>Added tests for abs() in lambdify(..., modules="numpy") (#2654)<commit_after>from sympy.utilities.pytest import XFAIL, raises
from sympy import (symbols, lambdify, sqrt, sin, cos, pi, atan, Rational, Float,
        Matrix, Lambda, exp, Integral, oo, I, Abs)
from sympy.printing.lambdarepr import LambdaPrinter
from sympy import mpmath
from sympy.utilities.lambdify import implemented_function
import math, sympy

x,y,z = symbols('x,y,z')

#================== Test different arguments ==============
def test_no_args():
    f = lambdify([], 1)
    try:
        f(-1)
        assert False
    except TypeError:
        pass
    assert f() == 1

def test_single_arg():
    f = lambdify(x, 2*x)
    assert f(1) == 2

def test_list_args():
    f = lambdify([x,y], x+y)
    assert f(1,2) == 3

def test_str_args():
    f = lambdify('x,y,z', 'z,y,x')
    assert f(3,2,1) == (1,2,3)
    assert f(1.0,2.0,3.0) == (3.0,2.0,1.0)
    # make sure correct number of args required
    try:
        f(0)
        assert False
    except TypeError:
        pass

def test_own_namespace():
    myfunc = lambda x:1
    f = lambdify(x, sin(x), {"sin":myfunc})
    assert f(0.1) == 1
    assert f(100) == 1

def test_own_module():
    f = lambdify(x, sin(x), math)
    assert f(0)==0.0
    f = lambdify(x, sympy.ceiling(x), math)
    try:
        f(4.5)
        assert False
    except NameError:
        pass

def test_bad_args():
    try:
        # no vargs given
        f = lambdify(1)
        assert False
    except TypeError:
        pass
    try:
        # same with vector exprs
        f = lambdify([1,2])
        assert False
    except TypeError:
        pass

def test_atoms():
    # Non-Symbol atoms should not be pulled out from the expression namespace
    f = lambdify(x, pi + x, {"pi": 3.14})
    assert f(0) == 3.14
    f = lambdify(x, I + x, {"I": 1j})
    assert f(1) == 1 + 1j

#================== Test different modules ================

# high precision output of sin(0.2*pi) is used to detect if precision is lost unwanted

def test_sympy_lambda():
    dps = mpmath.mp.dps
    mpmath.mp.dps = 50
    try:
        sin02 = mpmath.mpf("0.19866933079506121545941262711838975037020672954020")
        f = lambdify(x, sin(x), "sympy")
        assert f(x) == sin(x)
        prec = 1e-15
        assert -prec < f(Rational(1,5)).evalf() - Float(str(sin02)) < prec
        try:
            # arctan is in numpy module and should not be available
            f = lambdify(x, arctan(x), "sympy")
            assert False
        except NameError:
            pass
    finally:
        mpmath.mp.dps = dps

def test_math_lambda():
    dps = mpmath.mp.dps
    mpmath.mp.dps = 50
    try:
        sin02 = mpmath.mpf("0.19866933079506121545941262711838975037020672954020")
        f = lambdify(x, sin(x), "math")
        prec = 1e-15
        assert -prec < f(0.2) - sin02 < prec
        try:
            f(x) # if this succeeds, it can't be a python math function
            assert False
        except ValueError:
            pass
    finally:
        mpmath.mp.dps = dps

def test_mpmath_lambda():
    dps = mpmath.mp.dps
    mpmath.mp.dps = 50
    try:
        sin02 = mpmath.mpf("0.19866933079506121545941262711838975037020672954020")
        f = lambdify(x, sin(x), "mpmath")
        prec = 1e-49 # mpmath precision is around 50 decimal places
        assert -prec < f(mpmath.mpf("0.2")) - sin02 < prec
        try:
            f(x) # if this succeeds, it can't be a mpmath function
            assert False
        except TypeError:
            pass
    finally:
        mpmath.mp.dps = dps

@XFAIL
def test_number_precision():
    dps = mpmath.mp.dps
    mpmath.mp.dps = 50
    try:
        sin02 = mpmath.mpf("0.19866933079506121545941262711838975037020672954020")
        f = lambdify(x, sin02, "mpmath")
        prec = 1e-49 # mpmath precision is around 50 decimal places
        assert -prec < f(0) - sin02 < prec
    finally:
        mpmath.mp.dps = dps

#================== Test Translations =====================
# We can only check if all translated functions are valid. It has to be checked
# by hand if they are complete.

def test_math_transl():
    from sympy.utilities.lambdify import MATH_TRANSLATIONS
    for sym, mat in MATH_TRANSLATIONS.iteritems():
        assert sym in sympy.__dict__
        assert mat in math.__dict__

def test_mpmath_transl():
    from sympy.utilities.lambdify import MPMATH_TRANSLATIONS
    for sym, mat in MPMATH_TRANSLATIONS.iteritems():
        assert sym in sympy.__dict__ or sym == 'Matrix'
        assert mat in mpmath.__dict__

def test_numpy_transl():
    f = lambdify(x, Abs(x), "numpy")
    assert f(-1) == 1
    assert f(1) == 1

#================== Test some functions ===================
def test_exponentiation():
    f = lambdify(x, x**2)
    assert f(-1) == 1
    assert f(0) == 0
    assert f(1) == 1
    assert f(-2) == 4
    assert f(2) == 4
    assert f(2.5) == 6.25

def test_sqrt():
    f = lambdify(x, sqrt(x))
    assert f(0) == 0.0
    assert f(1) == 1.0
    assert f(4) == 2.0
    assert abs(f(2) - 1.414) < 0.001
{'fraction_non_alphanumeric': 0.14215637200921014, 'fraction_numerical': 0.06967664430873961, 'mean_word_length': 2.389888021717, 'pattern_counts': {'":': 6, '<': 21, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2634694', 'n_tokens_mistral': 3987, 'n_tokens_neox': 3548, 'n_words': 975}