Re: Maintaining the order of terms when adding symbolic expressions
- To: mathgroup at smc.vnet.net
- Subject: [mg118913] Re: Maintaining the order of terms when adding symbolic expressions
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Sun, 15 May 2011 07:06:05 -0400 (EDT)
data = Array[r[i, #] &, 41]; Table[Inner[HoldForm[#1 - #2] &, Drop[RotateLeft[data, m], -m], Drop[data, -m], List], {m, 0, 5}] Bob Hanlon ---- Andrew DeYoung <adeyoung at andrew.cmu.edu> 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