## Section16.10Sage

Rings are very important in your study of abstract algebra, and similarly, they are very important in the design and use of Sage. There is a lot of material in this chapter, and there are many corresponding commands in Sage.

### SubsectionCreating Rings

Here is a list of various rings, domains and fields you can construct simply.

1. Integers(), ZZ: the integral domain of positive and negative integers, $${\mathbb Z}\text{.}$$

2. Integers(n): the integers mod $$n\text{,}$$ $${\mathbb Z_n}\text{.}$$ A field when $$n$$ is prime, but just a ring for composite $$n\text{.}$$

3. QQ: the field of rational numbers, $${\mathbb Q}\text{.}$$

4. RR, CC: the field of real numbers and the field of complex numbers, $${\mathbb R}\text{,}$$ $${\mathbb C}\text{.}$$ It is impossible to create every real number inside a computer, so technically these sets do not behave as fields, but only give a good imitiation of the real thing. We say they are inexact rings to make this point.

5. QuadraticField(n): the field formed by combining the rationals with a solution to the polynomial equation $$x^2-n=0\text{.}$$ The notation in the text is $${\mathbb Q}[\sqrt{n}]\text{.}$$ A functional equivalent can be made with the syntax QQ[sqrt(n)]. Note that n can be negative.

6. CyclotomicField(n): the field formed by combining the rationals with the solutions to the polynomial equation $$x^n-1=0\text{.}$$

7. QQbar: the field formed by combining the rationals with the solutions to every polynomial equation with integer coefficients. This is known as a the field of algebraic numbers, denoted as $$\overline{{\mathbb Q}}\text{.}$$

8. FiniteField(p): for a prime $$p\text{,}$$ the field of integers $${\mathbb Z_p}\text{.}$$

If you print a description of some of the above rings, you will sometimes see a new symbol introduced. Consider the following example:

F = QuadraticField(7)
F

root = F.gen(0)
root^2

root

(2*root)^3


Here Number Field describes an object generally formed by combining the rationals with another number (here $$\sqrt{7}$$). “a” is a new symbol which behaves as a root of the polynomial $$x^2-7\text{.}$$ We do not say which root, $$\sqrt{7}$$ or $$-\sqrt{7}\text{,}$$ and as we understand the theory better we will see that this does not really matter.

We can obtain this root as a generator of the number field, and then manipulate it. First squaring root yields 7. Notice that root prints as a. Notice, too, that computations with root behave as if it was either root of $$x^2-7\text{,}$$ and results print using a.

This can get a bit confusing, inputing computations with root and getting output in terms of a. Fortunately, there is a better way. Consider the following example:

F.<b> = QuadraticField(7)
F

b^2

(2*b)^3


With the syntax F.<b> we can create the field F along with specifying a generator b using a name of our choosing. Then computations can use b in both input and output as a root of $$x^2-7\text{.}$$

Here are three new rings that are best created using this new syntax.

1. F.<a> = FiniteField(p^n): We will later have a theorem that tells us that finite fields only exist with orders equal to to a power of a prime. When the power is larger than 1, then we need a generator, here given as a.

2. P.<x>=R[]: the ring of all polynomials in the variable x, with coefficients from the ring R. Notice that R can be any ring, so this is a very general construction that uses one ring to form another. See an example below.

3. Q.<r,s,t> = QuaternionAlgebra(n, m): the rationals combined with indeterminates r, s and t such that $$r^2=n\text{,}$$ $$s^2=m$$ and $$t = rs = -sr\text{.}$$ This is a generalization of the quaternions described in this chapter, though over the rationals rather than the reals, so it is an exact ring. Notice that this is one of the few noncommutative rings in Sage. The “usual” quaternions would be constructed with Q.<I,J,K> = QuaternionAlgebra(-1, -1). (Notice that using I here is not a good choice, because it will then clobber the symbol I used for complex numbers.)

Syntax specifying names for generators can be used for many of the above rings as well, such as demonstrated above for quadratic fields and below for cyclotomic fields.

C.<t> = CyclotomicField(8)
C.random_element()


### SubsectionProperties of Rings

The examples below demonstrate how to query certain properties of rings. If you are playing along, be sure to execute the first compute cell to define the various rings involved in the examples.

Z7 = Integers(7)
Z9 = Integers(9)
F.<a> = FiniteField(3^2)
P.<x> = Z7[]
S.<f,g,h> = QuaternionAlgebra(-7, 3)


Exact versus inexact.

QQ.is_exact()

RR.is_exact()


Finite versus infinite.

Z7.is_finite()

Z7.is_finite()


Integral domain?

Z7.is_integral_domain()

Z9.is_integral_domain()


Field?

Z9.is_field()

F.is_field()

Q.is_field()


Commutative?

Q.is_commutative()

S.is_commutative()


Characteristic.

Z7.characteristic()

Z9.characteristic()

Q.characteristic()

F.characteristic()

P.characteristic()

S.characteristic()


Additive and multiplicative identities print like you would expect, but notice that while they may print identically, they could be different because of the ring they live in.

b = Z9.zero(); b

b.parent()

c = Q.zero(); c

c.parent()

b == c

d = Z9.one(); d

d.parent()

e = Q.one(); e

e.parent()

d == e


There is some support for subrings. For example, Q and S are extensions of the rationals, while F is totally distinct from the rationals.

QQ.is_subring(Q)

QQ.is_subring(S)

QQ.is_subring(F)


Not every element of a ring may have a multiplicative inverse, in other words, not every element has to be a unit (unless the ring is a field). It would now be good practice to check if an element is a unit before you try computing its inverse.

three = Z9(3)
three.is_unit()

three*three

four = Z9(4)
four.is_unit()

g = four^-1; g

four*g


### SubsectionQuotient Structure

Ideals are the normal subgroups of rings and allow us to build “quotients” — basically new rings defined on equivalence classes of elements of the original ring. Sage support for ideals is variable. When they can be created, there is not always a lot you can do with them. But they work well in certain very important cases.

The integers, $${\mathbb Z}\text{,}$$ have ideals that are just multiples of a single integer. We can create them with the .ideal() method or just by wrting a scalar multiple of ZZ. And then the quotient is isomorphic to a well-understood ring. (Notice that I is a bad name for an ideal if we want to work with complex numbers later.)

I1 = ZZ.ideal(4)
I2 = 4*ZZ
I3 = (-4)*ZZ
I1 == I2

I2 == I3

Q = ZZ.quotient(I1); Q

Q == Integers(4)


We might normally be more careful about the last statement. The quotient is a set of equivalence classes, each infinite, and certainly not a single integer. But the quotient is isomorphic to $${\mathbb Z}_4\text{,}$$ so Sage just makes this identification.

Z7 = Integers(7)
P.<y> = Z7[]
M = P.ideal(y^2+4)
Q = P.quotient(M)
Q

Q.random_element()

Q.order()

Q.is_field()


Notice that the construction of the quotient ring has created a new generator, converting y($$y$$) to ybar ($$\overline{y}$$). We can override this as before with the syntax demonstrated below.

Q.<t> = P.quotient(M); Q

Q.random_element()


So from a quotient of an infinite ring and an ideal (which is also a ring), we create a field, which is finite. Understanding this construction will be an important theme in the next few chapters. To see how remarkable it is, consider what happens with just one little change.

Z7 = Integers(7)
P.<y> = Z7[]
M = P.ideal(y^2+3)
Q.<t> = P.quotient(M)
Q

Q.random_element()

Q.order()

Q.is_field()


There are a few methods available which will give us properties of ideals. In particular, we can check for prime and maximal ideals in rings of polynomials. Examine the results above and below in the context of Theorem 16.35.

Z7 = Integers(7)
P.<y> = Z7[]
M = P.ideal(y^2+4)
N = P.ideal(y^2+3)
M.is_maximal()

N.is_maximal()


The fact that M is a prime ideal is verification of Corollary 16.40.

M.is_prime()

N.is_prime()


### SubsectionRing Homomorphisms

When Sage is presented with 3 + 4/3, how does it know that 3 is meant to be an integer? And then to add it to a rational, how does it know that we really want to view the computation as 3/1 + 4/3? This is really easy for you and me, but devilishly hard for a program, and you can imagine it getting ever more complicated with the many possible rings in Sage, subrings, matrices, etc. Part of the answer is that Sage uses ring homomorphisms to “translate” objects (numbers) between rings.

We will give an example below, but not pursue the topic much further. For the curious, reading the Sage documentation and experimenting would be a good exercise.

H = Hom(ZZ, QQ)
phi = H([1])
phi

phi.parent()

a = 3; a

a.parent()

b = phi(3); b

b.parent()


So phi is a homomorphism (“morphism”) that converts integers (the domain is ZZ) into rationals (the codomain is QQ), whose parent is a set of homomorphisms that Sage calls a “homset.” Even though a and b both print as 3, which is indistinguishable to our eyes, the parents of a and b are different. Yet the numerical value of the two objects has not changed.