Thanks!
I have something that is passing the tests. I thought I would be able to do
better by getting rid of the two for loops and using list comprehensions.
But, according to kernprof all I managed to do was make things worse.
def _mul_(self, y):
"""
Return product of self and y (another free algebra element with the
same parents)
EXAMPLES::
sage: A.<x,y,z>=FreeAlgebra(ZZ,3)
sage: (x+y+x*y)*(x+y+1) # indirect doctest
x + y + x^2 + 2*x*y + y*x + y^2 + x*y*x + x*y^2
"""
A = self.parent()
z_elt = {}
keys = product(self.__monomial_coefficients.keys(),
y.__monomial_coefficients\
.keys())
keys = [reduce(lambda x, y: x*y, i) for i in keys]
values = product(self.__monomial_coefficients.values(),
y.__monomial_coeffici\
ents.values())
values = [reduce(lambda x, y: x*y, i) for i in values]
for i in range(len(keys)):
key = keys[i]
if key in z_elt:
z_elt[key] += values[i]
else:
z_elt[key] = values[i]
z = A(0)
z.__monomial_coefficients = z_elt
return z
On Sunday, July 14, 2013 1:41:52 PM UTC-4, Simon King wrote:
>
> Hi!
>
> On 2013-07-14, broken_symlink <[email protected] <javascript:>> wrote:
> > Is the code it uses for multiplication in free_algebra_element.py under
> > devel/sage/sage/algebras? If so, I'm pretty sure for multiplication at
> > least it should be possible to do better. Is there a way I can mess
> around
> > with the _mul_ function easily?
>
> I think so. If you want to be sure: Create an element x of a free algebra,
> and then do
> sage: edit(x._mul_, 'vim')
> (where 'vim' can be replaced by the name of your favourite editor), and
> then you can simply edit the sources.
>
> Of course, you could also use inspection to find out the source file and
> edit it outside of a sage session:
>
> sage: F.<a,b> = FreeAlgebra(QQ)
> sage: import inspect
> sage: inspect.getfile(a._mul_)
>
>
> '.../local/lib/python2.7/site-packages/sage/algebras/free_algebra_element.py'
>
>
> Note: Do *not* edit the __mul__ (double underscore) method,
> which is code that is (or should be) common for all elements in
> Sage.
> And also note: For Cython code, the "inspect" module would not always
> give the right answer. But you can use
> sage.misc.sageinspect.sage_getfile and similar methods, and of course
> you could simply inspect the source code by
>
> sage: a._mul_??
>
> Anyway, when you have edited the _mul_ (*single* underscore) method, you
> can test it after doing "sage -b" or "sage -br" (the latter will start a
> new Sage session after rebuilding the changes).
>
> And if you are happy with your changes, you can get a trac account, open
> a ticket, post a patch there, and get someone to review your changes.
> More details can be found in the "Sage developers guide".
>
> Best regards,
> Simon
>
>
--
You received this message because you are subscribed to the Google Groups
"sage-support" 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-support.
For more options, visit https://groups.google.com/groups/opt_out.