On Aug 29, 2008, at 5:01 AM, John Cremona wrote:

> The trouble was that I wanted common code for both additive and
> multiplicative groups, which I can -- almost-- implement.  Your way,
> we have one type of element derived from some MultiplicativeElement
> class and another from an Additive one, with a lot of duplicated code.

OK, I think I understand your question better then. In that case, I  
think the right approach is to define a wrapper class that provides a  
multiplicative interface to a additive group (and vice-versa). A  
tight wrapper in Cython would be very little overhead, and I think  
this would allow for a lot of code re-use.

>
> But I'll look at your code and see if it can help what I was trying  
> to do.
>
> John
>
> 2008/8/29 Robert Bradshaw <[EMAIL PROTECTED]>:
>>
>> On Aug 29, 2008, at 2:24 AM, Nils Skoruppa wrote:
>>
>>> On 28 Aug., 15:20, "John Cremona" <[EMAIL PROTECTED]> wrote:
>>>> Thanks for your comments, David.  I am having some success:
>>>>
>>>> The one thing I cannot get to work in the additive case is  (for
>>>> example) 2*g where g is a group element.  I have tried all possible
>>>> combinations of __lmul__, _lmul_, __rmul__, _rmul_, but although I
>>>> have this ok:
>>>>
>>>> sage: a,b=A.gens()
>>>> sage: b._lmul_(20)
>>>> 2*b
>>>> sage: 2*b
>>>>
>>>> inputting 20*b gives an error:
>>>> TypeError                                 Traceback (most recent
>>>> call last)
>>>
>>>>>> John
>>>
>>> Hi John,
>>>
>>> I have a class which derives from AdditiveGroupElement and whose
>>> parent derives from AbelianGroup. Concerning multiplication I did  
>>> not
>>> implement more than __mul__()  with two underscores ( and no _lmul_,
>>> _rmul_, mil_impl-c ... and the like). It seems to work OK:
>>>
>>> sage: A.<a,b,c> = FiniteQuadraticModule ('3^3')
>>> sage: 5*a
>>> 2*a
>>> sage: b*7
>>> b
>>> sage: -a +
>>> 2*c
>>> 2*a + 2*c
>>>
>>> I cannot explain why this works, but if you want to have a look:
>>> http://hg.countnumber.de/fqm-devel/file/98bb736f0c07/cn_group/
>>> finite_quadratic_module.sage
>>> (and then  line  2132  class
>>> FiniteQuadraticModuleElement(AdditiveGroupElement).
>>
>> Here is what should be implemented (and I just tried it out and it
>> does for me with the latest Sage). Most of the coercion model focus
>> has been considered in the context of Rings, but groups are fine too.
>> Let me know if this doesn't work for you.
>>
>>
>> from sage.groups.group import AbelianGroup
>> from sage.structure.element import AdditiveGroupElement
>>
>> class MyAbelianGroup(AbelianGroup):
>>     def __call__(self, n):
>>         return MyAdditiveGroupElement(self, n)
>>
>> class MyAdditiveGroupElement(AdditiveGroupElement):
>>     def __init__(self, parent, n):
>>         AdditiveGroupElement.__init__(self, parent)
>>         self.n = n
>>     def __repr__(self):
>>         return "My(%s)" % self.n
>>     def _add_(left, right):
>>         # Guaranteed left and right have the same parent.
>>         return MyAdditiveGroupElement(left.parent(), left.n +  
>> right.n)
>>     def _rmul_(self, a):
>>         # Guaranteed a is in the basering (probably ZZ)
>>         return MyAdditiveGroupElement(self.parent(), self.n * a)
>>     def _lmul_(self, a):
>>         # Guaranteed a is in the basering (probably ZZ)
>>         return self._rmul_(a) # should this be a default?
>>
>>
>> sage: G = MyAbelianGroup()
>> sage: G.base_ring()
>> Integer Ring
>> sage: G(3) + G(9)
>> My(12)
>> sage: 7*G(3)
>> My(21)
>> sage: G(2)*5
>> My(10)
>> sage: sum([G(2), G(3), G(7)])
>> My(12)
>> sage: G(55) * int(4)
>> My(220)
>>
>>
>> One thing that mystifies me is that I though repeated doubling was
>> supposed to be provided by default if neither _rmul_ nor _lmul_ were
>> provided. I'll have to look at this, but it's orthogonal to what you
>> are trying to do.
>>
>> - Robert
>>
>>
>>>
>>
>
> >


--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to