Ask Your Question
13

What are the different real numbers in Sage ?

asked 2013-03-28 12:36:28 +0100

tmonteil gravatar image

updated 2013-04-23 08:26:49 +0100

No computer can grasp the genuine real numbers, so I am trying to understand how sage handles them (and going to write a small tutorial about this). Here is a list of approaching objects i found:

Exact subfields of the reals:

  • QQ = RationalField() The genuine rationals. Denominators can be arbitrary big, the computations are exact, so the rounding error is only made when approaching a real by a rational, not during furher computations. Notice that the size of the denominators may grow along the computations, which can therefore become very slow.

  • NumberField? number fields described by an irreducible polynomial.

  • AA = AlgebraicField() The genuine algebraic numbers. Computations are exact.

  • SR = sage.symbolic.ring.SymbolicRing() This is the field made of expressions like sqrt(2+pi). It handles a lot of real numbers but seems very slow. Computations are not necessarily exact, since this field contains floating points real numbers. Is there a way to express any algebraic number in SR ?

  • CFF = ContinuedFractionField() reals are represented as a finite list of convergents. Claims it is exact but it actually approaches real numbers by rationals since the list of convergents are finite. However, we could imagine a future version using iterators instead of lists.

sage: CFF(sqrt(2))^2

[1, 1, 1491038293021224]

Floating point numbers:

sage: a = RLF(pi)

sage: RealField(100)(a+2)

5.1415926535897932384626433833

Relations between those fields

  • Two fields approaching the real numbers K and L can be compared using the function composite_field(K,L), that finds the "best" field containing both K and L.

sage: composite_field(RDF,QQbar)

Complex Double Field

  • Some maps are also defined to embed an abstract number field into the real line.

  • The method algebraic_dependency() that allows to guess whether a floating number corresponds to some algebraic number of a given degree.

Is this list complete or am i missing other real representations ? Im i wrong in the previous descriptions ? Could you order them by speed, by number of methods available (without coercion) ? Do you have any remark that could help the beginner to understand the subtilities in the use of those fields in Sage ?

Given an algebraic ... (more)

edit retag flag offensive close merge delete

Comments

2

It would be really nice to have a list like this in the documentation.

Eviatar Bach gravatar imageEviatar Bach ( 2013-03-28 18:16:55 +0100 )edit

Apparently there are also RealLiterals, which seem to be none of the above.

kcrisman gravatar imagekcrisman ( 2017-03-03 14:51:41 +0100 )edit

See https://trac.sagemath.org/ticket/15944 for a sample worksheet.

tmonteil gravatar imagetmonteil ( 2018-10-12 11:33:16 +0100 )edit

2 Answers

Sort by ยป oldest newest most voted
4

answered 2013-03-29 06:37:34 +0100

vdelecroix gravatar image

updated 2013-03-29 06:41:53 +0100

Hello,

As Eviatar said, it would be very nice to have it in Sage documentation! Here are some more remarks.

0) RLF is not a floating point field but an exact field: those are real numbers with an arbitrary precision. But as said in the documentation, it is mostly used as an "intermediate" field between number fields and all different floating point fields.

1) There are three nice methods to get from RR to QQ:

sage: a = RR(1.2254)
sage: a.simplest_rational()
6127/5000
sage: a.exact_rational()
2759355491689903/2251799813685248
sage: a.nearby_rational(max_error=0.0001)
87/71

Note that .nearby_rational() implements its own continued fraction algorithm.

2) There should be a huge warning for comparison of elements from different fields of real numbers::

sage: pi_rlf = RLF(pi)
sage: pi_rr = RR(pi)
sage: pi_rlf
3.141592653589794?
sage: pi_rr
3.14159265358979

sage: pi_rlf == pi_rr
True
sage: RLF(pi_rr) == pi_rlf
False

More generally, if you intend to make some documentation, it may be very useful to have a two dimensional array which says given x from field F1 and y from field F2 where is made the comparison x == y.

3) RIF is especially useful to locate roots of a polynomial. Actually, AA uses RIF with various precisions in background (which is essential for comparisons):

sage: a = AA(sqrt(2) + sqrt(3) + sqrt(7))
sage: a._value.parent()
Real Interval Field with 64 bits of precision
sage: a.interval_diameter(2**-20)
5.792015681006562933?
sage: a.interval_diameter(2**-70)
5.7920156810065629328307508193548308713?
sage: a._value.parent()
Real Interval Field with 128 bits of precision

There is a big difference between AA and number fields: number fields, even with a specified embedding, are not able to make comparisons (but you may have a look at tickets #13213 and #7160).

4) SR contains much more than numbers:

sage: sin(x).parent()
Symbolic Ring

And there is a wrapper for symbolic numbers to make them an element of RLF:

sage: log(2).parent()
Symbolic Ring
sage: log2 = RLF(log(2))
sage: log2
0.6931471805599453?

But which does not work quite well:

sage: exp(log2) == 2
False

Vincent

edit flag offensive delete link more
2

answered 2013-04-03 15:19:55 +0100

tmonteil gravatar image

Perhaps should we add the Python builtin float as well.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

4 followers

Stats

Asked: 2013-03-28 12:36:28 +0100

Seen: 8,198 times

Last updated: Apr 23 '13