[Next] [Prev] [Right] [____] [Up] [Index] [Root]
Introduction

Introduction

Real and complex numbers can only be stored in the computer effectively in the form of approximations. Magma provides a number of facilities for calculating with such approximations. Currently two different types of real number approximation can be used. The first type of real field contains elements of a given, fixed (but arbitrary) precision; the functions built on this type of real number are based on R.P. Brent's MP-package. We will call this type of structure a real field of fixed precision. The second type of real structure contains real numbers carrying their own precision with them: this structure contains real numbers to any precision. We will sometimes refer to this object as the free real field. Functions operating on this type of real approximations are based on Pari code.

Although we use the terms real field and complex field for Magma structures containing real or complex approximations, it should be noted that such a subset of the real or complex field may not even form a commutative ring.

Subsections

Fixed Precision Real Numbers

Real numbers of fixed precision are stored internally as expansions sum b_ir^i with respect to radix r=10^4. To make sure that no digits are hidden from the user (which would cause undesired effects in equality testing etc.), the precision of real and complex fields of fixed precision must be a multiple of 4. To store a real number d_1d_2 ... d_k.e_1e_2 ... e_n, where the d_i and e_j are decimal digits, with d_1 not equal to 0 (except, possibly, for k=1), requires Ceiling(k/4) radix 10^4 digits for d_1d_2 ... d_k, and Ceiling(n/4) radix 10^4 digits for e_1e_2 ... e_n, hence precision at least 4( Ceiling(k/4) + Ceiling(n/4)). Thus, it takes precision at least 4 to store 0.123 accurately, but at least precision 8 to store 1.23. The fixed precision is a relative precision, by which we mean that the fixed precision refers to the mantissa of the real number, and a separate exponent is kept by which the number can be `shifted' left of right.

Fixed precision complex numbers consist of a pair of fixed precision real numbers (with the same precision).

Magma stores a list of real and complex fields that have been created during a session, and it is assured that any two fields of the same fixed precision are the same. This means in particular that changing the name of sqrt(-1) (see AssignNames below) on one of the complex fields of precision r will change the name on every complex field of that same precision.


Example FldRe_FixedPrecision (H37E1)

We show how the radix 10^4 representation of some real numbers results in the creation of elements in the real field of fixed precision 4, in which only one digit with respect to that radix can be stored. Note that with r=10^4: eqalign( 12345 &= 1.r^1 + 2345.r^0, cr 1234 &= 1234.r^0, cr 1.234 &= 1.r^0 + 234.r^(-1), cr 0.1234 &= 1234.r^(-1), cr 0.001234 &= 12.r^(-1) + 34.r^(-2), cr 0.000001234 &= 123.r^(-2) + 45.r^(-3).cr)

> S := RealField(4);
> S ! 1e10;
1e10
> S ! 1234;
1234
> S ! 1.234;
1
> S ! 0.1234;
0.1234
> S ! 0.001234;
0.0012
> S ! 0.000001234;
1.23e-6
A warning is in place here; in the examples above, the real number on the right hand side had to be constructed in some real field before it could be coerced into S. That real field is the so-called default real field. In these examples it is assumed that the default field has sufficiently large precision to store the real numbers on the right accurately to the last digit.

Free Real Numbers

Besides the fields of fixed precision there is a `free' real field with elements of arbitrary precision. Every such free real number carries its own precision along. Internally these free reals are stored with respect to radix 2^(32). Integer and rational elements of the free real field will be known to infinite precision. Free complex numbers consist of a pair of free reals.

The precision of the result of some operation on free real (or complex) numbers will depend on the precision of the arguments and the kind of operation; generally the result will have the maximum possible precision with the given inputs.

Associated with the free real field is a precision parameter. Elements in the free field will be created to this default precision (unless it is explicitly done otherwise). There is also an output precision parameter. Printing of free reals is done to this precision (if it is not -1, meaning print the real to its precision). Changing both parameters can be done using AssertAttribute described below.

There is only one free real field and one free complex field in Magma, but a user can have references with different names to it; in particular, changing the default precision will change it for every free real and complex field.

Coercion

Automatic coercion ensures that all functions listed below that take an element of some real field as an argument, will also accept an integer or a rational number as an argument; in this case the integer or rational number will be coerced automatically into the default real field. For the binary operations (such as +, *) coercion also takes place: if one argument is real and the other is integral or rational, automatic coercion will put them both in the parent field of the real argument. If the arguments are real numbers of different fixed precision, the result will have the larger precision of the two, and the real argument with smaller precision will be padded with zeroes. If a binary operation on a fixed precision real and a free real number is attempted, the real number of fixed precision will be coerced into the free real field, and the operation will take place in there.

Note that some conversion will have to take place between a fixed real field and the free real field, since elements in the free real field are stored with respect to a 2-power radix while fixed precision reals are stored with respect to a 10-power radix.

The same coercion rules apply for functions taking a complex number as an argument; in that case real numbers will be valid input as well: if necessary reals, rationals or integers will be coerced into the appropriate complex field.

Elements of quadratic and cyclotomic fields that are real can be coerced into any real field using !; any quadratic or cyclotomic field element can be coerced by ! into any complex field. Functions taking real or complex arguments will not automatically coerce such arguments though.

Homomorphisms

The only homomorphisms that have a real field or a complex field as domain are the coercion functions. Therefore, homomorphisms from the reals or complexes may be specified as follows.

hom< R -> S | > : FldRe, Str -> Map
Here S must be a structure into which all elements of the real or complex field R are coercible, such as another real or complex field, or a polynomial ring over one of these. These homomorphisms can also be obtained as map by using the function Coercion, also called Bang.

Example FldRe_Homomorphisms (H37E2)

Here are two equivalent ways of creating the embedding function from a real field into a polynomial ring over some complex field.

> Re := RealField(20);
> PC<x, y> := PolynomialRing(ComplexField(8), 2);
> f := hom< Re -> PC | >;
> bangf := Bang(Re, PC);
> f(Pi(Re));
3.1416
> f(Pi(Re)) eq bangf(Pi(Re));
true

Special Options

When Magma is started up, a free real and complex field (with precision 28) is around by default. It serves (among others) as a parent for reals that are created as literals, such as 1.2345, in the same way as the default ring of integers is the parent for literal integers. It is possible to change this default real field with SetDefaultRealField.

It is also possible to change the precision of the free real (and complex) fields, using AssertAttribute.

Finally, AssignNames can be used to change the name for sqrt(-1) in a complex field.

SetDefaultRealField(R) : FldRe ->
SetDefaultRealField(R) : FldPr ->
Procedure to change the default parent for literal real numbers to the real field R. This parent is the free real field (with precision 28) by default.
GetDefaultRealField() : Null -> FldPr
GetDefaultRealField() : Null -> FldRe
Return the current parent for literal real numbers.
AssertAttribute(FldPr, "Precision", n) : Cat, MonStgElt, BoolElt ->
Procedure to change the precision on the free real and complex fields; the precision will be set to n. The precision determines the default size on the creation of elements of free real and complex fields. New elements may well be stored internally to a slightly larger precision because a representation to the base 2^(32) is used. Also note that this attribute is currently set on the category FldPr rather than any of its members, because the value is necessarily the same for all members of the category.
AssertAttribute(FldPr, "OutputPrecision", l) : Cat, MonStgElt, BoolElt ->
Procedure to change the output precision on the free real and complex fields; the output precision will be set to n. Elements of free real and complex fields will be printed with precision equal to n. A value of -1 means that elements will be printed according to their own precision. Also note that this attribute is currently set on the category FldPr rather than any of its members, because the value is necessarily the same for all members of the category.
HasAttribute(FldPr, "Precision") : Cat, MonStgElt -> BoolElt, RngIntElt
Function that returns a Boolean indicating whether a precision has been set on the free real and complex fields (which will always be true), as well as the value of the precision. This is 28 by default.
HasAttribute(FldPr, "OutputPrecision") : Cat, MonStgElt -> BoolElt, RngIntElt
Function that returns a Boolean indicating whether an output precision has been set on the free real and complex fields (which will always be true), as well as the value of the output precision. This is -1 by default.
AssignNames(~C, [s]) : FldPr, [ MonStgElt ]) ->
AssignNames(~C, [s]) : FldRe, [ MonStgElt ]) ->
Procedure to change the name of the purely imaginary element sqrt(-1) in the complex field C to the contents of the string s. When C is created, the name will be i, so suitable choices of s might be "I" or "j".

This procedure only changes the name used in printing the elements of C. It does not assign to an identifier called s the value of sqrt(-1) in C; to do this, use an assignment statement, or use angle brackets when creating the field.

Note that since this is a procedure that modifies C, it is necessary to have a reference ~C to C in the call to this function.

Name(C, 1) : FldPr, RngIntElt -> FldComElt
Name(C, 1) : FldCom, RngIntElt -> FldComElt
Given a complex field C (free or of fixed precision), return the element which has the name attached to it, that is, return the purely imaginary element sqrt(-1) of C.
[Next] [Prev] [Right] [____] [Up] [Index] [Root]