2
$\begingroup$

I have a problem of arranging an expression as shown in the following picture. The first expression exp is derived from other functions and equations. I want to get the result like (2) expArranged, which is formed from the constant, the functions of x1 and the functions of x2 in every terms of exp.

Can you give the approach to obtain the result? Thank you.

Note:

The name expArranged in expression (2) is only for the convenience to express my problem, not variable symbol.

The codes have been presented in the end of the post.

Picture

enter image description here

Codes and the method converting it to standard form

enter image description here

exp = a Subscript[Q, i][x1] Subscript[Q, j][x2] Subscript[Q, m][x1] Subscript[Q, n][x2] + b DiracDelta[x1] Subscript[Q, i][x1] Subscript[Q, j][x2] Subscript[Q, n][x2] Derivative[1][Subscript[Q, m]][x1] + c DiracDelta[x2] Subscript[Q, i][x1] Subscript[Q, j][x2] Subscript[Q, m][x1] Derivative[1][Subscript[Q, n]][x2] + d Subscript[Q, i][x1] Subscript[Q, j][x2] Subscript[Q, n][x2] (Subscript[Q, m]^\[Prime]\[Prime])[x1];(*1*)
expArranged = {{a, Subscript[Q, i][x1] Subscript[Q, m][x1], Subscript[Q, j][x2] Subscript[Q, n][x2]}, {b, DiracDelta[x1] Subscript[Q, i][x1] Derivative[1][Subscript[Q, m]][ x1], Subscript[Q, j][x2] Subscript[Q, n][x2]}, {c, Subscript[Q, i][x1] Subscript[Q, m][x1], DiracDelta[x2] Subscript[Q, j][x2] Derivative[1][Subscript[Q, n]][ x2]}, {d, Subscript[Q, i][x1] (Subscript[Q, m]^\[Prime]\[Prime])[x1], Subscript[Q, j][x2] Subscript[Q, n][x2]}};(*2*)
expArranged // MatrixForm // TraditionalForm
(*end here*)
$\endgroup$
2
  • 2
    $\begingroup$ Times @@@ {Cases[#, Except[_[x1] | _[x2]]], Cases[#, _[x1]], Cases[#, _[x2]]} & /@ List @@ exp $\endgroup$
    – Hausdorff
    Commented Jan 14, 2022 at 13:46
  • $\begingroup$ @Hausdorff, Thanks for your concise and excellent solution. $\endgroup$
    – likehust
    Commented Jan 14, 2022 at 14:27

1 Answer 1

2
$\begingroup$
eArranged = Values @ 
  GroupBy[SortBy[First[#, #] &] @ Apply[List] @ #, First[#, #] &, Times @@ # &] & /@ 
   MonomialList[exp];

eArranged // MatrixForm // TraditionalForm

enter image description here

Also

$p = Cases[x1 | x2];

eArranged2 = Times @@@ GatherBy[SortBy[List @@ ##, $p], $p] & /@ MonomialList[exp];

eArranged2 // MatrixForm // TraditionalForm

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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