2.3 Basic, and not-so-basic, Rings

We illustrate some basic rings in Sage. For example, the field $ \mathbf{Q}$ of rational numbers may be referred to using either RationalField() or QQ:

sage: RationalField()
Rational Field
sage: QQ
Rational Field
sage: 1/2 in QQ
True
The decimal number $ 1.2$ is considered to be in $ \mathbf{Q}$ , since there is a coercion map from the reals to the rationals:
sage: 1.2 in QQ
True
However, the following doesn't work, since there is no coercion:
sage: I = ComplexField().0
sage: I in QQ
False
Also, of course, the symbolic constant $ \pi$ is not in $ \mathbf{Q}$ :
sage: pi in QQ
False

If you use QQ as a variable, you can still fetch the rational numbers using the command RationalField(). By the way, some other pre-defined Sage rings include the integers ZZ, the real numbers RR, the complex numbers CC (which uses I (or i), as usual, for the square root of $ -1$ ). We discuss polynomial rings in Section 2.4.

Do not redefine Integer or RealNumber unless you really know what you are doing. They are used by the Sage interpreter to wrap integer and real literals. For example, if you type Integer = int, then integer literals will behave as they usually do in Python, so e.g., 4/3 evaluates to the Python int 1. For example,

sage: 4/3
4/3
sage: parent(_)
Rational Field
sage: prev = Integer
sage: Integer = int           # bad idea!
sage: 4/3
1
sage: parent(_)
<type 'int'>
sage: Integer = prev
sage: 4/3
4/3

Now we illustrate some arithmetic involving various numbers.

sage: a, b = 4/3, 2/3
sage: a + b
2
sage: 2*b == a
True
sage: parent(2/3)
Rational Field
sage: parent(4/2)
Rational Field
sage: 2/3 + 0.1       # automatic coercion before addition
0.766666666666667
sage: 0.1 + 2/3       # coercion rules are symmetric in SAGE
0.766666666666667
sage: i = CC.0        # floating point complex number
sage: z = a + b*i
sage: z
1.33333333333333 + 0.666666666666667*I
sage: z.real() == a        # automatic coercion before comparison
True
sage: QQ(11.1)
111/10

Python is dynamically typed, so the value referred to by each variable has a type associated with it, but a given variable may hold values of any Python type within a given scope:

sage: a = 5
sage: type(a)
<type 'sage.rings.integer.Integer'>
sage: a = 5/3
sage: type(a)
<type 'sage.rings.rational.Rational'>
sage: a = 'hello'
sage: type(a)
<type 'str'>
The C programming language, which is statically typed, is much different; a variable declared to hold an int can only hold an int in its scope.

A Python oddity that is a potential source of confusion is that an integer literal that begins with a zero is treated as an octal number, i.e., a number in base 8.

sage: 011
9
sage: 8 + 1
9
sage: n = 011; n.str(8)
'11'
This is consistent with the C programming language, which treats integer literals beginning with a 0 as octal numbers.

The field of $ p$ -adic numbers is implemented as well: Note that once a $ p$ -adic field is created, you can not change its precision.

sage: K = Qp(11); K
11-adic Field with capped relative precision 20
sage: a = K(211/17); a
4 + 4*11 + 11^2 + 7*11^3 + 9*11^5 + 5*11^6 + 4*11^7 + 8*11^8 + 7*11^9 
  + 9*11^10 + 3*11^11 + 10*11^12 + 11^13 + 5*11^14 + 6*11^15 + 2*11^16 
  + 3*11^17 + 11^18 + 7*11^19 + O(11^20)
sage: b = K(3211/11^2); b
10*11^-2 + 5*11^-1 + 4 + 2*11 + O(11^18)

Much work has been done implementing rings of integers in $ p$ -adic fields or number fields other than $ \mathbf{Q}$ . The interested reader is invited to ask the experts on the mailing list for further details.

A number of related methods are already implemented in the NumberField class.

sage: R.<x> = PolynomialRing(QQ)
sage: K = NumberField(x^3 + x^2 - 2*x + 8, 'a')
sage: K.integral_basis()
[1, a, 1/2*a^2 + 1/2*a]

sage: K.galois_group()
Galois group PARI group [6, -1, 2, "S3"] of degree 3 of the Number Field 
in a with defining polynomial x^3 + x^2 - 2*x + 8

sage: K.polynomial_quotient_ring()
Univariate Quotient Polynomial Ring in a over Rational Field with modulus 
x^3 + x^2 - 2*x + 8
sage: K.units()
[3*a^2 + 13*a + 13]
sage: K.discriminant()
-503
sage: K.class_group()
Class group of order 1 with structure  of Number Field in a with 
defining polynomial x^3 + x^2 - 2*x + 8
sage: K.class_number()
1

See About this document... for information on suggesting changes.