I have a suggestion of some functions to add to the sympy core for the
purpose of implementing multilinear/graded algebras. Consider a sympy
expression 'expr' that contains the noncommuting symbols e_1,...,e_s in
a linear manner. That is 'expr' can be put in the form:
expr = expr_0+expr_1*e_1+...+expr_s*e_s
then the function 'linear_expand' would return:
([expr_0,expr_1,...,expr_s],[1,e_1,...,e_s]) = linear_expand(expr)
my function for doing this is:
def linear_expand(expr):
"""
If a sympy 'Expr' is of the form:
expr = expr_0+expr_1*a_1+...+expr_n*a_n
where all the a_j are noncommuting symbols in basis then
(expr_0,...,expr_n) and (1,a_1,...,a_n) are returned. Note that
expr_j*a_j does not have to be of that form, but rather can be any
Mul with a_j as a factor (it doen not have to be a postmultiplier).
expr_0 is the scalar part of the expression.
"""
expr = expand(expr)
if isinstance(expr,Mul):
(coefs,bases) = expr.args_cnc()
coefs = Mul(*coefs)
bases = bases[0]
elif isinstance(expr,Symbol):
if expr.is_commutative:
coefs = expr
bases = ONE
else:
coefs = ONE
bases = expr
elif isinstance(expr,Add):
#print 'linear expand =',expr
scalar = ZERO
coefs = []
bases = []
for arg in expr.args:
term = arg.args_cnc()
if term[1] == []:
scalar += Mul(*term[0])
else:
coef = Mul(*term[0])
base = term[1][0]
#print '(coef,base) =',coef,base
if base in bases:
ibase = bases.index(base)
coefs[ibase] += coef
#print 'REPEAT'
else:
coefs.append(coef)
bases.append(base)
coefs = [scalar]+coefs
bases = [ONE]+bases
else:
coefs = expr
bases = ONE
if not isinstance(coefs,list):
coefs = [coefs]
if not isinstance(bases,list):
bases = [bases]
coefs = tuple(coefs)
bases = tuple(bases)
#print zip(bases,coefs)
return(coefs,bases)
which is probably nowhere as efficient as it could be if it were
integrated into the core and written by someone who truely understands
the core. Two other core routines that would complement 'linear_expand'
would be 'scalar_part' and 'projection'. Scalar part would return:
expr_0 = scalar_part(expr)
For 'projection' one would specifiy a list of noncommuting symbols e then:
project(expr,e) = sum of all terms expr_j*e_j such that e_j in e
this would be of great utility in graded algebras and would project out
of expr a specific grade defined by e.
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sympy?hl=en.