# Is it possible to ask Mathematica to separate all the terms containing $x$ and $y$ in the given function?

I have a 6-variable function like this

$$f(x,y,z,t,w,v)= 8 t^6 e^{i (3 t v+2 t w-x+2 y)}+t^6 e^{i (2 t v-x+y)}-2 t^5 e^{i t (v+3 w)}+2 t^5 e^{4 i t w}+3 t^4 e^{i t (v+w)}+2 t^4 e^{2 i t w}+2 e^{i (t (v-2 w))} e^{i (3 t v+y)}+16 e^{i (2 t w+x+y)}+...$$

Indeed, my original function is very longer than this and contains more than a hundred terms like the above function.

I want to know if it possible to ask Mathematica to collect (or separate) all the terms containing the two variables $$x$$ and $$y$$ separately as follows?

$$f(x,y,z,t,w,v)=f_1(x,y) +f_2(z,t,w,v)$$

 f[x_, y_, z_, t_, w_, v_] :=
(2 E^(I (3 t v + y)) E^(I (t (v - 2 w))) +
16 E^(I (2 t w + x + y)) + 2 E^(2 I t w) t^4 +
3 E^(I t (v + w)) t^4 + 2 E^(4 I t w) t^5 -
2 E^(I t (v + 3 w)) t^5 + E^(I (2 t v - x + y)) t^6 +
8 E^(I (3 t v + 2 y - x + 2 t w)) t^6);


You may first expand your expression. Then you may collect all terms containing x/y and all terms not containing x/y.

Now, in your example, every term contains x because of the prefactor:  E^(I (t (v - 2 w) + x)). I therefore delete the x in the prefactor to get a more telling example:

f[x_, y_, z_, t_, w_, v_] :=
E^(I (t (v - 2 w))) (2 E^(I (3 t v + y)) + 16 E^(I (2 t w + y)) +
2 E^(2 I t w) t^4 + 3 E^(I t (v + w)) t^4 + 2 E^(4 I t w) t^5 -
2 E^(I t (v + 3 w)) t^5 + E^(I (2 t v + y)) t^6 +
8 E^(I (3 t v + 2 t w + y)) t^6);


Now we assemble the terms with and without x/y:

fwith = Select[Expand@f[x, y, z, t, w, v],! FreeQ[#, x | y] &] // Simplify
fwithout = Select[Expand@f[x, y, z, t, w, v], FreeQ[#, x | y] &] // Simplify


And finally, if needed, you may redefine the original function:

fnew[x_, y_, z_, t_, w_, v_] := fwith + fwithout
fnew[x, y, z, t, w, v]


FactorList@*Exp will separate terms in exponentiated form, which can then be grouped.

terms = (2 E^(I (3 t v + y)) E^(I (t (v - 2 w))) +
16 E^(I (2 t w + x + y)) + 2 E^(2 I t w) t^4 +
3 E^(I t (v + w)) t^4 + 2 E^(4 I t w) t^5 -
2 E^(I t (v + 3 w)) t^5 + E^(I (2 t v - x + y)) t^6 +
8 E^(I (3 t v + 2 y - x + 2 t w)) t^6) // Exp // FactorList;
terms = Replace[terms, (* take log of exponentiated terms *)
{{Power[E, b_], n_} :> Simplify[n*b],
{b_, n_} :> Simplify[n*Log[b]]}, 1];
separated = terms // GroupBy[FreeQ[x | y]] // Map@Total


The separated terms may be put back together, but Plus is Orderless and will automatically sort them:

separated // Total


In this case, each of the separated groups have a common factor. Factoring it out will keep the terms from being rearranged automatically by Plus, but that having a common factor won't happen in general:

separated // Map[Factor] // Total