def jv(v, x): """Bessel function J_v(x).""" if isint(v): if isinstance(x, mpf): return make_mpf(libhyper.mpf_besseljn(int(v), x._mpf_, mp.prec)) if isinstance(x, mpc): return make_mpc(libhyper.mpc_besseljn(int(v), x._mpc_, mp.prec)) hx = x / 2 return hx**v * hyp0f1(v + 1, -hx**2) / factorial(v)
def jv(v, x): """Bessel function J_v(x).""" if isint(v): if isinstance(x, mpf): return make_mpf(libhyper.mpf_besseljn(int(v), x._mpf_, mp.prec)) if isinstance(x, mpc): return make_mpc(libhyper.mpc_besseljn(int(v), x._mpc_, mp.prec)) hx = x/2 return hx**v * hyp0f1(v+1, -hx**2) / factorial(v)
def hypsum(ar, af, ac, br, bf, bc, x): prec, rnd = prec_rounding if hasattr(x, "_mpf_") and not (ac or bc): v = libhyper.hypsum_internal(ar, af, ac, br, bf, bc, x._mpf_, None, prec, rnd) return make_mpf(v) else: if hasattr(x, "_mpc_"): re, im = x._mpc_ else: re, im = x._mpf_, libmpf.fzero v = libhyper.hypsum_internal(ar, af, ac, br, bf, bc, re, im, prec, rnd) return make_mpc(v)
def nthroot(x, n): """principal n-th root""" n = int(n) if isinstance(x, mpf): try: return make_mpf(libelefun.mpf_nthroot(x._mpf_, n, *prec_rounding)) except ComplexResult: if mp.trap_complex: raise x = (x._mpf_, libmpf.fzero) else: x = x._mpc_ return make_mpc(libmpc.mpc_nthroot(x, n, *prec_rounding))
def psi(m, z): """ Gives the polygamma function of order m of z, psi^(m)(z). Special cases are the digamma function (psi0), trigamma function (psi1), tetragamma (psi2) and pentagamma (psi4) functions. The parameter m should be a nonnegative integer. """ z = convert_lossless(z) m = int(m) if isinstance(z, mpf): return make_mpf(gammazeta.mpf_psi(m, z._mpf_, *prec_rounding)) else: return make_mpc(gammazeta.mpc_psi(m, z._mpc_, *prec_rounding))
def f(x, **kwargs): if not isinstance(x, mpnumeric): x = convert_lossless(x) prec, rounding = prec_rounding if kwargs: prec = kwargs.get('prec', prec) if 'dps' in kwargs: prec = dps_to_prec(kwargs['dps']) rounding = kwargs.get('rounding', rounding) if isinstance(x, mpf): try: return make_mpf(real_f(x._mpf_, prec, rounding)) except ComplexResult: # Handle propagation to complex if mp.trap_complex: raise return make_mpc(complex_f((x._mpf_, libmpf.fzero), prec, rounding)) elif isinstance(x, mpc): return make_mpc(complex_f(x._mpc_, prec, rounding)) elif isinstance(x, mpi): if interval_f: return make_mpi(interval_f(x._val, prec)) raise NotImplementedError("%s of a %s" % (name, type(x)))
def f(x, **kwargs): if not isinstance(x, mpnumeric): x = convert_lossless(x) prec, rounding = prec_rounding if kwargs: prec = kwargs.get('prec', prec) if 'dps' in kwargs: prec = dps_to_prec(kwargs['dps']) rounding = kwargs.get('rounding', rounding) if isinstance(x, mpf): try: return make_mpf(real_f(x._mpf_, prec, rounding)) except ComplexResult: # Handle propagation to complex if mp.trap_complex: raise return make_mpc( complex_f((x._mpf_, libmpf.fzero), prec, rounding)) elif isinstance(x, mpc): return make_mpc(complex_f(x._mpc_, prec, rounding)) elif isinstance(x, mpi): if interval_f: return make_mpi(interval_f(x._val, prec)) raise NotImplementedError("%s of a %s" % (name, type(x)))
def bernoulli(n): """nth Bernoulli number, B_n""" return make_mpf(gammazeta.mpf_bernoulli(int(n), *prec_rounding))
def frexp(x): """Convert x to a scaled number y in the range [0.5, 1). Returns (y, n) such that x = y * 2**n. No rounding is performed.""" x = convert_lossless(x) y, n = libmpf.mpf_frexp(x._mpf_) return make_mpf(y), n
def atan2(y,x): """atan2(y, x) has the same magnitude as atan(y/x) but accounts for the signs of y and x. (Defined for real x and y only.)""" x = convert_lossless(x) y = convert_lossless(y) return make_mpf(libelefun.mpf_atan2(y._mpf_, x._mpf_, *prec_rounding))
def ldexp(x, n): """Calculate mpf(x) * 2**n efficiently. No rounding is performed.""" x = convert_lossless(x) return make_mpf(libmpf.mpf_shift(x._mpf_, n))
def hypot(x, y): """Returns the Euclidean distance sqrt(x*x + y*y). Both x and y must be real.""" x = convert_lossless(x) y = convert_lossless(y) return make_mpf(libmpf.mpf_hypot(x._mpf_, y._mpf_, *prec_rounding))
def atan2(y, x): """atan2(y, x) has the same magnitude as atan(y/x) but accounts for the signs of y and x. (Defined for real x and y only.)""" x = convert_lossless(x) y = convert_lossless(y) return make_mpf(libelefun.mpf_atan2(y._mpf_, x._mpf_, *prec_rounding))
def sum_hyp2f1_rat(a, b, c, z): prec, rnd = prec_rounding if hasattr(z, "_mpf_"): return make_mpf(libhyper.mpf_hyp2f1_rat(a, b, c, z._mpf_, prec, rnd)) else: return make_mpc(libhyper.mpc_hyp2f1_rat(a, b, c, z._mpc_, prec, rnd))
def NEG(x): return make_mpf(mpf_neg(x._mpf_))