Re: Maintaining the order of terms when adding symbolic expressions
- To: mathgroup at smc.vnet.net
- Subject: [mg118956] Re: Maintaining the order of terms when adding symbolic expressions
- From: Peter Breitfeld <phbrf at t-online.de>
- Date: Tue, 17 May 2011 07:48:47 -0400 (EDT)
- References: <iql9ta$9t2$1@smc.vnet.net>
You have two (maybe more) possibilities: 1. Replace Print[numlist] with Print[numlist//TraditionalForm]. 2. If you don't want the output in TraditionalForm, you can use PolynomialForm instead, with Option TraditionalOrdering->True. I use to make this setting the default, because otherwise PolynomialForm seems to do nothing. SetOptions[PolynomialForm,TraditionalOrder->True], then again: Print[numlist//PolynomialForm] But beware: PolynomialForm is a form similar to NumberForm, MatrixForm, etc, so can't be used for further calculations, compare expr = (Expand[(x - 2)^3]) // PolynomialForm expr^2 // Expand expr[[1]]^2 // Expand //Peter Andrew DeYoung wrote: > Hi, > > I am writing a function that will print a list of displacements: > > tlist = Range[1, 41]; > Do[ > numlist = {}; > dt = m; > k = First[tlist]; > While[k + dt <= Last[tlist], > AppendTo[numlist, r[i, k + dt] - r[i, k]]; > k++; > ] > Print[numlist], > {m, 0, 5}] > > where r is an undefined function that determines the position vector > of particle i (the first argument) at the time given by the second > argument. > > In the output, I get lists like the following: > > {-r[i,1]+r[i,2],-r[i,2]+r[i,3],-r[i,3]+r[i,4],-r[i,4]+r[i,5],-r[i, > 5]+r[i,6],-r[i,6]+r[i,7], ... } > > Notice how it places the subtracted term first; for example, it prints > "-r[i,1]+r[i,2]" instead of "r[i,2]-r[i,1]". Of course, addition is > commutative. Still, for the presentation/report I am trying to make, > for pedagogical clarity I would prefer that Mathematica keep the order > of terms exactly how I have specified it in my line of code: > > AppendTo[numlist, r[i, k + dt] - r[i, k]]; > > Is there any way that I can maintain this ordering in the output? It > seems that I need something like "hold" or similar. So, I tried the > following: > > AppendTo[numlist, Hold[r[i, k + dt] - r[i, k]]]; > > But then this strictly holds everything and prevents r[i, k + dt] - > r[i, k] to be evaluated at the various values of k and dt. In other > words, I get output like this: > > {Hold[r[i,k+dt]-r[i,k]],Hold[r[i,k+dt]-r[i,k]],Hold[r[i,k+dt]- > r[i,k]],Hold[r[i,k+dt]-r[i,k]], ... } > > which is not what I would like; I would like the expression to be > evaluated at the various values of k and dt, but just with the order > terms held fixed. > > I also tried HoldForm: > > AppendTo[numlist, HoldForm[r[i, k + dt] - r[i, k]]]; > > But this also does not allow the expression be evaluated at the > various values of k and dt: > > {r[i,k+dt]-r[i,k],r[i,k+dt]-r[i,k],r[i,k+dt]-r[i,k],r[i,k+dt]- > r[i,k], ... } > > Do you have any ideas how I might be able to maintain the order of > terms? (One note: while it would be very helpful if the order of > terms were maintained AND the output kept in Mathematica format, it > will be okay if the only way to accomplish the maintenance of ordering > is by converting to a text format.) > > Many thanks for all your time and help! I really appreciate it. > > Sincerely, > > Andrew DeYoung > Carnegie Mellon University > -- _________________________________________________________________ Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de