4
$\begingroup$

Is there a better way to replace a variable, not powers of it, than first to replace all unwanted terms by dummy terms and replacing them back later?

Input: expr = x + x^2 + x^3
Expected output: y + x^2 + x^3

Replace Replace Replace: expr /. Power[x_, n_] :> z^n /. {x -> y} /. {z -> x}

Edit:

It should also work on this:

Input: expr = a x + b x^2 +c x^3
Expected output: a y +b x^2 +c x^3

$\endgroup$
7
  • $\begingroup$I'm always wary about making algebra-relevant transformations based solely on expression patterns. I'd probably use CoefficientRules and FromCoefficientRules or some other combination that utilizes the actual semantics of polynomials.$\endgroup$
    – lericr
    Commentedyesterday
  • $\begingroup$Or, if you literally just need this specific transformation and only ever this one, why not just expr - x + y?$\endgroup$
    – lericr
    Commentedyesterday
  • 5
    $\begingroup$Taking advantage of the fact that ReplaceAll will replace a subexpression only once in a single pass through, you can do: x + x^2 + x^3 /. {x^n_ :> x^n, x -> y}. I'm sure there is a duplicate somewhere.$\endgroup$
    – march
    Commentedyesterday
  • 1
    $\begingroup$Try: expr/. x^n_. :> If[n == 1, y, x^n]$\endgroup$Commentedyesterday
  • 1
    $\begingroup$@lericr because the polynomial has coefficients and I don't know them that I could do expr - a x + a y$\endgroup$Commentedyesterday

4 Answers 4

7
$\begingroup$

A very simple trick is to include an earlier (null) replacement matching what you don't want to change

rule = {u : x^_ -> u, x -> y}; expr = x + x^2 + x^3; expr /. rule (* x^2 + x^3 + y *) expr = a x + b x^2 + c x^3; expr /. rule (* b x^2 + c x^3 + a y *) 
$\endgroup$
    6
    $\begingroup$

    Here are a couple of options:

    Replace[Expand@expr, c_. x :> c y, {0, 1}] (* x^2 + x^3 + y *) CoefficientList[expr, x] . ReplacePart[x^Range[0, 3], 2 -> y] (* x^2 + x^3 + y *) 

    It's an odd replacement on an oddly simplistic expr. If you don't want 2x to be replaced by 2y, remove the c_. and c in the first code; the second code can't be fixed for this case. Note the first code also works on expr = x.

    $\endgroup$
      5
      $\begingroup$
      Quit[]; expr = x + x^2 + x^3 theRules = CoefficientRules[expr, {x, y}] theRules = theRules /. {1, 0} -> {0, 1}; FromCoefficientRules[theRules, {x, y}] 

      enter image description here

      expr = a x + b x^2 + c x^3; theRules = CoefficientRules[expr, {x, y}] theRules = theRules /. {1, 0} -> {0, 1} FromCoefficientRules[theRules, {x, y}] 

      enter image description here

      Quit[]; expr = 5*x + 29*x^2 + c*x^3; theRules = CoefficientRules[expr, {x, y}] theRules = theRules /. {1, 0} -> {0, 1} FromCoefficientRules[theRules, {x, y}] 

      enter image description here

      To change the quadratic term instead of the linear term:

      Quit[]; expr = 5*x + 29*x^2 + c*x^3; theRules = CoefficientRules[expr, {x, y}] theRules = theRules /. {2, 0} -> {0, 2} FromCoefficientRules[theRules, {x, y}] 

      enter image description here

      $\endgroup$
        4
        $\begingroup$

        Declare the einviroment of the linear term, here e.g.

         x + x^2 + x^3 /. {Plus[a___, x] :> Plus[a, y} x^2 + x^3 + y 
        $\endgroup$

          Start asking to get answers

          Find the answer to your question by asking.

          Ask question

          Explore related questions

          See similar questions with these tags.