1 | initial version |

Another way (less complete than slelievre's answer but more efficient for just a quick computation) is to compute directly in the Free algebra, but instead of using the product, use the following:

```
def fast_product(a,b): res = 0 a = F(a) b = F(b)
```

data_a =[(w.to_word(), cf) for w, cf in a] data_b =[(w.to_word(), cf) for w, cf in b] for wa,cfa in data_a: for wb,cfb in data_b: if len(wb) + len(wa) <= k: res = res + cfacfbF.monomial(wa)*F.monomial(wb) return res

2 | No.2 Revision |

Another way (less complete than slelievre's answer but more efficient for just a quick computation) is to compute directly in the Free algebra, but instead of using the product, use the following:

`def fast_product(a,b): res = 0 a = F(a) b = `~~F(b)
~~

F(b) data_a =[(w.to_word(), cf) for w, cf in a]
data_b =[(w.to_word(), cf) for w, cf in b] for wa,cfa in ~~data_a: ~~data_a:
for wb,cfb in data_b: if len(wb) + len(wa) <= ~~k: res =
~~k:
res = res + cfacfbF.monomial(wa)*F.monomial(wb)
return ~~res~~

3 | No.3 Revision |

Another way (less complete than ~~slelievre's ~~@slelievre's answer but more efficient for just a quick computation) is to compute directly in the Free algebra, but instead of using the product, use the following:

`def `~~fast_product(a,b):
~~fast_product(a, b):
res = ~~0 ~~0
a = ~~F(a) ~~F(a)
b = ~~F(b)
~~F(b)
data_a ~~=[(w.to_word(), ~~= [(w.to_word(), cf) for w, cf in ~~a]
~~a]
data_b ~~=[(w.to_word(), ~~= [(w.to_word(), cf) for w, cf in ~~b]
~~b]
for wa, cfa ~~ for wa,cfa ~~in data_a:
~~ ~~for ~~wb,cfb ~~wb, cfb in ~~data_b:
~~data_b:
if len(wb) + len(wa) <= k:
res += cfa * cfb * F.monomial(wa) * F.monomial(wb) ~~ res = res + cfacfbF.monomial(wa)*F.monomial(wb)
~~ return res

4 | No.4 Revision |

Another way (less complete than @slelievre's answer but more efficient for just a quick computation) is to compute directly in the ~~Free ~~free algebra, ~~but ~~using the following instead of ~~using ~~the ~~product, use the following:~~product:

```
def fast_product(a, b):
res = 0
a = F(a)
b = F(b)
data_a = [(w.to_word(), cf) for w, cf in a]
data_b = [(w.to_word(), cf) for w, cf in b]
for wa, cfa in data_a:
for wb, cfb in data_b:
if len(wb) + len(wa) <= k:
res += cfa * cfb * F.monomial(wa) * F.monomial(wb)
return res
```

5 | No.5 Revision |

Another way (less complete than @slelievre's answer but more efficient for just a quick computation) is to compute directly in the free algebra, using the following instead of the product:

```
def fast_product(a, b):
res =
```~~0
~~F.zero()
a = F(a)
b = F(b)
data_a = [(w.to_word(), cf) for w, cf in a]
data_b = [(w.to_word(), cf) for w, cf in b]
for wa, cfa in data_a:
for wb, cfb in data_b:
if len(wb) + len(wa) <= k:
res += cfa * cfb * F.monomial(wa) * F.monomial(wb)
return res

Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.