Re: Maintaining the order of terms when adding symbolic expressions
- To: mathgroup at smc.vnet.net
- Subject: [mg118910] Re: Maintaining the order of terms when adding symbolic expressions
- From: Peter Pein <petsie at dordos.net>
- Date: Sun, 15 May 2011 07:05:33 -0400 (EDT)
- References: <iql9ta$9t2$1@smc.vnet.net>
Am 14.05.2011 09:10, schrieb Andrew DeYoung:
> 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]];
>
...
> Many thanks for all your time and help! I really appreciate it.
>
> Sincerely,
>
> Andrew DeYoung
> Carnegie Mellon University
>
Hi Andrew,
TraditionalForm does this. Replace Print[numlist] by Print[numlist //
TraditionalForm].
Peter