#18758: Allow actions of a parent on itself
-------------------------------------+-------------------------------------
       Reporter:  SimonKing          |        Owner:
           Type:  enhancement        |       Status:  needs_review
       Priority:  major              |    Milestone:  sage-6.8
      Component:  coercion           |   Resolution:
       Keywords:  actions on itself  |    Merged in:
        Authors:  Simon King         |    Reviewers:
Report Upstream:  N/A                |  Work issues:
         Branch:                     |       Commit:
  u/SimonKing/allow_actions_of_parent_on_itself|  
d71a456dbd312980490f17d39ed97d3df6ccdcb9
   Dependencies:  #18756             |     Stopgaps:
-------------------------------------+-------------------------------------

Comment (by SimonKing):

 Here are some timings. The code used:
 {{{
 sage: from sage.structure.element import Element, ModuleElement,
 RingElement
 sage: class MyElement(Element):
 ....:     def __init__(self, P, L):
 ....:         Element.__init__(self, P)
 ....:         self.l = L
 ....:     def _repr_(self):
 ....:         return "<{}>".format(self.l)
 ....:     def _add_(self, other):
 ....:         return self.parent()(zip(self.l, other.l))
 ....:     def _mul_(self, other):
 ....:         return self.parent()(self.l+other.l)
 sage: class MyMElement(ModuleElement):
 ....:     def __init__(self, P, L):
 ....:         ModuleElement.__init__(self, P)
 ....:         self.l = L
 ....:     def _repr_(self):
 ....:         return "<{}>".format(self.l)
 ....:     def _add_(self, other):
 ....:         return self.parent()(zip(self.l, other.l))
 ....:     def _mul_(self, other):
 ....:         return self.parent()(self.l+other.l)
 sage: class MyRElement(RingElement):
 ....:     def __init__(self, P, L):
 ....:         RingElement.__init__(self, P)
 ....:         self.l = L
 ....:     def _repr_(self):
 ....:         return "<{}>".format(self.l)
 ....:     def _add_(self, other):
 ....:         return self.parent()(zip(self.l, other.l))
 ....:     def _mul_(self, other):
 ....:         return self.parent()(self.l+other.l)
 sage: class MyParent(Parent):
 ....:     Element = MyElement
 sage: class MyMParent(Parent):
 ....:     Element = MyMElement
 sage: class MyRParent(Parent):
 ....:     Element = MyRElement
 }}}

 Here are different settings, where in each case I also compare with
 vanilla sage-6.8.beta5.
 1. Action obtained from the category:
 {{{
 sage: P = MyParent(category=Rings())
 sage: l = P([1,2])
 sage: %timeit l*l
 The slowest run took 64.43 times longer than the fastest. This could mean
 that an intermediate result is being cached
 100000 loops, best of 3: 7.82 µs per loop  # 8.87 µs in beta5
 sage: %timeit l+l
 The slowest run took 24.53 times longer than the fastest. This could mean
 that an intermediate result is being cached
 100000 loops, best of 3: 8.44 µs per loop # 9.49 µs in beta5
 }}}

 2. The coercion model using single underscore methods. Both examples won't
 work in beta5
 {{{
 sage: P = MyParent(category=Sets())
 sage: l = P([1,2])
 sage: %timeit l*l
 The slowest run took 15.44 times longer than the fastest. This could mean
 that an intermediate result is being cached
 100000 loops, best of 3: 10.3 µs per loop
 sage: %timeit l+l
 The slowest run took 11.54 times longer than the fastest. This could mean
 that an intermediate result is being cached
 100000 loops, best of 3: 10.7 µs per loop
 }}}

 3. !ModuleElement using a short-cut for "+" and not complaining about a
 multiplication action obtained from the category:
 {{{
 sage: P = MyMParent(category=Rings())
 sage: l = P([1,2])
 sage: %timeit l*l
 The slowest run took 108.72 times longer than the fastest. This could mean
 that an intermediate result is being cached
 100000 loops, best of 3: 7.88 µs per loop  # won't work in beta5
 sage: %timeit l+l
 The slowest run took 5.98 times longer than the fastest. This could mean
 that an intermediate result is being cached
 100000 loops, best of 3: 8.05 µs per loop  # 8.01 µs in beta5 (of course
 no significant change)
 }}}

 4. !ModuleElement using a shortcut for "+" and allowing the coercion model
 to use single underscore methods for "*":
 {{{
 sage: P = MyMParent(category=Sets())
 sage: l = P([1,2])
 sage: %timeit l*l
 The slowest run took 14.45 times longer than the fastest. This could mean
 that an intermediate result is being cached
 100000 loops, best of 3: 10.7 µs per loop  # won't work in beta5
 sage: %timeit l+l
 The slowest run took 4.25 times longer than the fastest. This could mean
 that an intermediate result is being cached
 100000 loops, best of 3: 8.69 µs per loop  # 8.02 µs in beta5. Should
 actually be the same time! This branch hasn't changed!
 }}}

 5. !RingElement using short-cuts for "+" and for "*". Of course there are
 no significant changes wrt. beta5, as the short-cut didn't change.
 {{{
 sage: P = MyRParent(category=Sets())
 sage: l = P([1,2])
 sage: %timeit l*l
 The slowest run took 4.75 times longer than the fastest. This could mean
 that an intermediate result is being cached
 100000 loops, best of 3: 7.12 µs per loop  # 7.02 µs with beta5
 sage: %timeit l+l
 The slowest run took 4.58 times longer than the fastest. This could mean
 that an intermediate result is being cached
 100000 loops, best of 3: 8.07 µs per loop  # 7.98 µs with beta5
 }}}

--
Ticket URL: <http://trac.sagemath.org/ticket/18758#comment:18>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

-- 
You received this message because you are subscribed to the Google Groups 
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/d/optout.

Reply via email to