def sinh_cosh(x): """ sinh_cosh(x) -> (number, number) Return a tuple containing the hyperbolic sine and cosine of x. """ if isinstance(x, mpfr): res1 = _new_mpfr(gmp.mpfr_get_prec(x._mpfr)) res2 = _new_mpfr(gmp.mpfr_get_prec(x._mpfr)) mpfr_x = x._mpfr elif isinstance(x, float): res1 = _new_mpfr() res2 = _new_mpfr() mpfr_x = res1 gmp.mpfr_set_d(mpfr_x, x, gmp.MPFR_RNDN) elif isinstance(x, (int, long)): res1 = _new_mpfr() res2 = _new_mpfr() mpfr_x = res1 _pyint_to_mpfr(x, mpfr_x) elif isinstance(x, mpz): res1 = _new_mpfr() res2 = _new_mpfr() mpfr_x = res1 gmp.mpfr_set_z(mpfr_x, x._mpz, gmp.MPFR_RNDN) elif isinstance(x, mpq): res1 = _new_mpfr() res2 = _new_mpfr() mpfr_x = res1 gmp.mpfr_set_q(mpfr_x, x._mpq, gmp.MPFR_RNDN) else: raise TypeError gmp.mpfr_sinh_cosh(res1, res2, mpfr_x, gmp.MPFR_RNDN) return (mpfr._from_c_mpfr(res1), mpfr._from_c_mpfr(res2))
def sin_cos(x): """ sin_cos(x) -> (number, number) Return a tuple containing the sine and cosine of x; x in radians. """ if isinstance(x, mpfr): res1 = _new_mpfr(gmp.mpfr_get_prec(x._mpfr)) res2 = _new_mpfr(gmp.mpfr_get_prec(x._mpfr)) mpfr_x = x._mpfr elif isinstance(x, float): res1 = _new_mpfr() res2 = _new_mpfr() mpfr_x = res1 elif isinstance(x, (int, long)): res1 = _new_mpfr() res2 = _new_mpfr() mpfr_x = res1 _pyint_to_mpfr(x, mpfr_x) elif isinstance(x, mpz): res1 = _new_mpfr() res2 = _new_mpfr() mpfr_x = res1 gmp.mpfr_set_z(mpfr_x, x._mpz, gmp.MPFR_RNDN) elif isinstance(x, mpq): res1 = _new_mpfr() res2 = _new_mpfr() mpfr_x = res1 gmp.mpfr_set_q(mpfr_x, x._mpq, gmp.MPFR_RNDN) else: if isinstance(x, mpc): res1 = _new_mpc() res2 = _new_mpc() mpc_x = x._mpc elif isinstance(x, complex): res1 = _new_mpc() res2 = _new_mpc() mpc_x = res1 gmp.mpc_set_d_d(mpc_x, x.real, x.imag, gmp.MPC_RNDNN) else: raise TypeError gmp.mpc_sin_cos(res1, res2, mpc_x, gmp.MPC_RNDNN, gmp.MPC_RNDNN) return (mpc._from_c_mpc(res1), mpc._from_c_mpc(res2)) gmp.mpfr_sin_cos(res1, res2, mpfr_x, gmp.MPFR_RNDN) return (mpfr._from_c_mpfr(res1), mpfr._from_c_mpfr(res2))
def cot(x): """ cot(x) -> number Return the cotangent of x; x in radians. """ res, x = _init_check_mpfr(x) gmp.mpfr_cot(res, x, gmp.MPFR_RNDN) return mpfr._from_c_mpfr(res)
def sec(x): """ sec(x) -> number Return the secant of x; x in radians. """ res, x = _init_check_mpfr(x) gmp.mpfr_sec(res, x, gmp.MPFR_RNDN) return mpfr._from_c_mpfr(res)
def lngamma(x): """ lngamma(x) -> number Return logarithm of gamma(x). """ res, x = _init_check_mpfr(x) gmp.mpfr_lngamma(res, x, gmp.MPFR_RNDN) return mpfr._from_c_mpfr(res)
def log2(x): """ log2(x) -> number Return the base-2 logarithm of x. """ res, x = _init_check_mpfr(x) gmp.mpfr_log2(res, x, gmp.MPFR_RNDN) return mpfr._from_c_mpfr(res)
def coth(x): """ coth(x) -> number Return the hyperbolic cotangent of x. """ res, x = _init_check_mpfr(x) gmp.mpfr_coth(res, x, gmp.MPFR_RNDN) return mpfr._from_c_mpfr(res)
def sech(x): """ sech(x) -> number Return the hyperbolic secant of x. """ res, x = _init_check_mpfr(x) gmp.mpfr_sech(res, x, gmp.MPFR_RNDN) return mpfr._from_c_mpfr(res)
def ai(x): """ ai(x) -> number Return the Airy function of x. """ res, mpfr_x = _init_check_mpfr(x) gmp.mpfr_ai(res, mpfr_x, gmp.MPFR_RNDN) return mpfr._from_c_mpfr(res)
def exp10(x): """ exp10(x) -> number Return 10**x. """ res, x = _init_check_mpfr(x) gmp.mpfr_exp10(res, x, gmp.MPFR_RNDN) return mpfr._from_c_mpfr(res)
def y1(x): """ y1(x) -> number Return the second kind Bessel function of order 1 of x. """ res, x = _init_check_mpfr(x) gmp.mpfr_y1(res, x, gmp.MPFR_RNDN) return mpfr._from_c_mpfr(res)
def erfc(x): """ zeta(x) -> number Return complementary error function of x. """ res, x = _init_check_mpfr(x) gmp.mpfr_erfc(res, x, gmp.MPFR_RNDN) return mpfr._from_c_mpfr(res)
def j0(x): """ j0(x) -> number Return the first kind Bessel function of order 0 of x. """ res, x = _init_check_mpfr(x) gmp.mpfr_j0(res, x, gmp.MPFR_RNDN) return mpfr._from_c_mpfr(res)
def digamma(x): """ digamma(x) -> number Return digamma of x. """ res, x = _init_check_mpfr(x) gmp.mpfr_digamma(res, x, gmp.MPFR_RNDN) return mpfr._from_c_mpfr(res)
def li2(x): """ li2(x) -> number Return the real part of dilogarithm of x. """ res, x = _init_check_mpfr(x) gmp.mpfr_li2(res, x, gmp.MPFR_RNDN) return mpfr._from_c_mpfr(res)
def eint(x): """ eint(x) -> number Return the exponential integral of x. """ res, x = _init_check_mpfr(x) gmp.mpfr_eint(res, x, gmp.MPFR_RNDN) return mpfr._from_c_mpfr(res)
def expm1(x): """ expm1(x) -> number Return exponential(x) - 1. """ res, x = _init_check_mpfr(x) gmp.mpfr_expm1(res, x, gmp.MPFR_RNDN) return mpfr._from_c_mpfr(res)
def log1p(x): """ log1p(x) -> number Return the logarithm of (1+x). """ res, x = _init_check_mpfr(x) gmp.mpfr_log1p(res, x, gmp.MPFR_RNDN) return mpfr._from_c_mpfr(res)
def const_catalan(precision=0): """ const_catalan([precision=0]) -> mpfr Return the catalan constant using the specified precision. If no precision is specified, the default precision is used. """ res = _new_mpfr(precision) gmp.mpfr_const_catalan(res, gmp.MPFR_RNDN) return mpfr._from_c_mpfr(res)
def yn(x, n): """ yn(x, n) -> number Return the second kind Bessel function of order n of x. """ if not (isinstance(n, (int, long)) and -sys.maxsize-1 <= n <= sys.maxsize): raise TypeError("yn() requires 'mpfr', 'int' arguments") res, x = _init_check_mpfr(x) gmp.mpfr_yn(res, n, x, gmp.MPFR_RNDN) return mpfr._from_c_mpfr(res)
def hypot(x, y): """ hypot(y, x) -> number Return the square root of (x**2 + y**2). """ # XXX Optimise res, mpfr_x = _init_check_mpfr(x) res, mpfr_y = _init_check_mpfr(y) gmp.mpfr_hypot(res, mpfr_x, mpfr_y, gmp.MPFR_RNDN) return mpfr._from_c_mpfr(res)
def lgamma(x): """ lgamma(x) -> (number, int) Return a tuple containing the logarithm of the absolute value of gamma(x) and the sign of gamma(x) """ res, x = _init_check_mpfr(x) sgn = ffi.new('int *') gmp.mpfr_lgamma(res, sgn, x, gmp.MPFR_RNDN) return (mpfr._from_c_mpfr(res), int(sgn[0]))
def agm(x, y): """ agm(x, y) -> number Return the arithmetic-geometric mean of x and y. """ # XXX Optimise res, mpfr_x = _init_check_mpfr(x) res, mpfr_y = _init_check_mpfr(y) gmp.mpfr_agm(res, mpfr_x, mpfr_y, gmp.MPFR_RNDN) return mpfr._from_c_mpfr(res)
def fms(x, y, z): """ fms(x, y, z) -> number Return the correctly rounded result of (x * y) - z. """ # XXX Optimise res, mpfr_x = _init_check_mpfr(x) res, mpfr_y = _init_check_mpfr(y) res, mpfr_z = _init_check_mpfr(z) gmp.mpfr_fms(res, mpfr_x, mpfr_y, mpfr_z, gmp.MPFR_RNDN) return mpfr._from_c_mpfr(res)
def zeta(x): """ zeta(x) -> number Return Riemann zeta of x. """ # if isinstance(x, (int, long)) and 0 <= x <= MAX_UI: # res = _new_mpfr() # gmp.mpfr_zeta_ui(res, x, gmp.MPFR_RNDN) res, x = _init_check_mpfr(x) gmp.mpfr_zeta(res, x, gmp.MPFR_RNDN) return mpfr._from_c_mpfr(res)
def sinh(x): """ sinh(x) -> number Return the hyperbolic sine of x. """ try: res, x = _init_check_mpfr(x) gmp.mpfr_sinh(res, x, gmp.MPFR_RNDN) return mpfr._from_c_mpfr(res) except TypeError: res, x = _init_check_mpc(x) gmp.mpc_sinh(res, x, gmp.MPC_RNDNN) return mpc._from_c_mpc(res)
def atanh(x): """ atanh(x) -> number Return the inverse hyperbolic tangent of x. """ try: res, x = _init_check_mpfr(x) gmp.mpfr_atanh(res, x, gmp.MPFR_RNDN) return mpfr._from_c_mpfr(res) except TypeError: res, x = _init_check_mpc(x) gmp.mpc_atanh(res, x, gmp.MPC_RNDNN) return mpc._from_c_mpc(res)
def atan(x): """ atan(x) -> number Return the arc-tangent of x; x in radians. """ try: res, x = _init_check_mpfr(x) gmp.mpfr_atan(res, x, gmp.MPFR_RNDN) return mpfr._from_c_mpfr(res) except TypeError: res, x = _init_check_mpc(x) gmp.mpc_atan(res, x, gmp.MPC_RNDNN) return mpc._from_c_mpc(res)
def sin(x): """ sin(x) -> number Return the sine of x; x in radians. """ try: res, x = _init_check_mpfr(x) gmp.mpfr_sin(res, x, gmp.MPFR_RNDN) return mpfr._from_c_mpfr(res) except TypeError: res, x = _init_check_mpc(x) gmp.mpc_sin(res, x, gmp.MPC_RNDNN) return mpc._from_c_mpc(res)
def log(x): """ log(x) -> number Return the natural logarithm of x. """ try: res, x = _init_check_mpfr(x) gmp.mpfr_log(res, x, gmp.MPFR_RNDN) return mpfr._from_c_mpfr(res) except TypeError: res, x = _init_check_mpc(x) gmp.mpc_log(res, x, gmp.MPC_RNDNN) return mpc._from_c_mpc(res)