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

I have a functional polynomial expression of the form:

expr = f[x] + f[x] g[w] + Conjugate[f[y]] g[z] +
       Conjugate[g[z]] + g[w] + f[x] f[y] g[w] + 1 +
       Conjugate[a] f[t] + Conjugate[b] Conjugate[c]

I would like to write a function that selects terms that are up to linear (i.e. zero and first order) in the functions f and g, eg. in my case the correct output would be:

1 + f[x] + Conjugate[g[z]] + g[w] + Conjugate[a] f[t] +
Conjugate[b] Conjugate[c] 

Note that the functions are complex, and the conjugate terms are also considered. I basically just want to neglect second-order terms and higher.

share|improve this question
You could build rules as in : expr /. {f[x_] f[y_] -> 0, g[x_] g[y_] -> 0}. – b.gatessucks May 7 '13 at 11:43
the expression i put is just a simplification, in the general case it contains a lot more terms with much higher orders, so i would need very many rules – Andrei May 7 '13 at 12:38
Take the Jacobian, zero everything as for the constant term just zero everything in the initial expression. – Spawn1701D May 7 '13 at 13:29
up vote 1 down vote accepted

Maybe this

Replace[Expand@expr, Times[terms__ /; Count[{terms}, _f | _g, Infinity] > 1] -> 0, 1]
share|improve this answer

How about this:

(Series[Simplify[expr/.{forg_[x_] -> m forg[x]}, m \[Element] Reals], {m, 0, 1}] // Normal) /. m -> 1


If there are other functions within the expression that are not f or g then you have to be a bit more explicit:

(Series[Simplify[expr/.{f[x_] -> m f[x],g[x_]->m g[x]}, m \[Element] Reals], {m, 0, 1}] // Normal) /. m -> 1
share|improve this answer
thanks, this works for the above example, but unfortunately my (real) expression also has terms of the form: Conjugate[a]*f[t] and Conjugate[b]*Conjugate[c], for which it fails – Andrei May 7 '13 at 13:05
@user4794, the edit should solve that problem. – Jonathan Shock May 7 '13 at 22:06

Try this

First harvest the terms:

terms = Cases[expr, (_f | _g) | Conjugate[_g | _f], Infinity]//Union

and then

(D[expr, {terms}] /. {f -> (0 &), g -> (0 &)}).terms + (expr/.Thread[terms -> 0])


For a faster alternative use CoefficientArrays:

#1 + #2.terms & @@ (Take[CoefficientArrays[expr, terms], 2] // Normal)
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.