cmath.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:ouroboros 作者: pybee 项目源码 文件源码
def tanh(x):

    _tanh_special = [
        [-1, None, complex(-1, -0.0), -1, None, -1, -1],
        [nan+nanj, None, None, None, None, nan+nanj, nan+nanj],
        [nan+nanj, None, complex(-0.0, -0.0), complex(-0.0, 0.0), None, nan+nanj, nan+nanj],
        [nan+nanj, None, complex(0.0, -0.0), 0.0, None, nan+nanj, nan+nanj],
        [nan+nanj, None, None, None, None, nan+nanj, nan+nanj],
        [1, None, complex(1, -0.0), 1, None, 1, 1],
        [nan+nanj, nan+nanj, complex(float("nan"), -0.0), nan, nan+nanj, nan+nanj, nan+nanj]
    ]

    z = _make_complex(x)

    if not isfinite(z):
        if math.isinf(z.imag) and math.isfinite(z.real):
            raise ValueError
        if math.isinf(z.real) and math.isfinite(z.imag) and z.imag != 0:
            if z.real > 0:
                return complex(1, math.copysign(0.0, math.sin(z.imag)
                                                * math.cos(z.imag)))
            return complex(-1, math.copysign(0.0, math.sin(z.imag)
                                             * math.cos(z.imag)))
        return _tanh_special[_special_type(z.real)][_special_type(z.imag)]

    if abs(z.real) > _LOG_LARGE_DOUBLE:
        return complex(
            math.copysign(1, z.real),
            4*math.sin(z.imag)*math.cos(z.imag)*math.exp(-2*abs(z.real))
        )
    tanh_x = math.tanh(z.real)
    tan_y = math.tan(z.imag)
    cx = 1/math.cosh(z.real)
    denom = 1 + tanh_x * tanh_x * tan_y * tan_y
    return complex(tanh_x * (1 + tan_y*tan_y)/denom,
                   ((tan_y / denom) * cx) * cx)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号