def _evalf_(self, x, parent): """ EXAMPLES:: sage: erf(2).n() 0.995322265018953 sage: erf(2).n(200) 0.99532226501895273416206925636725292861089179704006007673835 sage: erf(pi - 1/2*I).n(100) 1.0000111669099367825726058952 + 1.6332655417638522934072124548e-6*I TESTS: Check that PARI/GP through the GP interface gives the same answer:: sage: gp.set_real_precision(59) # random 38 sage: print gp.eval("1 - erfc(1)"); print erf(1).n(200); 0.84270079294971486934122063508260925929606699796630290845994 0.84270079294971486934122063508260925929606699796630290845994 """ try: prec = parent.prec() except AttributeError: # not a Sage parent prec = 0 return parent(1) - parent(pari(x).erfc(prec))
def _evalf_(self, x, parent): """ EXAMPLES:: sage: erf(2).n() 0.995322265018953 sage: erf(2).n(200) 0.99532226501895273416206925636725292861089179704006007673835 sage: erf(pi - 1/2*I).n(100) 1.0000111669099367825726058952 + 1.6332655417638522934072124548e-6*I TESTS: Check that PARI/GP through the GP interface gives the same answer:: sage: gp.set_real_precision(59) # random 38 sage: print gp.eval("1 - erfc(1)"); print erf(1).n(200); 0.84270079294971486934122063508260925929606699796630290845994 0.84270079294971486934122063508260925929606699796630290845994 """ try: prec = parent.prec() except AttributeError: # not a Sage parent prec = 0 return parent(1) - parent(pari(x).erfc(prec))
def _eval_(self, x, y): """ EXAMPLES:: sage: gamma_inc(2.,0) 1.00000000000000 sage: gamma_inc(2,0) 1 sage: gamma_inc(1/2,2) -(erf(sqrt(2)) - 1)*sqrt(pi) sage: gamma_inc(1/2,1) -(erf(1) - 1)*sqrt(pi) sage: gamma_inc(1/2,0) sqrt(pi) sage: gamma_inc(x,0) gamma(x) sage: gamma_inc(1,2) e^(-2) sage: gamma_inc(0,2) -Ei(-2) """ if not isinstance(x, Expression) and not isinstance(y, Expression) and \ (is_inexact(x) or is_inexact(y)): x, y = coercion_model.canonical_coercion(x, y) return self._evalf_(x, y, parent(x)) if y == 0: return gamma(x) if x == 1: return exp(-y) if x == 0: return -Ei(-y) if x == Rational(1) / 2: #only for x>0 return sqrt(pi) * (1 - erf(sqrt(y))) return None
def _eval_(self, z): """ EXAMPLES:: sage: z = var('z') sage: sin_integral(z) sin_integral(z) sage: sin_integral(3.0) 1.84865252799947 sage: sin_integral(0) 0 """ if not isinstance(z, Expression) and is_inexact(z): return self._evalf_(z, parent(z)) # special case: z = 0 if isinstance(z, Expression): if z.is_trivial_zero(): return z else: if not z: return z return None # leaves the expression unevaluated
def _eval_(self, n, x): """ EXAMPLES:: sage: y=var('y') sage: bessel_I(y,x) bessel_I(y, x) sage: bessel_I(0.0, 1.0) 1.26606587775201 sage: bessel_I(1/2, 1) sqrt(2)*sinh(1)/sqrt(pi) sage: bessel_I(-1/2, pi) sqrt(2)*cosh(pi)/pi """ if (not isinstance(n, Expression) and not isinstance(x, Expression) and (is_inexact(n) or is_inexact(x))): coercion_model = get_coercion_model() n, x = coercion_model.canonical_coercion(n, x) return self._evalf_(n, x, parent(n)) # special identities if n == Integer(1) / Integer(2): return sqrt(2 / (pi * x)) * sinh(x) elif n == -Integer(1) / Integer(2): return sqrt(2 / (pi * x)) * cosh(x) return None # leaves the expression unevaluated
def _eval_(self, n, m, theta, phi, **kwargs): r""" TESTS:: sage: x, y = var('x y') sage: spherical_harmonic(1, 2, x, y) 0 sage: spherical_harmonic(1, -2, x, y) 0 sage: spherical_harmonic(1/2, 2, x, y) spherical_harmonic(1/2, 2, x, y) sage: spherical_harmonic(3, 2, x, y) 15/4*sqrt(7/30)*cos(x)*e^(2*I*y)*sin(x)^2/sqrt(pi) sage: spherical_harmonic(3, 2, 1, 2) 15/4*sqrt(7/30)*cos(1)*e^(4*I)*sin(1)^2/sqrt(pi) sage: spherical_harmonic(3 + I, 2., 1, 2) -0.351154337307488 - 0.415562233975369*I """ from sage.structure.coerce import parent cc = get_coercion_model().canonical_coercion coerced = cc(phi, cc(theta, cc(n, m)[0])[0])[0] if is_inexact(coerced) and not isinstance(coerced, Expression): return self._evalf_(n, m, theta, phi, parent=parent(coerced)) elif n in ZZ and m in ZZ and n > -1: if abs(m) > n: return ZZ(0) return meval("spherical_harmonic({},{},{},{})".format( ZZ(n), ZZ(m), maxima(theta), maxima(phi))) return
def _eval_(self, z): """ EXAMPLES:: sage: z = var('z') sage: sin_integral(z) sin_integral(z) sage: sin_integral(3.0) 1.84865252799947 sage: sin_integral(0) 0 """ if not isinstance(z, Expression) and is_inexact(z): return self._evalf_(z, parent(z)) # special case: z = 0 if isinstance(z, Expression): if z.is_trivial_zero(): return z else: if not z: return z return None # leaves the expression unevaluated
def _eval_(self, n, m, theta, phi, **kwargs): r""" TESTS:: sage: x, y = var('x y') sage: spherical_harmonic(1, 2, x, y) 0 sage: spherical_harmonic(1, -2, x, y) 0 sage: spherical_harmonic(1/2, 2, x, y) spherical_harmonic(1/2, 2, x, y) sage: spherical_harmonic(3, 2, x, y) 15/4*sqrt(7/30)*cos(x)*e^(2*I*y)*sin(x)^2/sqrt(pi) sage: spherical_harmonic(3, 2, 1, 2) 15/4*sqrt(7/30)*cos(1)*e^(4*I)*sin(1)^2/sqrt(pi) sage: spherical_harmonic(3 + I, 2., 1, 2) -0.351154337307488 - 0.415562233975369*I """ from sage.structure.coerce import parent cc = get_coercion_model().canonical_coercion coerced = cc(phi, cc(theta, cc(n, m)[0])[0])[0] if is_inexact(coerced) and not isinstance(coerced, Expression): return self._evalf_(n, m, theta, phi, parent=parent(coerced)) elif n in ZZ and m in ZZ and n > -1: if abs(m) > n: return ZZ(0) return meval("spherical_harmonic({},{},{},{})".format(ZZ(n), ZZ(m), maxima(theta), maxima(phi))) return
def _eval_(self, x, y): """ EXAMPLES:: sage: gamma_inc(2.,0) 1.00000000000000 sage: gamma_inc(2,0) 1 sage: gamma_inc(1/2,2) -(erf(sqrt(2)) - 1)*sqrt(pi) sage: gamma_inc(1/2,1) -(erf(1) - 1)*sqrt(pi) sage: gamma_inc(1/2,0) sqrt(pi) sage: gamma_inc(x,0) gamma(x) sage: gamma_inc(1,2) e^(-2) sage: gamma_inc(0,2) -Ei(-2) """ if not isinstance(x, Expression) and not isinstance(y, Expression) and \ (is_inexact(x) or is_inexact(y)): x, y = coercion_model.canonical_coercion(x, y) return self._evalf_(x, y, parent(x)) if y == 0: return gamma(x) if x == 1: return exp(-y) if x == 0: return -Ei(-y) if x == Rational(1)/2: #only for x>0 return sqrt(pi)*(1-erf(sqrt(y))) return None
def _evalf_(self, *args, **kwds): """ Returns a numerical approximation of this function using Maxima. Currently, this is limited to 53 bits of precision. EXAMPLES:: sage: from sage.functions.special import MaximaFunction sage: f = MaximaFunction("jacobi_sn") sage: f(1/2,1/2) jacobi_sn(1/2, 1/2) sage: f(1/2,1/2).n() 0.470750473655657 TESTS:: sage: f(1/2,1/2).n(150) Traceback (most recent call last): ... NotImplementedError: jacobi_sn not implemented for precision > 53 """ parent = kwds["parent"] if hasattr(parent, "prec") and parent.prec() > 53: raise NotImplementedError("%s not implemented for precision > 53" % self.name()) _init() return parent(maxima("%s, numer" % self._maxima_init_evaled_(*args)))
def _eval_(self, n, x): """ EXAMPLES:: sage: y=var('y') sage: bessel_I(y,x) bessel_I(y, x) sage: bessel_I(0.0, 1.0) 1.26606587775201 sage: bessel_I(1/2, 1) sqrt(2)*sinh(1)/sqrt(pi) sage: bessel_I(-1/2, pi) sqrt(2)*cosh(pi)/pi """ if (not isinstance(n, Expression) and not isinstance(x, Expression) and (is_inexact(n) or is_inexact(x))): coercion_model = get_coercion_model() n, x = coercion_model.canonical_coercion(n, x) return self._evalf_(n, x, parent(n)) # special identities if n == Integer(1) / Integer(2): return sqrt(2 / (pi * x)) * sinh(x) elif n == -Integer(1) / Integer(2): return sqrt(2 / (pi * x)) * cosh(x) return None # leaves the expression unevaluated
def _evalf_(self, *args, **kwds): """ Returns a numerical approximation of this function using Maxima. Currently, this is limited to 53 bits of precision. EXAMPLES:: sage: from sage.functions.special import MaximaFunction sage: f = MaximaFunction("jacobi_sn") sage: f(1/2,1/2) jacobi_sn(1/2, 1/2) sage: f(1/2,1/2).n() 0.470750473655657 TESTS:: sage: f(1/2,1/2).n(150) Traceback (most recent call last): ... NotImplementedError: jacobi_sn not implemented for precision > 53 """ parent = kwds['parent'] if hasattr(parent, 'prec') and parent.prec() > 53: raise NotImplementedError("%s not implemented for precision > 53" % self.name()) _init() return parent(maxima("%s, numer" % self._maxima_init_evaled_(*args)))
def _eval_(self, n, z ): """ EXAMPLES:: """ # howto find a common parent for n and z here? if not isinstance(z, Expression) and is_inexact(z): return self._evalf_(n, z, parent(z)) return None
def __call__(self, x, prec=None, coerce=True, hold=False): """ Note that the ``prec`` argument is deprecated. The precision for the result is deduced from the precision of the input. Convert the input to a higher precision explicitly if a result with higher precision is desired.:: sage: t = gamma(RealField(100)(2.5)); t 1.3293403881791370204736256125 sage: t.prec() 100 sage: gamma(6, prec=53) doctest:...: DeprecationWarning: The prec keyword argument is deprecated. Explicitly set the precision of the input, for example gamma(RealField(300)(1)), or use the prec argument to .n() for exact inputs, e.g., gamma(1).n(300), instead. 120.000000000000 TESTS:: sage: gamma(pi,prec=100) 2.2880377953400324179595889091 sage: gamma(3/4,prec=100) 1.2254167024651776451290983034 """ if prec is not None: from sage.misc.misc import deprecation deprecation( "The prec keyword argument is deprecated. Explicitly set the precision of the input, for example gamma(RealField(300)(1)), or use the prec argument to .n() for exact inputs, e.g., gamma(1).n(300), instead." ) import mpmath return mpmath_utils.call(mpmath.gamma, x, prec=prec) # this is a kludge to keep # sage: Q.<i> = NumberField(x^2+1) # sage: gamma(i) # working, since number field elements cannot be coerced into SR # without specifying an explicit embedding into CC any more try: res = GinacFunction.__call__(self, x, coerce=coerce, hold=hold) except TypeError, err: # the __call__() method returns a TypeError for fast float arguments # as well, we only proceed if the error message says that # the arguments cannot be coerced to SR if not str(err).startswith("cannot coerce"): raise from sage.misc.misc import deprecation deprecation( "Calling symbolic functions with arguments that cannot be coerced into symbolic expressions is deprecated." ) parent = RR if prec is None else RealField(prec) try: x = parent(x) except (ValueError, TypeError): x = parent.complex_field()(x) res = GinacFunction.__call__(self, x, coerce=coerce, hold=hold)
def _evalf_(self, u, m, parent=None, algorithm=None): """ EXAMPLES:: sage: elliptic_eu(1,1).n() 0.761594155955765 sage: elliptic_eu(1,1).n(200) 0.7615941559557648881194582... """ R = parent or parent(z) return mpmath_utils.call(elliptic_eu_f, u, m, parent=R)
def _evalf_(self, u, m, parent=None, algorithm=None): """ EXAMPLES:: sage: elliptic_eu(1,1).n() 0.761594155955765 sage: elliptic_eu(1,1).n(200) 0.7615941559557648881194582... """ R = parent or parent(z) return mpmath_utils.call(elliptic_eu_f, u, m, parent=R)
def __call__(self, x, prec=None, coerce=True, hold=False): """ Note that the ``prec`` argument is deprecated. The precision for the result is deduced from the precision of the input. Convert the input to a higher precision explicitly if a result with higher precision is desired.:: sage: t = gamma(RealField(100)(2.5)); t 1.3293403881791370204736256125 sage: t.prec() 100 sage: gamma(6, prec=53) doctest:...: DeprecationWarning: The prec keyword argument is deprecated. Explicitly set the precision of the input, for example gamma(RealField(300)(1)), or use the prec argument to .n() for exact inputs, e.g., gamma(1).n(300), instead. 120.000000000000 TESTS:: sage: gamma(pi,prec=100) 2.2880377953400324179595889091 sage: gamma(3/4,prec=100) 1.2254167024651776451290983034 """ if prec is not None: from sage.misc.misc import deprecation deprecation("The prec keyword argument is deprecated. Explicitly set the precision of the input, for example gamma(RealField(300)(1)), or use the prec argument to .n() for exact inputs, e.g., gamma(1).n(300), instead.") import mpmath return mpmath_utils.call(mpmath.gamma, x, prec=prec) # this is a kludge to keep # sage: Q.<i> = NumberField(x^2+1) # sage: gamma(i) # working, since number field elements cannot be coerced into SR # without specifying an explicit embedding into CC any more try: res = GinacFunction.__call__(self, x, coerce=coerce, hold=hold) except TypeError, err: # the __call__() method returns a TypeError for fast float arguments # as well, we only proceed if the error message says that # the arguments cannot be coerced to SR if not str(err).startswith("cannot coerce"): raise from sage.misc.misc import deprecation deprecation("Calling symbolic functions with arguments that cannot be coerced into symbolic expressions is deprecated.") parent = RR if prec is None else RealField(prec) try: x = parent(x) except (ValueError, TypeError): x = parent.complex_field()(x) res = GinacFunction.__call__(self, x, coerce=coerce, hold=hold)
def eval_formula(self, n, x): """ Evaluate ``chebyshev_T`` using an explicit formula. See [ASHandbook]_ 227 (p. 782) for details for the recurions. See also [EffCheby]_ for fast evaluation techniques. INPUT: - ``n`` -- an integer - ``x`` -- a value to evaluate the polynomial at (this can be any ring element) EXAMPLES:: sage: chebyshev_T.eval_formula(-1,x) x sage: chebyshev_T.eval_formula(0,x) 1 sage: chebyshev_T.eval_formula(1,x) x sage: chebyshev_T.eval_formula(2,0.1) == chebyshev_T._evalf_(2,0.1) True sage: chebyshev_T.eval_formula(10,x) 512*x^10 - 1280*x^8 + 1120*x^6 - 400*x^4 + 50*x^2 - 1 sage: chebyshev_T.eval_algebraic(10,x).expand() 512*x^10 - 1280*x^8 + 1120*x^6 - 400*x^4 + 50*x^2 - 1 """ if n < 0: return self.eval_formula(-n, x) elif n == 0: return parent(x).one() res = parent(x).zero() for j in xrange(0, n // 2 + 1): f = factorial(n - 1 - j) / factorial(j) / factorial(n - 2 * j) res += (-1) ** j * (2 * x) ** (n - 2 * j) * f res *= n / 2 return res
def eval_formula(self, n, x): """ Evaluate ``chebyshev_T`` using an explicit formula. See [ASHandbook]_ 227 (p. 782) for details for the recurions. See also [EffCheby]_ for fast evaluation techniques. INPUT: - ``n`` -- an integer - ``x`` -- a value to evaluate the polynomial at (this can be any ring element) EXAMPLES:: sage: chebyshev_T.eval_formula(-1,x) x sage: chebyshev_T.eval_formula(0,x) 1 sage: chebyshev_T.eval_formula(1,x) x sage: chebyshev_T.eval_formula(2,0.1) == chebyshev_T._evalf_(2,0.1) True sage: chebyshev_T.eval_formula(10,x) 512*x^10 - 1280*x^8 + 1120*x^6 - 400*x^4 + 50*x^2 - 1 sage: chebyshev_T.eval_algebraic(10,x).expand() 512*x^10 - 1280*x^8 + 1120*x^6 - 400*x^4 + 50*x^2 - 1 """ if n < 0: return self.eval_formula(-n, x) elif n == 0: return parent(x).one() res = parent(x).zero() for j in xrange(0, n // 2 + 1): f = factorial(n - 1 - j) / factorial(j) / factorial(n - 2 * j) res += (-1)**j * (2 * x)**(n - 2 * j) * f res *= n / 2 return res
def _eval_(self, z): """ EXAMPLES:: sage: exp_integral_e1(x) exp_integral_e1(x) sage: exp_integral_e1(1.0) 0.219383934395520 """ if not isinstance(z, Expression) and is_inexact(z): return self._evalf_(z, parent(z)) return None # leaves the expression unevaluated
def _evalf_(self, z, parent=None, algorithm=None): """ EXAMPLES:: sage: elliptic_kc(1/2).n() 1.85407467730137 sage: elliptic_kc(1/2).n(200) 1.85407467730137191843385034... sage: elliptic_kc(I).n() 1.42127228104504 + 0.295380284214777*I """ R = parent or parent(z) from mpmath import ellipk return mpmath_utils.call(ellipk, z, parent=R)
def _evalf_(self, x, parent=None, algorithm=None): """ EXAMPLES:: sage: elliptic_ec(sqrt(2)/2).n() 1.23742252487318 sage: elliptic_ec(sqrt(2)/2).n(200) 1.237422524873181672854746084083... sage: elliptic_ec(I).n() 1.63241178144043 - 0.369219492375499*I """ R = parent or parent(z) from mpmath import ellipe return mpmath_utils.call(ellipe, x, parent=R)
def _evalf_(self, z, m, parent=None, algorithm=None): """ EXAMPLES:: sage: elliptic_f(1,1).n() 1.22619117088352 sage: elliptic_f(1,1).n(200) 1.22619117088351707081306096... sage: elliptic_f(I,I).n() 0.149965060031782 + 0.925097284105771*I """ R = parent or parent(z) from mpmath import ellipf return mpmath_utils.call(ellipf, z, m, parent=R)
def _evalf_(self, x, parent=None, algorithm=None): """ EXAMPLES:: sage: elliptic_ec(sqrt(2)/2).n() 1.23742252487318 sage: elliptic_ec(sqrt(2)/2).n(200) 1.237422524873181672854746084083... sage: elliptic_ec(I).n() 1.63241178144043 - 0.369219492375499*I """ R = parent or parent(z) from mpmath import ellipe return mpmath_utils.call(ellipe, x, parent=R)
def _evalf_(self, z, m, parent=None, algorithm=None): """ EXAMPLES:: sage: elliptic_f(1,1).n() 1.22619117088352 sage: elliptic_f(1,1).n(200) 1.22619117088351707081306096... sage: elliptic_f(I,I).n() 0.149965060031782 + 0.925097284105771*I """ R = parent or parent(z) from mpmath import ellipf return mpmath_utils.call(ellipf, z, m, parent=R)
def _eval_(self, z): """ EXAMPLES:: sage: exp_integral_e1(x) exp_integral_e1(x) sage: exp_integral_e1(1.0) 0.219383934395520 """ if not isinstance(z, Expression) and is_inexact(z): return self._evalf_(z, parent(z)) return None # leaves the expression unevaluated
def _evalf_(self, z, parent=None, algorithm=None): """ EXAMPLES:: sage: elliptic_kc(1/2).n() 1.85407467730137 sage: elliptic_kc(1/2).n(200) 1.85407467730137191843385034... sage: elliptic_kc(I).n() 1.42127228104504 + 0.295380284214777*I """ R = parent or parent(z) from mpmath import ellipk return mpmath_utils.call(ellipk, z, parent=R)
def _eval_(self, z): """ EXAMPLES:: sage: z = var('z') sage: cosh_integral(z) cosh_integral(z) sage: cosh_integral(3.0) 4.96039209476561 """ if not isinstance(z, Expression) and is_inexact(z): return self._evalf_(z, parent(z)) return None
def _eval_(self, z): """ EXAMPLES:: sage: z = var('z') sage: cosh_integral(z) cosh_integral(z) sage: cosh_integral(3.0) 4.96039209476561 """ if not isinstance(z, Expression) and is_inexact(z): return self._evalf_(z, parent(z)) return None
def _evalf_(self, n, z, m, parent=None, algorithm=None): """ EXAMPLES:: sage: elliptic_pi(pi,1/2,1).n() 0.795062820631931 sage: elliptic_pi(pi,1/2,1).n(200) 0.79506282063193125292514098445... sage: elliptic_pi(pi,1,1).n() 0.0991592574231369 - 1.30004368185937*I sage: elliptic_pi(pi,I,I).n() 0.0542471560940594 + 0.552096453413081*I """ R = parent or parent(z) from mpmath import ellippi return mpmath_utils.call(ellippi, n, z, m, parent=R)
def _evalf_(self, n, z, m, parent=None, algorithm=None): """ EXAMPLES:: sage: elliptic_pi(pi,1/2,1).n() 0.795062820631931 sage: elliptic_pi(pi,1/2,1).n(200) 0.79506282063193125292514098445... sage: elliptic_pi(pi,1,1).n() 0.0991592574231369 - 1.30004368185937*I sage: elliptic_pi(pi,I,I).n() 0.0542471560940594 + 0.552096453413081*I """ R = parent or parent(z) from mpmath import ellippi return mpmath_utils.call(ellippi, n, z, m, parent=R)
def eval_algebraic(self, n, x): """ Evaluate :class:`chebyshev_U` as polynomial, using a recursive formula. INPUT: - ``n`` -- an integer - ``x`` -- a value to evaluate the polynomial at (this can be any ring element) EXAMPLES:: sage: chebyshev_U.eval_algebraic(5,x) -2*((2*x + 1)*(2*x - 1)*x - 4*(2*x^2 - 1)*x)*(2*x + 1)*(2*x - 1) sage: parent(chebyshev_U(3, Mod(8,9))) Ring of integers modulo 9 sage: parent(chebyshev_U(3, Mod(1,9))) Ring of integers modulo 9 sage: chebyshev_U(-3,x) + chebyshev_U(1,x) 0 sage: chebyshev_U(-1,Mod(5,8)) 0 sage: parent(chebyshev_U(-1,Mod(5,8))) Ring of integers modulo 8 sage: R.<t> = ZZ[] sage: chebyshev_U.eval_algebraic(-2, t) -1 sage: chebyshev_U.eval_algebraic(-1, t) 0 sage: chebyshev_U.eval_algebraic(0, t) 1 sage: chebyshev_U.eval_algebraic(1, t) 2*t sage: n = 97; x = RIF(pi/n) sage: chebyshev_U(n-1, cos(x)).contains_zero() True sage: R.<t> = Zp(2, 6, 'capped-abs')[] sage: chebyshev_U(10^6+1, t) (2 + O(2^6))*t + (O(2^6)) """ if n == -1: return parent(x).zero() if n < 0: return -self._eval_recursive_(-n - 2, x)[0] return self._eval_recursive_(n, x)[0]
def eval_algebraic(self, n, x): """ Evaluate :class:`chebyshev_U` as polynomial, using a recursive formula. INPUT: - ``n`` -- an integer - ``x`` -- a value to evaluate the polynomial at (this can be any ring element) EXAMPLES:: sage: chebyshev_U.eval_algebraic(5,x) -2*((2*x + 1)*(2*x - 1)*x - 4*(2*x^2 - 1)*x)*(2*x + 1)*(2*x - 1) sage: parent(chebyshev_U(3, Mod(8,9))) Ring of integers modulo 9 sage: parent(chebyshev_U(3, Mod(1,9))) Ring of integers modulo 9 sage: chebyshev_U(-3,x) + chebyshev_U(1,x) 0 sage: chebyshev_U(-1,Mod(5,8)) 0 sage: parent(chebyshev_U(-1,Mod(5,8))) Ring of integers modulo 8 sage: R.<t> = ZZ[] sage: chebyshev_U.eval_algebraic(-2, t) -1 sage: chebyshev_U.eval_algebraic(-1, t) 0 sage: chebyshev_U.eval_algebraic(0, t) 1 sage: chebyshev_U.eval_algebraic(1, t) 2*t sage: n = 97; x = RIF(pi/n) sage: chebyshev_U(n-1, cos(x)).contains_zero() True sage: R.<t> = Zp(2, 6, 'capped-abs')[] sage: chebyshev_U(10^6+1, t) (2 + O(2^6))*t + (O(2^6)) """ if n == -1: return parent(x).zero() if n < 0: return -self._eval_recursive_(-n - 2, x)[0] return self._eval_recursive_(n, x)[0]
def _eval_(self, n, x): """ EXAMPLES:: sage: a, b = var('a, b') sage: bessel_J(a, b) bessel_J(a, b) sage: bessel_J(1.0, 1.0) 0.440050585744933 """ if (not isinstance(n, Expression) and not isinstance(x, Expression) and (is_inexact(n) or is_inexact(x))): coercion_model = get_coercion_model() n, x = coercion_model.canonical_coercion(n, x) return self._evalf_(n, x, parent(n)) return None
def _eval_(self, n, z): """ EXAMPLES:: sage: exp_integral_e(1.0, x) exp_integral_e(1.00000000000000, x) sage: exp_integral_e(x, 1.0) exp_integral_e(x, 1.00000000000000) sage: exp_integral_e(1.0, 1.0) 0.219383934395520 """ if not isinstance(n, Expression) and not isinstance(z, Expression) and \ (is_inexact(n) or is_inexact(z)): coercion_model = sage.structure.element.get_coercion_model() n, z = coercion_model.canonical_coercion(n, z) return self._evalf_(n, z, parent(n)) z_zero = False # special case: z == 0 and n > 1 if isinstance(z, Expression): if z.is_trivial_zero(): z_zero = True # for later if n > 1: return 1 / (n - 1) else: if not z: z_zero = True if n > 1: return 1 / (n - 1) # special case: n == 0 if isinstance(n, Expression): if n.is_trivial_zero(): if z_zero: return None else: return exp(-z) / z else: if not n: if z_zero: return None else: return exp(-z) / z return None # leaves the expression unevaluated
def _eval_(self, n, z): """ EXAMPLES:: sage: exp_integral_e(1.0, x) exp_integral_e(1.00000000000000, x) sage: exp_integral_e(x, 1.0) exp_integral_e(x, 1.00000000000000) sage: exp_integral_e(1.0, 1.0) 0.219383934395520 """ if not isinstance(n, Expression) and not isinstance(z, Expression) and \ (is_inexact(n) or is_inexact(z)): coercion_model = sage.structure.element.get_coercion_model() n, z = coercion_model.canonical_coercion(n, z) return self._evalf_(n, z, parent(n)) z_zero = False # special case: z == 0 and n > 1 if isinstance(z, Expression): if z.is_trivial_zero(): z_zero = True # for later if n > 1: return 1/(n-1) else: if not z: z_zero = True if n > 1: return 1/(n-1) # special case: n == 0 if isinstance(n, Expression): if n.is_trivial_zero(): if z_zero: return None else: return exp(-z)/z else: if not n: if z_zero: return None else: return exp(-z)/z return None # leaves the expression unevaluated
def _eval_(self, n, x): """ EXAMPLES:: sage: a,b = var('a, b') sage: bessel_Y(a, b) bessel_Y(a, b) sage: bessel_Y(0, 1).n(128) 0.088256964215676957982926766023515162828 """ if (not isinstance(n, Expression) and not isinstance(x, Expression) and (is_inexact(n) or is_inexact(x))): coercion_model = get_coercion_model() n, x = coercion_model.canonical_coercion(n, x) return self._evalf_(n, x, parent(n)) return None # leaves the expression unevaluated
def _eval_(self, n, x): """ EXAMPLES:: sage: a,b = var('a, b') sage: bessel_Y(a, b) bessel_Y(a, b) sage: bessel_Y(0, 1).n(128) 0.088256964215676957982926766023515162828 """ if (not isinstance(n, Expression) and not isinstance(x, Expression) and (is_inexact(n) or is_inexact(x))): coercion_model = get_coercion_model() n, x = coercion_model.canonical_coercion(n, x) return self._evalf_(n, x, parent(n)) return None # leaves the expression unevaluated
def _eval_(self, a, b, z, **kwargs): """ EXAMPLES:: sage: hypergeometric([], [], 0) 1 """ if not isinstance(a,tuple) or not isinstance(b,tuple): raise ValueError('First two parameters must be of type list.') coercion_model = get_coercion_model() co = reduce(lambda x, y: coercion_model.canonical_coercion(x, y)[0], a + b + (z,)) if is_inexact(co) and not isinstance(co, Expression): from sage.structure.coerce import parent return self._evalf_(a, b, z, parent=parent(co)) if not isinstance(z, Expression) and z == 0: # Expression is excluded return Integer(1) # to avoid call to Maxima return
def _eval_(self, a, b, z, **kwargs): """ EXAMPLES:: sage: hypergeometric([], [], 0) 1 """ if not isinstance(a,tuple) or not isinstance(b,tuple): raise ValueError('First two parameters must be of type list.') coercion_model = get_coercion_model() co = reduce(lambda x, y: coercion_model.canonical_coercion(x, y)[0], a + b + (z,)) if is_inexact(co) and not isinstance(co, Expression): from sage.structure.coerce import parent return self._evalf_(a, b, z, parent=parent(co)) if not isinstance(z, Expression) and z == 0: # Expression is excluded return Integer(1) # to avoid call to Maxima return
def _eval_(self, n, x): """ EXAMPLES:: sage: a, b = var('a, b') sage: bessel_J(a, b) bessel_J(a, b) sage: bessel_J(1.0, 1.0) 0.440050585744933 """ if (not isinstance(n, Expression) and not isinstance(x, Expression) and (is_inexact(n) or is_inexact(x))): coercion_model = get_coercion_model() n, x = coercion_model.canonical_coercion(n, x) return self._evalf_(n, x, parent(n)) return None
def _eval_(self, x ): """ EXAMPLES:: sage: Ei(10) Ei(10) sage: Ei(I) Ei(I) sage: Ei(1.3) 2.72139888023202 sage: Ei(10r) Ei(10) sage: Ei(1.3r) 2.7213988802320235 """ if not isinstance(x, Expression) and is_inexact(x): return self._evalf_(x, parent(x)) return None
def _eval_(self, x): """ EXAMPLES:: sage: Ei(10) Ei(10) sage: Ei(I) Ei(I) sage: Ei(1.3) 2.72139888023202 sage: Ei(10r) Ei(10) sage: Ei(1.3r) 2.72139888023202 """ if not isinstance(x, Expression) and is_inexact(x): return self._evalf_(x, parent(x)) return None
def _evalf_(self, x, parent_d=None): """ EXAMPLES:: sage: arg(0.0) 0.000000000000000 sage: arg(3.0) 0.000000000000000 sage: arg(3.00000000000000000000000000) 0.00000000000000000000000000 sage: arg(3.00000000000000000000000000).prec() 90 sage: arg(ComplexIntervalField(90)(3)).prec() 90 sage: arg(ComplexIntervalField(90)(3)).parent() Real Interval Field with 90 bits of precision sage: arg(3.0r) 0.000000000000000 sage: arg(RDF(3)) 0.0 sage: arg(RDF(3)).parent() Real Double Field sage: arg(-2.5) 3.14159265358979 sage: arg(2.0+3*i) 0.982793723247329 """ try: return x.arg() except AttributeError: pass # try to find a parent that support .arg() if parent_d is None: parent_d = parent(x) try: parent_d = parent_d.complex_field() except AttributeError: from sage.rings.complex_field import ComplexField try: parent_d = ComplexField(x.prec()) except AttributeError: parent_d = ComplexField() return parent_d(x).arg()
def _evalf_(self, x, parent_d=None): """ EXAMPLES:: sage: arg(0.0) 0.000000000000000 sage: arg(3.0) 0.000000000000000 sage: arg(3.00000000000000000000000000) 0.00000000000000000000000000 sage: arg(3.00000000000000000000000000).prec() 90 sage: arg(ComplexIntervalField(90)(3)).prec() 90 sage: arg(ComplexIntervalField(90)(3)).parent() Real Interval Field with 90 bits of precision sage: arg(3.0r) 0.000000000000000 sage: arg(RDF(3)) 0.0 sage: arg(RDF(3)).parent() Real Double Field sage: arg(-2.5) 3.14159265358979 sage: arg(2.0+3*i) 0.982793723247329 """ try: return x.arg() except AttributeError: pass # try to find a parent that support .arg() if parent_d is None: parent_d = parent(x) try: parent_d = parent_d.complex_field() except AttributeError: from sage.rings.complex_field import ComplexField try: parent_d = ComplexField(x.prec()) except AttributeError: parent_d = ComplexField() return parent_d(x).arg()
def _eval_(self, z): """ EXAMPLES:: sage: z = var('z') sage: cos_integral(z) cos_integral(z) sage: cos_integral(3.0) 0.119629786008000 sage: cos_integral(0) cos_integral(0) sage: N(cos_integral(0)) -infinity """ if not isinstance(z, Expression) and is_inexact(z): return self._evalf_(z, parent(z)) return None # leaves the expression unevaluated
def _eval_(self,z): """ EXAMPLES:: sage: z = var('z') sage: log_integral_offset(z) -log_integral(2) + log_integral(z) sage: log_integral_offset(3.0) 1.11842481454970 sage: log_integral_offset(2) 0 """ if not isinstance(z,Expression) and is_inexact(z): return self._evalf_(z,parent(z)) if z==2: import sage.symbolic.ring return sage.symbolic.ring.SR(0) return li(z)-li(2)
def _eval_(self, z): """ EXAMPLES:: sage: z = var('z') sage: cos_integral(z) cos_integral(z) sage: cos_integral(3.0) 0.119629786008000 sage: cos_integral(0) cos_integral(0) sage: N(cos_integral(0)) -infinity """ if not isinstance(z, Expression) and is_inexact(z): return self._evalf_(z, parent(z)) return None # leaves the expression unevaluated
def _eval_(self, z): """ EXAMPLES:: sage: z = var('z') sage: log_integral_offset(z) -log_integral(2) + log_integral(z) sage: log_integral_offset(3.0) 1.11842481454970 sage: log_integral_offset(2) 0 """ if not isinstance(z, Expression) and is_inexact(z): return self._evalf_(z, parent(z)) if z == 2: import sage.symbolic.ring return sage.symbolic.ring.SR(0) return li(z) - li(2)
def eval_algebraic(self, n, x): """ Evaluate :class:`chebyshev_T` as polynomial, using a recursive formula. INPUT: - ``n`` -- an integer - ``x`` -- a value to evaluate the polynomial at (this can be any ring element) EXAMPLES:: sage: chebyshev_T.eval_algebraic(5, x) 2*(2*(2*x^2 - 1)*x - x)*(2*x^2 - 1) - x sage: chebyshev_T(-7, x) - chebyshev_T(7,x) 0 sage: R.<t> = ZZ[] sage: chebyshev_T.eval_algebraic(-1, t) t sage: chebyshev_T.eval_algebraic(0, t) 1 sage: chebyshev_T.eval_algebraic(1, t) t sage: chebyshev_T(7^100, 1/2) 1/2 sage: chebyshev_T(7^100, Mod(2,3)) 2 sage: n = 97; x = RIF(pi/2/n) sage: chebyshev_T(n, cos(x)).contains_zero() True sage: R.<t> = Zp(2, 8, 'capped-abs')[] sage: chebyshev_T(10^6+1, t) (2^7 + O(2^8))*t^5 + (O(2^8))*t^4 + (2^6 + O(2^8))*t^3 + (O(2^8))*t^2 + (1 + 2^6 + O(2^8))*t + (O(2^8)) """ if n == 0: return parent(x).one() if n < 0: return self._eval_recursive_(-n, x)[0] return self._eval_recursive_(n, x)[0]
def eval_algebraic(self, n, x): """ Evaluate :class:`chebyshev_T` as polynomial, using a recursive formula. INPUT: - ``n`` -- an integer - ``x`` -- a value to evaluate the polynomial at (this can be any ring element) EXAMPLES:: sage: chebyshev_T.eval_algebraic(5, x) 2*(2*(2*x^2 - 1)*x - x)*(2*x^2 - 1) - x sage: chebyshev_T(-7, x) - chebyshev_T(7,x) 0 sage: R.<t> = ZZ[] sage: chebyshev_T.eval_algebraic(-1, t) t sage: chebyshev_T.eval_algebraic(0, t) 1 sage: chebyshev_T.eval_algebraic(1, t) t sage: chebyshev_T(7^100, 1/2) 1/2 sage: chebyshev_T(7^100, Mod(2,3)) 2 sage: n = 97; x = RIF(pi/2/n) sage: chebyshev_T(n, cos(x)).contains_zero() True sage: R.<t> = Zp(2, 8, 'capped-abs')[] sage: chebyshev_T(10^6+1, t) (2^7 + O(2^8))*t^5 + (O(2^8))*t^4 + (2^6 + O(2^8))*t^3 + (O(2^8))*t^2 + (1 + 2^6 + O(2^8))*t + (O(2^8)) """ if n == 0: return parent(x).one() if n < 0: return self._eval_recursive_(-n, x)[0] return self._eval_recursive_(n, x)[0]
def _evalf_(self, z, m, parent=None, algorithm=None): """ EXAMPLES:: sage: elliptic_e(0.5, 0.1) 0.498011394498832 sage: elliptic_e(1/2, 1/10).n(200) 0.4980113944988315331154610406... sage: elliptic_e(I, I).n() -0.189847437084712 + 1.03209769372160*I TESTS: This gave an error in Maxima (:trac:`15046`):: sage: elliptic_e(2.5, 2.5) 0.535647771608740 + 1.63996015168665*I """ R = parent or parent(z) from mpmath import ellipe return mpmath_utils.call(ellipe, z, m, parent=R)
def _evalf_(self, z, m, parent=None, algorithm=None): """ EXAMPLES:: sage: elliptic_e(0.5, 0.1) 0.498011394498832 sage: elliptic_e(1/2, 1/10).n(200) 0.4980113944988315331154610406... sage: elliptic_e(I, I).n() -0.189847437084712 + 1.03209769372160*I TESTS: This gave an error in Maxima (:trac:`15046`):: sage: elliptic_e(2.5, 2.5) 0.535647771608740 + 1.63996015168665*I """ R = parent or parent(z) from mpmath import ellipe return mpmath_utils.call(ellipe, z, m, parent=R)
def _eval_recursive_(self, n, x, both=False): """ If ``both=True``, compute ``(T(n,x), T(n-1,x))`` using a recursive formula. If ``both=False``, return instead a tuple ``(T(n,x), False)``. EXAMPLES:: sage: chebyshev_T._eval_recursive_(5, x) (2*(2*(2*x^2 - 1)*x - x)*(2*x^2 - 1) - x, False) sage: chebyshev_T._eval_recursive_(5, x, True) (2*(2*(2*x^2 - 1)*x - x)*(2*x^2 - 1) - x, 2*(2*x^2 - 1)^2 - 1) """ if n == 1: return x, parent(x).one() assert n >= 2 a, b = self._eval_recursive_((n + 1) // 2, x, both or n % 2) if n % 2 == 0: return 2 * a * a - 1, both and 2 * a * b - x else: return 2 * a * b - x, both and 2 * b * b - 1
def _evalf_(self, n, x, **kwds): """ Evaluate :class:`chebyshev_U` numerically with mpmath. EXAMPLES:: sage: chebyshev_U(5,-4+3.*I) 98280.0000000000 - 11310.0000000000*I sage: chebyshev_U(10,3).n(75) 4.661117900000000000000e7 sage: chebyshev_U._evalf_(1.5, Mod(8,9)) Traceback (most recent call last): ... TypeError: cannot evaluate chebyshev_U with parent Ring of integers modulo 9 """ try: real_parent = kwds['parent'] except KeyError: real_parent = parent(x) if not is_RealField(real_parent) and not is_ComplexField( real_parent): # parent is not a real or complex field: figure out a good parent if x in RR: x = RR(x) real_parent = RR elif x in CC: x = CC(x) real_parent = CC if not is_RealField(real_parent) and not is_ComplexField(real_parent): raise TypeError( "cannot evaluate chebyshev_U with parent {}".format( real_parent)) from sage.libs.mpmath.all import call as mpcall from sage.libs.mpmath.all import chebyu as mpchebyu return mpcall(mpchebyu, n, x, parent=real_parent)
def _eval_(self, z): """ EXAMPLES:: sage: z = var('z') sage: log_integral(z) log_integral(z) sage: log_integral(3.0) 2.16358859466719 sage: log_integral(0) 0 """ if isinstance(z, Expression): if z.is_trivial_zero(): # special case: z = 0 return z else: if is_inexact(z): return self._evalf_(z, parent(z)) elif not z: return z return None # leaves the expression unevaluated
def _eval_recursive_(self, n, x, both=False): """ If ``both=True``, compute ``(U(n,x), U(n-1,x))`` using a recursive formula. If ``both=False``, return instead a tuple ``(U(n,x), False)``. EXAMPLES:: sage: chebyshev_U._eval_recursive_(3, x) (4*((2*x + 1)*(2*x - 1) - 2*x^2)*x, False) sage: chebyshev_U._eval_recursive_(3, x, True) (4*((2*x + 1)*(2*x - 1) - 2*x^2)*x, ((2*x + 1)*(2*x - 1) + 2*x)*((2*x + 1)*(2*x - 1) - 2*x)) """ if n == 0: return parent(x).one(), 2 * x assert n >= 1 a, b = self._eval_recursive_((n - 1) // 2, x, True) if n % 2 == 0: return (b + a) * (b - a), both and 2 * b * (x * b - a) else: return 2 * a * (b - x * a), both and (b + a) * (b - a)
def _eval_recursive_(self, n, x, both=False): """ If ``both=True``, compute ``(U(n,x), U(n-1,x))`` using a recursive formula. If ``both=False``, return instead a tuple ``(U(n,x), False)``. EXAMPLES:: sage: chebyshev_U._eval_recursive_(3, x) (4*((2*x + 1)*(2*x - 1) - 2*x^2)*x, False) sage: chebyshev_U._eval_recursive_(3, x, True) (4*((2*x + 1)*(2*x - 1) - 2*x^2)*x, ((2*x + 1)*(2*x - 1) + 2*x)*((2*x + 1)*(2*x - 1) - 2*x)) """ if n == 0: return parent(x).one(), 2 * x assert n >= 1 a, b = self._eval_recursive_((n - 1) // 2, x, True) if n % 2 == 0: return (b + a) * (b - a), both and 2 * b * (x * b - a) else: return 2 * a * (b - x * a), both and (b + a) * (b - a)
def _eval_recursive_(self, n, x, both=False): """ If ``both=True``, compute ``(T(n,x), T(n-1,x))`` using a recursive formula. If ``both=False``, return instead a tuple ``(T(n,x), False)``. EXAMPLES:: sage: chebyshev_T._eval_recursive_(5, x) (2*(2*(2*x^2 - 1)*x - x)*(2*x^2 - 1) - x, False) sage: chebyshev_T._eval_recursive_(5, x, True) (2*(2*(2*x^2 - 1)*x - x)*(2*x^2 - 1) - x, 2*(2*x^2 - 1)^2 - 1) """ if n == 1: return x, parent(x).one() assert n >= 2 a, b = self._eval_recursive_((n + 1) // 2, x, both or n % 2) if n % 2 == 0: return 2 * a * a - 1, both and 2 * a * b - x else: return 2 * a * b - x, both and 2 * b * b - 1
def _eval_(self, z): """ EXAMPLES:: sage: z = var('z') sage: log_integral(z) log_integral(z) sage: log_integral(3.0) 2.16358859466719 sage: log_integral(0) 0 """ if isinstance(z, Expression): if z.is_trivial_zero(): # special case: z = 0 return z else: if is_inexact(z): return self._evalf_(z, parent(z)) elif not z: return z return None # leaves the expression unevaluated