Package sage :: Package structure :: Module sequence :: Class Sequence
[hide private]
[frames] | no frames]

Class Sequence

source code

            object --+    
                     |    
sage_object.SageObject --+
                         |
            object --+   |
                     |   |
                  list --+
                         |
                        Sequence
Known Subclasses:
geometry.lattice_polytope.NEFPartition


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 -- a list or tuple instance
    universe -- (default: None) the universe of elements; if None determined
                using canonical coercions and the entire list of elements.
                If list is empty, is category Objects() of all objects.
    check -- (default: True) whether to coerce the elements of x into the universe
    immutable -- (default: True) whether or not this sequence is immutable
    cr -- (default: False) if True, then print a carriage return after each
                     comma when printing this sequence.

OUTPUT:
    a sequence

EXAMPLES:
    sage: v = Sequence(range(10))
    sage: v.universe()
    <type 'int'>
    sage: v
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

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
    sage: v.parent()
    Category of sequences in Rational Field
    sage: v.parent()([3,4/3])
    [3, 4/3]
    

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 coercion of this rational to integer

Sequences can be used absolutely anywhere lists or tuples can be used:
    sage: isinstance(v, list)
    True

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.

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.  The hash
is cached, and is only recomputed if the sequence is changed
(which has a small performance penalty for assignment).

    sage: v = Sequence(range(10), ZZ); v[3] = 5 
    sage: hash(v)
    2083920238            # 32-bit
    -8049699692026128018  # 64-bit
    sage: v[0] = 10
    sage: hash(v)
    -377547984            # 32-bit
    -2271601447248391376  # 64-bit

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
    [10, 2/3, 2, 5, 4, 5, 6, 7, 8, 9]
    sage: 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
    [10, 2, 2, 5, 4, 5, 6, 7, 8, 9]
    sage: w.universe()
    Rational Field
    sage: w[1] = 2/3
    sage: w
    [10, 2/3, 2, 5, 4, 5, 6, 7, 8, 9]

Sequences themselves live in a category, the category of all sequences
in the given universe. 
    sage: w.category()
    Category of sequences in Rational Field
    
This is also the parent of any sequence:
    sage: w.parent()
    Category of sequences in Rational Field

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
    sage: v.parent()
    Category of sequences in Finite Field of size 5
    sage: v.parent()([7,8,9])
    [2, 3, 4]



Instance Methods [hide private]
 
__init__(self, x, universe=['4ti2-20061025', 'R-2.6.0', 'atlas-3.7.37', 'atlas-3.8.1', 'a..., check=True, immutable=False, cr=False, cr_str=['4ti2-20061025', 'R-2.6.0', 'atlas-3.7.37', 'atlas-3.8.1', 'a...)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
source code
 
reverse(self)
Reverse the elements of self, in place.
source code
 
__setitem__(self, n, x)
x[i]=y
source code
 
__setslice__(self, i, j, seq)
EXAMPLES: sage: v = Sequence([1,2,3,4], immutable=True) sage: v[1:3] = [5,7] Traceback (most recent call last): ...
source code
 
__getslice__(self, i, j)
EXAMPLES:...
source code
 
append(self, x)
EXAMPLES: sage: v = Sequence([1,2,3,4], immutable=True) sage: v.append(34) Traceback (most recent call last): ...
source code
 
extend(self, iterable)
Extend list by appending elements from the iterable.
source code
 
insert(self, index, object)
Insert object before index.
source code
 
pop(self, index=-1)
remove and return item at index (default last)...
source code
 
remove(self, value)
Remove first occurrence of value...
source code
 
sort(self, cmp=['4ti2-20061025', 'R-2.6.0', 'atlas-3.7.37', 'atlas-3.8.1', 'a..., key=['4ti2-20061025', 'R-2.6.0', 'atlas-3.7.37', 'atlas-3.8.1', 'a..., reverse=False)
Sort this list *IN PLACE*.
source code
 
__hash__(self)
hash(x)
source code
 
_repr_(self) source code
 
__str__(self)
EXAMPLES:...
source code
 
category(self)
File: sage/structure/sage_object.pyx (starting at line 200)
source code
 
parent(self) source code
 
universe(self) source code
 
_require_mutable(self) source code
 
set_immutable(self)
Make this object immutable, so it can never again be changed.
source code
 
is_immutable(self)
Return True if this object is immutable (can not be changed) and False if it is not.
source code
 
is_mutable(self) source code
 
__copy__(self)
Return a copy of this sequence...
source code

Inherited from sage_object.SageObject: __new__, __repr__, _axiom_, _axiom_init_, _gap_, _gap_init_, _gp_, _gp_init_, _interface_, _interface_init_, _interface_is_cached_, _kash_, _kash_init_, _macaulay2_, _macaulay2_init_, _magma_, _magma_init_, _maple_, _maple_init_, _mathematica_, _mathematica_init_, _maxima_, _maxima_init_, _octave_, _octave_init_, _pari_, _pari_init_, _r_init_, _sage_, _singular_, _singular_init_, db, dump, dumps, plot, rename, reset_name, save, version

Inherited from list: __add__, __contains__, __delitem__, __delslice__, __eq__, __ge__, __getattribute__, __getitem__, __gt__, __iadd__, __imul__, __iter__, __le__, __len__, __lt__, __mul__, __ne__, __reversed__, __rmul__, count, index

Inherited from object: __delattr__, __reduce__, __reduce_ex__, __setattr__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, x, universe=['4ti2-20061025', 'R-2.6.0', 'atlas-3.7.37', 'atlas-3.8.1', 'a..., check=True, immutable=False, cr=False, cr_str=['4ti2-20061025', 'R-2.6.0', 'atlas-3.7.37', 'atlas-3.8.1', 'a...)
(Constructor)

source code 
x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Returns:
new list

Overrides: list.__init__
(inherited documentation)

reverse(self)

source code 

Reverse the elements of self, in place.

EXAMPLES:
    sage: B = Sequence([1,2,3])
    sage: B.reverse(); B
    [3, 2, 1]

Overrides: list.reverse

__setitem__(self, n, x)
(Index assignment operator)

source code 
x[i]=y

Overrides: list.__setitem__
(inherited documentation)

__setslice__(self, i, j, seq)
(Slice assignment operator)

source code 

EXAMPLES:
    sage: v = Sequence([1,2,3,4], immutable=True)
    sage: v[1:3] = [5,7]
    Traceback (most recent call last):
    ...
    ValueError: object is immutable; please change a copy instead.
    sage: v = Sequence([1,2,3,4])
    sage: v[1:3] = [5, 3/1]
    sage: v
    [1, 5, 3, 4]
    sage: type(v[2])
    <type 'sage.rings.integer.Integer'>

Overrides: list.__setslice__

__getslice__(self, i, j)
(Slicling operator)

source code 

EXAMPLES:
    sage: v = Sequence([1,2,3,4], immutable=True)
    sage: w = v[2:]
    sage: w
    [3, 4]
    sage: type(w)
    <class 'sage.structure.sequence.Sequence'>
    sage: w[0] = 5; w
    [5, 4]
    sage: v
    [1, 2, 3, 4]

Overrides: list.__getslice__

append(self, x)

source code 

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])
    <type 'sage.rings.rational.Rational'>

Overrides: list.append

extend(self, iterable)

source code 

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]

Overrides: list.extend

insert(self, index, object)

source code 

Insert object before index.

EXAMPLES:
    sage: B = Sequence([1,2,3])
    sage: B.insert(10, 5)
    sage: B
    [1, 2, 3, 5]

Overrides: list.insert

pop(self, index=-1)

source code 

remove and return item at index (default last)

EXAMPLES:
    sage: B = Sequence([1,2,3])
    sage: B.pop(1)
    2
    sage: B
    [1, 3]            

Returns:
item

Overrides: list.pop

remove(self, value)

source code 

Remove first occurrence of value

EXAMPLES:
    sage: B = Sequence([1,2,3])
    sage: B.remove(2)
    sage: B
    [1, 3]

Overrides: list.remove

sort(self, cmp=['4ti2-20061025', 'R-2.6.0', 'atlas-3.7.37', 'atlas-3.8.1', 'a..., key=['4ti2-20061025', 'R-2.6.0', 'atlas-3.7.37', 'atlas-3.8.1', 'a..., reverse=False)

source code 

Sort this list *IN PLACE*.

cmp(x, y) -> -1, 0, 1

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]
    sage: B.sort(cmp = lambda x,y: cmp(y,x)); B
    [3, 2, 1/5]
    sage: B.sort(cmp = lambda x,y: cmp(y,x), reverse=True); B
    [1/5, 2, 3]

Overrides: list.sort

__hash__(self)
(Hashing function)

source code 
hash(x)

Overrides: sage_object.SageObject.__hash__
(inherited documentation)

__str__(self)
(Informal representation operator)

source code 

        EXAMPLES:
            sage: s = Sequence([1,2,3], cr=False)
            sage: str(s)
            '[1, 2, 3]'
            sage: repr(s)
            '[1, 2, 3]'
            sage: print s
            [1, 2, 3]
            sage: s = Sequence([1,2,3], cr=True)
            sage: str(s)
            '[
1,
2,
3
]'
        

Overrides: object.__str__

category(self)

source code 
File: sage/structure/sage_object.pyx (starting at line 200)

Overrides: sage_object.SageObject.category
(inherited documentation)

set_immutable(self)

source code 

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.

is_immutable(self)

source code 

Return True if this object is immutable (can not be changed)
and False if it is not.

To make this object immutable use self.set_immutable().

EXAMPLE:
    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

__copy__(self)

source code 

Return a copy of this sequence

EXAMPLES:
    sage: s = seq(range(10))
    sage: t = copy(s)
    sage: t == s
    True
    sage: t.is_immutable == s.is_immutable
    True
    sage: t.is_mutable == s.is_mutable
    True
    sage: t.parent() == s.parent()
    True