I'm looking to learn how to search through a polynomial and replace all points P[x,y] that are contained within a list of finite points {P[a,b], P[c,d],..., P[x,y]} with a term such as (q+ab) and replace all points, P[a,b] that are not contained in the list with a different term, (w+cd), for example.
Here's some code that I've been trying...
K[poly_, pairs_] :=
poly //. IF[MemberQ[pairs, P[a_, b_]], P[a_, b_] :> (q+xy),
P[a_, b_] :> (w+cd)]
where //. is to replace through all pairs, IF conditional to replace pairs with the appropriate term, and MemberQ to check whether a pair P[a,b] is within a given list, 'pairs'
To verify any suggestions, the input
K[ -q P[1,3] P[4,6] , {P[1,3], P[2,7]}]
should output
-ab cd q - cd q^2 - ab q w - q^2 w
Thank you in advance for any help!
try something like this
K[poly_, pairs_] := poly /. ( #-> q+ab & /@ pairs ) /. P[__,__]-> w + cd
Or, closer to your approach:
K[poly_, pairs_] := poly /. P[a_,b_] :> If[MemberQ[pairs,P[a,b]], q+ab, w +cd ]
note you need to expand to get the desired form..
Expand[K[-q P[1, 3] P[4, 6], {P[1, 3], P[2, 7]}]]
(* -ab cd q - cd q^2 - ab q w - q^2 w *)