# How to collect terms of a given type (with subscript and power) in an expression?

Suppose that I have an expression:

expr=(2 λ f[
z]^3 (λ (Subscript[λ, 0, 2] +
Subscript[λ, 2, 0]) - Subscript[λ^2, 0, 1] -
Subscript[λ^2, 1, 0]) - λ^2 (Subscript[z^2, 0,
1] + Subscript[z^2, 1, 0])^2 Derivative[1][f][z]^2 + f[z]^2 (4 λ^2 (-Subscript[z, 0, 2] Subscript[z, 2, 0] +
Subscript[z^2, 1, 1]) +
2 λ (Subscript[z, 0,
1] ((-Subscript[z, 0, 2] + Subscript[z, 2,
0]) Subscript[λ, 0, 1] -
2 Subscript[z, 1, 1] Subscript[λ, 1, 0]) +
Subscript[z^2, 0,
1] (Subscript[λ, 0, 2] + Subscript[λ, 2,
0]) + Subscript[z, 1,
0] (-2 Subscript[z, 1, 1] Subscript[λ, 0,
1] + (Subscript[z, 0, 2] - Subscript[z, 2,
0]) Subscript[λ, 1, 0] +
Subscript[z, 1,
0] (Subscript[λ, 0, 2] + Subscript[λ, 2,
0]))) - (Subscript[z^2, 0, 1] + Subscript[z^2, 1,
0]) (Subscript[λ^2, 0, 1] + Subscript[λ^2, 1,
0]) + 2 λ^3 ((Subscript[z, 0, 2] + Subscript[z, 2,
0]) Derivative[1][f][
z] + (Subscript[z^2, 0, 1] + Subscript[z^2, 1, 0]) (
f^\[Prime]\[Prime])[z])) + 2 λ f[
z] ((Subscript[z^2, 0, 1] + Subscript[z^2, 1,
0]) (Subscript[z, 0, 1] Subscript[λ, 0, 1] +
Subscript[z, 1, 0] Subscript[λ, 1, 0]) Derivative[1][
f][z] - λ^2 (Subscript[z^2, 0, 1] + Subscript[z^2, 1,
0]) Derivative[1][f][
z]^2 + λ (2 (-2 Subscript[z, 0, 1] Subscript[z, 1, 0]
Subscript[z, 1, 1] +
Subscript[z, 2, 0] Subscript[z^2, 0, 1] +
Subscript[z, 0, 2] Subscript[z^2, 1, 0]) Derivative[1][f][
z] + (Subscript[z^2, 0, 1] + Subscript[z^2, 1, 0])^2 (
f^\[Prime]\[Prime])[z])))/(4 λ f[
z] (λ f[z] + Subscript[z^2, 0, 1] + Subscript[z^2, 1, 0]))


and I want to collect the terms of the type

Subscript[z, 0, 1]


and

Subscript[z^2, 0, 1]


here 0, and 1 can be change.

I can do for the first one as

Collect[ expr// ExpandAll, Subscript[z, _, _], Simplify]


but while I try to use

Collect[ expr// ExpandAll, Subscript[z^2, _, _], Simplify]


to collect the second one, it doesn't work, Why?

What's more, can I write a pattern that will collect both of them at the same time?

• Why (f^\[Prime]\[Prime])[z] instead of Derivative[2][f][z] ? Nov 20, 2012 at 14:40
• This seems to work Collect[expr // Together // Numerator, Subscript[z^2, _, _], Simplify]  Nov 20, 2012 at 14:46

Collect[expr // Together // Numerator, Subscript[z^2, _, _], Simplify]