Using Sympy, a python symbolic maths package.
You will need to read the documentation if you want to expand on what I have written. Your question is difficult to understand, so this is the best I can do.
import sympy
a, b, c, d, f = sympy.symbols('abcdf')
g = sympy.Symbol('g')
m, n, o, p, q = sympy.symbols('mnopq')
eq1 = a**g**n + b**g**o + c**g**p + d**g**q
def s1(k, eqn):
"""This is a helper function to simplify 's'."""
var1, exp1 = eqn.args
var2, exp2 = exp1.args
if var2 == g:
return var1**var2**(exp2 + k)
return eqn
def s(k, eqn):
return sum(s1(k, part) for part in eqn.args)
print eq1 + a**g**f
print s(m, eq1) + a**g**f
PreludeAndFugue
2010-07-15 17:51:02