# Symbolic calculation with generators and relations

Is it possible to do the following in Mathematica:

Define an algebra $A$ via generators and relations and multiply some tensors in $A\otimes A$ (and of course simplify the relations via the relations).

Edit. Based on the comments, here is a more concrete answer to what I think you'd like. My previous answer can be found below.

Defining a ring symbolically. Let's consider some ring (or algebra) generated by symbols $g_i^\pm$ for $1\leq i\leq n$. Actually I'm not going to specify $n$ in the following as I don't think there's any need for it.

First of all let's make these generators look nice, based on your other question,

Format[g[i_, 1]] := Subsuperscript[g, i, "+"]
Format[g[i_, -1]] := Subsuperscript[g, i, "-"]


Next we are going to set up the product. Let us use CenterDot, which can be conveniently entered as [esc].[esc] and by default looks like •. Say we want this product to be additive, unital, and associative:

CenterDot[X___, Y_Plus, Z___] := CenterDot[X, #, Z] & /@ Y (* additivity *)
CenterDot[] = 1;
CenterDot[X_] := X
CenterDot[X___, 1, Y___] := CenterDot[X, Y] (* unital*)
SetAttributes[CenterDot, Flat] (* associativity *)


Here the second and third line ensure that we don't get stuck with a product of zero or one generator when we pull out the unit (identity element) 1 from a product.

We might further want to implement some multiplication rules. Let's say, for example, I want to make all generators anticommutative, $g_i^s g_j^r = -g_j^r g_i^s$ (where $s,r=\pm$) and have $g_i^- = (g_i^+)^{-1}$ each other's inverse. We're going to implement this as replacement rules, so that you can choose wheter or not you'd like to use these rules. (As I mention in my older answer below, you can also build these rules into the definition of CenterDot to get automatic simplifications, just like we automatically implemented additivity etc above. I choose not to do so for the multiplication rules.)

reorder[expr_] := expr //. CenterDot[X___, g[j_, r_], g[i_, s_], Y___] /; Not@OrderedQ@{j, i} -> -CenterDot[X, g[i, s], g[j, r],Y]
cancelinverses[expr_] := expr //. CenterDot[X___, g[i_, s_], g[j_, r_], Y___] /; i == j && s == -r -> CenterDot[X, Y]


These rules reduce expressions to ensure that in the result the $g_i^\pm$ will be ordered by increasing subscripts. For example, you can try

g[1, 1]\[CenterDot]g[2, 1] // reorder (* already ordered, so nothing happens *)
g[i, 1]\[CenterDot]g[j, 1] // reorder (* likewise, where the ordering is now alphabetical *)
g[i, 1]\[CenterDot]g[i, -1] // reorder (* likewise *)
g[j, -1]\[CenterDot]g[i, 1] // reorder (* reordered, yielding a sign *)
g[i, 1]\[CenterDot]g[k, -1]\[CenterDot]g[j, -1] // reorder (* likewise *)
g[k, 1]\[CenterDot]g[i, -1]\[CenterDot]g[j, -1] // reorder (* likewise, now yielding two signs, so a plus *)
g[i, 1]\[CenterDot]g[i, -1] // cancelinverses (* = 1 *)
g[i, 1]\[CenterDot]g[j, -1]\[CenterDot]g[i, -1] // reorder // cancelinverses (* = -g[j,-1] *)


Note that cancelinverses decreases the number of elements in the product, so the preceding examples use the fact that above we set CenterDot[]=1; and CenterDot[X_]:=X.

Defining the tensor products of this ring. Next we turn to tensor products. We're going to do this by defining one more product, which we will do just like before. Let us use CircleTimes, which can be entered as [esc]c*[esc] and appropriately looks like $\otimes$.

Give it all the properties that the tensor product should have, similar to what we did for CenterDot above. Let's say we want the tensor product to be linear over scalars, where the latter means anything not involving the generators. For this we set

CentralQ[expr_] := FreeQ[expr, g]


to check if something is a scalar, and then define

CircleTimes[X___, Y_Plus, Z___] := CircleTimes[X, #, Z] & /@ Y (* additive *)
CircleTimes[X___, s_?CentralQ Y_, Z___] := s CircleTimes[X, Y, Z] (* linear *)

CircleTimes[] = 1;
CircleTimes[X_] := X (* avoid getting stuck with a single CircleTimes[X] after the following *)
CircleTimes[X___, s_?CentralQ , Y___] := s CircleTimes[X, Y] (* unital*)

SetAttributes[CircleTimes, Flat] (* associativity *)


Note that the unital property is now implemented for any scalar multiple of 1. Then things like

(g[i,1] - g[j,-1])\[CircleTimes](g[k,-1] + 2)


will simplify correctly.

The final step is to extend the product given by CenterDot to this tensor product:

CenterDot[X_CircleTimes, Y_CircleTimes] := Inner[CenterDot, X, Y, CircleTimes]


Try it out for

(g[i,1]\[CircleTimes]g[j,-1])\[CenterDot](g[k,-1]\[CircleTimes]g[l,1])
(g[i,1]\[CircleTimes]g[j,-1])\[CenterDot](g[k,-1]\[CircleTimes]g[l,1])\[CenterDot](g[i,-1]\[CircleTimes]g[n,-1]) // reorder // cancelinverses
(g[i,1]\[CircleTimes]g[j,-1])\[CenterDot](g[k,-1]\[CircleTimes](g[l,1] + g[m,1]))


Yes, that's possible. Here is one way. First we specify the generators we want to work with:

NClist = {g1, ..., gN};
CentralQ[expr_] := And @@ (FreeQ[expr, #]& /@ NClist)


The function CentralQ checks whether an expression does not involve any of the generators.

Next we define a general noncommutative product CenterDot (with in-built shorthand [esc].[esc]) that is multilinear (over scalars = anything not in NClist), unital and associative:

CenterDot[X___, Y_Plus, Z___] := CenterDot[X, #, Z] & /@ Y (* additivity *)
CenterDot[X___, s_?CentralQ Y_, Z___] := s CenterDot[X, Y, Z] (* extract scalars *)
CenterDot[X_] := X (* avoid getting stuck with a single CenterDot[X] after the folowing *)
CenterDot[X___, s_?CentralQ, Z___] := s CenterDot[X, Z] (* unital *)
SetAttributes[CenterDot, {Flat}] (* associativity *)


You can now implement the relations in two ways:

• by directly giving further definitions for CenterDot, so that all rules will be applied automatically, or
• by defining lists of replacement rules, so that you can implement the relations by hand (once using /., or until you've exhausted them using //.).

Which works better depends on the context; of course you can mix the two too. I tend to prefer the latter, so that you can keep track of what is going on; for example you might divide the relations over several lists, and choose to apply one type of relations only, etc.

See also the documentation of NonCommutativeMultiply; I prefer CenterDot for aesthetical reasons only.

I learned the trick of defining an CentralQ, which allows one to define e.g. an algebra over functions, from Marius de Leeuw.

• One question if that is ok for you: How would I define the addition and scalar multiplication? Or I don't have to? The relations I'll need involve e.g. the '+'.
– user55170
Feb 15 '18 at 12:40
• They are implemented in the definition of CenterDot above: for example, the first line there says that X•(Y1+Y2+...)•Z = X•Y1•Z + X•Y2•Z + ... for any choice of X, Y1, Y2, ..., Z (including no X or no Z). (I use a lot of shorthands there to implement this, but it's really just that.) Likewise for scalar multiplication: the second line says X•(s Y)•Z= s X•Y•Z, and the fourth line X•s•Z= s X•Z whenever s is a scalar, i.e. not explicitly in the list NClist Feb 15 '18 at 18:43
• So there are three components in the function CenterDot because you implement e.g. right and left distributivity at the same time? How would I implement the relation $g_ig_j=-g_jg_i$? My main problem is: In my example I don't know how many generators I have. There is a natural $N$ which can be anything and I have $g_1,...,g_N$ as generators. If I write it as you like $NClist=\{g1,...gN\}$ then Mathematica complains. I could write $NClist=\{g1,g2,g3,g4\}$ but I want to work with a specific $N$. Besides. if I type e.g. $(g1+g2).g3$ how can I get Mathematica to produce $g1.g2+g2.g3$?
– user55170
Feb 16 '18 at 7:43
• Yes, the first line has three arguments for that reason. I guess that typically you'd want to use such (anti)commutation rules to ensure that all elements will be ordered with the subscripts increasing to the right. I don't have time now but will expand my answer to explain how to achieve that tomorrow or so. Feb 16 '18 at 9:16
• And what does the Plus in Y_Plus? That was not exactly my point, it is just one out of four relations I want to implement. You already said that this will go with centerdot but I do not know how. Remember my aim to define that algebra in order to compute later some tensors in $A\otimes A\otimes A$.
– user55170
Feb 16 '18 at 9:27