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

Code before:
    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_lambdify_complex_identities():
    # Define symbol
    z = symbols('z')
    # Explicit mappings for re and im
    custom_mappings = {"re": lambda z: z.real, "im": lambda z: z.imag}
    expr = cos(z) - cos(re(z)) * cosh(im(z)) + I * sin(re(z)) * sinh(im(z))
    func = lambdify([z], expr, modules=[custom_mappings, "cmath", "math"])
    hpi = math.pi / 2
    assert func(hpi + 1j * hpi) == 0j

    # Euler's Formula
    func = lambdify([z], exp(I * z) - (cos(z) + I * sin(z)), modules=["cmath", "math"])
    assert func(hpi) == 0j

    # Exponential Identity: e^z = e^(Re(z)) * (cos(Im(z)) + i sin(Im(z)))
    func_exp = lambdify([z], exp(z) - exp(re(z)) * (cos(im(z)) + I * sin(im(z))),
                        modules=[custom_mappings, "cmath", "math"])
    assert func_exp(hpi + 1j * hpi) == 0j

    # Complex Cosine Identity: cos(z) = cos(Re(z)) * cosh(Im(z)) - i sin(Re(z)) * sinh(Im(z))
    func_cos = lambdify([z], cos(z) - (cos(re(z)) * cosh(im(z)) - I * sin(re(z)) * sinh(im(z))),
                        modules=[custom_mappings, "cmath", "math"])
    assert func_cos(hpi + 1j * hpi) == 0j

    # Complex Sine Identity: sin(z) = sin(Re(z)) * cosh(Im(z)) + i cos(Re(z)) * sinh(Im(z))
    func_sin = lambdify([z], sin(z) - (sin(re(z)) * cosh(im(z)) + I * cos(re(z)) * sinh(im(z))),
                        modules=[custom_mappings, "cmath", "math"])
    assert func_sin(hpi + 1j * hpi) == 0j

    # Complex Hyperbolic Cosine Identity: cosh(z) = cosh(Re(z)) * cos(Im(z)) + i sinh(Re(z)) * sin(Im(z))

Review comment (suggestion):
The CMathCode printer should not need any custom mappings, these functions are available in Python's `cmath` module.

Code after:


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


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"])
    hpi = math.pi / 2
    assert abs(func(hpi + 1j * hpi)) < 4e-16

    # Euler's Formula: e^(i*z) = cos(z) + i*sin(z)
    func = lambdify([z], exp(I * z) - (cos(z) + I * sin(z)), modules=["cmath", "math"])
    assert abs(func(hpi)) < 4e-16

    # Exponential Identity: e^z = e^(Re(z)) * (cos(Im(z)) + i*sin(Im(z)))
    func_exp = lambdify([z], exp(z) - exp(re(z)) * (cos(im(z)) + I * sin(im(z))),
                        modules=["cmath", "math"])
    assert abs(func_exp(hpi + 1j * hpi)) < 4e-16

    # Complex Cosine Identity: cos(z) = cos(Re(z)) * cosh(Im(z)) - i*sin(Re(z)) * sinh(Im(z))
    func_cos = lambdify([z], cos(z) - (cos(re(z)) * cosh(im(z)) - I * sin(re(z)) * sinh(im(z))),
                        modules=["cmath", "math"])