Finite Homogeneous Sequences¶
A mutable sequence of elements with a common guaranteed category, which can be set immutable.
Sequence derives from list, so has all the functionality of lists and
can be used wherever lists are used. When a sequence is created
without explicitly given the common universe of the elements, the
constructor coerces the first and second element to some
canonical common parent, if possible, then the second and
third, etc. If this is possible, it then coerces everything into the
canonical parent at the end. (Note that canonical coercion is very
restrictive.) The sequence then has a function universe()
which returns either the common canonical parent (if the coercion
succeeded), or the category of all objects (Objects()). So if you
have a list \(v\) and type:
sage: v = [1, 2/3, 5]
sage: w = Sequence(v)
sage: w.universe()
Rational Field
>>> from sage.all import *
>>> v = [Integer(1), Integer(2)/Integer(3), Integer(5)]
>>> w = Sequence(v)
>>> w.universe()
Rational Field
v = [1, 2/3, 5] w = Sequence(v) w.universe()
then since w.universe()
is \(\QQ\), you’re guaranteed that all
elements of \(w\) are rationals:
sage: v[0].parent()
Integer Ring
sage: w[0].parent()
Rational Field
>>> from sage.all import *
>>> v[Integer(0)].parent()
Integer Ring
>>> w[Integer(0)].parent()
Rational Field
v[0].parent() w[0].parent()
If you do assignment to \(w\) this property of being rationals is guaranteed to be preserved:
sage: w[0] = 2
sage: w[0].parent()
Rational Field
sage: w[0] = 'hi'
Traceback (most recent call last):
...
TypeError: unable to convert 'hi' to a rational
>>> from sage.all import *
>>> w[Integer(0)] = Integer(2)
>>> w[Integer(0)].parent()
Rational Field
>>> w[Integer(0)] = 'hi'
Traceback (most recent call last):
...
TypeError: unable to convert 'hi' to a rational
w[0] = 2 w[0].parent() w[0] = 'hi'
However, if you do w = Sequence(v)
and the resulting universe
is Objects()
, the elements are not guaranteed to have any
special parent. This is what should happen, e.g., with finite field
elements of different characteristics:
sage: v = Sequence([GF(3)(1), GF(7)(1)])
sage: v.universe()
Category of objects
>>> from sage.all import *
>>> v = Sequence([GF(Integer(3))(Integer(1)), GF(Integer(7))(Integer(1))])
>>> v.universe()
Category of objects
v = Sequence([GF(3)(1), GF(7)(1)]) v.universe()
You can make a list immutable with v.freeze()
. Assignment is
never again allowed on an immutable list.
Creation of a sequence involves making a copy of the input list, and substantial coercions. It can be greatly sped up by explicitly specifying the universe of the sequence:
sage: v = Sequence(range(10000), universe=ZZ)
>>> from sage.all import *
>>> v = Sequence(range(Integer(10000)), universe=ZZ)
v = Sequence(range(10000), universe=ZZ)
- sage.structure.sequence.Sequence(x, universe=None, check=True, immutable=False, cr=False, cr_str=None, use_sage_types=False)[source]¶
A mutable list of elements with a common guaranteed universe, which can be set immutable.
A universe is either an object that supports coercion (e.g., a parent), or a category.
INPUT:
x
– list or tuple instanceuniverse
– (default:None
) the universe of elements; ifNone
determined using canonical coercions and the entire list of elements. If list is empty, is categoryObjects()
of all objects.check
– boolean (default:True
); whether to coerce the elements of x into the universeimmutable
– boolean (default:True
); whether or not this sequence is immutablecr
– boolean (default:False
); ifTrue
, then print a carriage return after each comma when printing this sequencecr_str
– boolean (default:False
); ifTrue
, then print a carriage return after each comma when callingstr()
on this sequenceuse_sage_types
– boolean (default:False
); ifTrue
, coerce the built-in Python numerical types int, float, complex to the corresponding Sage types (this makes functions likevector()
more flexible)
OUTPUT: a sequence
EXAMPLES:
sage: v = Sequence(range(10)) sage: v.universe() <class 'int'> sage: v [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> from sage.all import * >>> v = Sequence(range(Integer(10))) >>> v.universe() <class 'int'> >>> v [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
v = Sequence(range(10)) v.universe() v
We can request that the built-in Python numerical types be coerced to Sage objects:
sage: v = Sequence(range(10), use_sage_types=True) sage: v.universe() Integer Ring sage: v [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> from sage.all import * >>> v = Sequence(range(Integer(10)), use_sage_types=True) >>> v.universe() Integer Ring >>> v [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
v = Sequence(range(10), use_sage_types=True) v.universe() v
You can also use seq for “Sequence”, which is identical to using Sequence:
sage: v = seq([1,2,1/1]); v [1, 2, 1] sage: v.universe() Rational Field
>>> from sage.all import * >>> v = seq([Integer(1),Integer(2),Integer(1)/Integer(1)]); v [1, 2, 1] >>> v.universe() Rational Field
v = seq([1,2,1/1]); v v.universe()
Note that assignment coerces if possible,:
sage: v = Sequence(range(10), ZZ) sage: a = QQ(5) sage: v[3] = a sage: parent(v[3]) Integer Ring sage: parent(a) Rational Field sage: v[3] = 2/3 Traceback (most recent call last): ... TypeError: no conversion of this rational to integer
>>> from sage.all import * >>> v = Sequence(range(Integer(10)), ZZ) >>> a = QQ(Integer(5)) >>> v[Integer(3)] = a >>> parent(v[Integer(3)]) Integer Ring >>> parent(a) Rational Field >>> v[Integer(3)] = Integer(2)/Integer(3) Traceback (most recent call last): ... TypeError: no conversion of this rational to integer
v = Sequence(range(10), ZZ) a = QQ(5) v[3] = a parent(v[3]) parent(a) v[3] = 2/3
Sequences can be used absolutely anywhere lists or tuples can be used:
sage: isinstance(v, list) True
>>> from sage.all import * >>> isinstance(v, list) True
isinstance(v, list)
Sequence can be immutable, so entries can’t be changed:
sage: v = Sequence([1,2,3], immutable=True) sage: v.is_immutable() True sage: v[0] = 5 Traceback (most recent call last): ... ValueError: object is immutable; please change a copy instead.
>>> from sage.all import * >>> v = Sequence([Integer(1),Integer(2),Integer(3)], immutable=True) >>> v.is_immutable() True >>> v[Integer(0)] = Integer(5) Traceback (most recent call last): ... ValueError: object is immutable; please change a copy instead.
v = Sequence([1,2,3], immutable=True) v.is_immutable() v[0] = 5
Only immutable sequences are hashable (unlike Python lists), though the hashing is potentially slow, since it first involves conversion of the sequence to a tuple, and returning the hash of that.:
sage: v = Sequence(range(10), ZZ, immutable=True) sage: hash(v) == hash(tuple(range(10))) True
>>> from sage.all import * >>> v = Sequence(range(Integer(10)), ZZ, immutable=True) >>> hash(v) == hash(tuple(range(Integer(10)))) True
v = Sequence(range(10), ZZ, immutable=True) hash(v) == hash(tuple(range(10)))
If you really know what you are doing, you can circumvent the type checking (for an efficiency gain):
sage: list.__setitem__(v, int(1), 2/3) # bad circumvention sage: v [0, 2/3, 2, 3, 4, 5, 6, 7, 8, 9] sage: list.__setitem__(v, int(1), int(2)) # not so bad circumvention
>>> from sage.all import * >>> list.__setitem__(v, int(Integer(1)), Integer(2)/Integer(3)) # bad circumvention >>> v [0, 2/3, 2, 3, 4, 5, 6, 7, 8, 9] >>> list.__setitem__(v, int(Integer(1)), int(Integer(2))) # not so bad circumvention
list.__setitem__(v, int(1), 2/3) # bad circumvention v list.__setitem__(v, int(1), int(2)) # not so bad circumvention
You can make a sequence with a new universe from an old sequence.:
sage: w = Sequence(v, QQ) sage: w [0, 2, 2, 3, 4, 5, 6, 7, 8, 9] sage: w.universe() Rational Field sage: w[1] = 2/3 sage: w [0, 2/3, 2, 3, 4, 5, 6, 7, 8, 9]
>>> from sage.all import * >>> w = Sequence(v, QQ) >>> w [0, 2, 2, 3, 4, 5, 6, 7, 8, 9] >>> w.universe() Rational Field >>> w[Integer(1)] = Integer(2)/Integer(3) >>> w [0, 2/3, 2, 3, 4, 5, 6, 7, 8, 9]
w = Sequence(v, QQ) w w.universe() w[1] = 2/3 w
The default universe for any sequence, if no compatible parent structure can be found, is the universe of all Sage objects.
This example illustrates how every element of a list is taken into account when constructing a sequence.:
sage: v = Sequence([1, 7, 6, GF(5)(3)]); v [1, 2, 1, 3] sage: v.universe() Finite Field of size 5
>>> from sage.all import * >>> v = Sequence([Integer(1), Integer(7), Integer(6), GF(Integer(5))(Integer(3))]); v [1, 2, 1, 3] >>> v.universe() Finite Field of size 5
v = Sequence([1, 7, 6, GF(5)(3)]); v v.universe()
- class sage.structure.sequence.Sequence_generic(x, universe=None, check=True, immutable=False, cr=False, cr_str=None, use_sage_types=False)[source]¶
Bases:
SageObject
,list
A mutable list of elements with a common guaranteed universe, which can be set immutable.
A universe is either an object that supports coercion (e.g., a parent), or a category.
INPUT:
x
– list or tuple instanceuniverse
– (default:None
) the universe of elements; ifNone
determined using canonical coercions and the entire list of elements. If list is empty, is categoryObjects()
of all objects.check
– boolean (default:True
); whether to coerce the elements of x into the universeimmutable
– boolean (default:True
); whether or not this sequence is immutablecr
– boolean (default:False
); ifTrue
, then print a carriage return after each comma when printing this sequenceuse_sage_types
– boolean (default:False
); ifTrue
, coerce the built-in Python numerical types int, float, complex to the corresponding Sage types (this makes functions likevector()
more flexible)
OUTPUT: a sequence
EXAMPLES:
sage: v = Sequence(range(10)) sage: v.universe() <class 'int'> sage: v [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> from sage.all import * >>> v = Sequence(range(Integer(10))) >>> v.universe() <class 'int'> >>> v [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
v = Sequence(range(10)) v.universe() v
We can request that the built-in Python numerical types be coerced to Sage objects:
sage: v = Sequence(range(10), use_sage_types=True) sage: v.universe() Integer Ring sage: v [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> from sage.all import * >>> v = Sequence(range(Integer(10)), use_sage_types=True) >>> v.universe() Integer Ring >>> v [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
v = Sequence(range(10), use_sage_types=True) v.universe() v
You can also use seq for “Sequence”, which is identical to using Sequence:
sage: v = seq([1,2,1/1]); v [1, 2, 1] sage: v.universe() Rational Field
>>> from sage.all import * >>> v = seq([Integer(1),Integer(2),Integer(1)/Integer(1)]); v [1, 2, 1] >>> v.universe() Rational Field
v = seq([1,2,1/1]); v v.universe()
Note that assignment coerces if possible,
sage: v = Sequence(range(10), ZZ) sage: a = QQ(5) sage: v[3] = a sage: parent(v[3]) Integer Ring sage: parent(a) Rational Field sage: v[3] = 2/3 Traceback (most recent call last): ... TypeError: no conversion of this rational to integer
>>> from sage.all import * >>> v = Sequence(range(Integer(10)), ZZ) >>> a = QQ(Integer(5)) >>> v[Integer(3)] = a >>> parent(v[Integer(3)]) Integer Ring >>> parent(a) Rational Field >>> v[Integer(3)] = Integer(2)/Integer(3) Traceback (most recent call last): ... TypeError: no conversion of this rational to integer
v = Sequence(range(10), ZZ) a = QQ(5) v[3] = a parent(v[3]) parent(a) v[3] = 2/3
Sequences can be used absolutely anywhere lists or tuples can be used:
sage: isinstance(v, list) True
>>> from sage.all import * >>> isinstance(v, list) True
isinstance(v, list)
Sequence can be immutable, so entries can’t be changed:
sage: v = Sequence([1,2,3], immutable=True) sage: v.is_immutable() True sage: v[0] = 5 Traceback (most recent call last): ... ValueError: object is immutable; please change a copy instead.
>>> from sage.all import * >>> v = Sequence([Integer(1),Integer(2),Integer(3)], immutable=True) >>> v.is_immutable() True >>> v[Integer(0)] = Integer(5) Traceback (most recent call last): ... ValueError: object is immutable; please change a copy instead.
v = Sequence([1,2,3], immutable=True) v.is_immutable() v[0] = 5
Only immutable sequences are hashable (unlike Python lists), though the hashing is potentially slow, since it first involves conversion of the sequence to a tuple, and returning the hash of that.
sage: v = Sequence(range(10), ZZ, immutable=True) sage: hash(v) == hash(tuple(range(10))) True
>>> from sage.all import * >>> v = Sequence(range(Integer(10)), ZZ, immutable=True) >>> hash(v) == hash(tuple(range(Integer(10)))) True
v = Sequence(range(10), ZZ, immutable=True) hash(v) == hash(tuple(range(10)))
If you really know what you are doing, you can circumvent the type checking (for an efficiency gain):
sage: list.__setitem__(v, int(1), 2/3) # bad circumvention sage: v [0, 2/3, 2, 3, 4, 5, 6, 7, 8, 9] sage: list.__setitem__(v, int(1), int(2)) # not so bad circumvention
>>> from sage.all import * >>> list.__setitem__(v, int(Integer(1)), Integer(2)/Integer(3)) # bad circumvention >>> v [0, 2/3, 2, 3, 4, 5, 6, 7, 8, 9] >>> list.__setitem__(v, int(Integer(1)), int(Integer(2))) # not so bad circumvention
list.__setitem__(v, int(1), 2/3) # bad circumvention v list.__setitem__(v, int(1), int(2)) # not so bad circumvention
You can make a sequence with a new universe from an old sequence.
sage: w = Sequence(v, QQ) sage: w [0, 2, 2, 3, 4, 5, 6, 7, 8, 9] sage: w.universe() Rational Field sage: w[1] = 2/3 sage: w [0, 2/3, 2, 3, 4, 5, 6, 7, 8, 9]
>>> from sage.all import * >>> w = Sequence(v, QQ) >>> w [0, 2, 2, 3, 4, 5, 6, 7, 8, 9] >>> w.universe() Rational Field >>> w[Integer(1)] = Integer(2)/Integer(3) >>> w [0, 2/3, 2, 3, 4, 5, 6, 7, 8, 9]
w = Sequence(v, QQ) w w.universe() w[1] = 2/3 w
The default universe for any sequence, if no compatible parent structure can be found, is the universe of all Sage objects.
This example illustrates how every element of a list is taken into account when constructing a sequence.
sage: v = Sequence([1, 7, 6, GF(5)(3)]); v [1, 2, 1, 3] sage: v.universe() Finite Field of size 5
>>> from sage.all import * >>> v = Sequence([Integer(1), Integer(7), Integer(6), GF(Integer(5))(Integer(3))]); v [1, 2, 1, 3] >>> v.universe() Finite Field of size 5
v = Sequence([1, 7, 6, GF(5)(3)]); v v.universe()
- append(x)[source]¶
EXAMPLES:
sage: v = Sequence([1,2,3,4], immutable=True) sage: v.append(34) Traceback (most recent call last): ... ValueError: object is immutable; please change a copy instead. sage: v = Sequence([1/3,2,3,4]) sage: v.append(4) sage: type(v[4]) <class 'sage.rings.rational.Rational'>
>>> from sage.all import * >>> v = Sequence([Integer(1),Integer(2),Integer(3),Integer(4)], immutable=True) >>> v.append(Integer(34)) Traceback (most recent call last): ... ValueError: object is immutable; please change a copy instead. >>> v = Sequence([Integer(1)/Integer(3),Integer(2),Integer(3),Integer(4)]) >>> v.append(Integer(4)) >>> type(v[Integer(4)]) <class 'sage.rings.rational.Rational'>
v = Sequence([1,2,3,4], immutable=True) v.append(34) v = Sequence([1/3,2,3,4]) v.append(4) type(v[4])
- extend(iterable)[source]¶
Extend list by appending elements from the iterable.
EXAMPLES:
sage: B = Sequence([1,2,3]) sage: B.extend(range(4)) sage: B [1, 2, 3, 0, 1, 2, 3]
>>> from sage.all import * >>> B = Sequence([Integer(1),Integer(2),Integer(3)]) >>> B.extend(range(Integer(4))) >>> B [1, 2, 3, 0, 1, 2, 3]
B = Sequence([1,2,3]) B.extend(range(4)) B
- insert(index, object)[source]¶
Insert object before index.
EXAMPLES:
sage: B = Sequence([1,2,3]) sage: B.insert(10, 5) sage: B [1, 2, 3, 5]
>>> from sage.all import * >>> B = Sequence([Integer(1),Integer(2),Integer(3)]) >>> B.insert(Integer(10), Integer(5)) >>> B [1, 2, 3, 5]
B = Sequence([1,2,3]) B.insert(10, 5) B
- is_immutable()[source]¶
Return
True
if this object is immutable (can not be changed) andFalse
if it is not.To make this object immutable use
set_immutable()
.EXAMPLES:
sage: v = Sequence([1, 2, 3, 4/5]) sage: v[0] = 5 sage: v [5, 2, 3, 4/5] sage: v.is_immutable() False sage: v.set_immutable() sage: v.is_immutable() True
>>> from sage.all import * >>> v = Sequence([Integer(1), Integer(2), Integer(3), Integer(4)/Integer(5)]) >>> v[Integer(0)] = Integer(5) >>> v [5, 2, 3, 4/5] >>> v.is_immutable() False >>> v.set_immutable() >>> v.is_immutable() True
v = Sequence([1, 2, 3, 4/5]) v[0] = 5 v v.is_immutable() v.set_immutable() v.is_immutable()
- is_mutable()[source]¶
EXAMPLES:
sage: a = Sequence([1, 2/3, -2/5]) sage: a.is_mutable() True sage: a[0] = 100 sage: type(a[0]) <class 'sage.rings.rational.Rational'> sage: a.set_immutable() sage: a[0] = 50 Traceback (most recent call last): ... ValueError: object is immutable; please change a copy instead. sage: a.is_mutable() False
>>> from sage.all import * >>> a = Sequence([Integer(1), Integer(2)/Integer(3), -Integer(2)/Integer(5)]) >>> a.is_mutable() True >>> a[Integer(0)] = Integer(100) >>> type(a[Integer(0)]) <class 'sage.rings.rational.Rational'> >>> a.set_immutable() >>> a[Integer(0)] = Integer(50) Traceback (most recent call last): ... ValueError: object is immutable; please change a copy instead. >>> a.is_mutable() False
a = Sequence([1, 2/3, -2/5]) a.is_mutable() a[0] = 100 type(a[0]) a.set_immutable() a[0] = 50 a.is_mutable()
- pop(index=-1)[source]¶
Remove and return item at index
index
(default: last).EXAMPLES:
sage: B = Sequence([1,2,3]) sage: B.pop(1) 2 sage: B [1, 3]
>>> from sage.all import * >>> B = Sequence([Integer(1),Integer(2),Integer(3)]) >>> B.pop(Integer(1)) 2 >>> B [1, 3]
B = Sequence([1,2,3]) B.pop(1) B
- remove(value)[source]¶
Remove first occurrence of
value
.EXAMPLES:
sage: B = Sequence([1,2,3]) sage: B.remove(2) sage: B [1, 3]
>>> from sage.all import * >>> B = Sequence([Integer(1),Integer(2),Integer(3)]) >>> B.remove(Integer(2)) >>> B [1, 3]
B = Sequence([1,2,3]) B.remove(2) B
- reverse()[source]¶
Reverse the elements of self, in place.
EXAMPLES:
sage: B = Sequence([1,2,3]) sage: B.reverse(); B [3, 2, 1]
>>> from sage.all import * >>> B = Sequence([Integer(1),Integer(2),Integer(3)]) >>> B.reverse(); B [3, 2, 1]
B = Sequence([1,2,3]) B.reverse(); B
- set_immutable()[source]¶
Make this object immutable, so it can never again be changed.
EXAMPLES:
sage: v = Sequence([1, 2, 3, 4/5]) sage: v[0] = 5 sage: v [5, 2, 3, 4/5] sage: v.set_immutable() sage: v[3] = 7 Traceback (most recent call last): ... ValueError: object is immutable; please change a copy instead.
>>> from sage.all import * >>> v = Sequence([Integer(1), Integer(2), Integer(3), Integer(4)/Integer(5)]) >>> v[Integer(0)] = Integer(5) >>> v [5, 2, 3, 4/5] >>> v.set_immutable() >>> v[Integer(3)] = Integer(7) Traceback (most recent call last): ... ValueError: object is immutable; please change a copy instead.
v = Sequence([1, 2, 3, 4/5]) v[0] = 5 v v.set_immutable() v[3] = 7
- sort(key=None, reverse=False)[source]¶
Sort this list IN PLACE.
INPUT:
key
– see Pythonlist sort
reverse
– see Pythonlist sort
EXAMPLES:
sage: B = Sequence([3,2,1/5]) sage: B.sort() sage: B [1/5, 2, 3] sage: B.sort(reverse=True); B [3, 2, 1/5]
>>> from sage.all import * >>> B = Sequence([Integer(3),Integer(2),Integer(1)/Integer(5)]) >>> B.sort() >>> B [1/5, 2, 3] >>> B.sort(reverse=True); B [3, 2, 1/5]
B = Sequence([3,2,1/5]) B.sort() B B.sort(reverse=True); B
- universe()[source]¶
Return the universe of the sequence.
EXAMPLES:
sage: Sequence([1, 2/3, -2/5]).universe() Rational Field sage: Sequence([1, 2/3, '-2/5']).universe() Category of objects
>>> from sage.all import * >>> Sequence([Integer(1), Integer(2)/Integer(3), -Integer(2)/Integer(5)]).universe() Rational Field >>> Sequence([Integer(1), Integer(2)/Integer(3), '-2/5']).universe() Category of objects
Sequence([1, 2/3, -2/5]).universe() Sequence([1, 2/3, '-2/5']).universe()
- sage.structure.sequence.seq(x, universe=None, check=True, immutable=False, cr=False, cr_str=None, use_sage_types=False)[source]¶
A mutable list of elements with a common guaranteed universe, which can be set immutable.
A universe is either an object that supports coercion (e.g., a parent), or a category.
INPUT:
x
– list or tuple instanceuniverse
– (default:None
) the universe of elements; ifNone
determined using canonical coercions and the entire list of elements. If list is empty, is categoryObjects()
of all objects.check
– boolean (default:True
); whether to coerce the elements of x into the universeimmutable
– boolean (default:True
); whether or not this sequence is immutablecr
– boolean (default:False
); ifTrue
, then print a carriage return after each comma when printing this sequencecr_str
– boolean (default:False
); ifTrue
, then print a carriage return after each comma when callingstr()
on this sequenceuse_sage_types
– boolean (default:False
); ifTrue
, coerce the built-in Python numerical types int, float, complex to the corresponding Sage types (this makes functions likevector()
more flexible)
OUTPUT: a sequence
EXAMPLES:
sage: v = Sequence(range(10)) sage: v.universe() <class 'int'> sage: v [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> from sage.all import * >>> v = Sequence(range(Integer(10))) >>> v.universe() <class 'int'> >>> v [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
v = Sequence(range(10)) v.universe() v
We can request that the built-in Python numerical types be coerced to Sage objects:
sage: v = Sequence(range(10), use_sage_types=True) sage: v.universe() Integer Ring sage: v [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> from sage.all import * >>> v = Sequence(range(Integer(10)), use_sage_types=True) >>> v.universe() Integer Ring >>> v [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
v = Sequence(range(10), use_sage_types=True) v.universe() v
You can also use seq for “Sequence”, which is identical to using Sequence:
sage: v = seq([1,2,1/1]); v [1, 2, 1] sage: v.universe() Rational Field
>>> from sage.all import * >>> v = seq([Integer(1),Integer(2),Integer(1)/Integer(1)]); v [1, 2, 1] >>> v.universe() Rational Field
v = seq([1,2,1/1]); v v.universe()
Note that assignment coerces if possible,:
sage: v = Sequence(range(10), ZZ) sage: a = QQ(5) sage: v[3] = a sage: parent(v[3]) Integer Ring sage: parent(a) Rational Field sage: v[3] = 2/3 Traceback (most recent call last): ... TypeError: no conversion of this rational to integer
>>> from sage.all import * >>> v = Sequence(range(Integer(10)), ZZ) >>> a = QQ(Integer(5)) >>> v[Integer(3)] = a >>> parent(v[Integer(3)]) Integer Ring >>> parent(a) Rational Field >>> v[Integer(3)] = Integer(2)/Integer(3) Traceback (most recent call last): ... TypeError: no conversion of this rational to integer
v = Sequence(range(10), ZZ) a = QQ(5) v[3] = a parent(v[3]) parent(a) v[3] = 2/3
Sequences can be used absolutely anywhere lists or tuples can be used:
sage: isinstance(v, list) True
>>> from sage.all import * >>> isinstance(v, list) True
isinstance(v, list)
Sequence can be immutable, so entries can’t be changed:
sage: v = Sequence([1,2,3], immutable=True) sage: v.is_immutable() True sage: v[0] = 5 Traceback (most recent call last): ... ValueError: object is immutable; please change a copy instead.
>>> from sage.all import * >>> v = Sequence([Integer(1),Integer(2),Integer(3)], immutable=True) >>> v.is_immutable() True >>> v[Integer(0)] = Integer(5) Traceback (most recent call last): ... ValueError: object is immutable; please change a copy instead.
v = Sequence([1,2,3], immutable=True) v.is_immutable() v[0] = 5
Only immutable sequences are hashable (unlike Python lists), though the hashing is potentially slow, since it first involves conversion of the sequence to a tuple, and returning the hash of that.:
sage: v = Sequence(range(10), ZZ, immutable=True) sage: hash(v) == hash(tuple(range(10))) True
>>> from sage.all import * >>> v = Sequence(range(Integer(10)), ZZ, immutable=True) >>> hash(v) == hash(tuple(range(Integer(10)))) True
v = Sequence(range(10), ZZ, immutable=True) hash(v) == hash(tuple(range(10)))
If you really know what you are doing, you can circumvent the type checking (for an efficiency gain):
sage: list.__setitem__(v, int(1), 2/3) # bad circumvention sage: v [0, 2/3, 2, 3, 4, 5, 6, 7, 8, 9] sage: list.__setitem__(v, int(1), int(2)) # not so bad circumvention
>>> from sage.all import * >>> list.__setitem__(v, int(Integer(1)), Integer(2)/Integer(3)) # bad circumvention >>> v [0, 2/3, 2, 3, 4, 5, 6, 7, 8, 9] >>> list.__setitem__(v, int(Integer(1)), int(Integer(2))) # not so bad circumvention
list.__setitem__(v, int(1), 2/3) # bad circumvention v list.__setitem__(v, int(1), int(2)) # not so bad circumvention
You can make a sequence with a new universe from an old sequence.:
sage: w = Sequence(v, QQ) sage: w [0, 2, 2, 3, 4, 5, 6, 7, 8, 9] sage: w.universe() Rational Field sage: w[1] = 2/3 sage: w [0, 2/3, 2, 3, 4, 5, 6, 7, 8, 9]
>>> from sage.all import * >>> w = Sequence(v, QQ) >>> w [0, 2, 2, 3, 4, 5, 6, 7, 8, 9] >>> w.universe() Rational Field >>> w[Integer(1)] = Integer(2)/Integer(3) >>> w [0, 2/3, 2, 3, 4, 5, 6, 7, 8, 9]
w = Sequence(v, QQ) w w.universe() w[1] = 2/3 w
The default universe for any sequence, if no compatible parent structure can be found, is the universe of all Sage objects.
This example illustrates how every element of a list is taken into account when constructing a sequence.:
sage: v = Sequence([1, 7, 6, GF(5)(3)]); v [1, 2, 1, 3] sage: v.universe() Finite Field of size 5
>>> from sage.all import * >>> v = Sequence([Integer(1), Integer(7), Integer(6), GF(Integer(5))(Integer(3))]); v [1, 2, 1, 3] >>> v.universe() Finite Field of size 5
v = Sequence([1, 7, 6, GF(5)(3)]); v v.universe()