That doesn't seem it would work in general unless I already knew g was x*y.
What I really have is a free algebra with generators as0, as1, as2, a1s, a11, a12, a2s, a21, a22, b11, b12, b21, b22, c11, c12, c21, c22, d11, d12, d21, d22, e11, e22 and function that returns a matrix like this,
[2*a21 - a21*a12*a21 1 - a21*a12]
[ -1 + a12*a21 a12]
I would like to be able to go through the matrix and replace products aij*akl with -2*aij -2akl -4. So, for example, 1 - a21*a12 should become 5 + 2*a21 + 2*a12. I guess in order to be able to do that I need to be able index elements. So, if e = 1 - a21*a12 then e[0] would be 1, e[1] would be -a21*a12 and then e[1][0] would be -a21 and e[1][1] would be a12.
Finally, the bij generators are of degree 1 and the aij generators are of degree 0, so I have a differential from b to a. For example, assuming I can do the above, the differential for b21 would be -6 + -2*a12 -*a21. I would like to build another matrix with bij's labeled as rows and 1 and aij labeled as columns so the row b21 would have a -6 in the first column, -2 in the a12 column and -1 in the a21 column. Then row reduce that matrix to get homology. I guess again though in order to be able to do this I need to be able to index algebra elements.
Would it be possible to implement something like this by subclassing FreeAlgebraElement or something else?