Ask Your Question

Revision history [back]

Python classes inherit from other classes using syntax like:

class MyGroup(Group):
    .
    .
    .

(see http://docs.python.org/tutorial/classes.html)

This defines a new class MyGroup that derives from the Group class. See group?? for more about the generic Group class.

To keep track of some data in the MyGroup class, you would put something like the following inside your class definition:

def __init__(self):
    self.data = []

For specific groups (like SL(3,GF(2)), a finite group) you can ask sage about the class of the group and the class of an element by doing:

sage: G = SL(3,GF(2))
sage: type(G)
<class 'sage.groups.matrix_gps.special_linear.SpecialLinearGroup_finite_field_with_category'>

sage: G.gens()
[
[1 1 0]
[0 1 0]
[0 0 1],
[0 0 1]
[1 0 0]
[0 1 0]
]
sage: type(G.gens()[1])
<class 'sage.groups.matrix_gps.matrix_group_element.SpecialLinearGroup_finite_field_with_category.element_class'>
click to hide/show revision 2
added the last paragraph as an example.

Python classes inherit from other classes using syntax like:

class MyGroup(Group):
    def my_group_method1(self):
        . . .
    .
    .
    .

(see http://docs.python.org/tutorial/classes.html)

This defines a new class MyGroup that derives from the Group class. See group?? for more about the generic Group class.

To keep track of some data in the MyGroup class, you would put something like the following inside your class definition:

def __init__(self):
    self.data = []

For specific groups (like SL(3,GF(2)), a finite group) you can ask sage about the class of the group and the class of an element by doing:

sage: G = SL(3,GF(2))
sage: type(G)
<class 'sage.groups.matrix_gps.special_linear.SpecialLinearGroup_finite_field_with_category'>

sage: G.gens()
[
[1 1 0]
[0 1 0]
[0 0 1],
[0 0 1]
[1 0 0]
[0 1 0]
]
sage: type(G.gens()[1])
<class 'sage.groups.matrix_gps.matrix_group_element.SpecialLinearGroup_finite_field_with_category.element_class'>

So if you want to have a (for example) special linear group over GF(q) that carries extra data with each element and has new methods to do things with that data, you can inherit from sage.groups.matrix_gps.special_linear.SpecialLinearGroup_finite_field_with_category. You probably need to check the constructor __init__() there to see how elements of the group are stored so you can add your rational numbers to each element in your new __init__() method.