Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Starting from the general expression:

i A(a x + b y + c z + ...) + j B(a' x + b' y + c' z + ...) + k C(a'' x +b'' y + c'' z + ...)

I'd like to rearrange it to obtain:

x(a i A + a' j B + a'' k C) + y(b i A + b' j B + b'' k C) + z(c i A + c' j B +c'' k C) + ...

Without using $x$, $y$, $z$ etc. as arguments for Collect, since there are too many parameters for that to be practical and each of $x$, $y$, $z$ etc. have a form that can't be known beforehand.

To clarify, the initial expression would be something like this: $$a_T \sum _{i=1}^6 \sum _{j=1}^6 \left(\text{phi}_{2,1} \text{Bin}[[i,j,5]] \text{Bout}[[i,j,4]]\right)+a_v \sum _{i=1}^6 \sum _{j=1}^6 \left(\text{phi}_{1,4} \text{Bin}[[i,j,6]] (-\text{Bout}[[i,j,1]])\right)+b_T \sum _{i=1}^6 \sum _{j=1}^6 \left(\text{phi}_{1,j} (-\text{Bout}[[2,3,i]]) \text{Bin}[[i,j,6]]\right)+b_V \sum _{i=1}^6 \sum _{j=1}^6 \left(\text{phi}_{2,j} \text{Bout}[[i,2,3]] \text{Bin}[[i,j,5]]\right)+c_V \sum _{i=1}^6 \sum _{j=1}^6 \sum _{k=1}^6 \left(\text{phi}_{5,k} \text{Bout}[[i,j,3]] \text{Bin}[[i,j,k]]-\text{phi}_{6,k} \text{Bout}[[i,j,4]] \text{Bin}[[i,j,k]]\right)$$

Every instance of $phi_{i,j}$, $bin_{i,j,k}$ and $bout_{i,j,k}$ (the elements of the Bin and Bout arrays) is a sum of symbolic terms so that in the end $x$, $y$ or $z$ may end up being something like $\Lambda \Omega \Sigma$. These final combinations are numerous enough that using them as an argument for a command is impractical (as writting them down would defeat the purpose of using the expression in the first place).

An example result of the above expression would be $$ 2 c_V \left(-8 \sqrt{2} \Xi ^0 \overline{\Xi ^0} K^{*0}\text{(0)}-12 \sqrt{2} \overline{\Xi ^0} K^0 \Xi ^0+5 \sqrt{6} \overline{\Sigma ^0} \omega _8\text{(0)} \Xi ^0\right) + b_T \left(8 \sqrt{2} \overline{\Xi ^0} K^0 \Xi ^0 + 3 \sqrt{6} \overline{\Sigma ^0} \omega _8 \text{(0)} \Xi ^0 - \sqrt{2} \overline{\Sigma ^0} \Xi ^0 \pi ^0 \right) + b_V\left(6 \sqrt{2} \unicode{f3b5} \unicode{f3b5} \Xi ^0 \overline{\Xi ^0} K^{*0}\text{(0)}+10 \sqrt{2} \overline{\Xi ^0} K^0 \Xi ^0-3 \sqrt{6} \overline{\Sigma ^0} \omega _8\text{(0)} \Xi ^0\right)$$

share|improve this question
Hello ! It is recommended that you also share the code that troubles you, not just expect people to solve the problems you present to them. – Sektor Jan 29 '14 at 16:16
I see that you extended your question. If my answer doesn't work for you, please give me additional input-output examples in Mathematica code. Thanks. – Mr.Wizard Jan 30 '14 at 0:32

Based on your sole example and no additional description I would use:

expr = i A (a x + b y + c z) + j B (a' x + b' y + c' z) + k C (a'' x + b'' y + c'' z);

co = Intersection @@ Variables /@ Cases[expr, _Plus, {2}]

Collect[expr, co]
{x, y, z}

x (a A i+B j a'+C k a'')+y (A b i+B j b'+C k b'')+z (A c i+B j c'+C k c'')

Note: C is a system Symbol.

share|improve this answer

I'll bet there's a more elegant way to do this, but the following brute-force expression seems to do the trick as well

expr = i A (a x + b y + c z) + j B (a' x + b' y + c' z) +  k CC (a'' x + b'' y + c'' z)

Expand@expr //.
   i___ j_ x + k_ x :> (i j + k) x,
   i___ j_ y + k_ y :> (i j + k) y,
   i___ j_ z + k_ z :> (i j + k) z

which produces:

enter image description here

I have used CC instead of C for the reason Mr. Wizard pointed out.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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