# Replacing term by variable with Mathematica [duplicate]

Possible Duplicate:
Replacing composite variables by a single variable

In order to simplify an given expression with several variables, I want to replace a subterm by a new variable. I tried to do the following with Mathematica:

myexpression := <variables, sums, products etc. here, something like a^2+b^2>
subterm := <variables, sums, products etc. here, something like a^2>
myexpression /. subterm -> newvar


For simple examples this works fine, but for my case it just returns the unchanged expression myexpression.

Can I be sure that the subterm can't be found in myexpression? Is there any good formal definition for "a subterm $t$ can't be found in expression $e$"? (of corse you could replace the first $t$ in $e -t + t = e$, but this is obviously not what we want...)

The code above is a simplified example, the Mathematica code for my specific problem looks like this:

Quit[]
Ra[i_] := ((1 + P[i])/2)*Pa[i] + ((1 - P[i])/2)*Pb[i]
Rb[i_] := ((1 - P[i])/2)*Pa[i] + ((1 + P[i])/2)*Pb[i]
Rab[i_] := Pab[i]
PXa := Ra[1]*Ra[2] + Ra[1]*Rab[2] + Rab[1]*Ra[2]
PXab :=  Ra[1]*Rb[2] + Rb[1]*Ra[2] + Rab[1]*Rab[2]
DX := PXa + ((PXab)/2) - (1 + P0)/2
DY[i_] := Pa[i] + ((Pab[i])/2) - (1 + (P0/P[i]))/2
DX /. DY[1] -> DY1
DX /. DY[2] -> DY2

-

## marked as duplicate by rm -rf♦Oct 7 '12 at 21:03

This general question has been raised on the main StackOverflow site. The answers to these questions may be helpful:

How to reduce the number of independent variables in mathematica

Question on “smart” replacing in mathematica

How to perform a complicated change of variables for a polynomial (in Mathematica)

Get mathematica to simplify expression with another equation

Look specifically at the answers by Daniel Lichtblau.

More questions on the topic:

Can I simplify an expression into form which uses my own definitions?

How do I replace a variable in a polynomial?

Replacing composite variables by a single variable

Replace expressions with symbols

Reduce an equation by putting a new variable

-
 I would suggest making this a comment instead, since they're only links and we have other duplicates on this topic (I know, this was answered on Mathematics well before the site was born...) – rm -rf♦ Oct 7 '12 at 21:09 @rm-rf too long for a comment, and serves as a good place to coral all these posts since we agreed they are too distinct to close. – Mr.Wizard♦ Oct 7 '12 at 21:17 Why remove what I added to the top banner then, since it is more prominent there and these were actual duplicates (just different flavors)? Or why not make it CW if all you're doing is collecting links? I don't recall agreeing that they were distinct to close... in that case they shouldn't be closed. I only argued that they were distinct enough to not be merged. – rm -rf♦ Oct 7 '12 at 21:24 @rm-rf sorry, I'm not ignoring you, I didn't get the ping on this comment. I'm about to head out (it's a nice day) but I'll be happy to discuss this later. I'm trying to be both moderator and user here; I wouldn't convert the post above to CW if it was not mine, so I didn't think it was necessary for me to convert it just because it is. I removed the links (which I now think seems a bit rude; sorry) because it was redundant and incomplete. – Mr.Wizard♦ Oct 7 '12 at 22:10