Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My apologies if this has been asked before. Please also be gentle, as this is my first question here.

I am trying to define a commutative algebra by generators and relations in the following way. For a simple example, we can consider the Z[b] algebra generated by a, modulo a^2=b. I would like to introduce a rule (or a list of rules in more complicated examples):

In[1]: TheAlgebra = a*a -> b

It would be cool to evaluate products like this:

In[2]: a^3 //. TheAlgebra

However, this input simply returns the symbol a^3. I would have expected the result a*b. The following input works though:

In[2]': (a*a//.TheAlgebra)*a //. TheAlgebra

My question is this: how can I get Mathematica to simplify an expression such as a^kb^l in a smart way? There are similar questions on the site, but maybe the answer for this one is simpler, since I'm only interested in simplifying products of the various symbols together with some integers.

Maybe there is a way to force Mathematica to write a^3 in a full-form expression that I like, such as: Times[a,Times[a,a]]?

share|improve this question
Does a^3 //. {a^n_ /; n >= 2 :> a^(n - 2)\[CircleTimes]b} do what you want? – Stephen Luttrell Mar 3 '14 at 1:29
No, not really. The question pertains the general method of applying relations of the form a*b = (linear combination of other variables) to expressions, which would apply to more complicated examples. – Matt Hogancamp Mar 3 '14 at 3:36
Check docs for GroebnerBasis and PolynomialReduce. They are made for exactly this type of computation. – Daniel Lichtblau Mar 3 '14 at 15:41

The general case of simplifying a sum of products according to a predefined algebra can be solved by representing each product in the form prod[a1,a2,a3,...] — i.e. a general non-commutative product. Defining your own prod like this neatly avoids any of Mathematica's built-in rules for simplification of expressions.

Define rules for expanding out any Plus that is embedded inside prod, and extracting any coefficients not involved in the algebra (here I assume the algebra has 3 elements a, b, c):

distribrule = {prod[u___, x_ + y_, v___] :> prod[u, x, v] + prod[u, y, v], 
  prod[u___, Times[x_, y : a | b | c], v___] :> x prod[u, y, v]};

Define a function for building a product rule to use in the algebra:

prodrule[x_, y_, z_] := prod[u___, x, y, v___] :> prod[u, z, v];

Define a toy algebra with only one product rule, but you can have as many product rules as you like:

algebra = Flatten@{distribrule, prodrule[a, a, b - 2 c]};

As a test of this approach, construct a random product and simplify it:

expr = prod @@ RandomChoice[{a, b}, 10]
expr //. algebra // Expand
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.