Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #2351 > unrolled thread
| Started by | Andrew DeYoung <adeyoung@andrew.cmu.edu> |
|---|---|
| First post | 2011-05-14 07:10 +0000 |
| Last post | 2011-05-17 11:48 +0000 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Maintaining the order of terms when adding symbolic expressions Andrew DeYoung <adeyoung@andrew.cmu.edu> - 2011-05-14 07:10 +0000
Re: Maintaining the order of terms when adding symbolic expressions Ray Koopman <koopman@sfu.ca> - 2011-05-15 11:03 +0000
Re: Maintaining the order of terms when adding symbolic expressions Peter Pein <petsie@dordos.net> - 2011-05-15 11:05 +0000
Re: Maintaining the order of terms when adding symbolic expressions Peter Breitfeld <phbrf@t-online.de> - 2011-05-17 11:48 +0000
| From | Andrew DeYoung <adeyoung@andrew.cmu.edu> |
|---|---|
| Date | 2011-05-14 07:10 +0000 |
| Subject | Maintaining the order of terms when adding symbolic expressions |
| Message-ID | <iql9ta$9t2$1@smc.vnet.net> |
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
[toc] | [next] | [standalone]
| From | Ray Koopman <koopman@sfu.ca> |
|---|---|
| Date | 2011-05-15 11:03 +0000 |
| Message-ID | <iqobuv$m70$1@smc.vnet.net> |
| In reply to | #2351 |
On May 14, 12:10 am, Andrew DeYoung <adeyo...@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
numlist = {-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]};
numlist /. -a_ + b_ -> HoldForm[b - a]
{r[i,2]-r[i,1], r[i,3]-r[i,2], r[i,4]-r[i,3],
r[i,5]-r[i,4], r[i,6]-r[i,5], r[i,7]-r[i,6]}
[toc] | [prev] | [next] | [standalone]
| From | Peter Pein <petsie@dordos.net> |
|---|---|
| Date | 2011-05-15 11:05 +0000 |
| Message-ID | <iqoc1v$m9k$1@smc.vnet.net> |
| In reply to | #2351 |
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
[toc] | [prev] | [next] | [standalone]
| From | Peter Breitfeld <phbrf@t-online.de> |
|---|---|
| Date | 2011-05-17 11:48 +0000 |
| Message-ID | <iqtnb1$inf$1@smc.vnet.net> |
| In reply to | #2351 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web