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 two functions f and g and a polynomial


and I want to transform the poly into


Generally, I want to transform $$poly = \sum (-1)^{\tau_{i,j}} a_{ij}*p[i,j]$$ into $$\sum_{\tau_{i,j}=0} a_{ij}*f[i,j] + \sum_{\tau_{i,j}=1} a_{ij}*g[i,j]$$

What's the most elegant and efficient way?

share|improve this question
@belisarius Thanks! One can define f and g himself, this is not the key point. I think the main obstacle is how to detect the sign of the coefficient. – Eden Harder Dec 23 '13 at 6:35
up vote 2 down vote accepted

Not very general, as for symbolic coefficients you may not know the sign, but works in the case at hand:

poly /. (Times[x_, a : p[__]] /; Negative[x] :> (-x g @@ a)) /. p :> f
f[1, 1] + 2 f[1, 2] + f[2, 2] + 3 g[1, 3] + g[2, 1] + g[4, 3]
share|improve this answer
Perfect! can you explain this code? – Eden Harder Dec 23 '13 at 9:11
+1 for using SetDelayed :D – Mr.Wizard Dec 23 '13 at 15:34
@Mr.Wizard Take a look at the edit history – Dr. belisarius Dec 23 '13 at 15:36
I already did. ;^) – Mr.Wizard Dec 23 '13 at 15:37

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.