Hi,

On 2013-07-10, broken_symlink <syamaj...@gmail.com> wrote:
> I've done some profiling of my code and it seems that my main slowdowns are 
> coming from multiplication and addition of free algebra elements.
> Is there 
> anyway I can make it go faster?

You define

  self.alg = FreeAlgebra(self.br, len(g.split(',')), g)

This yields a very generic and very slow implementation, in particular
by the method of constructing elements later in your code (see below).

If all your elements are homogeneous (or at least: weighted homogeneous with 
respect
to positive integer weights of the algebra generators), then you might instead 
use

  self.alg = FreeAlgebra(self.br, len(g.split(',')), g, 
implementation='letterplace')

I think their arithmetic is faster, and in particular you can do weighted
homogeneous Gröbner basis computations (which might be interesting in
your application, I don't know).

If your algebra elements are not weighted homogeneous, then you might
consider to use gap (via pexpect) or libgap (i.e., would be possible to
do on C library level) as a back-end. Gap has an implementation of free
associative unital algebras, and I think it is faster than the native
implementation in Sage. However, the (lib)gap free algebras are not
wrapped in Sage yet, hence, you would need to speak with (lib)gap on a
lower level.

One remark:

In your code, you construct an algebra element by
  FreeAlgebraElement(self.alg, k)
Please don't do this!
In general, elements of an algebraic structure "A" given by data "d" should be
created via "A(d)" or via arithmetic operations on A.gens(), but by calling some
class constructor.

The reason is that A knows which implementation to use for the elements.
For example, if A happens to be a free algebra with Letterplace as back-end,
then the elements of A will *not* be instances of FreeAlgebraElement!
And if you use the generic (slow) implementation, then the elements will
use a *sub*class of FreeAlgebraElement. Example:

  sage: FL = FreeAlgebra(QQ, 'a,b,c', 3, implementation='letterplace')
  sage: type(FL.0).mro()
  
[sage.algebras.letterplace.free_algebra_element_letterplace.FreeAlgebraElement_letterplace,
   sage.structure.element.AlgebraElement,
   sage.structure.element.RingElement,
   sage.structure.element.ModuleElement,
   sage.structure.element.Element,
   sage.structure.sage_object.SageObject,
   object]
  sage: FG = FreeAlgebra(QQ, 'a,b,c', 3)
  sage: type(FG.0).mro()
  
[sage.algebras.free_algebra_element.FreeAlgebra_generic_with_category.element_class,
   sage.algebras.free_algebra_element.FreeAlgebraElement,
   sage.structure.element.AlgebraElement,
   ...
  ]

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 sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to