Re: Rearranging the Terms of a Polynomial
- To: mathgroup at smc.vnet.net
- Subject: [mg131325] Re: Rearranging the Terms of a Polynomial
- From: David Bailey <dave at removedbailey.co.uk>
- Date: Fri, 28 Jun 2013 04:14:33 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- Delivered-to: l-mathgroup@wolfram.com
- Delivered-to: mathgroup-outx@smc.vnet.net
- Delivered-to: mathgroup-newsendx@smc.vnet.net
- References: <kqh3re$2ia$1@smc.vnet.net>
On 27/06/2013 11:23, PiMan wrote: > Hi, I have another formatting question. > > I have a polynomial as the output of an expression, and I would like to rearrange the terms of the polynomial in the output. The current output I am getting is x1*x3-\[Alpha]2*x2-x3. However, I wish the output to read x1*x3 - x3 - \[Alpha]2*x2, that is, rearrange the second and third terms. Does anyone have an idea of how I might be able to do this for this polynomial or any polynomial in general? Thanks. > Your desired form seems to already be the canonical form coming out of Mathematica, so let me answer this question more generally. Mathematica always rearranges sums and products of terms into one form. This makes it easier for it to do algebra, but can be a nuisance, as you have found out. There are lots of ways to rearrange terms, but their effects are instantly cancelled out if you apply them directly to such expressions. For example: expr = Series[Exp[x], {x, 0, 5}] // Normal Out[6]= 1 + x + x^2/2 + x^3/6 + x^4/24 + x^5/120 In[7]:= Reverse[expr] Out[7]= 1 + x + x^2/2 + x^3/6 + x^4/24 + x^5/120 The key to manipulating such expressions into a desired form, is to use Hold or HoldForm to stop all evaluation (which includes rearrangements). Thus, to rearrange the contents or expr ( polynomial) you can do: In[8]:= Reverse[Hold[Evaluate[expr]], 2] Out[8]= Hold[x^5/120 + x^4/24 + x^3/6 + x^2/2 + x + 1] Notice that Hold[expr] wouldn't even evaluate expr - which is why I used Evaluate - you can also use With to achieve something similar. Hold is easy to use when exploring this sort of process, but once you understand what you are doing, you may want to switch to HoldForm. This works the same way, but HoldForm doesn't actually show, so the output looks nicer. You just need to understand clearly that expressions wrapped in HoldForm may look the same as normal ones, but won't evaluate properly in expressions unless you remove that outer wrapper. Finally, you can even create your own wrapper to make things more explicit, e.g. SetAttributes[wrapped,HoldAll] David Bailey http://www.dbaileyconsultancy.co.uk