Re: ordering of multivariate polynomial terms
- To: mathgroup at smc.vnet.net
- Subject: [mg86670] Re: [mg86621] ordering of multivariate polynomial terms
- From: danl at wolfram.com
- Date: Mon, 17 Mar 2008 00:22:53 -0500 (EST)
- References: <200803150810.DAA15280@smc.vnet.net>
> Hello, > > how it is possible to rearrange the terms of a multivariate polynomial > in Standard or Traditional Form in such a way that the terms are ordered > according to the number of their variables, i.e. first the constant > term, then the group of terms depending on one of the variables, then > the group of terms depening on two variables, then the group of those > with three variables, etc.? > > Thank > > Stefan The approach below might do what you want. The idea is to recast the polynomial into a list with elements of the form {exponent vector, coefficient} (this uses the function GroebnerBasis`DistributedTermsList). But to achieve your desired ordering we rig the vectors so that they only have ones, that is, we make terms behave as though they were multilinear. We also must keep track of the original terms. I'll do this by replacing each variable to a power, x^j, with x^j*newx (no power associated to that newx) and keep the original variables in coefficients, only using the new ones in exponent vectors. I also will return the result in a dummy head, myPlus, to which I attach a formatting rule to print like the usual Plus, only without evaluating as Plus and reordering terms in the process. Finally, I will note that the code below assumes the input is a polynomial in some "obvious" set of variables, and that it is in expanded form. Format[myPlus[ee__]] := Infix[myPlus[ee], "+"] orderByNumberVariables[poly_Plus] := Module[ {vars = Variables[poly], x, n, newvars, ruls, new}, n = Length[vars]; newvars = Array[x, n]; ruls = Table[vars[[j]]^n_. -> vars[[j]]^n*newvars[[j]], {j, n}]; new = GroebnerBasis`DistributedTermsList[poly /. ruls, newvars, MonomialOrder -> DegreeLexicographic]; Apply[myPlus, Map[Last, Reverse[First[new]]]] ] Here is a short example. In[95]:= x^2*y + x*y*z + z^2 Out[95]= x^2 y + x y z + z^2 In[97]:= orderByNumberVariables[x^2*y + x*y*z + z^2] Out[97]= 2 2 (z )+(x y)+(x y z) The formatting is a bit awkward because I had to convert to OutputForm to get the cut-paste to give anything legible. It is not hard to code variants that take a variable list into account in deciding, say, whether x comes before or after z. There is a project afoot to improve on TraditionalForm, in part by making it more configurable. A side effect of this project is that we might (finally) promote DistributedTermsList-- and its sibling FromDistributedTermsList-- to prime time, that is, System` context functions. Daniel Lichtblau Wolfram Research
- References:
- ordering of multivariate polynomial terms
- From: Stefan Porubsky <porubsky@cs.cas.cz>
- ordering of multivariate polynomial terms