Ask Your Question
1

specific representation for groups inheriting from Sage's Group class

asked 2011-02-09 20:59:07 +0100

shacsmuggler gravatar image

I am working on designing a class for the group of diagonal symmetries associated with a polynomial singularity. The representation of group elements matters - I want to store a tuple of rationals associated with each group element, and add a bunch of functions that use those values. I'm wondering if there is a way to do this while still inheriting from the Sage Group class (or something that inherits from the Group class).

Thanks!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2011-02-09 21:17:09 +0100

benjaminfjones gravatar image

updated 2011-02-09 21:33:27 +0100

Python classes inherit from other classes using syntax like:

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

(see http://docs.python.org/tutorial/class...)

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.

edit flag offensive delete link more
0

answered 2011-02-10 09:44:36 +0100

niles gravatar image

A useful example of defining an inherited class is also given in the description of Sage's coercion model. The example shows a simple implementation of p-local integers as a class which inherits from Rings. A crucial subtlety demonstrated there which I didn't see while skimming @benjaminfjones's reference is

  • You need to define a new __init__ method unless you want to use exactly the same one as the parent class.

  • In that __init__ method, you need to call the __init__ method of the parent class, and give self as an argument. This is the only case I've come across where self should be passed explicitly as an argument, and it took me a while to figure it out the first few times I tried to define a new class.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-02-09 20:59:07 +0100

Seen: 743 times

Last updated: Feb 10 '11