On 2011-06-30, Santanu Sarkar wrote:
> I have a set S of monomials in x1, x2, x3, x4 like
>
> S=[x3x4,x2x3,x1x4,x4,x3,x1x2,x1,x2,1]. I want to rearrange S in
> Lexicographic ordering like
>
> S=[1,x1,x2,x1x2,x3,x2x3,x4,x1x4, x3x4].
You can sort a list and specify the comparison function. See the help at
S.sort?
For instance, if your variables are among x0 to x9, you could do:
def compare_monomials(x,y):
xx = str(x); yy = str(y)
if not (xx.count('x') == yy.count('x')):
return int(sgn(xx.count('x') - yy.count('x')))
else:
return cmp(xx,yy)
S.sort(cmp=compare_monomials)
The cmp argument in sort should take two arguments and return -1, 0,
or 1 depending on how they compare.
You can cook up a better comparison function for your needs (unless
you find out there's a simpler built-in way to do what you want).
--
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/sage-support
URL: http://www.sagemath.org