CLASS HIERARCHY:
TESTS:
This came up in some subtle bug once.
sage: gp(2) + gap(3)
5
Bases: sage.structure.parent.Parent
Parents are the SAGE/mathematical analogues of container objects in computer science.
TESTS:
sage: V = VectorSpace(GF(2,'a'),2)
sage: V.list()
[(0, 0), (1, 0), (0, 1), (1, 1)]
sage: MatrixSpace(GF(3), 1, 1).list()
[[0], [1], [2]]
sage: DirichletGroup(3).list()
[Dirichlet character modulo 3 of conductor 1 mapping 2 |--> 1,
Dirichlet character modulo 3 of conductor 3 mapping 2 |--> -1]
sage: K = GF(7^6,'a')
sage: K.list()[:10] # long time
[0, 1, 2, 3, 4, 5, 6, a, a + 1, a + 2]
sage: K.<a> = GF(4)
sage: K.list()
[0, a, a + 1, 1]
EXAMPLES:
Check to make sure that we handle coerce maps from Python native types correctly:
sage: QQ['q,t'].coerce_map_from(int)
Composite map:
From: Set of Python objects of type 'int'
To: Multivariate Polynomial Ring in q, t over Rational Field
Defn: Native morphism:
From: Set of Python objects of type 'int'
To: Rational Field
then
Polynomial base injection morphism:
From: Rational Field
To: Multivariate Polynomial Ring in q, t over Rational Field
Return True if there is a natural map from S to self. Otherwise, return False.
Return a list of the elements of self.
OUTPUT:
A list of all the elements produced by the iterator defined for the object. The result is cached. An infinite set may define an iterator, allowing one to search through the elements, but a request by this method for the entire list should fail.
NOTE:
Some objects X do not know if they are finite or not. If X.is_finite() fails with a NotImplementedError, then X.list() will simply try. In that case, it may run without stopping.
However, if X knows that it is infinite, then running X.list() will raise an appropriate error, while running list(X) will run indefinitely. For many Sage objects X, using X.list() is preferable to using list(X).
Nevertheless, since the whole list of elements is created and cached by X.list(), it may be better to do for x in X:, not for x in X.list():.
EXAMPLES:
sage: R = Integers(11)
sage: R.list() # indirect doctest
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
sage: ZZ.list()
Traceback (most recent call last):
...
NotImplementedError: since it is infinite, cannot list Integer Ring
This is the motivation for trac ticket #10470
sage: (QQ^2).list()
Traceback (most recent call last):
...
NotImplementedError: since it is infinite, cannot list Vector space of dimension 2 over Rational Field
TESTS:
The following tests the caching by adjusting the cached version:
sage: R = Integers(3)
sage: R.list()
[0, 1, 2]
sage: R._list[0] = 'junk'
sage: R.list()
['junk', 1, 2]
Here we test that for an object that does not know whether it is finite or not. Calling X.list() simply tries to create the list (but here it fails, since the object is not iterable). This was fixed trac ticket #11350
sage: R.<t,p> = QQ[]
sage: Q = R.quotient(t^2-t+1)
sage: Q.is_finite()
Traceback (most recent call last):
...
NotImplementedError
sage: Q.list()
Traceback (most recent call last):
...
NotImplementedError: object does not support iteration
Here is another example. We artificially create a version of the ring of integers that does not know whether it is finite or not:
sage: from sage.rings.integer_ring import IntegerRing_class
sage: class MyIntegers_class(IntegerRing_class):
....: def is_finite(self):
....: raise NotImplementedError
sage: MyIntegers = MyIntegers_class()
sage: MyIntegers.is_finite()
Traceback (most recent call last):
...
NotImplementedError
Asking for list(MyIntegers) below will never finish without pressing Ctrl-C. We let it run for 1 second and then interrupt:
sage: alarm(1.0); list(MyIntegers)
Traceback (most recent call last):
...
AlarmInterrupt