1
$\begingroup$

I have an expression for example

psi = 0.24 FF1[0] FF2[1] FF3[-1] + 0.23 FF1[0] FF2[0] FF3[1] + 0.5 FF1[1] FF2[0] FF3[1] + 0.25 FF1[1] FF2[1] FF3[1]

I wonder how to get the expression with coefficients sorted in descending, such as

psi = 0.5 FF1[1] FF2[0] FF3[1] + 0.25 FF1[1] FF2[1] FF3[1]+ 0.24 FF1[0] FF2[1] FF3[-1] + 0.23 FF1[0] FF2[0] FF3[1]

Or get the list as

{{0.5,FF1[1] FF2[0] FF3[1]}, {0.25, FF1[1] FF2[1] FF3[1]}, {0.24,FF1[0] FF2[1] FF3[-1]}, {0.23, FF1[0] FF2[0] FF3[1]}}


What I only can do is just to extract the coefficients:

psi = 0.24 FF1[0] FF2[1] FF3[-1] + 0.23 FF1[0] FF2[0] FF3[1] + 0.5 FF1[1] FF2[0] FF3[1] + 0.25 FF1[1] FF2[1] FF3[1]
FCoefficients[HoldPattern[+z__]] := {z}
FCoefficients[else_] := {else}`

Now I extract all the terms:

ListCoefficients = FCoefficients[targetstate]

The output is :

{0.24 FF1[0] FF2[1] FF3[-1], 0.23 FF1[0] FF2[0] FF3[1], 0.5 FF1[1] FF2[0] FF3[1], 0.25 FF1[1] FF2[1] FF3[1]}

What I want now is to sort the coefficients and its corresponding terms from ListCoefficients in descending. I can get the descending coefficients:

cofnum = ListCoefficients /. {FF1[l1_] FF2[l2_] FF3[l3_] -> 1};
Sort[cofnum, Greater]

The out put is:

{0.5, 0.25, 0.24, 0.23}

Is there any good way to do get psi = 0.5 FF1[1] FF2[0] FF3[1] + 0.25 FF1[1] FF2[1] FF3[1]+ 0.24 FF1[0] FF2[1] FF3[-1] + 0.23 FF1[0] FF2[0] FF3[1] ?

Or is it possible to get something as following:

{{0.5,FF1[1] FF2[0] FF3[1]}, {0.25, FF1[1] FF2[1] FF3[1]}, {0.24,FF1[0] FF2[1] FF3[-1]}, {0.23, FF1[0] FF2[0] FF3[1]}}

If anyone knows how to do that, would be very cool if you can give me some hints. Thank you very much!

$\endgroup$
  • 1
    $\begingroup$ Why do you want to do this? Also, your last comment about using Total will not work... if you apply Total to the list it will sort the terms in a way you don't want. $\endgroup$ – bill s May 7 at 14:22
  • $\begingroup$ yes, you are right. The total doesn't not work. Sorry, I didn't check further. I will change the question. $\endgroup$ – Xuemei Gu May 7 at 14:25
  • $\begingroup$ @bills, because I want to pick up the three terms with large coefficients $\endgroup$ – Xuemei Gu May 7 at 14:27
  • $\begingroup$ Might be this: SortBy[List @@ psi, First] // Reverse? $\endgroup$ – Fred Simons May 7 at 14:29
  • $\begingroup$ @FredSimons, wow, thanks a lot. With SortBy[List @@ psi, First] // Reverse, I get the {0.5 FF1[1] FF2[0] FF3[1], 0.25 FF1[1] FF2[1] FF3[1], 0.24 FF1[0] FF2[1] FF3[-1], 0.23 FF1[0] FF2[0] FF3[1]}, cool! $\endgroup$ – Xuemei Gu May 7 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.