I have a variety of terms that have coefficients of what I'll call $c_x$ and terms that are without this coefficient. I have a variable $U$, and would like to substitute powers of $U^2 \to d$ but only if the terms do not contain $c_x$.

I can easily do this if I want to replace only terms with $c_x$ but not without.

In a minimal example I would like to go from:

U + U^2 + c_x U + c_x U^2
% /. unknownRule
Out[1]>> U + d + c_x U + c_x U^2

Any advice would be greatly appreciated.


2 Answers 2

expr = U + U^2 + cx U + cx U^2

1. Using a single replacement rule

expr /.  (_. | Except[cx])  U^2 -> d
d + U + cx U + U^2

2. Using two replacement rules:

expr /. {p : cx U^2 -> p, U^2 -> d}
d + U + cx U + cx U^2

How this works: The first rule keeps a part that matches the pattern cx U^2 as is and prevents the second rule from touching it.

ReplaceAll >> Details:

enter image description here

  • $\begingroup$ In the second example, does the notation p : ... imply something about it being a temporary replacement that is reverted after? Thanks for the answer by the way, this is exactly what I was looking for. $\endgroup$
    – akozi
    Commented Oct 31, 2021 at 14:27
  • 1
    $\begingroup$ @akozi, p : cx U^2 -> p is shorter form for cx U^2 -> cx U^2. The way it works is as follows: ReplaceAll tries the first rule in the rules list (that is, the rule p : cx U^2 -> p) on each part (that is, the parts U, U^2 , cx U and cx U^2) and makes the replacement for parts that match the LHS of the pattern (that is, it replaces cx U^2 with itself ). Then it tries the second rule (U^2 ->d) on all parts except the parts that are already replaced by previous rules (in this case, the part cx U^2) and it finds U^2 matching its LHS and replaces it with d. $\endgroup$
    – kglr
    Commented Oct 31, 2021 at 21:49

First, note that underscores cannot be used in symbol names, so the c_x in your code example may not have the meaning that you intended.

We can make the substitution for $U^2$ only in the terms containing $c_x$ like this

expr = U + U^2 + cx U + cx U^2;

Plus @@ Cases[expr, β_ :> If[MemberQ[β, cx], β /. U^2 -> d, β]]

(*  cx d + U + cx U + U^2  *)

And, similarly, in the terms not containg cx like this

Plus @@ Cases[expr, β_ :> If[MemberQ[β, cx], β, β /. U^2 -> d]]

(*  d + U + cx U + cx U^2  *)

A slightly shorter way to obtain the same results is

If[MemberQ[#, cx], # /. U^2 -> d, #] &  /@  expr  (*  cx d + U + cx U + U^2  *)

If[MemberQ[#, cx], #, # /. U^2 -> d] &  /@  expr  (* d + U + cx U + cx U^2  *)
  • $\begingroup$ Thanks for the note about c_x. So the last two arguments of the If command are substituting differently depending on if the first conditional is true or false. And so swapping the order changes which is replaced? Very cool! Thank you! $\endgroup$
    – akozi
    Commented Oct 31, 2021 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.