def _evalf_(self, x, y, parent=None, algorithm='mpmath'): """ EXAMPLES:: sage: gamma_inc_lower(3,2.) 0.646647167633873 sage: gamma_inc_lower(3,2).n(200) 0.646647167633873081060005050275155... sage: gamma_inc_lower(0,2.) +infinity """ R = parent or s_parent(x) # C is the complex version of R # prec is the precision of R if R is float: prec = 53 C = complex else: try: prec = R.precision() except AttributeError: prec = 53 try: C = R.complex_field() except AttributeError: C = R if algorithm == 'pari': try: v = ComplexField(prec)(x).gamma() - ComplexField(prec)( x).gamma_inc(y) except AttributeError: if not (is_ComplexNumber(x)): if is_ComplexNumber(y): C = y.parent() else: C = ComplexField() x = C(x) v = ComplexField(prec)(x).gamma() - ComplexField(prec)( x).gamma_inc(y) else: import mpmath v = ComplexField(prec)(mpmath_utils.call(mpmath.gammainc, x, 0, y, parent=R)) if v.is_real(): return R(v) else: return C(v)
def pyobject(self, ex, obj): from mathics.core import expression from mathics.core.expression import Number if obj is None: return expression.Symbol('Null') elif isinstance(obj, (list, tuple)) or is_Vector(obj): return expression.Expression('List', *(from_sage(item, self.subs) for item in obj)) elif isinstance(obj, Constant): return expression.Symbol(obj._conversions.get('mathematica', obj._name)) elif is_Integer(obj): return expression.Integer(str(obj)) elif isinstance(obj, sage.Rational): rational = expression.Rational(str(obj)) if rational.value.denom() == 1: return expression.Integer(rational.value.numer()) else: return rational elif isinstance(obj, sage.RealDoubleElement) or is_RealNumber(obj): return expression.Real(str(obj)) elif is_ComplexNumber(obj): real = Number.from_string(str(obj.real())).value imag = Number.from_string(str(obj.imag())).value return expression.Complex(real, imag) elif isinstance(obj, NumberFieldElement_quadratic): # TODO: this need not be a complex number, but we assume so! real = Number.from_string(str(obj.real())).value imag = Number.from_string(str(obj.imag())).value return expression.Complex(real, imag) else: return expression.from_python(obj)
def _rounded_real(z): if is_ComplexNumber(z): return z.real().round() elif isinstance(z, complex): return round(z.real) else: raise ValueError('Unknown type %s.'%type(z))
def zeta_symmetric(s): r""" Completed function `\xi(s)` that satisfies `\xi(s) = \xi(1-s)` and has zeros at the same points as the Riemann zeta function. INPUT: - ``s`` - real or complex number If s is a real number the computation is done using the MPFR library. When the input is not real, the computation is done using the PARI C library. More precisely, .. MATH:: xi(s) = \gamma(s/2 + 1) * (s-1) * \pi^{-s/2} * \zeta(s). EXAMPLES:: sage: zeta_symmetric(0.7) 0.497580414651127 sage: zeta_symmetric(1-0.7) 0.497580414651127 sage: RR = RealField(200) sage: zeta_symmetric(RR(0.7)) 0.49758041465112690357779107525638385212657443284080589766062 sage: C.<i> = ComplexField() sage: zeta_symmetric(0.5 + i*14.0) 0.000201294444235258 + 1.49077798716757e-19*I sage: zeta_symmetric(0.5 + i*14.1) 0.0000489893483255687 + 4.40457132572236e-20*I sage: zeta_symmetric(0.5 + i*14.2) -0.0000868931282620101 + 7.11507675693612e-20*I REFERENCE: - I copied the definition of xi from http://web.viu.ca/pughg/RiemannZeta/RiemannZetaLong.html """ if not (is_ComplexNumber(s) or is_RealNumber(s)): s = ComplexField()(s) R = s.parent() if s == 1: # deal with poles, hopefully return R(0.5) return (s / 2 + 1).gamma() * (s - 1) * (R.pi()**(-s / 2)) * s.zeta()
def zeta_symmetric(s): r""" Completed function `\xi(s)` that satisfies `\xi(s) = \xi(1-s)` and has zeros at the same points as the Riemann zeta function. INPUT: - ``s`` - real or complex number If s is a real number the computation is done using the MPFR library. When the input is not real, the computation is done using the PARI C library. More precisely, .. math:: xi(s) = \gamma(s/2 + 1) * (s-1) * \pi^{-s/2} * \zeta(s). EXAMPLES:: sage: zeta_symmetric(0.7) 0.497580414651127 sage: zeta_symmetric(1-0.7) 0.497580414651127 sage: RR = RealField(200) sage: zeta_symmetric(RR(0.7)) 0.49758041465112690357779107525638385212657443284080589766062 sage: C.<i> = ComplexField() sage: zeta_symmetric(0.5 + i*14.0) 0.000201294444235258 + 1.49077798716757e-19*I sage: zeta_symmetric(0.5 + i*14.1) 0.0000489893483255687 + 4.40457132572236e-20*I sage: zeta_symmetric(0.5 + i*14.2) -0.0000868931282620101 + 7.11507675693612e-20*I REFERENCE: - I copied the definition of xi from http://web.viu.ca/pughg/RiemannZeta/RiemannZetaLong.html """ if not (is_ComplexNumber(s) or is_RealNumber(s)): s = ComplexField()(s) R = s.parent() if s == 1: # deal with poles, hopefully return R(0.5) return (s / 2 + 1).gamma() * (s - 1) * (R.pi() ** (-s / 2)) * s.zeta()
def numerical_approx(x, prec=None, digits=None, algorithm=None): r""" Returns a numerical approximation of an object ``x`` with at least ``prec`` bits (or decimal ``digits``) of precision. .. note:: Both upper case ``N`` and lower case ``n`` are aliases for :func:`numerical_approx`, and all three may be used as methods. INPUT: - ``x`` - an object that has a numerical_approx method, or can be coerced into a real or complex field - ``prec`` (optional) - an integer (bits of precision) - ``digits`` (optional) - an integer (digits of precision) - ``algorithm`` (optional) - a string specifying the algorithm to use for functions that implement more than one If neither the ``prec`` or ``digits`` are specified, the default is 53 bits of precision. If both are specified, then ``prec`` is used. EXAMPLES:: sage: numerical_approx(pi, 10) 3.1 sage: numerical_approx(pi, digits=10) 3.141592654 sage: numerical_approx(pi^2 + e, digits=20) 12.587886229548403854 sage: n(pi^2 + e) 12.5878862295484 sage: N(pi^2 + e) 12.5878862295484 sage: n(pi^2 + e, digits=50) 12.587886229548403854194778471228813633070946500941 sage: a = CC(-5).n(prec=100) sage: b = ComplexField(100)(-5) sage: a == b True sage: type(a) == type(b) True sage: numerical_approx(9) 9.00000000000000 You can also usually use method notation. :: sage: (pi^2 + e).n() 12.5878862295484 sage: (pi^2 + e).N() 12.5878862295484 sage: (pi^2 + e).numerical_approx() 12.5878862295484 Vectors and matrices may also have their entries approximated. :: sage: v = vector(RDF, [1,2,3]) sage: v.n() (1.00000000000000, 2.00000000000000, 3.00000000000000) sage: v = vector(CDF, [1,2,3]) sage: v.n() (1.00000000000000, 2.00000000000000, 3.00000000000000) sage: _.parent() Vector space of dimension 3 over Complex Field with 53 bits of precision sage: v.n(prec=75) (1.000000000000000000000, 2.000000000000000000000, 3.000000000000000000000) sage: u = vector(QQ, [1/2, 1/3, 1/4]) sage: n(u, prec=15) (0.5000, 0.3333, 0.2500) sage: n(u, digits=5) (0.50000, 0.33333, 0.25000) sage: v = vector(QQ, [1/2, 0, 0, 1/3, 0, 0, 0, 1/4], sparse=True) sage: u = v.numerical_approx(digits=4) sage: u.is_sparse() True sage: u (0.5000, 0.0000, 0.0000, 0.3333, 0.0000, 0.0000, 0.0000, 0.2500) sage: A = matrix(QQ, 2, 3, range(6)) sage: A.n() [0.000000000000000 1.00000000000000 2.00000000000000] [ 3.00000000000000 4.00000000000000 5.00000000000000] sage: B = matrix(Integers(12), 3, 8, srange(24)) sage: N(B, digits=2) [0.00 1.0 2.0 3.0 4.0 5.0 6.0 7.0] [ 8.0 9.0 10. 11. 0.00 1.0 2.0 3.0] [ 4.0 5.0 6.0 7.0 8.0 9.0 10. 11.] Internally, numerical approximations of real numbers are stored in base-2. Therefore, numbers which look the same in their decimal expansion might be different:: sage: x=N(pi, digits=3); x 3.14 sage: y=N(3.14, digits=3); y 3.14 sage: x==y False sage: x.str(base=2) '11.001001000100' sage: y.str(base=2) '11.001000111101' As an exceptional case, ``digits=1`` usually leads to 2 digits (one significant) in the decimal output (see :trac:`11647`):: sage: N(pi, digits=1) 3.2 sage: N(pi, digits=2) 3.1 sage: N(100*pi, digits=1) 320. sage: N(100*pi, digits=2) 310. In the following example, ``pi`` and ``3`` are both approximated to two bits of precision and then subtracted, which kills two bits of precision:: sage: N(pi, prec=2) 3.0 sage: N(3, prec=2) 3.0 sage: N(pi - 3, prec=2) 0.00 TESTS:: sage: numerical_approx(I) 1.00000000000000*I sage: x = QQ['x'].gen() sage: F.<k> = NumberField(x^2+2, embedding=sqrt(CC(2))*CC.0) sage: numerical_approx(k) 1.41421356237309*I sage: type(numerical_approx(CC(1/2))) <type 'sage.rings.complex_number.ComplexNumber'> The following tests :trac:`10761`, in which ``n()`` would break when called on complex-valued algebraic numbers. :: sage: E = matrix(3, [3,1,6,5,2,9,7,3,13]).eigenvalues(); E [18.16815365088822?, -0.08407682544410650? - 0.2190261484802906?*I, -0.08407682544410650? + 0.2190261484802906?*I] sage: E[1].parent() Algebraic Field sage: [a.n() for a in E] [18.1681536508882, -0.0840768254441065 - 0.219026148480291*I, -0.0840768254441065 + 0.219026148480291*I] Make sure we've rounded up log(10,2) enough to guarantee sufficient precision (trac #10164):: sage: ks = 4*10**5, 10**6 sage: check_str_length = lambda k: len(str(numerical_approx(1+10**-k,digits=k+1)))-1 >= k+1 sage: check_precision = lambda k: numerical_approx(1+10**-k,digits=k+1)-1 > 0 sage: all(check_str_length(k) and check_precision(k) for k in ks) True Testing we have sufficient precision for the golden ratio (:trac:`12163`), note that the decimal point adds 1 to the string length:: sage: len(str(n(golden_ratio, digits=5000))) 5001 sage: len(str(n(golden_ratio, digits=5000000))) # long time (4s on sage.math, 2012) 5000001 Check that :trac:`14778` is fixed:: sage: n(0, algorithm='foo') 0.000000000000000 """ if prec is None: if digits is None: prec = 53 else: prec = int((digits+1) * LOG_TEN_TWO_PLUS_EPSILON) + 1 try: return x._numerical_approx(prec, algorithm=algorithm) except AttributeError: from sage.rings.complex_double import is_ComplexDoubleElement from sage.rings.complex_number import is_ComplexNumber if not (is_ComplexNumber(x) or is_ComplexDoubleElement(x)): try: return sage.rings.real_mpfr.RealField(prec)(x) # Trac 10761: now catches ValueErrors as well as TypeErrors except (TypeError, ValueError): pass return sage.rings.complex_field.ComplexField(prec)(x)
def numerical_approx(x, prec=None, digits=None): """ Return a numerical approximation of x with at least prec bits of precision. .. note:: Both upper case N and lower case n are aliases for :func:`numerical_approx`. INPUT: - ``x`` - an object that has a numerical_approx method, or can be coerced into a real or complex field - ``prec (optional)`` - an integer (bits of precision) - ``digits (optional)`` - an integer (digits of precision) If neither the prec or digits are specified, the default is 53 bits of precision. EXAMPLES:: sage: numerical_approx(pi, 10) 3.1 sage: numerical_approx(pi, digits=10) 3.141592654 sage: numerical_approx(pi^2 + e, digits=20) 12.587886229548403854 sage: n(pi^2 + e) 12.5878862295484 sage: N(pi^2 + e) 12.5878862295484 sage: n(pi^2 + e, digits=50) 12.587886229548403854194778471228813633070946500941 sage: a = CC(-5).n(prec=100) sage: b = ComplexField(100)(-5) sage: a == b True sage: type(a) == type(b) True You can also usually use method notation:: sage: (pi^2 + e).n() 12.5878862295484 """ if prec is None: if digits is None: prec = 53 else: prec = int((digits + 1) * 3.32192) + 1 try: return x.numerical_approx(prec) except AttributeError: from sage.rings.complex_double import is_ComplexDoubleElement from sage.rings.complex_number import is_ComplexNumber if is_ComplexNumber(x) or is_ComplexDoubleElement(x): return sage.rings.complex_field.ComplexField(prec)(x) else: return sage.rings.real_mpfr.RealField_constructor(prec)(x)
def numerical_approx(x, prec=None, digits=None): r""" Returns a numerical approximation of an object ``x`` with at least ``prec`` bits (or decimal ``digits``) of precision. .. note:: Both upper case ``N`` and lower case ``n`` are aliases for :func:`numerical_approx`, and all three may be used as methods. INPUT: - ``x`` - an object that has a numerical_approx method, or can be coerced into a real or complex field - ``prec (optional)`` - an integer (bits of precision) - ``digits (optional)`` - an integer (digits of precision) If neither the ``prec`` or ``digits`` are specified, the default is 53 bits of precision. If both are specified, then ``prec`` is used. EXAMPLES:: sage: numerical_approx(pi, 10) 3.1 sage: numerical_approx(pi, digits=10) 3.141592654 sage: numerical_approx(pi^2 + e, digits=20) 12.587886229548403854 sage: n(pi^2 + e) 12.5878862295484 sage: N(pi^2 + e) 12.5878862295484 sage: n(pi^2 + e, digits=50) 12.587886229548403854194778471228813633070946500941 sage: a = CC(-5).n(prec=100) sage: b = ComplexField(100)(-5) sage: a == b True sage: type(a) == type(b) True sage: numerical_approx(9) 9.00000000000000 You can also usually use method notation. :: sage: (pi^2 + e).n() 12.5878862295484 sage: (pi^2 + e).N() 12.5878862295484 sage: (pi^2 + e).numerical_approx() 12.5878862295484 Vectors and matrices may also have their entries approximated. :: sage: v = vector(RDF, [1,2,3]) sage: v.n() (1.00000000000000, 2.00000000000000, 3.00000000000000) sage: v = vector(CDF, [1,2,3]) sage: v.n() (1.00000000000000, 2.00000000000000, 3.00000000000000) sage: _.parent() Vector space of dimension 3 over Complex Field with 53 bits of precision sage: v.n(prec=75) (1.000000000000000000000, 2.000000000000000000000, 3.000000000000000000000) sage: u = vector(QQ, [1/2, 1/3, 1/4]) sage: n(u, prec=15) (0.5000, 0.3333, 0.2500) sage: n(u, digits=5) (0.50000, 0.33333, 0.25000) sage: v = vector(QQ, [1/2, 0, 0, 1/3, 0, 0, 0, 1/4], sparse=True) sage: u = v.numerical_approx(digits=4) sage: u.is_sparse() True sage: u (0.5000, 0.0000, 0.0000, 0.3333, 0.0000, 0.0000, 0.0000, 0.2500) sage: A = matrix(QQ, 2, 3, range(6)) sage: A.n() [0.000000000000000 1.00000000000000 2.00000000000000] [ 3.00000000000000 4.00000000000000 5.00000000000000] sage: B = matrix(Integers(12), 3, 8, srange(24)) sage: N(B, digits=2) [0.00 1.0 2.0 3.0 4.0 5.0 6.0 7.0] [ 8.0 9.0 10. 11. 0.00 1.0 2.0 3.0] [ 4.0 5.0 6.0 7.0 8.0 9.0 10. 11.] Internally, numerical approximations of real numbers are stored in base-2. Therefore, numbers which look the same in their decimal expansion might be different:: sage: x=N(pi, digits=3); x 3.14 sage: y=N(3.14, digits=3); y 3.14 sage: x==y False sage: x.str(base=2) '11.001001000100' sage: y.str(base=2) '11.001000111101' As an exceptional case, ``digits=1`` usually leads to 2 digits (one significant) in the decimal output (see :trac:`11647`):: sage: N(pi, digits=1) 3.2 sage: N(pi, digits=2) 3.1 sage: N(100*pi, digits=1) 320. sage: N(100*pi, digits=2) 310. In the following example, ``pi`` and ``3`` are both approximated to two bits of precision and then subtracted, which kills two bits of precision:: sage: N(pi, prec=2) 3.0 sage: N(3, prec=2) 3.0 sage: N(pi - 3, prec=2) 0.00 TESTS:: sage: numerical_approx(I) 1.00000000000000*I sage: x = QQ['x'].gen() sage: F.<k> = NumberField(x^2+2, embedding=sqrt(CC(2))*CC.0) sage: numerical_approx(k) 1.41421356237309*I sage: type(numerical_approx(CC(1/2))) <type 'sage.rings.complex_number.ComplexNumber'> The following tests :trac:`10761`, in which ``n()`` would break when called on complex-valued algebraic numbers. :: sage: E = matrix(3, [3,1,6,5,2,9,7,3,13]).eigenvalues(); E [18.16815365088822?, -0.08407682544410650? - 0.2190261484802906?*I, -0.08407682544410650? + 0.2190261484802906?*I] sage: E[1].parent() Algebraic Field sage: [a.n() for a in E] [18.1681536508882, -0.0840768254441065 - 0.219026148480291*I, -0.0840768254441065 + 0.219026148480291*I] Make sure we've rounded up log(10,2) enough to guarantee sufficient precision (trac #10164):: sage: ks = 4*10**5, 10**6 sage: check_str_length = lambda k: len(str(numerical_approx(1+10**-k,digits=k+1)))-1 >= k+1 sage: check_precision = lambda k: numerical_approx(1+10**-k,digits=k+1)-1 > 0 sage: all(check_str_length(k) and check_precision(k) for k in ks) True Testing we have sufficient precision for the golden ratio (:trac:`12163`), note that the decimal point adds 1 to the string length:: sage: len(str(n(golden_ratio, digits=5000))) 5001 sage: len(str(n(golden_ratio, digits=5000000))) # long time (4s on sage.math, 2012) 5000001 """ if prec is None: if digits is None: prec = 53 else: prec = int((digits+1) * LOG_TEN_TWO_PLUS_EPSILON) + 1 try: return x._numerical_approx(prec) except AttributeError: from sage.rings.complex_double import is_ComplexDoubleElement from sage.rings.complex_number import is_ComplexNumber if not (is_ComplexNumber(x) or is_ComplexDoubleElement(x)): try: return sage.rings.real_mpfr.RealField(prec)(x) # Trac 10761: now catches ValueErrors as well as TypeErrors except (TypeError, ValueError): pass return sage.rings.complex_field.ComplexField(prec)(x)
def numerical_approx(x, prec=None, digits=None): """ Returns a numerical approximation of x with at least prec bits of precision. .. note:: Both upper case N and lower case n are aliases for :func:`numerical_approx`. INPUT: - ``x`` - an object that has a numerical_approx method, or can be coerced into a real or complex field - ``prec (optional)`` - an integer (bits of precision) - ``digits (optional)`` - an integer (digits of precision) If neither the prec or digits are specified, the default is 53 bits of precision. EXAMPLES:: sage: numerical_approx(pi, 10) 3.1 sage: numerical_approx(pi, digits=10) 3.141592654 sage: numerical_approx(pi^2 + e, digits=20) 12.587886229548403854 sage: n(pi^2 + e) 12.5878862295484 sage: N(pi^2 + e) 12.5878862295484 sage: n(pi^2 + e, digits=50) 12.587886229548403854194778471228813633070946500941 sage: a = CC(-5).n(prec=100) sage: b = ComplexField(100)(-5) sage: a == b True sage: type(a) == type(b) True sage: numerical_approx(9) 9.00000000000000 You can also usually use method notation:: sage: (pi^2 + e).n() 12.5878862295484 TESTS:: sage: numerical_approx(I) 1.00000000000000*I sage: x = QQ['x'].gen() sage: F.<k> = NumberField(x^2+2, embedding=sqrt(CC(2))*CC.0) sage: numerical_approx(k) 1.41421356237309*I sage: type(numerical_approx(CC(1/2))) <type 'sage.rings.complex_number.ComplexNumber'> The following tests Trac 10761, in which n() would break when called on complex-valued AlgebraicNumbers:: sage: E = matrix(3, [3,1,6,5,2,9,7,3,13]).eigenvalues(); E [18.16815365088822?, -0.08407682544410650? - 0.2190261484802906?*I, -0.08407682544410650? + 0.2190261484802906?*I] sage: E[1].parent() Algebraic Field sage: [a.n() for a in E] [18.1681536508882, -0.0840768254441065 - 0.219026148480291*I, -0.0840768254441065 + 0.219026148480291*I] """ if prec is None: if digits is None: prec = 53 else: prec = int((digits+1) * 3.32192) + 1 try: return x._numerical_approx(prec) except AttributeError: from sage.rings.complex_double import is_ComplexDoubleElement from sage.rings.complex_number import is_ComplexNumber if not (is_ComplexNumber(x) or is_ComplexDoubleElement(x)): try: return sage.rings.real_mpfr.RealField(prec)(x) # Trac 10761: now catches ValueErrors as well as TypeErrors except (TypeError, ValueError): pass return sage.rings.complex_field.ComplexField(prec)(x)
def numerical_approx(x, prec=None, digits=None): """ Return a numerical approximation of x with at least prec bits of precision. .. note:: Both upper case N and lower case n are aliases for :func:`numerical_approx`. INPUT: - ``x`` - an object that has a numerical_approx method, or can be coerced into a real or complex field - ``prec (optional)`` - an integer (bits of precision) - ``digits (optional)`` - an integer (digits of precision) If neither the prec or digits are specified, the default is 53 bits of precision. EXAMPLES:: sage: numerical_approx(pi, 10) 3.1 sage: numerical_approx(pi, digits=10) 3.141592654 sage: numerical_approx(pi^2 + e, digits=20) 12.587886229548403854 sage: n(pi^2 + e) 12.5878862295484 sage: N(pi^2 + e) 12.5878862295484 sage: n(pi^2 + e, digits=50) 12.587886229548403854194778471228813633070946500941 sage: a = CC(-5).n(prec=100) sage: b = ComplexField(100)(-5) sage: a == b True sage: type(a) == type(b) True You can also usually use method notation:: sage: (pi^2 + e).n() 12.5878862295484 """ if prec is None: if digits is None: prec = 53 else: prec = int((digits+1) * 3.32192) + 1 try: return x.numerical_approx(prec) except AttributeError: from sage.rings.complex_double import is_ComplexDoubleElement from sage.rings.complex_number import is_ComplexNumber if is_ComplexNumber(x) or is_ComplexDoubleElement(x): return sage.rings.complex_field.ComplexField(prec)(x) else: return sage.rings.real_mpfr.RealField_constructor(prec)(x)
def realpart(z): if is_ComplexNumber(z): return z.real() else: return z.real
def numerical_approx(x, prec=None, digits=None): """ Returns a numerical approximation of x with at least prec bits of precision. .. note:: Both upper case N and lower case n are aliases for :func:`numerical_approx`. INPUT: - ``x`` - an object that has a numerical_approx method, or can be coerced into a real or complex field - ``prec (optional)`` - an integer (bits of precision) - ``digits (optional)`` - an integer (digits of precision) If neither the prec or digits are specified, the default is 53 bits of precision. EXAMPLES:: sage: numerical_approx(pi, 10) 3.1 sage: numerical_approx(pi, digits=10) 3.141592654 sage: numerical_approx(pi^2 + e, digits=20) 12.587886229548403854 sage: n(pi^2 + e) 12.5878862295484 sage: N(pi^2 + e) 12.5878862295484 sage: n(pi^2 + e, digits=50) 12.587886229548403854194778471228813633070946500941 sage: a = CC(-5).n(prec=100) sage: b = ComplexField(100)(-5) sage: a == b True sage: type(a) == type(b) True sage: numerical_approx(9) 9.00000000000000 You can also usually use method notation:: sage: (pi^2 + e).n() 12.5878862295484 TESTS:: sage: numerical_approx(I) 1.00000000000000*I sage: x = QQ['x'].gen() sage: F.<k> = NumberField(x^2+2, embedding=sqrt(CC(2))*CC.0) sage: numerical_approx(k) 1.41421356237309*I sage: type(numerical_approx(CC(1/2))) <type 'sage.rings.complex_number.ComplexNumber'> The following tests Trac 10761, in which n() would break when called on complex-valued AlgebraicNumbers:: sage: E = matrix(3, [3,1,6,5,2,9,7,3,13]).eigenvalues(); E [18.16815365088822?, -0.08407682544410650? - 0.2190261484802906?*I, -0.08407682544410650? + 0.2190261484802906?*I] sage: E[1].parent() Algebraic Field sage: [a.n() for a in E] [18.1681536508882, -0.0840768254441065 - 0.219026148480291*I, -0.0840768254441065 + 0.219026148480291*I] """ if prec is None: if digits is None: prec = 53 else: prec = int((digits + 1) * 3.32192) + 1 try: return x._numerical_approx(prec) except AttributeError: from sage.rings.complex_double import is_ComplexDoubleElement from sage.rings.complex_number import is_ComplexNumber if not (is_ComplexNumber(x) or is_ComplexDoubleElement(x)): try: return sage.rings.real_mpfr.RealField(prec)(x) # Trac 10761: now catches ValueErrors as well as TypeErrors except (TypeError, ValueError): pass return sage.rings.complex_field.ComplexField(prec)(x)