ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 10 Feb 2011 02:44:36 -0600specific representation for groups inheriting from Sage's Group classhttps://ask.sagemath.org/question/7935/specific-representation-for-groups-inheriting-from-sages-group-class/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!Wed, 09 Feb 2011 13:59:07 -0600https://ask.sagemath.org/question/7935/specific-representation-for-groups-inheriting-from-sages-group-class/Answer by benjaminfjones for <p>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). </p>
<p>Thanks!</p>
https://ask.sagemath.org/question/7935/specific-representation-for-groups-inheriting-from-sages-group-class/?answer=12085#post-id-12085Python 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.Wed, 09 Feb 2011 14:17:09 -0600https://ask.sagemath.org/question/7935/specific-representation-for-groups-inheriting-from-sages-group-class/?answer=12085#post-id-12085Answer by niles for <p>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). </p>
<p>Thanks!</p>
https://ask.sagemath.org/question/7935/specific-representation-for-groups-inheriting-from-sages-group-class/?answer=12087#post-id-12087A 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](http://www.sagemath.org/doc/reference/coercion.html#example) 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.Thu, 10 Feb 2011 02:44:36 -0600https://ask.sagemath.org/question/7935/specific-representation-for-groups-inheriting-from-sages-group-class/?answer=12087#post-id-12087