# How to transform the polynomial in this way?

I have two functions f and g and a polynomial

poly=p[1,1]+2*p[1,2]-3*p[1,3]-p[2,1]+p[2,2]-p[4,3]

and I want to transform the poly into

f[1,1]+2*f[1,2]+3*g[1,3]+g[2,1]+f[2,2]+g[4,3]

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?

-
@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