Sagemath: Is there a way to print out all the elements of a Group or a Ring?

Viewed 294

How do I print out all the elements of a Group or Ring in Sagemath?

I am unable to find any command/function in the docs which do this. So I tried through a Python for & I am unable to understand the output

I tried this with a field

sage: A = GF(7)
sage: [print(i) for i in A]
0
1
2
3
4
5
6
[None, None, None, None, None, None, None]

I am unable to figure out what is the None, None, None ... at the end.

Unable also to understand the output of a Quotient ring

 R.<x> = PolynomialRing(Integers(7))
 A = R.quotient(x^2)
 [print(i) for i in A]

0
1
2
3
4
5
6
xbar
xbar + 1
...
xbar + 6
2*xbar
...
2*xbar + 6
...
3*xbar + 6
...
6*xbar + 6
[None,
...
None]

Here again, what is the xbar & what are the nones?

1 Answers

The function print prints its argument and returns None, which is the closest in Python to "not returning anything".

When the return value of a command is None, it does not get displayed. Here however you are building a list of these return values, so you get a list all of whose elements are None, and that list does get displayed.

To avoid that, use a for loop without building a list.

sage: A = GF(7)
sage: for i in A:
....:     print(i)
....:
0
1
2
3
4
5
6

Starting from a polynomial ring with variable x, and taking a quotient, Sage uses xbar as the default name for the image of the variable x in the quotient.

To choose a different name:

sage: R.<x> = PolynomialRing(Zmod(3))
sage: A.<t> = R.quotient(x^2)
sage: for i in A:
....:     print(i)
sage: R.<x> = PolynomialRing(Integers(3))
sage: A.<t> = R.quotient(x^2)
sage: for i in A:
....:     print(i)
....:
0
1
2
t
t + 1
t + 2
2*t
2*t + 1
2*t + 2

One can also use x for the variable name in the quotient:

sage: R.<x> = PolynomialRing(Zmod(3))
sage: A.<x> = R.quotient(x^2)
sage: for i in A:
....:     print(i)
....:
0
1
2
x
x + 1
x + 2
2*x
2*x + 1
2*x + 2

If you want a one-liner rather than a full-blown print loop, you can use consume from the more_itertools package (which you first have to install using pip).

sage: %pip install more_itertools
...
sage: from more_itertools import consume
sage: consume(print(i) for i in A)
0
1
2
x
x + 1
x + 2
2*x
2*x + 1
2*x + 2
Related