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 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)