Repository: sympy/sympy
File: sympy/utilities/tests/test_lambdify.py (Python)

Code before:
    f = lambdify(x, asin(x), "cmath")
    assert abs(f(0) - cmath.asin(0)) < 1e-15
    assert abs(f(1) - cmath.asin(1)) < 1e-15
    assert abs(f(-1) - cmath.asin(-1)) < 1e-15
    assert abs(f(2) - cmath.asin(2)) < 1e-15


def test_cmath_acos():
    f = lambdify(x, acos(x), "cmath")
    assert abs(f(1) - cmath.acos(1)) < 1e-15
    assert abs(f(-1) - cmath.acos(-1)) < 1e-15
    assert abs(f(2) - cmath.acos(2)) < 1e-15


def test_cmath_atan():
    f = lambdify(x, atan(x), "cmath")
    assert abs(f(0) - cmath.atan(0)) < 1e-15
    assert abs(f(1) - cmath.atan(1)) < 1e-15
    assert abs(f(-1) - cmath.atan(-1)) < 1e-15
    assert abs(f(2) - cmath.atan(2)) < 1e-15


def test_cmath_asinh():
    f = lambdify(x, asinh(x), "cmath")
    assert abs(f(0) - cmath.asinh(0)) < 1e-15
    assert abs(f(1) - cmath.asinh(1)) < 1e-15
    assert abs(f(-1) - cmath.asinh(-1)) < 1e-15
    assert abs(f(2) - cmath.asinh(2)) < 1e-15


def test_cmath_acosh():
    f = lambdify(x, acosh(x), "cmath")
    assert abs(f(1) - cmath.acosh(1)) < 1e-15
    assert abs(f(2) - cmath.acosh(2)) < 1e-15


def test_cmath_atanh():
    f = lambdify(x, atanh(x), "cmath")
    assert abs(f(0) - cmath.atanh(0)) < 1e-15
    assert abs(f(0.5) - cmath.atanh(0.5)) < 1e-15
    assert abs(f(-0.5) - cmath.atanh(-0.5)) < 1e-15
    assert abs(f(2) - cmath.atanh(2)) < 1e-15


def test_cmath_complex_identities():
    # Define symbol
    z = symbols('z')

    # Trigonometric identity using re(z) and im(z)
    expr = cos(z) - cos(re(z)) * cosh(im(z)) + I * sin(re(z)) * sinh(im(z))
    func = lambdify([z], expr, modules=["cmath", "math"])

Review comment (security):
This test, together with two other tests will pass even if we replace "cmath" with "math".
Since asinh is defined for the whole real line, you will need to use a complex argument. This is also the case for another of the tests, while the remaining test can use a real valued argument, but it needs to be outside the domain for which is defined for in `math`.

I'm intentionally being vague because it is important that the author of a PR understands all the implications of his or hers proposed changes.

Code after:
    f = lambdify(x, asin(x), "cmath")
    assert abs(f(0) - cmath.asin(0)) < 1e-15
    assert abs(f(1) - cmath.asin(1)) < 1e-15
    assert abs(f(-1) - cmath.asin(-1)) < 1e-15
    assert abs(f(2) - cmath.asin(2)) < 1e-15
    assert abs(f(1j) - cmath.asin(1j)) < 1e-15


def test_cmath_acos():
    f = lambdify(x, acos(x), "cmath")
    assert abs(f(1) - cmath.acos(1)) < 1e-15
    assert abs(f(-1) - cmath.acos(-1)) < 1e-15
    assert abs(f(2) - cmath.acos(2)) < 1e-15
    assert abs(f(1j) - cmath.acos(1j)) < 1e-15


def test_cmath_atan():
    f = lambdify(x, atan(x), "cmath")
    assert abs(f(0) - cmath.atan(0)) < 1e-15
    assert abs(f(1) - cmath.atan(1)) < 1e-15
    assert abs(f(-1) - cmath.atan(-1)) < 1e-15
    assert abs(f(2) - cmath.atan(2)) < 1e-15
    assert abs(f(2j) - cmath.atan(2j)) < 1e-15


def test_cmath_asinh():
    f = lambdify(x, asinh(x), "cmath")
    assert abs(f(0) - cmath.asinh(0)) < 1e-15
    assert abs(f(1) - cmath.asinh(1)) < 1e-15
    assert abs(f(-1) - cmath.asinh(-1)) < 1e-15
    assert abs(f(2) - cmath.asinh(2)) < 1e-15
    assert abs(f(2j) - cmath.asinh(2j)) < 1e-15


def test_cmath_acosh():
    f = lambdify(x, acosh(x), "cmath")
    assert abs(f(1) - cmath.acosh(1)) < 1e-15
    assert abs(f(2) - cmath.acosh(2)) < 1e-15
    assert abs(f(-1) - cmath.acosh(-1)) < 1e-15
    assert abs(f(2j) - cmath.acosh(2j)) < 1e-15


def test_cmath_atanh():
    f = lambdify(x, atanh(x), "cmath")
    assert abs(f(0) - cmath.atanh(0)) < 1e-15
    assert abs(f(0.5) - cmath.atanh(0.5)) < 1e-15
    assert abs(f(-0.5) - cmath.atanh(-0.5)) < 1e-15
    assert abs(f(2) - cmath.atanh(2)) < 1e-15
    assert abs(f(-2) - cmath.atanh(-2)) < 1e-15
    assert abs(f(2j) - cmath.atanh(2j)) < 1e-15