Collect2
Collect2[expr, x]
collects together terms which are not free of any occurrence of x
.
Collect2[expr, {x1, x2, ...}]
(or also Collect2[expr, x1, x2, ...]
) collects together terms which are not free of any occurrence of x1, x2, ...
.
The coefficients are put over a common denominator. If expr
is expanded before collecting depends on the option Factoring
, which may be set to Factor
, Factor2
, or any other function, which is applied to the coefficients. If expr
is already expanded with respect to x
(x1
, x2
, …), the option Expanding
can be set to False.
See also
Overview, Isolate.
Examples
Collect2[t1 = a + r a + k^2 f[a] - k f[a] + x/2 - y/w, a]
(k−1)kf(a)+a(r+1)+2wwx−2y
Collect2[t1, a, Factoring -> False]
(k2−k)f(a)+a(r+1)−wy+2x
Collect2[t1, a, Factoring -> Factor]
(k−1)kf(a)+a(r+1)+2wwx−2y
Collect2[t1, a, Factoring -> Simplify]
(k−1)kf(a)+a(r+1)−wy+2x
Collect2[2 a (b - a) (h - 1) - b^2 (e a - c) + b^2, {a, b}]
−2a2(h−1)−ab2e+2ab(h−1)+b2(c+1)
Collect2[Expand[(a - b - c - d)^5], a, IsolateNames -> KK]
FRH[%]
a5−5a4KK(19)+10a3KK(20)−10a2KK(22)+5aKK(21)−KK(23)
a5−5a4(b+c+d)+10a3(b+c+d)2−10a2(b+c+d)3+5a(b+c+d)4−(b+c+d)5
The option Head
is useful for subsequent manipulations of the output
Collect2[Expand[(a - b - c - d)^5], a, Head -> h]
h(a5)−5h(a4)(b+c+d)+10h(a3)(b+c+d)2−10h(a2)(b+c+d)3+5h(a)(b+c+d)4−(b+c+d)5
Collect2[Expand[(a - b - c - d)^5], a, Head -> {h1, h2}]
h2(1,h1(a5))+h2(−5(b+c+d),h1(a4))+h2(10(b+c+d)2,h1(a3))+h2(−10(b+c+d)3,h1(a2))+h2(5(b+c+d)4,h1(a))+h2(−(b+c+d)5,1)
Collect2[Expand[(a - b - c - d)^5], a, Head -> {Identity, h2}]
Cases2[%, h2]
h2(1,a5)+h2(−5(b+c+d),a4)+h2(10(b+c+d)2,a3)+h2(−10(b+c+d)3,a2)+h2(5(b+c+d)4,a)+h2(−(b+c+d)5,1)
{h2(1,a5),h2(−5(b+c+d),a4),h2(10(b+c+d)2,a3),h2(−10(b+c+d)3,a2),h2(5(b+c+d)4,a),h2(−(b+c+d)5,1)}
It is possible to use different factoring functions
Clear[fun]
Collect2[Expand[(a - b - c)^3], a, Factoring -> fun]
% /. fun -> FactorTerms
a3fun(1)+a2fun(−3b−3c)+afun(3b2+6bc+3c2)+fun(−b3−3b2c−3bc2−c3)
a3−3a2(b+c)+3a(b2+2bc+c2)−b3−3b2c−3bc2−c3
Another neat trick is to nest Collect2
using the Factoring
option
Collect2[Expand[((a1 + a2 + a3)^3 - (b1 + b2 + b3)^3 - (c1 + c2 + c3)^3)^2], {a1, a2, a3},
Factoring -> Function[x, Collect2[x, {b1, c1}]]]
a16+6a2a15+6a3a15+15a22a14+15a32a14+30a2a3a14+20a23a13+20a33a13+60a2a32a13+60a22a3a13+(−2b13−6(b2+b3)b12−6(b2+b3)2b1−2c13−6c1(c2+c3)2−6c12(c2+c3)−2(b2+b3+c2+c3)(b22+2b3b2−c2b2−c3b2+b32+c22+c32−b3c2−b3c3+2c2c3))a13+15a24a12+15a34a12+60a2a33a12+90a22a32a12+60a23a3a12+a2(−6b13−18(b2+b3)b12−18(b2+b3)2b1−6c13−18c1(c2+c3)2−18c12(c2+c3)−6(b2+b3+c2+c3)(b22+2b3b2−c2b2−c3b2+b32+c22+c32−b3c2−b3c3+2c2c3))a12+a3(−6b13−18(b2+b3)b12−18(b2+b3)2b1−6c13−18c1(c2+c3)2−18c12(c2+c3)−6(b2+b3+c2+c3)(b22+2b3b2−c2b2−c3b2+b32+c22+c32−b3c2−b3c3+2c2c3))a12+6a25a1+6a35a1+30a2a34a1+60a22a33a1+60a23a32a1+30a24a3a1+a2a3(−12b13−36(b2+b3)b12−36(b2+b3)2b1−12c13−36c1(c2+c3)2−36c12(c2+c3)−12(b2+b3+c2+c3)(b22+2b3b2−c2b2−c3b2+b32+c22+c32−b3c2−b3c3+2c2c3))a1+a22(−6b13−18(b2+b3)b12−18(b2+b3)2b1−6c13−18c1(c2+c3)2−18c12(c2+c3)−6(b2+b3+c2+c3)(b22+2b3b2−c2b2−c3b2+b32+c22+c32−b3c2−b3c3+2c2c3))a1+a32(−6b13−18(b2+b3)b12−18(b2+b3)2b1−6c13−18c1(c2+c3)2−18c12(c2+c3)−6(b2+b3+c2+c3)(b22+2b3b2−c2b2−c3b2+b32+c22+c32−b3c2−b3c3+2c2c3))a1+a26+a36+b16+c16+6a2a35+15a22a34+20a23a33+2b13c13+6b1(b2+b3)2c13+6b12(b2+b3)c13+15a24a32+15b14(b2+b3)2+15c14(c2+c3)2+6b13c1(c2+c3)2+18b1(b2+b3)2c1(c2+c3)2+18b12(b2+b3)c1(c2+c3)2+(b2+b3+c2+c3)2(b22+2b3b2−c2b2−c3b2+b32+c22+c32−b3c2−b3c3+2c2c3)2+6a25a3+6b15(b2+b3)+6c15(c2+c3)+6b13c12(c2+c3)+18b1(b2+b3)2c12(c2+c3)+18b12(b2+b3)c12(c2+c3)+6b1(b2+b3)2(b2+b3+c2+c3)(b22+2b3b2−c2b2−c3b2+b32+c22+c32−b3c2−b3c3+2c2c3)+6c1(c2+c3)2(b2+b3+c2+c3)(b22+2b3b2−c2b2−c3b2+b32+c22+c32−b3c2−b3c3+2c2c3)+2b13(10b23+30b3b22+30b32b2+10b33+c23+c33+3c2c32+3c22c3)+3b12(b2+b3)(5b23+15b3b22+15b32b2+5b33+2c23+2c33+6c2c32+6c22c3)+3c12(c2+c3)(2b23+6b3b22+6b32b2+2b33+5c23+5c33+15c2c32+15c22c3)+2c13(b23+3b3b22+3b32b2+b33+10c23+10c33+30c2c32+30c22c3)+a2a32(−6b13−18(b2+b3)b12−18(b2+b3)2b1−6c13−18c1(c2+c3)2−18c12(c2+c3)−6(b2+b3+c2+c3)(b22+2b3b2−c2b2−c3b2+b32+c22+c32−b3c2−b3c3+2c2c3))+a22a3(−6b13−18(b2+b3)b12−18(b2+b3)2b1−6c13−18c1(c2+c3)2−18c12(c2+c3)−6(b2+b3+c2+c3)(b22+2b3b2−c2b2−c3b2+b32+c22+c32−b3c2−b3c3+2c2c3))+a23(−2b13−6(b2+b3)b12−6(b2+b3)2b1−2c13−6c1(c2+c3)2−6c12(c2+c3)−2(b2+b3+c2+c3)(b22+2b3b2−c2b2−c3b2+b32+c22+c32−b3c2−b3c3+2c2c3))+a33(−2b13−6(b2+b3)b12−6(b2+b3)2b1−2c13−6c1(c2+c3)2−6c12(c2+c3)−2(b2+b3+c2+c3)(b22+2b3b2−c2b2−c3b2+b32+c22+c32−b3c2−b3c3+2c2c3))
The options IsolateFast
allows to save some time when Isolating prefactors, provided that no factoring is involved.
ClearAll[h, g, a, b, c];
exp = Sum[h[i], {i, 1, 200000}]*a + Sum[g[i], {i, 1, 200000}]*b + Sum[j[i], {i, 1, 200000}]*c;
AbsoluteTiming[Collect2[exp, {a, b, c}, Factoring -> False, IsolateNames -> KK, Expanding -> False]]
{2.28593,aKK(28)+bKK(29)+cKK(27)}
AbsoluteTiming[Collect2[exp, {a, b, c}, Factoring -> False, IsolateNames -> KK, IsolateFast -> True, Expanding -> False]]
{1.38613,aKK(28)+bKK(29)+cKK(27)}