3
$\begingroup$

How can I isolate only the first order "h" terms in the following expression?

expr = q^2 f[1][f[1][f[1][x0]]] + q^2 h[1] f[1][f[1][g[1][x0]]] + 
  p q f[1][f[2][x0]] + q^2 h[1] f[1][g[1][f[1][x0]]] + 
  q^2 h[1]^2 f[1][g[1][g[1][x0]]] + p q h[2] f[1][g[2][x0]] + 
  p q f[2][f[1][x0]] + p q h[1] f[2][g[1][x0]] + p^2 f[3][x0] + 
  q^2 h[1] g[1][f[1][f[1][x0]]] + q^2 h[1]^2 g[1][f[1][g[1][x0]]] + 
  p q h[1] g[1][f[2][x0]] + q^2 h[1]^2 g[1][g[1][f[1][x0]]] + 
  q^2 h[1]^3 g[1][g[1][g[1][x0]]] + p q h[1] h[2] g[1][g[2][x0]] + 
  p q h[2] g[2][f[1][x0]] + p q h[1] h[2] g[2][g[1][x0]] + 
  p^2 h[3] g[3][x0]

The result shold be

q^2 h[1] f[1][f[1][g[1][x0]]] + q^2 h[1] f[1][g[1][f[1][x0]]] + 
p q h[2] f[1][g[2][x0]] + p q h[1] f[2][g[1][x0]] + 
q^2 h[1] g[1][f[1][f[1][x0]]] + p q h[1] g[1][f[2][x0]] + 
p q h[2] g[2][f[1][x0]] + p^2 h[3] g[3][x0]
$\endgroup$
3
$\begingroup$
expr = q^2 f[1][f[1][f[1][x0]]] + q^2 h[1] f[1][f[1][g[1][x0]]] + 
  p q f[1][f[2][x0]] + q^2 h[1] f[1][g[1][f[1][x0]]] + 
  q^2 h[1]^2 f[1][g[1][g[1][x0]]] + p q h[2] f[1][g[2][x0]] + 
  p q f[2][f[1][x0]] + p q h[1] f[2][g[1][x0]] + p^2 f[3][x0] + 
  q^2 h[1] g[1][f[1][f[1][x0]]] + q^2 h[1]^2 g[1][f[1][g[1][x0]]] + 
  p q h[1] g[1][f[2][x0]] + q^2 h[1]^2 g[1][g[1][f[1][x0]]] + 
  q^2 h[1]^3 g[1][g[1][g[1][x0]]] + p q h[1] h[2] g[1][g[2][x0]] + 
  p q h[2] g[2][f[1][x0]] + p q h[1] h[2] g[2][g[1][x0]] + 
  p^2 h[3] g[3][x0]

Select or Pick the terms that does contain exactly one h[_] at level 1:

Select[expr, Count[#, _h, 1] == 1 &]
Pick[expr, Count[#, _h, 1] & /@ expr, 1]

or delete the ones that does not contain exactly one h[_] at level 1:

DeleteCases[expr, _?(Count[#, _h, 1] != 1 &)]

all three give

q^2 h[1] f[1][f[1][g[1][x0]]] + q^2 h[1] f[1][g[1][f[1][x0]]] + p q h[2] f[1][g[2][x0]] + p q h[1] f[2][g[1][x0]] + q^2 h[1] g[1][f[1][f[1][x0]]] + p q h[1] g[1][f[2][x0]] + p q h[2] g[2][f[1][x0]] + p^2 h[3] g[3][x0]

$\endgroup$
  • $\begingroup$ but with Count[p q^2 [Lambda][1]^2 [Lambda][ 2] Subscript[[ScriptCapitalE], 1][2][ Subscript[[ScriptCapitalE], 1][1][ Subscript[[ScriptCapitalE], 1][1][[Rho]0]]], [Lambda][_], 1] i obtain 1 $\endgroup$ – Kowalski Mar 26 '17 at 22:46
  • $\begingroup$ @Kowalski, you can try DeleteCases[exp, _?(Count[#, λ[_], 1] != 1 || Not[FreeQ[#, λ[_]^_]] &)] for expressions containing terms λ[1]^2 λ[2] . $\endgroup$ – kglr Mar 26 '17 at 23:06
1
$\begingroup$

I like using SeriesCoefficient for tasks like this. Use a replacement rule to add a scaling factor s to each instance of an h expression, and then use SeriesCoefficient on the scaling factor:

SeriesCoefficient[expr /. h->(s h[#]&), {s, 0, 1}]

q^2 h[1] f[1][f[1][g[1][x0]]] + q^2 h[1] f[1][g[1][f[1][x0]]] + p q h[2] f[1][g[2][x0]] + p q h[1] f[2][g[1][x0]] + q^2 h[1] g[1][f[1][f[1][x0]]] + p q h[1] g[1][f[2][x0]] + p q h[2] g[2][f[1][x0]] + p^2 h[3] g[3][x0]

While not needed for this example, the above replacement rule is useful when the h expression can be differentiated. For example:

{h[1], h'[2]} /. h -> (s h[#]&)

{s h[1], s Derivative[1][h][2]}

Note how both h[1] and h'[2]`` acquire the scaling factors``. Also, in contrast to the accepted answer, the SeriesCoefficient approach works even if the expression is not fully expanded. For instance:

expr = (1 + q h[1] + q^2 h[2])(1 + p h[1] + p q h[3]);

SeriesCoefficient[expr /. h -> (s h[#]&), {s, 0, 1}]

p h[1] + q h[1] + q^2 h[2] + p q h[3]

as compared to:

Select[expr, Count[#, _h, 1] == 1 &]
Pick[expr, Count[#, _h, 1] & /@ expr, 1]
DeleteCases[expr, _?(Count[#, _h, 1] != 1 &)]

1

Sequence[]

1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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