This question is an exact duplicate of:

To clarify the question that I posted here, I have the following question:

I am generating a very large term by the following:

n = 2;
permut = Permutations[Range[n]];
yIntegrationvariabes = Table[{y[i], -∞, ∞}, {i, 1, n, 1}];

list = {};
Do[list = Append[list, y[i]], {i, 1, n, 1}];

functionwhole[i_] := 
  Sum[(j - 1)*y[j], {j, 1, n, 1}]*
    Sum[(k - 1)*y[permut[[i,k]]], {k, 1, n, 1}]
             (B + Conjugate[B])*y[h]^2 - 
               2*μ[h]*(B*y[h] + Conjugate[B]*y[permut[[i,h]]]), 
             {h, 1, n, 1}], 

poly = Sum[functionwhole[p], {p, 1, n!, 1}] // Expand;

list2 = {};
Do[list2 = Append[list2, y[i] y[j]], {i, 1, n, 1}, {j, i, n, 1}]
poly2 = Collect[poly, list2] 

This generates the following expression for poly2:

E^((-B - Conjugate[B]) y[1]^2 + (-B - Conjugate[B]) y[2]^2 + 
  y[2] (2 Conjugate[B] μ[1] + 2 B μ[2]) + 
  y[1] (2 B μ[1] + 2 Conjugate[B] μ[2])) y[1] y[2] +
E^((-B - Conjugate[B]) y[1]^2 + (-B - Conjugate[B]) y[2]^2 + 
  y[1] (2 B μ[1] + 2 Conjugate[B] μ[1]) + 
  y[2] (2 B μ[2] + 2 Conjugate[B] μ[2])) y[2]^2

This outcome may be considered in Mathematica to be a list and certain components may be selected by accessing it in the following way:

pol2[[1, 1, 2, 1]]


(-B - Conjugate[B]) y[1]^2

I will be performing some operations on poly2 by accessing its elements in a similar manner. To do so, I need to be able to re-express the outcome of poly2 such that each element in the exponent has the term B to appear before the Conjugate[B] term, i.e. instead of the outcome shown above for poly2 I would like the following output:

E^((-B - Conjugate[B]) y[1]^2 + (-B - Conjugate[B]) y[2]^2 + 
  y[2] (2 B μ[2] + 2 Conjugate[B] μ[1]) + 
  y[1] (2 B μ[1] + 2 Conjugate[B] μ[2])) y[1] y[2] +
E^((-B - Conjugate[B]) y[1]^2 + (-B - Conjugate[B]) y[2]^2 + 
  y[1] (2 B μ[1] + 2 Conjugate[B] μ[1]) + 
  y[2] (2 B μ[2] + 2 Conjugate[B] μ[2])) y[2]^2

which is of course the same, but with the B terms always appearing before the Conjugate[B] terms in each parentheses. How could I achieve this for arbitrary $n$.


marked as duplicate by Dr. belisarius, MarcoB, m_goldberg, J. M. is away Aug 29 '15 at 1:43

This question was marked as an exact duplicate of an existing question.

  • 2
    $\begingroup$ You should edit your previous question rather than add a new one. $\endgroup$ – march Aug 28 '15 at 17:00
  • $\begingroup$ Although you certainly can access parts of a polynomial through Part as you show, I would be concerned that this is a very brittle method that depends on the polynomial taking a very specific FullForm representation. $\endgroup$ – MarcoB Aug 28 '15 at 17:00
  • $\begingroup$ Voting to close. Please edit your previous question instead of posting a new one. Thanks a lot $\endgroup$ – Dr. belisarius Aug 28 '15 at 18:03
  • $\begingroup$ The remaining question can now be posed quite simply as how do we tell mathematica to order something of the form Conjugate[B] x[1] + B x[2] so that the B term is first. Unfortunately I think the answer is you will end up pulling all your hair out if you don't learn to live with the way mathematica wants to order things. $\endgroup$ – george2079 Aug 28 '15 at 19:15
  • $\begingroup$ It seems it this case, if you can live with it, if you make the change mu[1]->mu1,mu[2]->mu2 (etc) that changes to pecking order of the symbols enough that the order comes out the way I think you want. $\endgroup$ – george2079 Aug 28 '15 at 19:20

I think you are making a mountain of a mole hill. Just because you can see a difference in the two orderings doesn't mean Mathematica can.

Consider the following expressions.

MatchQ[(2 Conjugate[B] μ[1] + 2 B μ[2]), (2 b_ h_[2] + 2 Conjugate[b_] h_[1] )]
Position[μ[2]] /@ 
  {(2 Conjugate[B] μ[1] + 2 B μ[2]), (2 B μ[2] + 2 Conjugate[B] μ[1] )}
{{{2, 3}}, {{2, 3}}}

This shows the internal representation of the two forms are identical. So it seems to me any expression manipulation you wish to do on your forms can proceed as if all of them were written in the way you prefer, because the front-end will automatically transform any input in your preferred form into Mathematica's canonical form.


Not the answer you're looking for? Browse other questions tagged or ask your own question.