Introduction

This online help node and the nodes below it describe functions relating to the field of rational numbers Q. Note that most functions for rational integers are classified as integer functions.

The rational field Q is automatically created when Magma is started up. That means that in Q, unlike most other structures, arithmetic can be done without the need to create the structure explicitly first. The same is true for the ring of integers.

In order to be compatible with the other rings and fields, Q.1 will return 1.

Contents

Representation

Rational numbers are stored as pairs of numerator and denominator. Whenever a rational number is created, it will be put in reduced form (coprime numerator and denominator, positive denominator). It is well possible that a rational number has denominator 1, and thus represents a rational integer; in such cases it will however never automatically be converted into an integer (that is, its type will not be changed).

Coercion

As a general rule, automatic coercion occurs between elements of Q and elements of any ring R of characteristic 0. That means, for example, that addition of any rational number and an element r of such ring can be performed without the need to coerce the elements first; the result will be in the larger of Q and R (usually R, unless R is a subring of Q such as Z). The most important exceptions to the above rule are those cases where the result would lie in a structure strictly larger than both Q and R. Examples of this are R=Z[x], and the result would generally be in Q(x), and R=OK, an order in a number field (and the result could be in K).

Example FldRat_Coercion (H21E1)

We give three examples of successful automatic coercion, and one where it does not work. Note that in the third case the result, although being integral, is still in the rational field.
> 1/2 + elt< CyclotomicField(3) | 1,2>;
1/2*(4*zeta_3 + 3)
> 1/2 - 0.12345;
0.37655
> 1/2 * 2;
1
> Parent(1/2 * 2);
Rational Field
> R<x> := PolynomialRing(Integers());
> // The following produces an error:
> 1/2 + x;
>> 1/2 + x;
       ^
Runtime error in '+': Bad argument types

Homomorphisms

Since homomorphisms are generally only allowed to be unitary, the specification of ring homomorphisms from Q=Q to a ring R is particularly simple: the image is completely determined by the image of 1, which we require to be 1 in R, so

hom< Q -> R | > : FldHom, Rng -> Map

suffices.

Note that Magma allows the user to define maps with hom that are not proper homomorphisms; this is sometimes useful, as the example below shows.

Example FldRat_homomorphism (H21E2)

Suppose we wish to coerce rational numbers with denominator not divisible by 11 into the ring Z/11Z in the obvious way by sending r/s to rs - 1 mod 11. The coercion rules do not allow you to do so using !, but a simple `homomorphism' will work.
> Z11 := Integers(11);
> Q := RationalField();
> h := hom< Q -> Z11 | >;
> h(1/2);
6
V2.28, 13 July 2023